I wrote a simple code that exchanges my eth with dai in Rinkeby using the ethToTokenSwapInput function from the Uniswap exchange protocol.
I am getting the UnhandledPromiseRejectionWarning: Error: Transaction has been reverted by the EVM
. Since I do get the transaction hash, it seems to me that something is failing inside the contract execution, but I haven't been able to spot the problem.
Can anyone be so kind to tell me what might be causing this error?
I get this message in Etherscan: Warning! Error encountered during contract execution [evm: invalid jump destination]
the error message in Terminal:
Result: 0x74e5d541b1da690f26a61e1b59ab27da6b6483125d601ea840b1f1e2912cbc28
(node:81484) UnhandledPromiseRejectionWarning: Error: Transaction has been reverted by the EVM:
{
"blockHash": "0x92971d266195178d324c6901a793882c6de6c4e9d4114d936732a44e6087b48b",
"blockNumber": 4990066,
"contractAddress": null,
"cumulativeGasUsed": 705035,
"from": "0xd0633f104cbb4f6631268f2f73ea428715253517",
"gasUsed": 42000,
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": false,
"to": "0x77db9c915809e7be439d2ab21032b1b8b58f6891",
"transactionHash": "0x74e5d541b1da690f26a61e1b59ab27da6b6483125d601ea840b1f1e2912cbc28",
"transactionIndex": 5
}
at /myproject/node_modules/web3-core-method/src/index.js:364:46
at process.internalTickCallback (internal/process/next_tick.js:77:7)
(node:81484) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
the code:
import Web3 from "web3";
import EthTx from "ethereumjs-tx";
// declare a const variable for dai exchange contract address and its abi
const daiExchangeAddress = "0x77dB9C915809e7BE439D2AB21032B1b8B58F6891";
const daiExchangeAbi = '[ABI]';
// set up Web3 to use Infura as your web3 provider
const web3 = new Web3(
new Web3.providers.HttpProvider(
"https://rinkeby.infura.io/v3/[PROJECTID]"
)
);
// declare a const variable for your address and private key
const addressFrom = "[MYADDRESS]";
const privKey =
"[PRIVATEKEY]";
// instantiate the contract
const daiExchangeContract = new web3.eth.Contract(
JSON.parse(daiExchangeAbi),
daiExchangeAddress
);
// declare a const variable that is going to be passed to the value property in a transaction object
const ETH_SOLD = web3.utils.toHex(50000000000000000); // 0.05ETH
// declare const variables to pass to ethToTokenSwapInput function
const MIN_TOKENS = web3.utils.toHex(0.2 * 10 ** 18); // 0.2 DAI
const DEADLINE = 1567123200; // 08/30/2019 @ 12:00am (UTC)
// Create a transaction object for the ethToTokenSwapInput function and encode the abi for it, which will be sent later
const encodedABI = daiExchangeContract.methods
.ethToTokenSwapInput(MIN_TOKENS, DEADLINE)
.encodeABI();
function sendSignedTx(transactionObject, cb) {
let transaction = new EthTx(transactionObject);
const privateKey = new Buffer.from(privKey, "hex");
transaction.sign(privateKey);
const serializedEthTx = transaction.serialize().toString("hex");
web3.eth.sendSignedTransaction(`0x${serializedEthTx}`, cb);
}
web3.eth.getTransactionCount(addressFrom).then(transactionNonce => {
const transactionObject = {
chainId: 4,
nonce: web3.utils.toHex(transactionNonce),
gasLimit: web3.utils.toHex(42000),
gasPrice: web3.utils.toHex(5000000),
to: daiExchangeAddress,
from: addressFrom,
data: encodedABI,
value: ETH_SOLD
};
sendSignedTx(transactionObject, function(error, result){
if(error) return console.log("error ===>", error);
console.log("sent ===>", result);
})
}
);
the code updated on August 30, 2019.
Best Answer
Inside function
sendSignedTx
, you are mixing synchronous and asynchronous function-handling.So to begin with, instead of passing a callback to function
web3.eth.sendSignedTransaction
, do:And then proceed according to the contents of
receipt
.Update according to changes in the question:
Try this: