Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. contract ERC20 =
  2. record state = {
  3. _owner : address,
  4. _totalSupply : int,
  5. _balances : map(address, int),
  6. _allowed : map((address,address), int)}
  7.  
  8. public stateful function init() = {
  9. _owner = Call.caller,
  10. _totalSupply = 0,
  11. _balances = {},
  12. _allowed = {}}
  13.  
  14. private function lookupByAddress(k : address, m, v) =
  15. switch(Map.lookup(k, m))
  16. None => v
  17. Some(x) => x
  18.  
  19. public function totalSupply() : int = state._totalSupply
  20.  
  21. public function balanceOf(who: address) : int = lookupByAddress(who, state._balances, 0)
  22.  
  23. public stateful function transfer(to: address, value: int) : bool =
  24. _transfer(Call.caller, to, value)
  25.  
  26. private stateful function _transfer(from: address, to: address, value: int) : bool =
  27. require(value > 0, "Value is sub zero")
  28. require(value =< balanceOf(from), "Not enough balance")
  29. require(to != #0, "Invalid address")
  30.  
  31. put(state{
  32. _balances[from] = sub(balanceOf(from), value),
  33. _balances[to] = add(balanceOf(to), value)})
  34.  
  35. true
  36.  
  37. public stateful function transferFrom(from: address, to: address, value: int) : bool =
  38. _transfer(from, to, value)
  39.  
  40. true
  41.  
  42. public stateful function mint(account: address, value: int) : bool =
  43. require(account != #0, "Invalid address")
  44.  
  45. put(state{_totalSupply = add(state._totalSupply, value),
  46. _balances[account] = add(balanceOf(account), value)})
  47.  
  48. true
  49.  
  50. private function add(_a : int, _b : int) : int =
  51. let c : int = _a + _b
  52. require(c >= _a, "Error")
  53. c
  54.  
  55. private function sub(_a : int, _b : int) : int =
  56. require(_b =< _a, "Error")
  57. _a - _b
  58.  
  59. private function require(b : bool, err : string) =
  60. if(!b)
  61. abort(err)
  62.  
  63. private function onlyOwner() =
  64. require(Call.caller == state._owner, "Only owner can mint!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement