1

What is the encodeWithSelector (from solidity) equivalent in vyper?

For example, in solidity, if I wanted to directly the transfer function on our own contract using raw calls, I'd do:

function callTransferFunctionDirectly(address someAddress, uint256 amount)
        public
        returns (bytes4, bool)
    {
         bytes4 selector = bytes4(keccak256(bytes("transfer(address,uint256)")));
        (bool success, bytes memory returnData) = address(this).call(
            // getDataToCallTransfer(someAddress, amount);
            abi.encodeWithSelector(selector, someAddress, amount)
        );
        return (bytes4(returnData), success);
    }

How would I do this in vyper?

Yes, I'm aware we'd just call transfer since it's in it's own contract. But this is for the learning.

1 Answer 1

4

You could use concat or _abi_encode for the abi.encodeWithSelector equivalence, and use the method_id function to get function selector.

The (near) equivalence of the above solidity in vyper would be:

@external
def callTransferFunctionDirectly(someAddress: address, amount: uint256) -> (Bytes[32])
  call_data: Bytes[68] = _abi_encode(someAddress, amount,  method_id=method_id("transfer(address,uint256)"))
  response: Bytes[32] = raw_call(self, call_data, max_outsize=32)
  • method_id: Gives you the selector
  • raw_call: Is the .call equivalence
  • _abi_encode: Is sort of the abi.encodeWithSelector

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.