Created
August 22, 2017 23:03
-
-
Save agentbond007/b49203c681b162d3da0b94e623dd7c9c to your computer and use it in GitHub Desktop.
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
1 contract TokenContractFragment { | |
2 | |
3 // Balances for each account | |
4 mapping(address => uint256) balances; | |
5 | |
6 // Owner of account approves the transfer of an amount to another account | |
7 mapping(address => mapping (address => uint256)) allowed; | |
8 | |
9 // What is the balance of a particular account? | |
10 function balanceOf(address _owner) constant returns (uint256 balance) { | |
11 return balances[_owner]; | |
12 } | |
13 | |
14 // Transfer the balance from owner's account to another account | |
15 function transfer(address _to, uint256 _amount) returns (bool success) { | |
16 if (balances[msg.sender] >= _amount | |
17 && _amount > 0 | |
18 && balances[_to] + _amount > balances[_to]) { | |
19 balances[msg.sender] -= _amount; | |
20 balances[_to] += _amount; | |
21 return true; | |
22 } else { | |
23 return false; | |
24 } | |
25 } | |
26 | |
27 // Send _value amount of tokens from address _from to address _to | |
28 // The transferFrom method is used for a withdraw workflow, allowing contracts to send | |
29 // tokens on your behalf, for example to "deposit" to a contract address and/or to charge | |
30 // fees in sub-currencies; the command should fail unless the _from account has | |
31 // deliberately authorized the sender of the message via some mechanism; we propose | |
32 // these standardized APIs for approval: | |
33 function transferFrom( | |
34 address _from, | |
35 address _to, | |
36 uint256 _amount | |
37 ) returns (bool success) { | |
38 if (balances[_from] >= _amount | |
39 && allowed[_from][msg.sender] >= _amount | |
40 && _amount > 0 | |
41 && balances[_to] + _amount > balances[_to]) { | |
42 balances[_from] -= _amount; | |
43 allowed[_from][msg.sender] -= _amount; | |
44 balances[_to] += _amount; | |
45 return true; | |
46 } else { | |
47 return false; | |
48 } | |
49 } | |
50 | |
51 // Allow _spender to withdraw from your account, multiple times, up to the _value amount. | |
52 // If this function is called again it overwrites the current allowance with _value. | |
53 function approve(address _spender, uint256 _amount) returns (bool success) { | |
54 allowed[msg.sender][_spender] = _amount; | |
55 return true; | |
56 } | |
57 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment