Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Automation gas threshold script #68

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions automation-gas-threshold/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Setting a gas price threshold on Chainlink Automation upkeeps

This project provides a script you can use to configure and set a maximum gas price for your Automation upkeep.

## Installation

To set up the project, follow these steps:

1. Clone the repository:
```bash
git clone https://github.com/smartcontractkit/automation-gas-threshold
```
1. Navigate to the directory for this script:
```bash
cd automation-gas-threshold
```
1. Install the required dependencies:

```bash
npm install
```

## Usage

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.

1. 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.

`{"maxGasPrice":2000000000}`

1. Run the script:
```bash
node index.js
```
68 changes: 68 additions & 0 deletions automation-gas-threshold/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { ethers } = require('ethers');
const cbor = require('cbor');

// Replace with your own provider
const provider = new ethers.providers.JsonRpcProvider('YOUR_RPC_URL');
const privateKey = 'YOUR_PRIVATE_KEY'; // Replace with your wallet private key
const wallet = new ethers.Wallet('0x...privateKey', provider);

// Replace with the upkeep ID you want to use
const id = 'YOUR_UPKEEP_ID';

// The string to be encoded, representing your offchain config
thedriftofwords marked this conversation as resolved.
Show resolved Hide resolved
// maxGasPrice is in wei. Do not use quotation marks around the value.
const offchainConfig = {"maxGasPrice":2000000000};


// The contract address and ABI
const contractAddress = 'RegistryAddress';
// Registry v2.1 ABI from NPM
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);

// Encode the string to CBOR
const encodedConfig = cbor.encode(offchainConfig);
const hexConfig = encodedConfig.toString('hex');

// Add the 0x prefix
const hexConfigWithPrefix = '0x' + hexConfig;

console.log('hexConfigWithPrefix:', hexConfigWithPrefix);

// The function to call the contract
async function setUpkeepOffchainConfig() {
try {
const tx = await contract.setUpkeepOffchainConfig(id, hexConfigWithPrefix);
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();
12 changes: 12 additions & 0 deletions automation-gas-threshold/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "automation-gas-threshold",
"version": "1.0.0",
"description": "Set a gas price threshold on Automation upkeeps",
"main": "index.js",
"author": "",
"license": "MIT",
"dependencies": {
"cbor": "^9.0.2",
"ethers": "^5.7.2"
}
}