简体   繁体   中英

How to properly use the call (or delegatecall) in solidity

I'm trying to use this method:

function test(uint amount) public {
        address(0xf5eA38B6b9644224dA1aECbC1219e8543c0689b2).call(abi.encodeWithSignature("deposit(uint)",amount));
    }

but the transaction gets reverted, this is because the amount is not hashed in some way, and I don't really know to do it, what should I do on the amount? these are transactions:

-the failed one using the method: https://bscscan.com/tx/0x7fdd50cee23295ea866baa8961a2105c58162e77125df852a4fc5bf0fad2f507

and this was the input data:

Function: test(uint256 tAmount) ***

MethodID: 0x29e99f07 [0]: 0000000000000000000000000000000000000000000000000000000000000001

-the successfull one directly calling the contract from the site instead of using call inside another smart contract: https://bscscan.com/tx/0xfd4158766f25761fa5dddb0683c677085a04ea6db05e03794be375a8243d7128

and this was the input data:

Function: deposit(uint256 _amount) ***

MethodID: 0xb6b55f25 [0]: 000000000000000000000000000000000000000000000000005427aedb41a400

do I need to hash amount in someway?

The target contract defines uint as the argument type, but it's just an alias uint256 . ABI encode methods don't automatically convert the aliases, so you need to change it in your code.

// `uint256` instead of `uint`
abi.encodeWithSignature("deposit(uint256)",amount)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM