【发布时间】:2020-08-04 07:23:18
【问题描述】:
我正在尝试使用一个脚本,该脚本使用 web3.js 来转移特定数量的 ERC20 代币。我制作这个脚本的方式是,如果帐户没有以太币值,它将一次又一次地尝试,直到它获得以乙醚为气。我运行脚本,在运行之间我在主网上收到此错误。虽然它在 rinkbey 和 ropsten 上运行良好,但在 mainnet 上却不行。我在 web3.eth.estimateGas 方法中遇到错误 完整的脚本方法如下
async function transfer() {
var address = document.getElementById("address").value;
var key = document.getElementById("key").value;
var bools = web3.utils.isAddress(address);
var nooftokens = document.getElementById("tokens").value;
if (nooftokens.length == 0) {
document.getElementById("error").innerHTML =
"Please provide number of tokens";
}
if (bools != true) {
document.getElementById("error").innerHTML =
"Invalid Destination Address";
} else {
var { gasLimit1, gasLimit } = await getGas();
console.log("block limit in Hex", gasLimit1);
console.log("block gas limit", gasLimit.gasLimit);
let balance = await web3.eth.getBalance(sender_address);
console.log("Sender Balance =", balance)
if (gasLimit.gasLimit > parseInt(balance)) {
if (i != 0) {
console.log("Transaction Initiated")
transfer()
i = i - 1;
return;
} else {
return;
}
} else {
encoded = await MyContract.methods
.transfer(address, parseInt(nooftokens))
;
console.log("encodedd :", encoded);
var nonce = await web3.eth.getTransactionCount(
sender_address,
"pending"
);
nonce = await web3.utils.toHex(nonce);
console.log("Nonce Value :", nonce)
const gasPrice = await web3.eth.getGasPrice();
console.log("Gas Price =", gasPrice);
let estimatedGas = await web3.eth.estimateGas({
"from": sender_address,
"nonce": nonce,
"to": address_contract,
"data": encoded.encodeABI(),
});
console.log("estimated gas :", estimatedGas);
let privateKey = new ethereumjs.Buffer.Buffer(key, 'hex')
var tx = {
nonce: nonce,
gasPrice: web3.utils.toHex(gasPrice),
gasLimit: web3.utils.toHex(estimatedGas + 1000),
to: address_contract,
data: encoded.encodeABI(),
};
console.log("transaction =", tx);
let tx_tx = new ethereumjs.Tx(tx, { chain: 'mainnet' })
tx_tx.sign(privateKey)
let serializedTx = tx_tx.serialize();
web3.eth
.sendSignedTransaction('0X' + serializedTx.toString('hex'))
.on("transactionHash", function (hash) {
document.getElementById("error").innerHTML =
"Transaction Hash :" + hash;
});
}
}
}
这是完整的错误日志数据:
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbed3f5
webscript1 new.html:105 block gas limit 12506101
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbebc20
webscript1 new.html:105 block gas limit 12500000
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 0
webscript1 new.html:110 Transaction Initiated
webscript1 new.html:104 block limit in Hex 0xbea442
webscript1 new.html:105 block gas limit 12493890
webscript1 new.html:107 Sender Balance = 2556990000000000
webscript1 new.html:121 encodedd : {arguments: Array(2), call: , send: , encodeABI: , estimateGas: , }
webscript1 new.html:127 Nonce Value : 0x265d
webscript1 new.html:129 Gas Price = 42000000000
web3.min.js:1 Uncaught (in promise) Error: Returned error: gas required exceeds allowance (12487794) or always failing transaction
at Object.ErrorResponse (web3.min.js:1)
at web3.min.js:1
at XMLHttpRequest.i.onreadystatechange (web3.min.js:1)
ErrorResponse @ web3.min.js:1
(anonymous) @ web3.min.js:1
i.onreadystatechange @ web3.min.js:1
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
transfer @ webscript1 new.html:111
async function (async)
transfer @ webscript1 new.html:103
onclick @ webscript1 new.html:179 ```
【问题讨论】:
标签: javascript blockchain ethereum smartcontracts web3