'Got "gas" is missing error when trying to run a node.js script using web3.js to interact with smart contract function
I am trying to use Web3.js to claim a token from a Smart Contract. I am meeting the error return new Error('"gas" is missing');
.
const Web3 = require('Web3');
const OptimizedTransparentUpgradeableProxy = require('./config')
var url = "https://api.avax.network/ext/bc/C/rpc";
var web3 = new Web3(url)
// ENTER PUBLIC AND PRIVATE KEY HERE
var address = '';
var private_key = '';
var address = '0x5857019c749147EEE22b1Fe63500F237F3c1B692'
var abi = ''
var contract = new web3.eth.Contract(abi, address)
var claimable = contract.methods.claimable(address).call();
//console.log("Total amount: " + claimable);
contract.methods.claimable(address).call().then(claimable => {
console.log(claimable)
})
var nonce = web3.eth.getTransactionCount(address);
var claim = contract.methods.claim();
const signed_txn = web3.eth.accounts.signTransaction(
claim, private_key = private_key
);
console.log("signed_txn")
console.log(signed_txn)
const gasPrice = web3.eth.getGasPrice();
const gasEstimate = contract.methods.claim().estimateGas({ from: address });
console.log("Sending Claim");
contract.methods.claim().send({from: address, gasPrice: gasPrice, gas: gasEstimate});
console.log("Sent")
const tx_token = web3.eth.sendSignedTransaction(signed_txn.rawTransaction);
//console.log("LP claimed: " + web3.utils.toHex(tx_token))
// var check = true;
// var claim = contract.methods.claim().buildTransaction({
// 'from': address,
// //'gasPrice': web3.toWei('50', 'gwei'),
// 'nonce': nonce,
// })
// var signed_txn = web3.eth.account.sign_transaction(claim, private_key = private_key)
// var tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
// console.log("LP claimed: " + web3.toHex(tx_token))
I've already followed the steps as outlined here https://ethereum.stackexchange.com/questions/65695/unhandledpromiserejectionwarning-error-gas-is-missing but I can't seem to get the code to work. I am trying to mimic a web3.py program that i wrote which does what i intend to. The working python version is here:
from web3 import Web3
from config import *
# Connection to RPC Node
avax = "https://api.avax.network/ext/bc/C/rpc"
web3 = Web3(Web3.HTTPProvider(avax))
print(web3.isConnected())
# ENTER PUBLIC AND PRIVATE KEY HERE
address = web3.toChecksumAddress('')
private = ''
contract = web3.eth.contract(OptimizedTransparentUpgradeableProxy.ContractAddress, abi = OptimizedTransparentUpgradeableProxy.abi)
print(contract)
claimable = contract.functions.claimable(address).call()
print("Total amount", claimable)
nonce = web3.eth.get_transaction_count(address)
check = True
claim = contract.functions.claim().buildTransaction({
'from': address,
'gasPrice': web3.toWei('50', 'gwei'),
'nonce': nonce,
})
# try:
# claim = contract.functions.claim().buildTransaction({
# 'from': address,
# 'gasPrice': web3.toWei('50', 'gwei'),
# 'nonce': nonce,
# })
# except:
# print("Transaction did not go through.")
# exit()
signed_txn = web3.eth.account.sign_transaction(claim, private_key=private)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("LP claimed: " + web3.toHex(tx_token))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|