Skip to content

Commit b95d7e7

Browse files
Skygenventuro
authored andcommitted
Add a simple wrapper for address. (#1773)
* Updated code style to no more than120 characters per line. * Unify code comments style with Doxygen-style tags. * Fix the conflicts. * Add a return value in the contract ERC20Burnable. * A Add a wrapper function to change type of address to address payable. * U Modify Address utils. * A Add test case for Address. * U Modify code style in ERC20Burnable. * Add changelog entry. * Improved dev docs.
1 parent 96e4950 commit b95d7e7

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.4.0 (unreleased)
4+
5+
### New features:
6+
* `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))
7+
38
## 2.3.0 (2019-05-27)
49

510
### New features:

contracts/mocks/AddressImpl.sol

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ contract AddressImpl {
66
function isContract(address account) external view returns (bool) {
77
return Address.isContract(account);
88
}
9+
10+
function toPayable(address account) external pure returns (address payable) {
11+
return Address.toPayable(account);
12+
}
913
}

contracts/utils/Address.sol

+8
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ library Address {
2424
assembly { size := extcodesize(account) }
2525
return size > 0;
2626
}
27+
28+
/**
29+
* @dev Converts an `address` into `address payable`. Note that this is
30+
* simply a type cast: the actual underlying value is not changed.
31+
*/
32+
function toPayable(address account) internal pure returns (address payable) {
33+
return address(uint160(account));
34+
}
2735
}

test/utils/Address.test.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
1-
require('openzeppelin-test-helpers');
1+
const { constants } = require('openzeppelin-test-helpers');
22

33
const AddressImpl = artifacts.require('AddressImpl');
44
const SimpleToken = artifacts.require('SimpleToken');
55

66
contract('Address', function ([_, other]) {
7+
const ALL_ONES_ADDRESS = '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF';
8+
79
beforeEach(async function () {
810
this.mock = await AddressImpl.new();
911
});
1012

11-
it('should return false for account address', async function () {
12-
(await this.mock.isContract(other)).should.equal(false);
13+
describe('isContract', function () {
14+
it('should return false for account address', async function () {
15+
(await this.mock.isContract(other)).should.equal(false);
16+
});
17+
18+
it('should return true for contract address', async function () {
19+
const contract = await SimpleToken.new();
20+
(await this.mock.isContract(contract.address)).should.equal(true);
21+
});
1322
});
1423

15-
it('should return true for contract address', async function () {
16-
const contract = await SimpleToken.new();
17-
(await this.mock.isContract(contract.address)).should.equal(true);
24+
describe('toPayable', function () {
25+
it('should return a payable address when the account is the zero address', async function () {
26+
(await this.mock.toPayable(constants.ZERO_ADDRESS)).should.equal(constants.ZERO_ADDRESS);
27+
});
28+
29+
it('should return a payable address when the account is an arbitrary address', async function () {
30+
(await this.mock.toPayable(other)).should.equal(other);
31+
});
32+
33+
it('should return a payable address when the account is the all ones address', async function () {
34+
(await this.mock.toPayable(ALL_ONES_ADDRESS)).should.equal(ALL_ONES_ADDRESS);
35+
});
1836
});
1937
});

0 commit comments

Comments
 (0)