forked from GeensNPO/gee-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigratableToken.sol
91 lines (76 loc) · 2.66 KB
/
MigratableToken.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
pragma solidity ^0.4.16;
import "./Token.sol";
/*
Inspired by Civic and Golem
*/
/*
Interface of migrate agent contract (the new token contract)
*/
contract MigrateAgent {
function migrateFrom(address _tokenHolder, uint256 _amount) external returns (bool);
}
contract MigratableToken is Token {
MigrateAgent public migrateAgent;
//Total migrated tokens
uint256 public totalMigrated;
/**
* Migrate states.
*
* - NotAllowed: The child contract has not reached a condition where the upgrade can bgun
* - WaitingForAgent: Token allows upgrade, but we don't have a new agent yet
* - ReadyToMigrate: The agent is set, but not a single token has been upgraded yet
* - Migrating: Upgrade agent is set and the balance holders can upgrade their tokens
*
*/
enum MigrateState {Unknown, NotAllowed, WaitingForAgent, ReadyToMigrate, Migrating}
event Migrate (address indexed _from, address indexed _to, uint256 _value);
event MigrateAgentSet (address _agent);
function migrate(uint256 _value) external {
MigrateState state = getMigrateState();
//Migrating has started
require(state == MigrateState.ReadyToMigrate || state == MigrateState.Migrating);
//Migrates user balance
balances[msg.sender] = balances[msg.sender].SUB(_value);
//Migrates total supply
_totalSupply = _totalSupply.SUB(_value);
//Counts migrated tokens
totalMigrated = totalMigrated.ADD(_value);
//Upgrade agent reissues the tokens
migrateAgent.migrateFrom(msg.sender, _value);
Migrate(msg.sender, migrateAgent, _value);
}
/*
Set migrating agent and start migrating
*/
function setMigrateAgent(MigrateAgent _agent)
external
onlyOwner
notZeroAddress(_agent)
afterCrowdsale
{
//cannot interrupt migrating
require(getMigrateState() != MigrateState.Migrating);
//set migrate agent
migrateAgent = _agent;
//Emit event
MigrateAgentSet(migrateAgent);
}
/*
Migrating status
*/
function getMigrateState() public constant returns (MigrateState) {
if (block.number <= crowdsaleEndBlock) {
//Migration is not allowed on funding
return MigrateState.NotAllowed;
} else if (address(migrateAgent) == address(0)) {
//Migrating address is not set
return MigrateState.WaitingForAgent;
} else if (totalMigrated == 0) {
//Migrating hasn't started yet
return MigrateState.ReadyToMigrate;
} else {
//Migrating
return MigrateState.Migrating;
}
}
}