0
ganache-cli --fork rpc@12121 --unblock address_with_ether

I've set the forked network inside truffle-config.js as mainfork

SOLIDITY CODE:

interface IERC20 {
    ...
}
interface IUniswapV2Router {
    ...
}
interface IWETH {
    function deposit() external payable;
    function transfer(address to, uint256 value) external returns (bool);
    function withdraw(uint256) external;
}
contract ABC {
    struct Response {
        uint256 x;
        uint256 y;
        uint256 z;
        uint256 kk;
    }
function Test() external payable returns (Response memory response) {
    address wethAaddress = IUniswapV2Router(router).WETH();
    IWETH(wethAddress).deposit{value:msg.value}();
    uint256 balance = IERC20(wethAddress).balanceOf(address(this));
    response = Response(msg.value, balance, address(this).balance, msg.sender.balance);
    return response;
}

}

Truffle part:

truffle console --network mainfork
truffle compile
truffle migrate --reset
f = await ABC.deployed()
k = await f.Test.call({'from':'...', 'to':'...','value':0.002*10**18, 'gas':200000, 'gasPrice':20})
k.toString()

OUTPUT:

'2000000000000000,0,0,96532476734486555522' //msg.value, WETH balance, ETH balance, msg.sender balance

So, final ETH and WETH balances are 0 but msg.value is seen correct inside the contract. What is the mistake in the code and how do I correct it?

I've checked the difference between a call and a transaction (What is the difference between a transaction and a call?). Since, my code is evaluating the result in a single call without ever making a second call, local execution should be sufficient without requiring any change in blockchain state.

aste123
  • 233
  • 2
  • 8
  • @Ismael "It simulates what would happen in a transaction, but discards all the state changes when it is done". I'm making a single call. The changes will not persist once call is done. But it should persist while the call is still active right? – aste123 Sep 25 '22 at 16:05
  • @Ismael Another answer from the link: "calls are transactions executed locally on the user's local machine which alone evaluates the result. These are read-only and fast. They can't change the blockchain in any way because they are never sent to the network.". My question evaluates the result and returns it without making a second call at all. So, it being a call shouldn't be the reason my code is not working. There has to be another reason that I'm trying to find out. Please check again. – aste123 Sep 25 '22 at 16:11

0 Answers0