I get this error when executing swap token for ether on arb detection.
Error Message: execution reverted
(uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
at
getReserves()
at
Opcode: [STATICCALL]
in
0xbc03cf2c6443d9bb25c8fd9b701d9eb22f85f584
Opcode: [CALL]
in
0xbc03cf2c6443d9bb25c8fd9b701d9eb22f85f584
In nodejs, the error response is:
err...Error: Transaction has been reverted by the EVM:
{
"blockHash": "0x1c668xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"blockNumber": 8143875,
"contractAddress": null,
"cumulativeGasUsed": 922169,
"from": "0xab3388xxxxxxxxxxxxx",
"gasUsed": 922169,
"logsBloom": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000
Since my buy token function in solidity works normally when I call it from truffle console, I was wondering what could be a likely cause.
Here is the buy token function:
function buyCryptoOnUniswap(uint256 etherCost , address cryptoToken) public payable returns (uint256) {
//address cryptoToken = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
if(etherCost > address(this).balance){
return 0;
}
uint deadline = now + 300; // using 'now' for convenience, for mainnet pass deadline from frontend!
uint[] memory amounts = usi.swapExactETHForTokens.value(etherCost)(0, getPathForETHToToken(cryptoToken), address(this), deadline);
uint256 outputTokenCount = uint256(amounts[amounts.length - 1]);
return outputTokenCount;
}
Here is a typical transaction that depicts this. I hope someone can clue me in on particular or likely causes of such.
Thanks.
UPDATE
With guidance from @goodvibration, I found that the error seems to be from this portion of code instead:
function buyCryptoOnKyber(uint256 etherCost , address cryptoToken) public payable returns(uint256){
//address cryptoToken = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
if(etherCost > address(this).balance){
return 0;
}
KyberNetworkProxyInterface proxy = getKyberExchange();
ERC20 token = ERC20(cryptoToken);
uint minRate;
(, minRate) = proxy.getExpectedRate(ETH_TOKEN_ADDRESS, token, etherCost);
//will send back tokens to this contract's address
uint destAmount = proxy.swapEtherToToken.value(etherCost)(token, minRate);
//send received tokens to destination address
require(token.transfer(address(this), destAmount));
// refund leftover ETH to user
//msg.sender.call.value(address(this).balance)("");
}
function kyberToUniSwapArb(address fromAddress,address toAddress, uint ethAmount) public payable returns (uint256){
uint256 cryptoAmountPurchased = buyCryptoOnKyber( ethAmount , fromAddress);
sellCryptoOnUniswap(cryptoAmountPurchased , toAddress);
}
function sellCryptoOnUniswap(uint256 cryptoQty , address cryptoToken) public payable returns(uint256){
///selling code
}
EDIT