Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jensendarren/5f3c40a82785e5c5572179d9e48e2fe1 to your computer and use it in GitHub Desktop.
Save jensendarren/5f3c40a82785e5c5572179d9e48e2fe1 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract AddressType {
address public basicAddress = 0xeCc6425dB956125989C2c1a58A817bD90f86B3C0;
address public basicAddress2 = 0x9Fa413fad794c18fdd3A5Ead5734f042Eb920A20;
address payable anotherPayableAddress = payable(0x94fe479322fa107933d67b9e230a9B12B6c9a96d);
address implictConvertPayableAddress = payable(0x94fe479322fa107933d67b9e230a9B12B6c9a96d);
// cannot implicity convert from address to address payable
// address payable payableAddress = 0xDBb078A7064DffdfAe6E69451941Da6627920F52;
// this contracts address
address public contractAddress;
// set owner and other addresses in constructor
address payable public owner;
address payable public other;
constructor(address payable _otherAddress) payable {
owner = payable(msg.sender);
other = _otherAddress;
contractAddress = address(this);
}
function contractAddressBalance() public view returns(uint256) {
return contractAddress.balance;
}
// will transfer 1 Ether from this contract to address other
function testTransferToOther() public {
other.transfer(1 ether);
}
// However, basicAddress is NOT payable so cannot do this!
// function testTransferToBasicAddress() public {
// basicAddress.transfer(1 ether);
// }
// Convert address examples
// 0xeCc6425dB956125989C2c1a58A817bD90f86B3C0
function convertAddressToUint160(address _addr) public pure returns (uint160) {
return uint160(_addr);
}
function convertUint160ToAddress(uint160 _number) public pure returns (address) {
return address(_number);
}
function convertAddressToBytes20(address _addr) public pure returns(bytes20) {
return bytes20(_addr);
}
function convertBytes20Address(bytes20 _bytes) public pure returns(address) {
return address(_bytes);
}
// Sometimes you might need to convert larger bytes types into addresses
// So you need the last 20 bytes of the larger bytes
// e.g. '0x000000000000000000000000697cb3a91d22f4cb39aeea7eb4a410fedd0bbe06'
function convertBytes32Address(bytes32 _addr) external pure returns (address) {
return address(uint160(uint256(_addr)));
}
// Can convert address to address payable using the payable type converter
function convertAddressPayable(address _addr) external pure returns (address payable) {
return payable(_addr);
}
// Explicit type conversion not allowed from bytes32 to address
// function explicitConvertNotAllowed1(bytes32 _addr) external pure returns (address) {
// return address(_addr);
// }
// Explicit type conversion not allowed from address to address payable
// function explicitConvertNotAllowed2(address _addr) external pure returns (address payable) {
// address payable _addrPayable = _addr;
// return _addrPayable;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment