Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple wrapper for address. #1773

Merged
merged 15 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.4.0 (unreleased)

### New features:
* `Address.toPayable`: added a helper to convert between address types without having to resort to low-level casting. ([#1773](https://github.com/OpenZeppelin/openzeppelin-solidity/pull/1773))

## 2.3.0 (2019-05-27)

### New features:
Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/AddressImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ contract AddressImpl {
function isContract(address account) external view returns (bool) {
return Address.isContract(account);
}

function toPayable(address account) external pure returns (address payable) {
return Address.toPayable(account);
}
}
8 changes: 8 additions & 0 deletions contracts/utils/Address.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ library Address {
assembly { size := extcodesize(account) }
return size > 0;
}

/**
* @dev Converts an `address` into `address payable`. Note that this is
* simply a type cast: the actual underlying value is not changed.
*/
function toPayable(address account) internal pure returns (address payable) {
return address(uint160(account));
}
}
30 changes: 24 additions & 6 deletions test/utils/Address.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
require('openzeppelin-test-helpers');
const { constants } = require('openzeppelin-test-helpers');

const AddressImpl = artifacts.require('AddressImpl');
const SimpleToken = artifacts.require('SimpleToken');

contract('Address', function ([_, other]) {
const ALL_ONES_ADDRESS = '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF';

beforeEach(async function () {
this.mock = await AddressImpl.new();
});

it('should return false for account address', async function () {
(await this.mock.isContract(other)).should.equal(false);
describe('isContract', function () {
it('should return false for account address', async function () {
(await this.mock.isContract(other)).should.equal(false);
});

it('should return true for contract address', async function () {
const contract = await SimpleToken.new();
(await this.mock.isContract(contract.address)).should.equal(true);
});
});

it('should return true for contract address', async function () {
const contract = await SimpleToken.new();
(await this.mock.isContract(contract.address)).should.equal(true);
describe('toPayable', function () {
it('should return a payable address when the account is the zero address', async function () {
(await this.mock.toPayable(constants.ZERO_ADDRESS)).should.equal(constants.ZERO_ADDRESS);
});

it('should return a payable address when the account is an arbitrary address', async function () {
(await this.mock.toPayable(other)).should.equal(other);
});

it('should return a payable address when the account is the all ones address', async function () {
(await this.mock.toPayable(ALL_ONES_ADDRESS)).should.equal(ALL_ONES_ADDRESS);
});
});
});