Browse Source

Merge pull request #515 from cwhinfrey/master

Create CappedToken, a capped MintableToken
Francisco Giordano 7 years ago
parent
commit
9001ce9d3a
2 changed files with 70 additions and 0 deletions
  1. 31 0
      contracts/token/CappedToken.sol
  2. 39 0
      test/CappedToken.js

+ 31 - 0
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 super.mint(_to, _amount);
+  }
+
+}

+ 39 - 0
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 is less than 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));
+  })
+
+});