From 297e5a17f991fe0157e3daf8f14554960e4808e4 Mon Sep 17 00:00:00 2001 From: Zubin Pratap Date: Tue, 19 Mar 2024 16:03:28 +1100 Subject: [PATCH 1/4] Additional Networks Added. 1. Optimism Sepolia 2. Base Sepolia. End to end CLI workflows have been tested. --- README.md | 17 +++++++++--- env.enc.example | 24 +++++++++++++++++ hardhat.config.js | 18 +++++++++++++ networks.js | 40 +++++++++++++++++++++++++++-- package-lock.json | 7 ++--- package.json | 2 +- tasks/Functions-billing/create.js | 7 ++++- tasks/Functions-consumer/request.js | 7 +++-- tasks/utils/network.js | 2 ++ 9 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 env.enc.example diff --git a/README.md b/README.md index 76de9f8f..8ff196d6 100644 --- a/README.md +++ b/README.md @@ -87,12 +87,12 @@ Install **both** of the following: 1. Clone this repository to your local machine

2. Open this directory in your command line/terminal app, then run `npm install` to install all dependencies.

-3. Obtain the values for following environment variables: +3. Obtain the values for following environment variables (examples only - please see `./env.enc.example` for env vars you may need): - `PRIVATE_KEY` for your development wallet - `POLYGON_MUMBAI_RPC_URL`, `ETHEREUM_SEPOLIA_RPC_URL`, or `AVALANCHE_FUJI_RPC_URL` - `POLYGONSCAN_API_KEY`, `ETHERSCAN_API_KEY`, or `FUJI_SNOWTRACE_API_KEY` blockchain explore API keys depending on which network you're using - `COINMARKETCAP_API_KEY` (from [here](https://pro.coinmarketcap.com/))

-4. Set the required environment variables. For improved security, Chainlink provides the NPM package [@chainlink/env-enc](https://www.npmjs.com/package/@chainlink/env-enc) which can be used to keep environment variables in a password encrypted `.env.enc` file instead of a plaintext `.env` for additional security. More detail on environment variable management and the tooling is provided in the [Environment Variable Management](#environment-variable-management) section. +4. Set the required environment variables (see `./env.enc.example` for the correctly capitalized names of environment variables used in this repo). For improved security, Chainlink provides the NPM package [@chainlink/env-enc](https://www.npmjs.com/package/@chainlink/env-enc) which can be used to keep environment variables in a password encrypted `.env.enc` file instead of a plaintext `.env` for additional security. More detail on environment variable management and the tooling is provided in the [Environment Variable Management](#environment-variable-management) section. 1. Set an encryption password for your environment variables to a secure password by running `npx env-enc set-pw`. This password needs to be set each time you create or restart a terminal shell session.
2. Use the command `npx env-enc set` to set the required environment variables. 3. Set any other values you intend to pass into the _secrets_ object in _Functions-request-config.js_ .

@@ -124,6 +124,8 @@ This repo uses the NPM package `@chainlink/env-enc` for keeping environment vari By default, all encrypted environment variables will be stored in a file named `.env.enc` in the root directory of this repo. This file is `.gitignore`'d. +For a full list of the Env Var names (keys) that this repo uses and has defined please look at `./env.enc.example`. + First, set the encryption password by running the command `npx env-enc set-pw`. > **NOTE:** On Windows, this command may show a security confirmation. @@ -377,6 +379,13 @@ Additionally, you can manually set a hardcoded transaction gas price in the Hard # Troubleshooting -1. When running Chainlink Functions make sure your subscription ID has your consumer contract added as an authorized consumer. Also make sure that your subscription has enough LINK balance. You do this by calling `npx hardhat functions-sub-info --network network_name_here --subid subscription_id_here` +1. If you get strange (and scary large) error output in your terminal because a transaction failed, it is super helpful to use [tenderly.co](https://tenderly.co). Once you create an account, and a project look for "Transactions" in the tab list on the left, and past in your Transaction Hash. Tenderly will look across various networks for it. It will then show you the causes for the error especially if the contract has been verified. Here is a useful video on how to debug transactions with Tenderly: + + +2. When running Chainlink Functions make sure your subscription ID has your `FunctionsConsumer` contract added as an authorized consumer. Also make sure that your subscription has enough LINK balance. You do this by calling `npx hardhat functions-sub-info --network network_name_here --subid subscription_id_here` + +3. When running Chainlink Functions with Automation you also need to ensure the Chainlink Automation upkeeps are funded to run the automation calls. The fastest way to maintain your Automation LINK subscription balance is through the Chainlink Automation web app here: [https://automation.chain.link/](https://automation.chain.link/) + +4. If you get a transaction failure when calling `npx hardhat functions-request` and its an out of gas error (you can tell from the block explorer or from [Tenderly](https://tenderly.co)) then you may need to add the optional `---requestgaslimit` flag with a value higher than than the default which is 1_500_000. For example: `npx hardhat functions-request --requestgaslimit 1750000`. Note that `./tasks/Functions-consumer/request.js` already has some logic around this that applies to some networks that require higher gas. -2. When running Chainlink Functions with Automation you also need to ensure the Chainlink Automation upkeeps are funded to run the automation calls. The fastest way to maintain your Automation LINK subscription balance is through the Chainlink Automation web app here: [https://automation.chain.link/](https://automation.chain.link/) +5. BASE Sepolia / Optimism Sepolia: if you see an error like `ProviderError: transaction underpriced: tip needed 50, tip permitted 0` then wait a few seconds and re-try. This can happen due to network spikes. Also double check the `./networks.js` file configs to make sure that `gasPrice` is set to `1000_000` as these networks can require higher request gas. diff --git a/env.enc.example b/env.enc.example new file mode 100644 index 00000000..bd73e16d --- /dev/null +++ b/env.enc.example @@ -0,0 +1,24 @@ +// The following environment variables are used in this example repo. When setting +// them using the npm env-enc package, please make sure you use the same names and casing for the +// keys, as this codebase uses them as follows. +// Reference: https://github.com/smartcontractkit/functions-hardhat-starter-kit?tab=readme-ov-file#environment-variable-management + +GITHUB_API_TOKEN +PRIVATE_KEY +SECOND_PRIVATE_KEY +ETHEREUM_SEPOLIA_RPC_URL +ETHERSCAN_API_KEY +POLYGONSCAN_API_KEY +POLYGON_MUMBAI_RPC_URL +MY_SECRET_KEY +AVALANCHE_FUJI_RPC_URL +FAKE_SECRET_1 +FAKE_SECRET_2 +FAKE_SECRET_3 +COINMARKETCAP_API_KEY +BASESCAN_API_KEY +ARBITRUM_RPC_URL +ARBISCAN_API_KEY +OPTIMISM_API_KEY +OPTIMISM_SEPOLIA_RPC_URL +BASE_SEPOLIA_RPC_URL \ No newline at end of file diff --git a/hardhat.config.js b/hardhat.config.js index fc95e9eb..75ca6efc 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -53,6 +53,8 @@ module.exports = { avalancheFujiTestnet: networks.avalancheFuji.verifyApiKey, arbitrum: networks.arbitrum.verifyApiKey, arbitrumSepolia: networks.arbitrumSepolia.verifyApiKey, + baseSepolia: networks.baseSepolia.verifyApiKey, + optimismSepolia: networks.optimismSepolia.verifyApiKey, }, customChains: [ { @@ -63,6 +65,22 @@ module.exports = { browserURL: "https://sepolia.arbiscan.io/", }, }, + { + network: "baseSepolia", + chainId: 84532, + urls: { + apiURL: "https://api-sepolia.basescan.org/api", + browserURL: "https://sepolia-explorer.base.org", + }, + }, + { + network: "optimismSepolia", + chainId: 11155420, + urls: { + apiURL: "https://api-sepolia-optimistic.etherscan.io/api", // https://docs.optimism.etherscan.io/v/optimism-sepolia-etherscan + browserURL: "https://sepolia-optimistic.etherscan.io/", + }, + }, ], }, gasReporter: { diff --git a/networks.js b/networks.js index 1d967885..280f9097 100644 --- a/networks.js +++ b/networks.js @@ -35,7 +35,7 @@ if (SECOND_PRIVATE_KEY) { const networks = { ethereum: { url: process.env.ETHEREUM_RPC_URL || "UNSET", - gasPrice: undefined, + gasPrice: undefined, // gas price for the functions request - default's to auto as per HH https://hardhat.org/hardhat-network/docs/reference#eth_gasprice nonce: undefined, accounts, verifyApiKey: process.env.ETHERSCAN_API_KEY || "UNSET", @@ -98,7 +98,7 @@ const networks = { }, polygonMumbai: { url: process.env.POLYGON_MUMBAI_RPC_URL || "UNSET", - gasPrice: 20_000_000_000, + gasPrice: 20_000_000_000, // gas price for the functions request nonce: undefined, accounts, verifyApiKey: process.env.POLYGONSCAN_API_KEY || "UNSET", @@ -167,6 +167,42 @@ const networks = { "https://02.functions-gateway.testnet.chain.link/", ], }, + baseSepolia: { + url: process.env.BASE_SEPOLIA_RPC_URL || "UNSET", // https://docs.basescan.org/v/sepolia-basescan/ + gasPrice: undefined, + nonce: undefined, + accounts, + verifyApiKey: process.env.BASESCAN_API_KEY || "UNSET", + chainId: 84532, + confirmations: DEFAULT_VERIFICATION_BLOCK_CONFIRMATIONS, + nativeCurrencySymbol: "ETH", + linkToken: "0xE4aB69C077896252FAFBD49EFD26B5D171A32410", + linkPriceFeed: "0x56a43EB56Da12C0dc1D972ACb089c06a5dEF8e69", // https://docs.chain.link/data-feeds/price-feeds/addresses?network=base&page=1 + functionsRouter: "0xf9B8fc078197181C841c296C876945aaa425B278", + donId: "fun-base-sepolia-1", + gatewayUrls: [ + "https://01.functions-gateway.testnet.chain.link/", + "https://02.functions-gateway.testnet.chain.link/", + ], + }, + optimismSepolia: { + url: process.env.OPTIMISM_SEPOLIA_RPC_URL || "UNSET", // https://docs.optimism.io/chain/networks#op-sepolia + gasPrice: undefined, + nonce: undefined, + accounts, + verifyApiKey: process.env.OPTIMISM_API_KEY || "UNSET", + chainId: 11155420, + confirmations: DEFAULT_VERIFICATION_BLOCK_CONFIRMATIONS, + nativeCurrencySymbol: "ETH", + linkToken: "0xE4aB69C077896252FAFBD49EFD26B5D171A32410", + linkPriceFeed: "0x98EeB02BC20c5e7079983e8F0D0D839dFc8F74fA", //https://docs.chain.link/data-feeds/price-feeds/addresses?network=optimism&page=1#optimism-sepolia + functionsRouter: "0xC17094E3A1348E5C7544D4fF8A36c28f2C6AAE28", + donId: "fun-optimism-sepolia-1", + gatewayUrls: [ + "https://01.functions-gateway.testnet.chain.link/", + "https://02.functions-gateway.testnet.chain.link/", + ], + }, // localFunctionsTestnet is updated dynamically by scripts/startLocalFunctionsTestnet.js so it should not be modified here localFunctionsTestnet: { url: "http://localhost:8545/", diff --git a/package-lock.json b/package-lock.json index e0fa057e..60ed3716 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "functions-hardhat-starter-kit", - "version": "0.1.0", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "functions-hardhat-starter-kit", - "version": "0.1.0", + "version": "0.1.2", "license": "MIT", "dependencies": { "@chainlink/contracts": "^0.7.1", @@ -18,7 +18,6 @@ "@nomicfoundation/hardhat-network-helpers": "^1.0.6", "@nomicfoundation/hardhat-toolbox": "^2.0.0", "@nomiclabs/hardhat-ethers": "^2.2.2", - "@nomiclabs/hardhat-etherscan": "^3.1.0", "@openzeppelin/contracts-upgradeable": "^4.9.3", "@typechain/ethers-v5": "^10.1.0", "@typechain/hardhat": "^6.1.3", @@ -1487,6 +1486,7 @@ "version": "3.1.7", "resolved": "https://registry.npmjs.org/@nomiclabs/hardhat-etherscan/-/hardhat-etherscan-3.1.7.tgz", "integrity": "sha512-tZ3TvSgpvsQ6B6OGmo1/Au6u8BrAkvs1mIC/eURA3xgIfznUZBhmpne8hv7BXUzw9xNL3fXdpOYgOQlVMTcoHQ==", + "peer": true, "dependencies": { "@ethersproject/abi": "^5.1.2", "@ethersproject/address": "^5.0.2", @@ -1507,6 +1507,7 @@ "version": "8.1.0", "resolved": "https://registry.npmjs.org/cbor/-/cbor-8.1.0.tgz", "integrity": "sha512-DwGjNW9omn6EwP70aXsn7FQJx5kO12tX0bZkaTjzdVFM6/7nhA4t0EENocKGx6D2Bch9PE2KzCUf5SceBdeijg==", + "peer": true, "dependencies": { "nofilter": "^3.1.0" }, diff --git a/package.json b/package.json index f0135172..f2da4cf5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "functions-hardhat-starter-kit", "license": "MIT", - "version": "0.1.1", + "version": "0.1.2", "description": "Tooling for interacting with Chainlink Functions", "scripts": { "prepare": "husky install", diff --git a/tasks/Functions-billing/create.js b/tasks/Functions-billing/create.js index fdf38cbc..ccdeb12f 100644 --- a/tasks/Functions-billing/create.js +++ b/tasks/Functions-billing/create.js @@ -17,7 +17,12 @@ task("functions-sub-create", "Creates a new billing subscription for Functions c const linkAmount = taskArgs.amount const confirmations = linkAmount > 0 ? networks[network.name].confirmations : 1 const consumerAddress = taskArgs.contract - const txOptions = { confirmations } + const txOptions = { + confirmations, + overrides: { + gasPrice: networks[network.name].gasPrice, + }, + } const sm = new SubscriptionManager({ signer, linkTokenAddress, functionsRouterAddress }) await sm.initialize() diff --git a/tasks/Functions-consumer/request.js b/tasks/Functions-consumer/request.js index a2c62f83..af0d63b1 100644 --- a/tasks/Functions-consumer/request.js +++ b/tasks/Functions-consumer/request.js @@ -174,9 +174,12 @@ task("functions-request", "Initiates an on-demand request from a Functions consu spinner.start( `Waiting for transaction for FunctionsConsumer contract ${contractAddr} on network ${network.name} to be confirmed...` ) - // Use a manual gas limit for the request transaction since estimated gas limit is not always accurate + // Use manual gas limits for the request transaction since estimated gas limit is not always accurate, + // and can vary significantly based on network. + higherGasNetworks = ["optimismSepolia", "baseSepolia"] // L2s appear to need more request gas. + const requestGasLimit = higherGasNetworks.includes(network.name) ? 1_750_000 : taskArgs.requestgaslimit const overrides = { - gasLimit: taskArgs.requestgaslimit, + gasLimit: requestGasLimit, } // If specified, use the gas price from the network config instead of Ethers estimated price if (networks[network.name].gasPrice) { diff --git a/tasks/utils/network.js b/tasks/utils/network.js index 45ee29b7..43faacac 100644 --- a/tasks/utils/network.js +++ b/tasks/utils/network.js @@ -7,6 +7,8 @@ const BASE_URLS = { 43113: "https://testnet.snowtrace.io/", 421614: "https://sepolia.arbiscan.io/", 42161: "https://arbiscan.io/", + 84532: "https://sepolia.basescan.org/", + 11155420: "https://sepolia-optimistic.etherscan.io/", } /** From bf5a696842bb4fd0885d881328f48afed7962b26 Mon Sep 17 00:00:00 2001 From: Zubin Pratap Date: Thu, 21 Mar 2024 16:02:00 +1100 Subject: [PATCH 2/4] Update Readme. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8ff196d6..1e48686d 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,9 @@ Install **both** of the following: - Node.js version [20](https://nodejs.org/en/download/) - Deno version [1.36](https://deno.land/manual@v1.36.4/getting_started/installation) (or the latest release of Deno v1 if a later one is available) -## Steps on live testnet +## Steps on Live (Public) Testnet -1. Clone this repository to your local machine

+1. Clone this repository to your local machine

. Also ensure that the testnet your wanting to deploy on is [supported](https://docs.chain.link/chainlink-functions/supported-networks) by Chainlink Functions. 2. Open this directory in your command line/terminal app, then run `npm install` to install all dependencies.

3. Obtain the values for following environment variables (examples only - please see `./env.enc.example` for env vars you may need): - `PRIVATE_KEY` for your development wallet - `POLYGON_MUMBAI_RPC_URL`, `ETHEREUM_SEPOLIA_RPC_URL`, or `AVALANCHE_FUJI_RPC_URL` From ecdcbef80f8d42d27964b57b4dc7fbc9ac2493ab Mon Sep 17 00:00:00 2001 From: Zubin Pratap Date: Thu, 21 Mar 2024 16:11:01 +1100 Subject: [PATCH 3/4] tiny change to test commit signing --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1e48686d..3a0a3830 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Install **both** of the following: - Node.js version [20](https://nodejs.org/en/download/) - Deno version [1.36](https://deno.land/manual@v1.36.4/getting_started/installation) (or the latest release of Deno v1 if a later one is available) -## Steps on Live (Public) Testnet +## Steps on Live (Public) Testnets 1. Clone this repository to your local machine

. Also ensure that the testnet your wanting to deploy on is [supported](https://docs.chain.link/chainlink-functions/supported-networks) by Chainlink Functions. 2. Open this directory in your command line/terminal app, then run `npm install` to install all dependencies.

From 4d47c98b71db9934e7204735341892e262556e2f Mon Sep 17 00:00:00 2001 From: Zubin Pratap Date: Thu, 21 Mar 2024 16:19:13 +1100 Subject: [PATCH 4/4] Tidy ups --- README.md | 2 +- env.enc.example | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3a0a3830..f45e79a2 100644 --- a/README.md +++ b/README.md @@ -382,7 +382,7 @@ Additionally, you can manually set a hardcoded transaction gas price in the Hard 1. If you get strange (and scary large) error output in your terminal because a transaction failed, it is super helpful to use [tenderly.co](https://tenderly.co). Once you create an account, and a project look for "Transactions" in the tab list on the left, and past in your Transaction Hash. Tenderly will look across various networks for it. It will then show you the causes for the error especially if the contract has been verified. Here is a useful video on how to debug transactions with Tenderly: -2. When running Chainlink Functions make sure your subscription ID has your `FunctionsConsumer` contract added as an authorized consumer. Also make sure that your subscription has enough LINK balance. You do this by calling `npx hardhat functions-sub-info --network network_name_here --subid subscription_id_here` +2. When running Chainlink Functions make sure your subscription ID has your `FunctionsConsumer` contract added as an authorized consumer. Also make sure that your subscription has enough LINK balance. You do this by calling `npx hardhat functions-sub-info --network network_name_here --subid subscription_id_here` to see your subscription details. If the Functions Router calculates that your subscription's balance is insufficient it will revert with a `InsufficientBalance` custom Solidity error. 3. When running Chainlink Functions with Automation you also need to ensure the Chainlink Automation upkeeps are funded to run the automation calls. The fastest way to maintain your Automation LINK subscription balance is through the Chainlink Automation web app here: [https://automation.chain.link/](https://automation.chain.link/) diff --git a/env.enc.example b/env.enc.example index bd73e16d..2b0352a2 100644 --- a/env.enc.example +++ b/env.enc.example @@ -10,15 +10,11 @@ ETHEREUM_SEPOLIA_RPC_URL ETHERSCAN_API_KEY POLYGONSCAN_API_KEY POLYGON_MUMBAI_RPC_URL -MY_SECRET_KEY AVALANCHE_FUJI_RPC_URL -FAKE_SECRET_1 -FAKE_SECRET_2 -FAKE_SECRET_3 -COINMARKETCAP_API_KEY BASESCAN_API_KEY ARBITRUM_RPC_URL ARBISCAN_API_KEY OPTIMISM_API_KEY OPTIMISM_SEPOLIA_RPC_URL -BASE_SEPOLIA_RPC_URL \ No newline at end of file +BASE_SEPOLIA_RPC_URL +COINMARKETCAP_API_KEY \ No newline at end of file