Könnte mir jemand zeigen, was hier das Problem ist?
Ich habe versucht, ERC20-Tokens mit der ERC20-Funktion „transferFrom“ zu senden. Ich habe eine sendSignedTransaction ausgeführt und folgende Fehlermeldung erhalten:
Error: Transaction has been reverted by the EVM:
{
"blockHash": "0xf8c3f5d7552bce0c3191160af6bedd1dccdee15de1605301d225b54d84bc219c",
"blockNumber": 5432016,
"contractAddress": null,
"cumulativeGasUsed": 576475,
"from": "0xc17fe972be596e119d7e51270f1c7d3bda726db9",
"gasUsed": 30631,
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": false,
"to": "0x6c9ba85bb7d0acba49aa13e1882565efd823a4c9",
"transactionHash": "0x338a60122583f3aff3c797dfd8fe5c25b8201ec1d1baff76af0a140dea20f8ce",
"transactionIndex": 3
}
at /Users/gemcutter/demo_peak/node_modules/web3-core-method/src/index.js:364:46
at process._tickCallback (internal/process/next_tick.js:68:7)````
Der Ausführungscode lautet wie folgt:
var tx=myContract2.methods.transferFrom(from,to,amount)
var encodedTx = tx.encodeABI();
tx.estimateGas({to:to, data:encodedTx, from:approved}).then((gasAmount) => {
console.log('app.js: estimated gas: ' + gasAmount.toString(10));
web3.eth.getTransactionCount(from).then(_nonce => {
nonce = web3.utils.toHex(_nonce);
console.log('app.js: tx nonce: ' + nonce + ' at account[0]: ' + to);
var tx = new ethTx({
nonce: nonce,
gasPrice: gasPrice,
gasLimit: gasLimit,
from: approved,
to: contract2Addr,
data: encodedTx
},{chain: 'rinkeby' , hardfork: 'petersburg'});
tx.sign(privKey);
var signedRawTx = '0x' + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(signedRawTx)
.on('receipt', receipt => {
console.log('app.js: signed tx receipt:\n' + JSON.stringify(receipt));
console.log('Successfully transferred a token!');
}).catch((err) => {
console.log(err);
});
});
}).then((result) => {
console.log('Transfered tokens:'+JSON.stringify(result));
}).catch((err) => {
console.log('ERROR: app.js: Gas Estimate failed');
console.log(err);
});
Der Smart Contract sieht wie folgt aus:
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = balances[from].sub(tokens);
allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
balances[to] = balances[to].add(tokens);
emit Transfer(from, to, tokens);
return true;
}
Der Vertrag wurde erfolgreich bereitgestellt.
Ich verwende folgende Versionen. Truffle v5.0.3, web3@1.0.0-beta.37, node.js v10.16.0
Ich habe etherscan.io überprüft, weiß aber nicht, wie ich damit umgehen soll.
Möglicher Grund Nr. 1:
balances[from] = balances[from].sub(tokens);
Diese Zeile würde zurückkehren, wenn balances[from] < tokens
.
Möglicher Grund Nr. 2:
allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
Diese Zeile würde zurückkehren, wenn allowed[from][msg.sender] < tokens
.
Um das zu vermeiden, müssen Sie zuerst anrufen approve(from,tokens)
.
Möglicher Grund Nr. 3:
balances[to] = balances[to].add(tokens);
Diese Zeile würde zurückkehren, wenn ~uint256(0) - balances[to] < tokens
.
Ich vermute, dass Ihr Funktionsaufruf aus Grund Nr. 2 zurückkehrt.
gute Stimmung
myContract2.methods.approve(from,amount)
zuerst ausführen.