forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OpenZeppelin#206 from DavidKnott/create-pausable-t…
…oken Create and test PausableToken Contract
- Loading branch information
Showing
7 changed files
with
152 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pragma solidity ^0.4.8; | ||
|
||
import './StandardToken.sol'; | ||
import '../lifecycle/Pausable.sol'; | ||
|
||
/** | ||
* Pausable token | ||
* | ||
* Simple ERC20 Token example, with pausable token creation | ||
* Issue: | ||
* https://github.com/OpenZeppelin/zeppelin-solidity/issues/194 | ||
* Based on code by BCAPtoken: | ||
* https://github.com/BCAPtoken/BCAPToken/blob/5cb5e76338cc47343ba9268663a915337c8b268e/sol/BCAPToken.sol#L27 | ||
**/ | ||
|
||
contract PausableToken is Pausable, StandardToken { | ||
|
||
function transfer(address _to, uint _value) whenNotPaused { | ||
return super.transfer(_to, _value); | ||
} | ||
|
||
function transferFrom(address _from, address _to, uint _value) whenNotPaused { | ||
return super.transferFrom(_from, _to, _value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
Pausable | ||
============================================= | ||
|
||
Base contract that provides an emergency stop mechanism. | ||
Base contract that provides a pause mechanism. | ||
|
||
Inherits from contract Ownable. | ||
|
||
emergencyStop( ) external onlyOwner | ||
pause() onlyOwner whenNotPaused returns (bool) | ||
""""""""""""""""""""""""""""""""""""" | ||
|
||
Triggers the stop mechanism on the contract. After this function is called (by the owner of the contract), any function with modifier stopInEmergency will not run. | ||
Triggers pause mechanism on the contract. After this function is called (by the owner of the contract), any function with modifier whenNotPaused will not run. | ||
|
||
modifier stopInEmergency | ||
|
||
modifier whenNotPaused() | ||
""""""""""""""""""""""""""""""""""""" | ||
|
||
Prevents function from running if stop mechanism is activated. | ||
Prevents function from running if pause mechanism is activated. | ||
|
||
modifier onlyInEmergency | ||
modifier whenPaused() | ||
""""""""""""""""""""""""""""""""""""" | ||
|
||
Only runs if stop mechanism is activated. | ||
Only runs if pause mechanism is activated. | ||
|
||
release( ) external onlyOwner onlyInEmergency | ||
unpause() onlyOwner whenPaused returns (bool) | ||
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | ||
|
||
Deactivates the stop mechanism. | ||
Deactivates the pause mechanism. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'user strict'; | ||
|
||
const assertJump = require('./helpers/assertJump'); | ||
var PausableTokenMock = artifacts.require('./helpers/PausableTokenMock.sol'); | ||
|
||
contract('PausableToken', function(accounts) { | ||
let token; | ||
|
||
beforeEach(async function() { | ||
token = await PausableTokenMock.new(accounts[0], 100); | ||
}); | ||
|
||
it('should return paused false after construction', async function() { | ||
let paused = await token.paused(); | ||
|
||
assert.equal(paused, false); | ||
}); | ||
|
||
it('should return paused true after pause', async function() { | ||
await token.pause(); | ||
let paused = await token.paused(); | ||
|
||
assert.equal(paused, true); | ||
}); | ||
|
||
it('should return paused false after pause and unpause', async function() { | ||
await token.pause(); | ||
await token.unpause(); | ||
let paused = await token.paused(); | ||
|
||
assert.equal(paused, false); | ||
}); | ||
|
||
it('should be able to transfer if transfers are unpaused', async function() { | ||
await token.transfer(accounts[1], 100); | ||
let balance0 = await token.balanceOf(accounts[0]); | ||
assert.equal(balance0, 0); | ||
|
||
let balance1 = await token.balanceOf(accounts[1]); | ||
assert.equal(balance1, 100); | ||
}); | ||
|
||
it('should be able to transfer after transfers are paused and unpaused', async function() { | ||
await token.pause(); | ||
await token.unpause(); | ||
await token.transfer(accounts[1], 100); | ||
let balance0 = await token.balanceOf(accounts[0]); | ||
assert.equal(balance0, 0); | ||
|
||
let balance1 = await token.balanceOf(accounts[1]); | ||
assert.equal(balance1, 100); | ||
}); | ||
|
||
it('should throw an error trying to transfer while transactions are paused', async function() { | ||
await token.pause(); | ||
try { | ||
await token.transfer(accounts[1], 100); | ||
} catch (error) { | ||
return assertJump(error); | ||
} | ||
assert.fail('should have thrown before'); | ||
}); | ||
|
||
it('should throw an error trying to transfer from another account while transactions are paused', async function() { | ||
await token.pause(); | ||
try { | ||
await token.transferFrom(accounts[0], accounts[1], 100); | ||
} catch (error) { | ||
return assertJump(error); | ||
} | ||
assert.fail('should have thrown before'); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pragma solidity ^0.4.8; | ||
|
||
import '../../contracts/token/PausableToken.sol'; | ||
|
||
// mock class using PausableToken | ||
contract PausableTokenMock is PausableToken { | ||
|
||
function PausableTokenMock(address initialAccount, uint initialBalance) { | ||
balances[initialAccount] = initialBalance; | ||
} | ||
|
||
} |