From 1529eb742adfdc3214c0f90b90bd7a4e24c17705 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 18 Jun 2024 14:23:29 -0400 Subject: [PATCH 1/9] Gas price threshold v1 --- src/config/sidebar.ts | 4 + .../guides/gas-price-threshold.mdx | 179 ++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 src/content/chainlink-automation/guides/gas-price-threshold.mdx diff --git a/src/config/sidebar.ts b/src/config/sidebar.ts index e6083c3ef28..5f6c6320159 100644 --- a/src/config/sidebar.ts +++ b/src/config/sidebar.ts @@ -449,6 +449,10 @@ export const SIDEBAR: Partial> = { title: "Manage your Upkeeps", url: "chainlink-automation/guides/manage-upkeeps", }, + { + title: "Set a gas price threshold on your upkeep", + url: "chainlink-automation/guides/gas-price-threshold", + }, { title: "Using the StreamsLookup error handler", url: "chainlink-automation/guides/streams-lookup-error-handler", diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx new file mode 100644 index 00000000000..5439155f1fc --- /dev/null +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -0,0 +1,179 @@ +--- +section: automation +date: Last Modified +title: "Set a gas price threshold on your upkeep" +isMdx: true +whatsnext: + { + "Register Custom Logic Upkeeps": "/chainlink-automation/guides/register-upkeep", + "Register Log Trigger Upkeeps": "/chainlink-automation/guides/log-trigger", + "Automation Architecture": "/chainlink-automation/concepts/automation-architecture", + "Billing and Costs": "/chainlink-automation/overview/automation-economics", + } +--- + +import { Aside, ClickToZoom } from "@components" +import { Tabs } from "@components/Tabs" + +You can set a gas price threshold on your upkeep to prevent your upkeep from being serviced when gas prices exceed the _maximum gas price_ you specify. (This is a different gas setting than the upkeep gas limit, which limits the _amount_ of gas used.) You can set a maximum gas price on conditional upkeeps and log trigger upkeeps. + +After you set your maximum gas price, it takes a few minutes to go into effect as it syncs with the nodes. Updating your maximum gas price does not impact any upkeep where the execution is in-flight. + +## Limitations + +Do not use the gas price control when speed of execution is important. The gas price control can significantly delay your execution for conditional upkeeps and prevent execution entirely for log trigger upkeeps. + +### Gas prices spike after your transaction is in flight + +Due to the decentralized nature of the Automation network and its transaction manager, it is possible that your upkeep will be performed at a gas price higher than the maximum gas price you set. This edge case can happen when: + +1. The Automation network determines that the upkeep should be performed while the onchain gas price is below your threshold. After that check occurs, the `performUpkeep` transaction is in flight (in the mempool). +1. After the transaction is already in flight, gas prices start spiking and move above your threshold. To ensure that the node's sending key nonce does not become blocked, the node automatically increases the gas to confirm the transaction and prevent the nonce from blocking subsequent transactions. + +To avoid this edge case, increase the buffer in your gas price threshold. + +### Log trigger upkeeps are not retried + +If a log trigger upkeep is triggered by a log event, and is declared ineligible to perform the upkeep because gas prices are above your gas price threshold, the upkeep is not retried. + +## Choose your maximum gas price + +Each node compares the specified threshold to its estimate of onchain gas price. A quorum is needed before upkeeps are performed. Nodes may bid aggressively on gas prices to ensure transactions go through. If the node's gas price bid is above your maximum gas price, the node will not perform your upkeep. For example, if you set a gas price threshold of 3 gwei, your upkeep's execution may stop as soon as the node's gas price bid hits 3 gwei, even if the actual gas price is only 2 gwei. To adjust for this, you may need to set a higher gas price threshold. + +## Create a new upkeep with a gas price threshold + +To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). You need to [format and encode your offchain config](#format-and-encode-your-offchain-config) before you set the `offchainConfig` parameter. + +## Set the maximum gas price on an existing upkeep + +Set the maximum gas price using the `offchainConfig` field in your upkeep. Only the upkeep admin can set gas controls for an upkeep. This setting is not yet available in the Automation UI, so it must be done programmatically. + +To set the maximum gas price on an upkeep, follow these steps: + +1. [Format and encode your offchain config](#format-and-encode-your-offchain-config). The offchain config is where you set your maximum gas price. +1. Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the encoded value of your offchain config. + +### Format and encode your offchain config + +Currently, the only parameter that you can set in your upkeep's offchain config is `maxGasPrice`. You need to format your offchain config as a JSON object and CBOR encode it before you update it on the registry. + +1. Format your offchain config as JSON. For example: `{"maxGasPrice": 100000000000}`. + Use quotation marks only around the key, `”maxGasPrice”`. Do not use quotation marks around the value of the maximum gas price you are setting. + +1. Encode the JSON object using CBOR encoding: + + {/* prettier-ignore */} + + Solidity + Go + + ```solidity + // SPDX-License-Identifier: MIT + pragma solidity 0.8.19; + + import { CBOR } from "../../vendor/solidity-cborutils/v2.0.0/CBOR.sol" + + contract CBOREncoder { + using CBOR for CBOR.CBORBuffer; + + /// @notice encodes a max gas price to CBOR encoded bytes + /// @param maxGasPrice The max gas price + /// @return CBOR encoded bytes and the struct depth + function encode(uint256 maxGasPrice, uint256 capacity) external pure returns (bytes memory, uint256) { + CBOR.CBORBuffer memory buffer = CBOR.create(capacity); + buffer.writeString("maxGasPrice"); + buffer.writeUInt256(maxGasPrice); + return (buffer.buf.buf, buffer.depth); + } + } + + ```` + + + For Go, create a simple struct like this and encode it with the cbor package. + ```go + type UpkeepOffchainConfig struct { + MaxGasPrice *big.Int `json:"maxGasPrice" cbor:"maxGasPrice"` + } + ```` + + + + +### Send the request + +Call `setUpkeepOffchainConfig` on the registry. You can do this directly with MetaMask or an RPC call to the node. + +### Code example + +To run this example, install its dependencies: `npm install ethers cbor`. + +```typescript +const { ethers } = require("ethers") +const cbor = require("cbor") + +// Replace with your own provider +const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID") +const privateKey = "YOUR_PRIVATE_KEY" // Replace with your wallet private key +const wallet = new ethers.Wallet(privateKey, provider) + +// The contract address and ABI +const contractAddress = "0x6593c7De001fC8542bB1703532EE1E5aA0D458fD" +const abi = [ + { + inputs: [ + { + internalType: "uint256", + name: "id", + type: "uint256", + }, + { + internalType: "bytes", + name: "config", + type: "bytes", + }, + ], + name: "setUpkeepOffchainConfig", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, +] + +// Initialize the contract +const contract = new ethers.Contract(contractAddress, abi, wallet) + +// The JSON object to be encoded +const myObject = { maxGasPrice: 100000000000 } + +// Encode the JSON object to CBOR +const encodedConfig = cbor.encode(myObject) + +// The id you want to use (replace with your actual id) +const id = 1 + +// The function to call the contract +async function setUpkeepOffchainConfig() { + try { + const tx = await contract.setUpkeepOffchainConfig(id, encodedConfig) + console.log("Transaction hash:", tx.hash) + + // Wait for the transaction to be mined + const receipt = await tx.wait() + console.log("Transaction was mined in block:", receipt.blockNumber) + } catch (error) { + console.error("Error sending transaction:", error) + } +} + +// Call the function +setUpkeepOffchainConfig() +``` + +### Remove the maximum gas price + +To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x`: + +- Format your request as JSON ({"maxGasPrice": 0x}) +- Encode this request with CBOR encoding. +- Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x`. From 7805f6bc2b7569d80b655fc1d0e76156a611360a Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 18 Jun 2024 14:26:47 -0400 Subject: [PATCH 2/9] prettier pls --- src/content/chainlink-automation/guides/gas-price-threshold.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index 5439155f1fc..67aa134bc92 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -108,6 +108,7 @@ Call `setUpkeepOffchainConfig` on the registry. You can do this directly with Me To run this example, install its dependencies: `npm install ethers cbor`. +{/* prettier-ignore */} ```typescript const { ethers } = require("ethers") const cbor = require("cbor") From 8e6d9c9d564bca8d84cf4a0cdcc5f9d05e2fff22 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 18 Jun 2024 14:38:51 -0400 Subject: [PATCH 3/9] uh formatting fix? --- .../guides/gas-price-threshold.mdx | 85 ++++++++++--------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index 67aa134bc92..5118fb155bd 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -58,47 +58,45 @@ To set the maximum gas price on an upkeep, follow these steps: Currently, the only parameter that you can set in your upkeep's offchain config is `maxGasPrice`. You need to format your offchain config as a JSON object and CBOR encode it before you update it on the registry. 1. Format your offchain config as JSON. For example: `{"maxGasPrice": 100000000000}`. - Use quotation marks only around the key, `”maxGasPrice”`. Do not use quotation marks around the value of the maximum gas price you are setting. + Use quotation marks only around the key, `"maxGasPrice"`. Do not use quotation marks around the value of the maximum gas price you are setting. 1. Encode the JSON object using CBOR encoding: - {/* prettier-ignore */} - - Solidity - Go - - ```solidity - // SPDX-License-Identifier: MIT - pragma solidity 0.8.19; - - import { CBOR } from "../../vendor/solidity-cborutils/v2.0.0/CBOR.sol" - - contract CBOREncoder { - using CBOR for CBOR.CBORBuffer; - - /// @notice encodes a max gas price to CBOR encoded bytes - /// @param maxGasPrice The max gas price - /// @return CBOR encoded bytes and the struct depth - function encode(uint256 maxGasPrice, uint256 capacity) external pure returns (bytes memory, uint256) { - CBOR.CBORBuffer memory buffer = CBOR.create(capacity); - buffer.writeString("maxGasPrice"); - buffer.writeUInt256(maxGasPrice); - return (buffer.buf.buf, buffer.depth); - } - } - - ```` - - - For Go, create a simple struct like this and encode it with the cbor package. - ```go - type UpkeepOffchainConfig struct { - MaxGasPrice *big.Int `json:"maxGasPrice" cbor:"maxGasPrice"` - } - ```` - - - + {/* prettier-ignore */} + + Solidity + Go + + ```solidity + // SPDX-License-Identifier: MIT + pragma solidity 0.8.19; + + import { CBOR } from "../../vendor/solidity-cborutils/v2.0.0/CBOR.sol" + + contract CBOREncoder { + using CBOR for CBOR.CBORBuffer; + + // @notice encodes a max gas price to CBOR encoded bytes + // @param maxGasPrice The max gas price + // @return CBOR encoded bytes and the struct depth + function encode(uint256 maxGasPrice, uint256 capacity) external pure returns (bytes memory, uint256) { + CBOR.CBORBuffer memory buffer = CBOR.create(capacity); + buffer.writeString("maxGasPrice"); + buffer.writeUInt256(maxGasPrice); + return (buffer.buf.buf, buffer.depth); + } + } + ``` + + + For Go, create a simple struct like this and encode it with the cbor package. + ```go + type UpkeepOffchainConfig struct { + MaxGasPrice *big.Int `json:"maxGasPrice" cbor:"maxGasPrice"` + } + ``` + + ### Send the request @@ -106,12 +104,16 @@ Call `setUpkeepOffchainConfig` on the registry. You can do this directly with Me ### Code example -To run this example, install its dependencies: `npm install ethers cbor`. +To run this example, install its dependencies: + +```sh +npm install ethers cbor +``` {/* prettier-ignore */} ```typescript const { ethers } = require("ethers") -const cbor = require("cbor") +const { cbor } = require("cbor") // Replace with your own provider const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID") @@ -166,7 +168,6 @@ async function setUpkeepOffchainConfig() { console.error("Error sending transaction:", error) } } - // Call the function setUpkeepOffchainConfig() ``` @@ -175,6 +176,6 @@ setUpkeepOffchainConfig() To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x`: -- Format your request as JSON ({"maxGasPrice": 0x}) +- Format your request as JSON (`{"maxGasPrice": 0x}`) - Encode this request with CBOR encoding. - Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x`. From 52261b48a8005d8f384d0ad385e4f46cbac2b495 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Thu, 20 Jun 2024 17:57:20 -0400 Subject: [PATCH 4/9] Restructuring and script instructions --- .../guides/gas-price-threshold.mdx | 127 ++++++------------ 1 file changed, 40 insertions(+), 87 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index 5118fb155bd..09f7bad143f 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -40,19 +40,54 @@ If a log trigger upkeep is triggered by a log event, and is declared ineligible Each node compares the specified threshold to its estimate of onchain gas price. A quorum is needed before upkeeps are performed. Nodes may bid aggressively on gas prices to ensure transactions go through. If the node's gas price bid is above your maximum gas price, the node will not perform your upkeep. For example, if you set a gas price threshold of 3 gwei, your upkeep's execution may stop as soon as the node's gas price bid hits 3 gwei, even if the actual gas price is only 2 gwei. To adjust for this, you may need to set a higher gas price threshold. -## Create a new upkeep with a gas price threshold - -To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). You need to [format and encode your offchain config](#format-and-encode-your-offchain-config) before you set the `offchainConfig` parameter. - ## Set the maximum gas price on an existing upkeep -Set the maximum gas price using the `offchainConfig` field in your upkeep. Only the upkeep admin can set gas controls for an upkeep. This setting is not yet available in the Automation UI, so it must be done programmatically. +Set the maximum gas price using the `offchainConfig` field in your upkeep. Only the upkeep admin can set gas controls for an upkeep. This setting is not yet available in the Chainlink Automation App, so it must be done programmatically. To set the maximum gas price on an upkeep, follow these steps: 1. [Format and encode your offchain config](#format-and-encode-your-offchain-config). The offchain config is where you set your maximum gas price. 1. Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the encoded value of your offchain config. +### Run the script + +The [Automation gas threshold](https://github.com/smartcontractkit/smart-contract-examples/tree/main/automation-gas-threshold) script encodes and sets your offchain config, which includes the maximum gas price in wei. + +1. To run this example, clone the repo and install its dependencies: + +```sh +git clone https://github.com/smartcontractkit/ && cd automation-gas-threshold +``` + +```sh +npm install +``` + +1. Set the following variables: + +- `YOUR_RPC_URL`: The RPC URL for your provider (such as Alchemy or Infura) +- `YOUR_PRIVATE_KEY`: Your wallet's private key +- `YOUR_UPKEEP_ID`: The ID of the Automation upkeep you want to configure. +- Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for maxGasPrice. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` + +1. Run the script: + +```sh +node index.js +``` + +### Remove the maximum gas price + +To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x`: + +- Format your request as JSON (`{"maxGasPrice": 0x}`) +- Encode this request with CBOR encoding. +- Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x`. + +## Create a new upkeep with a gas price threshold + +To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). You need to format and encode your offchain config before you set the `offchainConfig` parameter: + ### Format and encode your offchain config Currently, the only parameter that you can set in your upkeep's offchain config is `maxGasPrice`. You need to format your offchain config as a JSON object and CBOR encode it before you update it on the registry. @@ -97,85 +132,3 @@ Currently, the only parameter that you can set in your upkeep's offchain config ``` - -### Send the request - -Call `setUpkeepOffchainConfig` on the registry. You can do this directly with MetaMask or an RPC call to the node. - -### Code example - -To run this example, install its dependencies: - -```sh -npm install ethers cbor -``` - -{/* prettier-ignore */} -```typescript -const { ethers } = require("ethers") -const { cbor } = require("cbor") - -// Replace with your own provider -const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID") -const privateKey = "YOUR_PRIVATE_KEY" // Replace with your wallet private key -const wallet = new ethers.Wallet(privateKey, provider) - -// The contract address and ABI -const contractAddress = "0x6593c7De001fC8542bB1703532EE1E5aA0D458fD" -const abi = [ - { - inputs: [ - { - internalType: "uint256", - name: "id", - type: "uint256", - }, - { - internalType: "bytes", - name: "config", - type: "bytes", - }, - ], - name: "setUpkeepOffchainConfig", - outputs: [], - stateMutability: "nonpayable", - type: "function", - }, -] - -// Initialize the contract -const contract = new ethers.Contract(contractAddress, abi, wallet) - -// The JSON object to be encoded -const myObject = { maxGasPrice: 100000000000 } - -// Encode the JSON object to CBOR -const encodedConfig = cbor.encode(myObject) - -// The id you want to use (replace with your actual id) -const id = 1 - -// The function to call the contract -async function setUpkeepOffchainConfig() { - try { - const tx = await contract.setUpkeepOffchainConfig(id, encodedConfig) - console.log("Transaction hash:", tx.hash) - - // Wait for the transaction to be mined - const receipt = await tx.wait() - console.log("Transaction was mined in block:", receipt.blockNumber) - } catch (error) { - console.error("Error sending transaction:", error) - } -} -// Call the function -setUpkeepOffchainConfig() -``` - -### Remove the maximum gas price - -To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x`: - -- Format your request as JSON (`{"maxGasPrice": 0x}`) -- Encode this request with CBOR encoding. -- Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x`. From 5211a5772ef33e703f38621d11959f739c5d7816 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Fri, 21 Jun 2024 14:45:10 -0400 Subject: [PATCH 5/9] Update removal instructions --- .../chainlink-automation/guides/gas-price-threshold.mdx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index 09f7bad143f..e884b9859ca 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -56,7 +56,7 @@ The [Automation gas threshold](https://github.com/smartcontractkit/smart-contrac 1. To run this example, clone the repo and install its dependencies: ```sh -git clone https://github.com/smartcontractkit/ && cd automation-gas-threshold +git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/automation-gas-threshold ``` ```sh @@ -78,11 +78,12 @@ node index.js ### Remove the maximum gas price -To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x`: +To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x00`: -- Format your request as JSON (`{"maxGasPrice": 0x}`) - Encode this request with CBOR encoding. -- Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x`. +- Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x00`. + +If you're using [the gas threshold script](#run-the-script), set the `offchainConfig` variable in the script to `0`. ## Create a new upkeep with a gas price threshold From 2f883217c5dc269df1f24d7819657006be6e84ca Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Mon, 24 Jun 2024 11:18:54 -0400 Subject: [PATCH 6/9] Clarifications --- .../guides/gas-price-threshold.mdx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index e884b9859ca..82158ce2a46 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -21,7 +21,7 @@ After you set your maximum gas price, it takes a few minutes to go into effect a ## Limitations -Do not use the gas price control when speed of execution is important. The gas price control can significantly delay your execution for conditional upkeeps and prevent execution entirely for log trigger upkeeps. +Do not set a gas price threshold when speed of execution is important. The gas price threshold can significantly delay your execution for conditional upkeeps and prevent execution entirely for log trigger upkeeps. ### Gas prices spike after your transaction is in flight @@ -78,16 +78,23 @@ node index.js ### Remove the maximum gas price -To remove the maximum gas price, you can set the value of your `offchainConfig` back to `0x00`: +To remove the maximum gas price from your upkeep, set the value of your `offchainConfig` back to `0x00`: - Encode this request with CBOR encoding. - Run `setUpkeepOffchainConfig` on the registry using your upkeep ID and the CBOR encoded value for `0x00`. -If you're using [the gas threshold script](#run-the-script), set the `offchainConfig` variable in the script to `0`. +If you're using [the gas threshold script](#run-the-script), set the `offchainConfig` variable in the script to `0`: + +```js +// Change this value from `{"maxGasPrice":2000000000}` to `0`: +const offchainConfig = 0 +``` ## Create a new upkeep with a gas price threshold -To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). You need to format and encode your offchain config before you set the `offchainConfig` parameter: +To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). (The [Chainlink Automation App](https://automation.chain.link/) does not yet support setting a gas price threshold when creating a new upkeep.) + +You need to format and encode your offchain config before you set the `offchainConfig` parameter. You can adjust [the gas threshold script](#run-the-script) to get the encoded value for your offchain config and set that as the `offchainConfig` variable when [creating your new upkeep](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep), or you can encode your config using the Solidity or Go examples below. ### Format and encode your offchain config @@ -107,7 +114,7 @@ Currently, the only parameter that you can set in your upkeep's offchain config // SPDX-License-Identifier: MIT pragma solidity 0.8.19; - import { CBOR } from "../../vendor/solidity-cborutils/v2.0.0/CBOR.sol" + import { CBOR } from "@chainlink/contracts/src/v0.8/vendor/solidity-cborutils/v2.0.0/CBOR.sol" contract CBOREncoder { using CBOR for CBOR.CBORBuffer; @@ -125,7 +132,7 @@ Currently, the only parameter that you can set in your upkeep's offchain config ``` - For Go, create a simple struct like this and encode it with the cbor package. + For Go, create a simple struct like this and encode it with the [cbor package](https://github.com/fxamacker/cbor). ```go type UpkeepOffchainConfig struct { MaxGasPrice *big.Int `json:"maxGasPrice" cbor:"maxGasPrice"` From b5b9c4caeebfd27c875b275f674c3840c7d46ec9 Mon Sep 17 00:00:00 2001 From: Crystal Gomes Date: Mon, 24 Jun 2024 18:13:23 -0400 Subject: [PATCH 7/9] Apply suggestions from code review Co-authored-by: Karim H. <98668332+khadni@users.noreply.github.com> --- .../guides/gas-price-threshold.mdx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index 82158ce2a46..1199581777f 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -15,17 +15,18 @@ whatsnext: import { Aside, ClickToZoom } from "@components" import { Tabs } from "@components/Tabs" -You can set a gas price threshold on your upkeep to prevent your upkeep from being serviced when gas prices exceed the _maximum gas price_ you specify. (This is a different gas setting than the upkeep gas limit, which limits the _amount_ of gas used.) You can set a maximum gas price on conditional upkeeps and log trigger upkeeps. +You can set a gas price threshold for your upkeep to prevent it from being serviced when gas prices exceed the _maximum gas price_ you specify. You can set a maximum gas price on conditional upkeeps and log trigger upkeeps. +**Note:** This is a different gas setting than the upkeep gas limit, which limits the _amount_ of gas used. After you set your maximum gas price, it takes a few minutes to go into effect as it syncs with the nodes. Updating your maximum gas price does not impact any upkeep where the execution is in-flight. ## Limitations -Do not set a gas price threshold when speed of execution is important. The gas price threshold can significantly delay your execution for conditional upkeeps and prevent execution entirely for log trigger upkeeps. +Do not set a gas price threshold when speed of execution is important. The gas price threshold can significantly delay the execution of conditional upkeeps and prevent execution entirely for log trigger upkeeps. ### Gas prices spike after your transaction is in flight -Due to the decentralized nature of the Automation network and its transaction manager, it is possible that your upkeep will be performed at a gas price higher than the maximum gas price you set. This edge case can happen when: +Due to the decentralized nature of the Automation network and its transaction manager, your upkeep may be performed at a gas price higher than the maximum gas price you set. This edge case can happen when: 1. The Automation network determines that the upkeep should be performed while the onchain gas price is below your threshold. After that check occurs, the `performUpkeep` transaction is in flight (in the mempool). 1. After the transaction is already in flight, gas prices start spiking and move above your threshold. To ensure that the node's sending key nonce does not become blocked, the node automatically increases the gas to confirm the transaction and prevent the nonce from blocking subsequent transactions. @@ -68,7 +69,7 @@ npm install - `YOUR_RPC_URL`: The RPC URL for your provider (such as Alchemy or Infura) - `YOUR_PRIVATE_KEY`: Your wallet's private key - `YOUR_UPKEEP_ID`: The ID of the Automation upkeep you want to configure. -- Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for maxGasPrice. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` +- Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for `maxGasPrice`. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` 1. Run the script: @@ -92,7 +93,7 @@ const offchainConfig = 0 ## Create a new upkeep with a gas price threshold -To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). (The [Chainlink Automation App](https://automation.chain.link/) does not yet support setting a gas price threshold when creating a new upkeep.) +To create a new upkeep with a gas threshold in place, you can [create a conditional upkeep or log trigger upkeep programmatically](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep). **Note:** The [Chainlink Automation App](https://automation.chain.link/) does not yet support setting a gas price threshold when creating a new upkeep. You need to format and encode your offchain config before you set the `offchainConfig` parameter. You can adjust [the gas threshold script](#run-the-script) to get the encoded value for your offchain config and set that as the `offchainConfig` variable when [creating your new upkeep](/chainlink-automation/guides/register-upkeep-in-contract#register-the-upkeep), or you can encode your config using the Solidity or Go examples below. From 5e801b244a7be3145b5134af15f57d96a11a3f22 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Mon, 24 Jun 2024 18:20:43 -0400 Subject: [PATCH 8/9] indent --- .../guides/gas-price-threshold.mdx | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index 1199581777f..e49e96993a7 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -54,28 +54,28 @@ To set the maximum gas price on an upkeep, follow these steps: The [Automation gas threshold](https://github.com/smartcontractkit/smart-contract-examples/tree/main/automation-gas-threshold) script encodes and sets your offchain config, which includes the maximum gas price in wei. -1. To run this example, clone the repo and install its dependencies: + 1. To run this example, clone the repo and install its dependencies: -```sh -git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/automation-gas-threshold -``` + ```sh + git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/automation-gas-threshold + ``` -```sh -npm install -``` + ```sh + npm install + ``` -1. Set the following variables: + 1. Set the following variables: -- `YOUR_RPC_URL`: The RPC URL for your provider (such as Alchemy or Infura) -- `YOUR_PRIVATE_KEY`: Your wallet's private key -- `YOUR_UPKEEP_ID`: The ID of the Automation upkeep you want to configure. -- Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for `maxGasPrice`. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` + - `YOUR_RPC_URL`: The RPC URL for your provider (such as Alchemy or Infura) + - `YOUR_PRIVATE_KEY`: Your wallet's private key + - `YOUR_UPKEEP_ID`: The ID of the Automation upkeep you want to configure. + - Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for `maxGasPrice`. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` -1. Run the script: + 1. Run the script: -```sh -node index.js -``` + ```sh + node index.js + ``` ### Remove the maximum gas price From 34f2b4b63ccfbd0e773c70be568c0fc4ecbf4b95 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Mon, 24 Jun 2024 18:25:43 -0400 Subject: [PATCH 9/9] indentation fix --- .../guides/gas-price-threshold.mdx | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/content/chainlink-automation/guides/gas-price-threshold.mdx b/src/content/chainlink-automation/guides/gas-price-threshold.mdx index e49e96993a7..5b398798411 100644 --- a/src/content/chainlink-automation/guides/gas-price-threshold.mdx +++ b/src/content/chainlink-automation/guides/gas-price-threshold.mdx @@ -54,28 +54,28 @@ To set the maximum gas price on an upkeep, follow these steps: The [Automation gas threshold](https://github.com/smartcontractkit/smart-contract-examples/tree/main/automation-gas-threshold) script encodes and sets your offchain config, which includes the maximum gas price in wei. - 1. To run this example, clone the repo and install its dependencies: +1. To run this example, clone the repo and install its dependencies: - ```sh - git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/automation-gas-threshold - ``` + ```sh + git clone https://github.com/smartcontractkit/smart-contract-examples.git && cd smart-contract-examples/automation-gas-threshold + ``` - ```sh - npm install - ``` + ```sh + npm install + ``` - 1. Set the following variables: +1. Set the following variables: - - `YOUR_RPC_URL`: The RPC URL for your provider (such as Alchemy or Infura) - - `YOUR_PRIVATE_KEY`: Your wallet's private key - - `YOUR_UPKEEP_ID`: The ID of the Automation upkeep you want to configure. - - Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for `maxGasPrice`. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` + - `YOUR_RPC_URL`: The RPC URL for your provider (such as Alchemy or Infura) + - `YOUR_PRIVATE_KEY`: Your wallet's private key + - `YOUR_UPKEEP_ID`: The ID of the Automation upkeep you want to configure. + - Within the `offchainConfig` variable, set your `maxGasPrice` in wei. Do not use quotation marks around the value you set for `maxGasPrice`. If this string is formatted incorrectly, the feature does not work. Here's an example of correct formatting: `{"maxGasPrice":2000000000}` - 1. Run the script: +1. Run the script: - ```sh - node index.js - ``` + ```sh + node index.js + ``` ### Remove the maximum gas price