`

Address

 
阅读更多
// contracts/Box.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/Address.sol";
import "hardhat/console.sol";

contract MyTestContract {
    function foo(uint i) view external {
        console.log("foo",i);
    }

    function testContract() external{
        console.log("test is contract:");
        console.logBool(Address.isContract(msg.sender));
    }
}

contract TestAddress {
    using Address for address;
    //在构造函数中让另一个合约去判断msg.sender是否是合约会失败,因为构造的时候
    constructor(){
        MyTestContract f = new MyTestContract();
        f.testContract();
    }

    function testAddress(address addr) external {
        Address.functionCall(addr, abi.encodeWithSignature("foo(uint256)",1), "foo call reverted");
    }

    function testContract() external{
        MyTestContract f = new MyTestContract();
        console.logBool(Address.isContract(address(f)));
    }
}




const { ethers } = require("hardhat")

contractName = "TestAddress";
contractName2 = "MyTestContract";

describe(contractName, function () {
  it("test ", async function () {
    const Contract2 = await ethers.getContractFactory(contractName2);
    const contract2 = await Contract2.deploy();
    const Contract = await ethers.getContractFactory(contractName);
    const contract = await Contract.deploy();
    
    await contract.testAddress(contract2.address);
    await contract.testContract();
  });
});


  TestAddress
false
foo 1
true
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics