0

When we call a function on an external contract, we need to put calldata as an argument.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.10;

contract test1 {

    mapping (address => uint256) public _count;
    function countNum(address sender) public {
        _count[sender] ++; 
    }

}

contract test2 {
    function call(address _address,bytes memory data) public {
        _address.call(data);
    }

    function getdata1(bytes memory data,address from) public view returns(bytes memory) {
        return abi.encodePacked(data,from);
    }

    function getdata2(address _spender) public pure returns(bytes memory) {
        return abi.encodeWithSignature("countNum(address)", _spender);
    }
}

I called countNum() of contract test1 from call() in contract test2.

The code inside getdata1() and getdata2() uses different abi. getdata1() use encodePacked, getdata2() use encodeWithSignature. The return values ​​of the two functions are different.

However, when both result values ​​are put in call() , Both successfully called the countNum() function.

The two data are different values, why were both able to succeed?

.. and Thank you so much for attaching the link to the related content...

2
  • What the input are you passing to getdata1 as data?
    – 0xSanson
    May 17 at 12:10
  • input getdata2() return data
    – imelon2
    May 18 at 0:06

1 Answer 1

0

Both data succeed because one only appends some dirty bytes at the end.

This is the returned value of getdata2:

0x7f02eb76000000000000000000000000abcabcabcabcabcabcabcabcabcabcabcabcabca

where 0x7f02eb76 is the selector and abc..ca is the spender address.

When you calculate getdata1 using the previous value as input you just end up adding the from address at the end:

0x7f02eb76000000000000000000000000abcabcabcabcabcabcabcabcabcabcabcabcabcadefdef...efd

with def...efd the address.

When you call test1.countNum, the function will read only the first address as argument, it doesn't care that the data has extra bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.