Solidity Inline assembly CALL wrong result

20 views Asked by At

I have two methods that I'm calling in Hardhat within a single test, targeting the same address. Here's the test code:

it("TEMP", async function () {
    const {contract, owner, evil} = await loadFixture(initFixture);

    const balance = await contract.m2();
    const balance2 = await contract.m2_ass();

    console.log(BigInt(balance.data));
    console.log(BigInt(balance2.data));
});

However, the results I'm getting are different. I even tried inserting random values, but the second method returns an incorrect number. I don't understand where the error is and how I can get the balance through YUM.

Results Correct 2443864522n Wrong 4040075883n

function Correct() external returns (uint256){
        IERC20 token = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
        return token.balanceOf(0xADDRESS);
    }
function Wrong() external returns (uint256){
    uint256 balanceBefore;

    assembly {
        let inputBalanceOf := mload(0x40) // Allocate memory for input data

        mstore(inputBalanceOf, hex"70a08231") // Selector for balanceOf(address)
        mstore(add(inputBalanceOf, 0x04), 0xADDRESS) // Store the address

        let success := call(
            gas(),        // Remaining gas
            0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, // Token address
            0,                    // Amount of ether to send
            inputBalanceOf,       // Input data
            0x24,                 // Size of input data
            inputBalanceOf,       // Reuse input for output data
            0x20                  // Size of output data
        )

        if eq(success, 0) {revert(0, 0)} // Check if the call was successful

        balanceBefore := mload(inputBalanceOf) // Read the balance
    //mstore(0x40,add(inputBalanceOf,0x24)) // Set storage pointer to empty space
    }

    return balanceBefore;
}

I want to make a balance query using assembly, but for some reason, it doesn't work.

0

There are 0 answers