通过合约地址 和 接口 调用另一个合约的方法

  • Post author:
  • Post category:其他


合约在线部署和调用:

https://app.mycrypto.com/interact-with-contracts

A 合约:

测试链上地址:

0x98b298e7e51fb0b8b5432100f418319c428f2187

合约名称:

Number
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.0 <0.9.0;

contract Number {

uint256 c;

    function getNumber() public view returns (uint) {
        return c;
    }
    
    function setNumber(uint num) public  {
          c=num;
    }
}

bytecode:

solc –bin xxx.sol

608060405234801561001057600080fd5b5061012f806100206000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80633fb5c1cb146037578063f2c9ecd814604f575b600080fd5b604d600480360381019060499190608f565b6069565b005b60556073565b6040516060919060c2565b60405180910390f35b8060008190555050565b60008054905090565b60008135905060898160e5565b92915050565b60006020828403121560a057600080fd5b600060ac84828501607c565b91505092915050565b60bc8160db565b82525050565b600060208201905060d5600083018460b5565b92915050565b6000819050919050565b60ec8160db565b811460f657600080fd5b5056fea26469706673582212200ac1459b6441b72002a3f78ac58887f25786bd141eb2b8eb23a8a13058e5e1da64736f6c63430008010033

ABI:

[
	{
		"inputs": [],
		"name": "getNumber",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "num",
				"type": "uint256"
			}
		],
		"name": "setNumber",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
]

B合约通过设定A合约的地址 以及 方法接口 实现调用A合约:

测试链上地址:

0xa2001447a0a4734830c8aae1642ff415758bc020

合约名称:

InNumber
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.0 <0.9.0;

interface InterNumber{
         function getNumber()  external returns (uint);
         function setNumber(uint _num)  external;
 }
 
contract InNumber{
address _addr;
    function setSender(address addr)public{
    _addr=addr;
   }

    function getN() public returns (uint) {
        return InterNumber(_addr).getNumber();
    }
    
    function setN(uint num) public {
          InterNumber(_addr).setNumber(num);
    }
}

bytecode:

608060405234801561001057600080fd5b50610399806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80633e955225146100465780633f7a027014610064578063ced32b0c14610080575b600080fd5b61004e61009c565b60405161005b91906102de565b60405180910390f35b61007e6004803603810190610079919061027d565b610144565b005b61009a60048036038101906100959190610254565b6101d2565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663f2c9ecd86040518163ffffffff1660e01b8152600401602060405180830381600087803b15801561010757600080fd5b505af115801561011b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061013f91906102a6565b905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16633fb5c1cb826040518263ffffffff1660e01b815260040161019d91906102de565b600060405180830381600087803b1580156101b757600080fd5b505af11580156101cb573d6000803e3d6000fd5b5050505050565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b60008135905061022481610335565b92915050565b6000813590506102398161034c565b92915050565b60008151905061024e8161034c565b92915050565b60006020828403121561026657600080fd5b600061027484828501610215565b91505092915050565b60006020828403121561028f57600080fd5b600061029d8482850161022a565b91505092915050565b6000602082840312156102b857600080fd5b60006102c68482850161023f565b91505092915050565b6102d88161032b565b82525050565b60006020820190506102f360008301846102cf565b92915050565b60006103048261030b565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b61033e816102f9565b811461034957600080fd5b50565b6103558161032b565b811461036057600080fd5b5056fea26469706673582212208dfbc50d1298e8d0ed216a0523023ba0a0abed061b1c1c0fd8621b1977a939e664736f6c63430008010033

ABI:

[
	{
		"inputs": [],
		"name": "getN",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "num",
				"type": "uint256"
			}
		],
		"name": "setN",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "setSender",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
]



版权声明:本文为weixin_41303815原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。