Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample contract call and getting gas price from the fastgas chainlink contract
let ethers = require('ethers')
let provider = new ethers.providers.JsonRpcProvider(process.env.KOVAN_RPC_URL)
let address = '0x1510B61ba590751bB5dAa9757Bec1e8A0036E34f'
let abi = [
{
"inputs": [
{
"internalType": "uint256",
"name": "userProvidedSeed",
"type": "uint256"
}
],
"name": "getRandomNumber",
"outputs": [
{
"internalType": "bytes32",
"name": "requestId",
"type": "bytes32"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "requestId",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "randomness",
"type": "uint256"
}
],
"name": "rawFulfillRandomness",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "_keyHash",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "_fee",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_seed",
"type": "uint256"
}
],
"name": "requestRandomness",
"outputs": [
{
"internalType": "bytes32",
"name": "requestId",
"type": "bytes32"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "withdrawLink",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"name": "nonces",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "randomResult",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "round",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider)
const aggregatorV3InterfaceABI = [{ "inputs": [], "name": "decimals", "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "description", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint80", "name": "_roundId", "type": "uint80" }], "name": "getRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "version", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }]
const priceFeedAddr = "0x562C092bEb3a6DF77aDf0BB604F52c018E4f2814" // swap this for the fast gas address
let priceFeed = new ethers.Contract(priceFeedAddr, aggregatorV3InterfaceABI, wallet)
let gasPrice = 30000000000
const main = async () => {
roundData = await priceFeed.latestRoundData()
// gasPrice = roundData['1'] // this is what you'll use
console.log("Gas Price is now " + gasPrice)
let overrides = {
// The price (in wei) per unit of gas
gasPrice: gasPrice, // Set the price based on the fast gas here
}
let contract = new ethers.Contract(address, abi, wallet)
let contractWithSigner = contract.connect(wallet)
let currentValue = await contract.randomResult()
// console.log(currentValue)
let tx = await contractWithSigner.getRandomNumber(15, overrides)
console.log(tx.hash)
await tx.wait()
let newValue = await contract.randomResult()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment