forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherc20_ops.go
249 lines (203 loc) · 6.2 KB
/
erc20_ops.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package erc20
import (
"github.com/pkg/errors"
"github.com/s7techlab/cckit/identity"
r "github.com/s7techlab/cckit/router"
)
const (
BalancePrefix = `BALANCE`
AllowancePrefix = `APPROVE`
)
var (
ErrNotEnoughFunds = errors.New(`not enough funds`)
ErrForbiddenToTransferToSameAccount = errors.New(`forbidden to transfer to same account`)
ErrSpenderNotHaveAllowance = errors.New(`spender not have allowance for amount`)
)
type (
Transfer struct {
From identity.Id
To identity.Id
Amount int
}
Approve struct {
From identity.Id
Spender identity.Id
Amount int
}
)
func querySymbol(c r.Context) (interface{}, error) {
return c.State().Get(SymbolKey)
}
func queryName(c r.Context) (interface{}, error) {
return c.State().Get(NameKey)
}
func queryTotalSupply(c r.Context) (interface{}, error) {
return c.State().Get(TotalSupplyKey)
}
func queryBalanceOf(c r.Context) (interface{}, error) {
return getBalance(c, c.ArgString(`mspId`), c.ArgString(`certId`))
}
func invokeTransfer(c r.Context) (interface{}, error) {
// transfer target
toMspId := c.ParamString(`toMspId`)
toCertId := c.ParamString(`toCertId`)
//transfer amount
amount := c.ParamInt(`amount`)
// get information about tx creator
invoker, err := identity.FromStub(c.Stub())
if err != nil {
return nil, err
}
// Disallow to transfer token to same account
if invoker.GetMSPID() == toMspId && invoker.GetID() == toCertId {
return nil, ErrForbiddenToTransferToSameAccount
}
// get information about invoker balance from state
invokerBalance, err := getBalance(c, invoker.GetMSPID(), invoker.GetID())
if err != nil {
return nil, err
}
// Check the funds sufficiency
if invokerBalance-amount < 0 {
return nil, ErrNotEnoughFunds
}
// Get information about recipient balance from state
recipientBalance, err := getBalance(c, toMspId, toCertId)
if err != nil {
return nil, err
}
// Update payer and recipient balance
if err = setBalance(c, invoker.GetMSPID(), invoker.GetID(), invokerBalance-amount); err != nil {
return nil, err
}
if err = setBalance(c, toMspId, toCertId, recipientBalance+amount); err != nil {
return nil, err
}
// Trigger event with name "transfer" and payload - serialized to json Transfer structure
if err = c.SetEvent(`transfer`, &Transfer{
From: identity.Id{
MSP: invoker.GetMSPID(),
Cert: invoker.GetID(),
},
To: identity.Id{
MSP: toMspId,
Cert: toCertId,
},
Amount: amount,
}); err != nil {
return nil, err
}
// return current invoker balance
return invokerBalance - amount, nil
}
func queryAllowance(c r.Context) (interface{}, error) {
return getAllowance(c, c.ParamString(`ownerMspId`), c.ParamString(`ownerCertId`), c.ParamString(`spenderMspId`), c.ParamString(`spenderCertId`))
}
func invokeApprove(c r.Context) (interface{}, error) {
spenderMspId := c.ParamString(`spenderMspId`)
spenderCertId := c.ParamString(`spenderCertId`)
amount := c.ParamInt(`amount`)
invoker, err := identity.FromStub(c.Stub())
if err != nil {
return nil, err
}
if err = setAllowance(c, invoker.GetMSPID(), invoker.GetID(), spenderMspId, spenderCertId, amount); err != nil {
return nil, err
}
if err = c.SetEvent(`approve`, &Approve{
From: identity.Id{
MSP: invoker.GetMSPID(),
Cert: invoker.GetID(),
},
Spender: identity.Id{
MSP: spenderMspId,
Cert: spenderCertId,
},
Amount: amount,
}); err != nil {
return nil, err
}
return true, nil
}
func invokeTransferFrom(c r.Context) (interface{}, error) {
fromMspId := c.ParamString(`fromMspId`)
fromCertId := c.ParamString(`fromCertId`)
toMspId := c.ParamString(`toMspId`)
toCertId := c.ParamString(`toCertId`)
amount := c.ParamInt(`amount`)
invoker, err := identity.FromStub(c.Stub())
if err != nil {
return nil, err
}
// check method invoker has allowances
allowance, err := getAllowance(c, fromMspId, fromCertId, invoker.GetMSPID(), invoker.GetID())
if err != nil {
return nil, err
}
// transfer amount must be less or equal allowance
if allowance < amount {
return nil, ErrSpenderNotHaveAllowance
}
// current payer balance
balance, err := getBalance(c, fromMspId, fromCertId)
if err != nil {
return nil, err
}
// payer balance must be greater or equal amount
if balance-amount < 0 {
return nil, ErrNotEnoughFunds
}
// current recipient balance
recipientBalance, err := getBalance(c, toMspId, toCertId)
if err != nil {
return nil, err
}
// decrease payer balance
if err = setBalance(c, fromMspId, fromCertId, balance-amount); err != nil {
return nil, err
}
// increase recipient balance
if err = setBalance(c, toMspId, toCertId, recipientBalance+amount); err != nil {
return nil, err
}
// decrease invoker allowance
if err = setAllowance(c, fromMspId, fromCertId, invoker.GetMSPID(), invoker.GetID(), allowance-amount); err != nil {
return nil, err
}
if err = c.Event().Set(`transfer`, &Transfer{
From: identity.Id{
MSP: fromMspId,
Cert: fromCertId,
},
To: identity.Id{
MSP: toMspId,
Cert: toCertId,
},
Amount: amount,
}); err != nil {
return nil, err
}
// return current invoker balance
return balance - amount, nil
}
// === internal functions, not "public" chaincode functions
// setBalance puts balance value to state
func balanceKey(ownerMspId, ownerCertId string) []string {
return []string{BalancePrefix, ownerMspId, ownerCertId}
}
func allowanceKey(ownerMspId, ownerCertId, spenderMspId, spenderCertId string) []string {
return []string{AllowancePrefix, ownerMspId, ownerCertId, spenderMspId, spenderCertId}
}
func getBalance(c r.Context, mspId, certId string) (int, error) {
return c.State().GetInt(balanceKey(mspId, certId), 0)
}
// setBalance puts balance value to state
func setBalance(c r.Context, mspId, certId string, balance int) error {
return c.State().Put(balanceKey(mspId, certId), balance)
}
func getAllowance(c r.Context, ownerMspId, ownerCertId, spenderMspId, spenderCertId string) (int, error) {
return c.State().GetInt(allowanceKey(ownerMspId, ownerCertId, spenderMspId, spenderCertId), 0)
}
func setAllowance(c r.Context, ownerMspId, ownerCertId, spenderMspId, spenderCertId string, amount int) error {
return c.State().Put(allowanceKey(ownerMspId, ownerCertId, spenderMspId, spenderCertId), amount)
}