Нужна помощь: Ошибка: транзакция была отменена EVM:

Может ли кто-нибудь указать мне, в чем здесь проблема?

Я попытался отправить токены ERC20 с помощью функции ERC20 trasferFrom. Я выполнил sendSignedTransaction и получил следующую ошибку:

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)````

код выполнения следующий:

    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);
    });

Смарт-контракт выглядит следующим образом:

    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;
    }

Контракт был успешно развернут.

Я использую следующие версии. Трюфель v5.0.3, web3@1.0.0-beta.37, node.js v10.16.0

Я проверил etherscan.io, но не знаю, как с этим бороться.

Вы должны выполнить myContract2.methods.approve(from,amount)сначала.

Ответы (1)

Возможная причина №1:

balances[from] = balances[from].sub(tokens);

Эта строка вернется, если balances[from] < tokens.


Возможная причина №2:

allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);

Эта строка вернется, если allowed[from][msg.sender] < tokens.

Чтобы избежать этого, вы должны сначала позвонить approve(from,tokens).


Возможная причина №3:

balances[to] = balances[to].add(tokens);

Эта строка вернется, если ~uint256(0) - balances[to] < tokens.


Я предполагаю, что ваш вызов функции возвращается по причине № 2.

Спасибо за ответы. Это щелкнуло меня, где искать. Я проверил, и причина была № 2. И еще одна причина заключалась в том, что я использовал неправильный аккаунт в Metamask.