Skip to content

Commit

Permalink
Additional Networks Added.
Browse files Browse the repository at this point in the history
1. Optimism Sepolia
2. Base Sepolia.

End to end CLI workflows have been tested.
  • Loading branch information
zeuslawyer committed Mar 19, 2024
1 parent db09771 commit 297e5a1
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 13 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ Install **both** of the following:

1. Clone this repository to your local machine<br><br>
2. Open this directory in your command line/terminal app, then run `npm install` to install all dependencies.<br><br>
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/))
<br><br>
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.<br>
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_ .<br><br>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
<iframe width="360" height="215" src="https://www.youtube.com/embed/90GN9Ut8LhU?si=iLhHegpG1Mq59qtJ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

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. <b>BASE Sepolia / Optimism Sepolia:</b> 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.
24 changes: 24 additions & 0 deletions env.enc.example
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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: {
Expand Down
40 changes: 38 additions & 2 deletions networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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/",
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 6 additions & 1 deletion tasks/Functions-billing/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions tasks/Functions-consumer/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions tasks/utils/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
}

/**
Expand Down

0 comments on commit 297e5a1

Please sign in to comment.