I have a written contract in which I use functions like swapExactETHForTokens in a payable way. This one works for me.
When I try to execute the non-payable function of swapExactTokensForETH from my own contract, I get a "Fail" error, I can't find the reason for this whole thing.
My solidity code:
/* DEVELOP --> */
function swapExactTokensForETHOwn(address _tokenIn , uint256 _amountIn , uint256 _amountOutMin, uint256 deadline, address _contractAddress) external {
IERC20 erc20 = IERC20(_tokenIn);
erc20.transferFrom(msg.sender, _contractAddress, _amountIn);
erc20.approve(UNISWAP_ROUTER_ADDRESS, _amountIn);
//IERC20(token).transferFrom(msg.sender, ContractAddr , amountIn);
// require( IERC20(_tokenIn).transferFrom(msg.sender, _contractAddress, _amountIn) , 'TransferFrom failed.');
// require( IERC20(_tokenIn).approve(UNISWAP_ROUTER_ADDRESS, _amountIn) , 'Aprove failed' );
address[] memory path = new address[](2);
path[0] = _tokenIn;
path[1] = WETHR;
uniswap.swapExactTokensForETH(
_amountIn,
_amountOutMin,
path,
msg.sender,
deadline
);
}
My code in Javascript:
try {
// TRANSACTION REALIZATE
let qtyOutputMin = Math.pow(10, 18)*0.180053; // calculate manual // change and operate with usdc // OUTPUT TOKEN QUANTITY
let qtyInputMin = web3.utils.toHex("100000000000000000000");
const deadline = Math.floor(Date.now() / 1000) + 60 * 10; // 20 minutes from the current Unix time
// calculatePriceOfGas();
// function (address _tokenIn ,uint256 _amountOutMin, uint256 _amountIn, uint256 deadline, address _contractAddress)
var swap = tokenSwapContract.methods.swapExactTokensForETHOwn(_tokenIn, qtyInputMin, web3.utils.toHex("0") , deadline, personalContractAddress)
var encodedABI = swap.encodeABI()
var tx = {
from: activeAccount.address,
to: personalContractAddress,
gas: 392671,
gasPrice: 30000000000,
data: encodedABI
};
var signedTx = await activeAccount.signTransaction(tx)
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('transactionHash', function(hash) {
console.log("transactionHash", hash);
})
.on('confirmation', function(confirmationNumber, receipt) {
console.log("confirmation", confirmationNumber, receipt);
const gasUsed = tx.receipt.gasUsed;
console.log("Gas Used in Transaction --> " + gasUsed);
})
.on('receipt', function(receipt) {
console.log("receipt" , receipt);
return receipt;
// logic here -->
// recepti**
})
.on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
console.error("Error:", error, "Receipt:", receipt)
});
}
catch(error) {
console.log(error)
}
As I mentioned I have other functions in my contract, only payable ones like "swapExactETHForTokens" work for me. When I try to exchange tokens for ETH I can't get it to work
I also tried to run the function outside of my contract using Uniswap's own and it worked fine. When I erase the lines
IERC20 erc20 = IERC20(_tokenIn);
erc20.transferFrom(msg.sender, _contractAddress, _amountIn);
erc20.approve(UNISWAP_ROUTER_ADDRESS, _amountIn);
The error I get is: Fail with error 'TransferHelper: TRANSFER_FROM_FAILED', When I have them the error is the following: Error: Error: Transaction has been reverted by the EVM:
Here examples with approve and without approve: https://ropsten.etherscan.io/tx/0xce81e4d043ca8345dba4a9c94736074725ec6eca3f785888f5a4ae5111c684a9 https://ropsten.etherscan.io/tx/0x367fb91ee0a37d4b4f7fc535d48cfce487cc5d99e18dd0040d103dcfbb0aab81
Any extra information I can provide.
Thank you
versions: Truffle v5.3.10 (core: 5.3.10) Solidity - ^ 0.7.0 (solc-js) Node v14.15.4 Web3.js v1.3.6