1

Here is my Library Contract deployed at 0xdeadbeef:

library UsefulLib {
    function caculateAddress(address origin, uint nonce)
    pure
    external
    returns (address) {
        if(nonce == 0x00)     return address(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), origin, byte(0x80))));
        if(nonce <= 0x7f)     return address(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), origin, byte(nonce))));
        return address(keccak256(abi.encodePacked(byte(0xda), byte(0x94), origin, byte(0x84), uint32(nonce))));
    }
}

I want to use delegate call for cacuateAddress(address,uint) function from another contract.

contract Another {
    address Lib = 0xdeadbeef;
    function test(address _myaddr, uint256 nonce) public {
        address childAddr = Lib.delegatecall(bytes4(keccak256("calculateAddress(address,uint)")), _myaddr, nonce);
    }
}

Is this the correct way? Do I need to add Library interface anywhere in contract Another?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy