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.