1

I am using solidity to write a program that interacts with exchanges

It is well known that the contract.function.call() used in web3 can execute transactions locally and get the returned result, but this kind of thing is not found in solidity. Visual inspection can only be converted to view to get the returned result. I added the exchange contract interface view, and then when I call it in solidity, there will be an error, the original interface code ↓

interface pancackswap_interface {

// Original interface of exchange contract 👇
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external returns (uint[] memory amounts);
}

The problem now is that the original interface is called to send the transaction normally, and the return value cannot be obtained, but the view will report an error, although it can also be connected to the exchange contract

"message": "execution reverted: TransferHelper: TRANSFER_FROM_FAILED" Report this error I checked the official documents and said it was because of the insufficient amount or something, but I think it has nothing to do with that

Because when I use web3 to call the original interface, using call(), I can get the return value normally

My current requirement is to get the return value without on-chain transactions in solidity, just like the call() of web3. Is there such a method?

Below is my error code

interface pancackswap_interface {

    // Exchange contract interface. view is added by me
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
    external view returns (uint[] memory amounts); 
}

contract my_contract {
    // contract router address
    address pancack_swap_contract = 0x10ED43C718714eb63d5aA57B78B54704E256024E;

    // Create contract object
    pancackswap_interface use_pancake = pancackswap_interface(pancack_swap_contract);

        // error function
    function view_token_for_eth_swap(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) public view returns(uint[] memory amounts) {
            return use_pancake.swapExactTokensForETH(amountIn, amountOutMin, path, to, deadline);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.