From 7a26a0ecf1fde35be1ee2bb7683ec4a471cad477 Mon Sep 17 00:00:00 2001 From: Chris Whinfrey Date: Wed, 25 Oct 2017 23:57:21 -0400 Subject: [PATCH 1/4] Add capped token contract --- contracts/token/CappedToken.sol | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 contracts/token/CappedToken.sol diff --git a/contracts/token/CappedToken.sol b/contracts/token/CappedToken.sol new file mode 100644 index 000000000..ff28f9aba --- /dev/null +++ b/contracts/token/CappedToken.sol @@ -0,0 +1,31 @@ +pragma solidity ^0.4.11; + +import './MintableToken.sol'; + +/** + * @title Capped token + * @dev Mintable token with a token cap. + */ + +contract CappedToken is MintableToken { + + uint256 public cap; + + function CappedToken(uint256 _cap) { + require(_cap > 0); + cap = _cap; + } + + /** + * @dev Function to mint tokens + * @param _to The address that will receive the minted tokens. + * @param _amount The amount of tokens to mint. + * @return A boolean that indicates if the operation was successful. + */ + function mint(address _to, uint256 _amount) onlyOwner canMint public returns (bool) { + require(totalSupply.add(_amount) <= cap); + + return MintableToken.mint(_to, _amount); + } + +} From 575372f6608a85084ecc611ee8cd7aca05ab5db6 Mon Sep 17 00:00:00 2001 From: Chris Whinfrey Date: Wed, 25 Oct 2017 23:58:36 -0400 Subject: [PATCH 2/4] Add Capped Token tests --- test/CappedToken.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/CappedToken.js diff --git a/test/CappedToken.js b/test/CappedToken.js new file mode 100644 index 000000000..34955dbf1 --- /dev/null +++ b/test/CappedToken.js @@ -0,0 +1,39 @@ +'use strict'; + +import expectThrow from './helpers/expectThrow'; +import ether from './helpers/ether'; +var CappedToken = artifacts.require('../contracts/Tokens/CappedToken.sol'); + +const BigNumber = web3.BigNumber + +contract('Capped', function(accounts) { + const cap = ether(1000); + + let token; + + beforeEach(async function() { + token = await CappedToken.new(cap); + }) + + it('should start with the correct cap', async function() { + let _cap = await token.cap(); + + assert.equal(cap.toNumber(), _cap.toNumber()); + }) + + it('should mint when amount does amount does not exceed the cap', async function() { + const result = await token.mint(accounts[0], 100); + assert.equal(result.logs[0].event, 'Mint'); + }) + + it('should fail to mint if the ammount exceeds the cap', async function() { + await token.mint(accounts[0], cap.sub(1)); + await expectThrow(token.mint(accounts[0], 100)); + }) + + it('should fail to mint after cap is reached', async function() { + await token.mint(accounts[0], cap); + await expectThrow(token.mint(accounts[0], 1)); + }) + +}); From 61b5921ab21e8e092f0270c385522cb42b29f155 Mon Sep 17 00:00:00 2001 From: Chris Whinfrey Date: Fri, 10 Nov 2017 20:20:44 -0500 Subject: [PATCH 3/4] Fix typo --- test/CappedToken.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CappedToken.js b/test/CappedToken.js index 34955dbf1..1f60dc8eb 100644 --- a/test/CappedToken.js +++ b/test/CappedToken.js @@ -21,7 +21,7 @@ contract('Capped', function(accounts) { assert.equal(cap.toNumber(), _cap.toNumber()); }) - it('should mint when amount does amount does not exceed the cap', async function() { + it('should mint when amount is less than cap', async function() { const result = await token.mint(accounts[0], 100); assert.equal(result.logs[0].event, 'Mint'); }) From 8765e2a53ff26414972197fc015c4fb910703157 Mon Sep 17 00:00:00 2001 From: Chris Whinfrey Date: Wed, 15 Nov 2017 16:14:28 -0500 Subject: [PATCH 4/4] Use instead of directly calling on --- contracts/token/CappedToken.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/token/CappedToken.sol b/contracts/token/CappedToken.sol index ff28f9aba..508669172 100644 --- a/contracts/token/CappedToken.sol +++ b/contracts/token/CappedToken.sol @@ -25,7 +25,7 @@ contract CappedToken is MintableToken { function mint(address _to, uint256 _amount) onlyOwner canMint public returns (bool) { require(totalSupply.add(_amount) <= cap); - return MintableToken.mint(_to, _amount); + return super.mint(_to, _amount); } }