-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoken.rb
143 lines (106 loc) · 4.1 KB
/
token.rb
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# encoding: utf-8
######################################
# Minimum Viable Token
require "michelson"
type :Account, {
balance: Nat,
allowances: Map‹Address→Nat› }
type :Storage, {
accounts: BigMap‹Address→Account›,
version: Nat,
total_supply: Nat,
decimals: Nat,
name: String,
symbol: String,
owner: Address }
init [Address, Nat, Nat, String, String],
def storage( owner, total_supply, decimals, name, symbol )
owner_account = Account.new( total_supply, {} )
accounts = Map.add( owner, owner_account, {} )
Storage.new( accounts, 1.p, total_supply, decimals, name, symbol, owner )
end
sig [Address, BigMap‹Address→Account›],
def get_account(a, accounts)
match Map.find(a, accounts), {
None: ->() { Account.new( 0.p, {} ) }, ## fix: allow (struct) init with keys too
Some: ->(account) { account }}
end
sig [Address, Address, Nat],
def perform_transfer(from, dest, tokens, storage)
accounts = storage.accounts
account_sender = get_account( from, accounts )
new_account_sender =
match is_nat(account_sender.balance - tokens), {
None: ->() { failwith( "Not enough tokens for transfer", account_sender.balance ) },
Some: ->(b) { account_sender.update( balance: b )}
}
accounts = Map.add(from, new_account_sender, accounts)
account_dest = get_account( dest, accounts )
new_account_dest = account_dest.update( balance: account_dest.balance + tokens )
accounts = Map.add(dest, new_account_dest, accounts)
[[], storage.update( accounts: accounts )]
end
entry [Address, Nat],
def transfer( dest, tokens, storage )
perform_transfer( Current.sender, dest, tokens, storage )
end
entry [Address, Nat],
def approve( spender, tokens, storage )
account_sender = get_account( Current.sender, storage.accounts)
account_sender = account_sender.update( allowances:
if tokens == 0.p
Map.remove( spender, account_sender.allowances )
else
Map.add( spender, tokens, account_sender.allowances )
end )
storage = storage.update( accounts: Map.add( Current.sender, account_sender, storage.accounts))
[[], storage]
end
entry [Address, Address, Nat],
def transfer_from( from, dest, tokens, storage)
account_from = get_account( from, storage.accounts )
new_allowances_from =
match Map.find( Current.sender, account_from.allowances ), {
None: ->() { failwith( "Not allowed to spend from", from ) },
Some: ->(allowed) {
match is_nat(allowed - tokens), {
None: ->() { failwith( "Not enough allowance for transfer", allowed ) },
Some: ->(allowed) {
if allowed == 0.p
Map.remove( Current.sender, account_from.allowances )
else
Map.add( Current.sender, allowed, account_from.allowances )
end
}
}
}
}
account_from = account_from.update( allowances: new_allowances_from )
storage = storage.update( accounts: Map.add( from, account_from, storage.accounts )
perform_transfer( from, dest, tokens, storage )
end
entry [Address, Nat],
def create_account( dest, tokens, storage )
if Current.sender != storage.owner
failwith( "Only owner can create accounts" )
end
perform_transfer( storage.owner, dest, tokens, storage )
end
############################
# Test, Test, Test
storage = storage( "0x1111", 100_000_000, 2, "Shilling", "BTS")
get_account( "0x7777", storage.accounts )
get_account( "0x1111", storage.accounts )
_, storage = perform_transfer( "0x1111", "0xaaaa", 1000, storage )
# _, storage = transfer( "0xbbbb", 1000, storage )
#=> !! RuntimeError: failwith - Not enough tokens for transfer: 0
Current.sender = "0x1111"
_, storage = transfer( "0xbbbb", 1000, storage )
_, storage = approve( "0xcccc", 1000, storage )
_, storage = approve( "0xdddd", 1000, storage )
Current.sender = "0xcccc"
_, storage = transfer_from( "0x1111", "0x2222", 200, storage )
_, storage = transfer_from( "0x1111", "0x3333", 300, storage )
Current.sender = "0x1111"
_, storage = create_account( "0xeeee", 5000, storage )
_, storage = create_account( "0xffff", 6000, storage )