赞
踩
1. 通过接口的形式
- pragma solidity >=0.7.0 <=0.8.0;
- // SPDX-License-Identifier: MIT
-
- interface IRC20 {
-
- function depositEther() external payable;
-
- function withdraw(uint256 amount) external;
-
- function getBalance(address addr) external view returns(uint256);
-
- }
-
- contract Attack {
-
- address constant private addr = 0x78E74b14512f2f9d3C26aeE24d902Fef10F46d72;
-
- IRC20 private tract;
-
- event withdrawEth(address sender,uint256 value);
-
- constructor(){
- tract = IRC20(addr);
- }
-
-
- function despoit() public payable{
- tract.depositEther{value:2 ether}();
- tract.withdraw(1 ether);
- }
-
- }

通过定义需要调用的接口,在合约构造函数里面实现加载,方法里面实现调用。以上列子在IRC20中匹配需要调用的合约接口方法。
2.通过签名的方式
- pragma solidity >=0.7.0 <=0.8.0;
- // SPDX-License-Identifier: MIT
-
-
- contract Test {
-
- address constant private addr = 0x78E74b14512f2f9d3C26aeE24d902Fef10F46d72;
-
-
- event balanceAmount(address sender,uint256 value);
-
- function getBalanceValue(address value) public view returns(uint256){
- bytes4 methodId = bytes4(keccak256("getBalance(address)"));
- (bool success,bytes memory data) = addr.staticcall(abi.encodeWithSelector(methodId,value));
- if(success){
- uint256 amount = abi.decode(data,(uint256));
- return amount;
- }else{
- return 0;
- }
- }
-
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。