forked from nami-land/walleter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalleter.go
287 lines (250 loc) · 7.22 KB
/
walleter.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package walleter
import (
"errors"
"os"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// WalletCommand is an object of the operation for withdrawals or depositions.
// Anytime when users deposit, withdraw or spend an asset, we should
// construct a WalletCommand object to operate the wallet in database.
type WalletCommand struct {
// User account id. unique
AccountId uint64
// 0: ERC20 token, 1: erc1155 token. 2. other type.
AssetType AssetType
// Action of this command. initialize, withdraw, deposit, etc.
ActionType WalletActionType
// ERC20 command, if we want to operate ERC20 asset, this should not be nil. otherwise this must be nil.
ERC20Commands []ERC20Command
// ERC20 command, if we want to operate ERC1155 asset, this should not be nil. otherwise this must be nil.
ERC1155Command ERC1155Command
// Fee charging command, if len(FeeCommands) > 0, assets should be deducted from user's account.
FeeCommands []ERC20Command
// A name about which part sent this command
BusinessModule string
// Command happened source.
CommandSource CommandSourceType
}
type ERC20Command struct {
Token ERC20TokenEnum
Value float64
Decimal uint64
}
type ERC1155Command struct {
Ids []uint64
Values []uint64
}
// Walleter the library entry object.
type Walleter struct {
db *gorm.DB
}
var feeChargerAccountId uint64
func New(db *gorm.DB, chargerAccountId uint64) *Walleter {
migration(db)
feeChargerAccountId = chargerAccountId
walleter := Walleter{db: db}
_, err := walleter.setFeeChargerAccount()
if err != nil {
panic("initialize fee charger account failed")
}
return &walleter
}
func (s *Walleter) HandleWalletCommand(db *gorm.DB, command WalletCommand) (Wallet, error) {
switch command.ActionType {
case Initialize:
wallet, err := s.GetWalletByAccountId(command.AccountId)
// if user's wallet doesn't exist, create a new one.
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
command := NewInitWalletCommand(feeChargerAccountId)
return initWallet(db, command)
}
// otherwise return the old one.
return wallet, nil
default:
return updateWallet(db, command)
}
}
func (s *Walleter) GetWalletByAccountId(accountId uint64) (Wallet, error) {
return walletDAO.getWallet(s.db, accountId)
}
// initialize fee charger account in database.
func (s *Walleter) setFeeChargerAccount() (Wallet, error) {
if feeChargerAccountId == 0 {
panic("Please assign official fee charge account.")
}
wallet, err := s.GetWalletByAccountId(feeChargerAccountId)
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
command := NewInitWalletCommand(feeChargerAccountId)
return s.HandleWalletCommand(s.db, command)
}
return wallet, nil
}
func migration(db *gorm.DB) {
_ = db.AutoMigrate(ERC20TokenWallet{})
_ = db.AutoMigrate(ERC1155TokenWallet{})
_ = db.AutoMigrate(Wallet{})
_ = db.AutoMigrate(ERC20WalletLog{})
_ = db.AutoMigrate(ERC1155WalletLog{})
}
func init() {
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
}
func initWallet(db *gorm.DB, command WalletCommand) (Wallet, error) {
err := db.Transaction(func(tx1 *gorm.DB) error {
// 1. Insert change logs, including ERC20 logs and ERC1155 Log.
walletLogService := newWalletLogService()
erc20WalletLog, err := walletLogService.insertNewERC20WalletLog(tx1, command, Wallet{})
if err != nil {
return err
}
erc115WalletLog, err := walletLogService.insertNewERC1155WalletLog(tx1, command, Wallet{})
if err != nil {
return err
}
// 2. initialize user's wallet data.
erc20DataArray := parseCommandToERC20WalletArray(command)
erc1155Data := parseCommandToERC1155Wallet(command)
wallet := Wallet{
AccountId: command.AccountId,
ERC20TokenData: erc20DataArray,
ERC1155TokenData: erc1155Data,
CheckSign: "",
}
// 3. generate a new check sign
newCheckSign, err := newWalletValidator().generateNewSignHash(wallet)
if err != nil {
return err
}
wallet.CheckSign = newCheckSign
err = walletDAO.createWallet(tx1, wallet)
if err != nil {
return err
}
// 4. change log statuses
_, err = walletLogService.updateERC20WalletLog(tx1, erc20WalletLog, Done, wallet)
if err != nil {
return err
}
_, err = walletLogService.updateERC1155WalletLog(tx1, erc115WalletLog, Done, wallet)
if err != nil {
return err
}
return nil
})
if err != nil {
return Wallet{}, err
}
return walletDAO.getWallet(db, command.AccountId)
}
func updateWallet(db *gorm.DB, command WalletCommand) (Wallet, error) {
if command.AssetType == Other {
return Wallet{}, ErrIncorrectAssetType
}
switch command.AssetType {
case ERC20AssetType:
return handleERC20Command(db, command)
case ERC1155AssetType:
return handleERC1155Command(db, command)
}
return Wallet{}, ErrAssetTypeNotSupport
}
func NewInitWalletCommand(accountId uint64) WalletCommand {
var erc20Commands []ERC20Command
for _, item := range supportedERC20Tokens {
erc20Commands = append(erc20Commands, ERC20Command{
Token: ERC20TokenEnum(item.Index),
Value: 0,
Decimal: item.Decimal,
})
}
return WalletCommand{
AccountId: accountId,
AssetType: Other,
ERC20Commands: erc20Commands,
ERC1155Command: ERC1155Command{},
BusinessModule: "Initialization",
ActionType: Initialize,
FeeCommands: []ERC20Command{},
}
}
func NewERC20WalletCommand(
accountId uint64,
actionType WalletActionType,
businessModule string,
commandSource CommandSourceType,
erc20Tokens map[ERC20TokenEnum]float64,
fees map[ERC20TokenEnum]float64,
) WalletCommand {
var erc20Commands []ERC20Command
for key, value := range erc20Tokens {
for _, supportToken := range supportedERC20Tokens {
if key.String() == supportToken.Symbol {
erc20Commands = append(erc20Commands, ERC20Command{
Token: key,
Value: value,
Decimal: supportToken.Decimal,
})
break
}
}
}
var feeCommands []ERC20Command
for key, value := range fees {
for _, supportToken := range supportedERC20Tokens {
if key.String() == supportToken.Symbol {
feeCommands = append(feeCommands, ERC20Command{
Token: key,
Value: value,
Decimal: supportToken.Decimal,
})
break
}
}
}
return WalletCommand{
AccountId: accountId,
AssetType: ERC20AssetType,
ERC20Commands: erc20Commands,
ERC1155Command: ERC1155Command{},
BusinessModule: businessModule,
ActionType: actionType,
CommandSource: commandSource,
FeeCommands: feeCommands,
}
}
func NewERC1155WalletCommand(
accountId uint64,
actionType WalletActionType,
businessModule string,
commandSource CommandSourceType,
ids []uint64,
values []uint64,
fees map[ERC20TokenEnum]float64,
) WalletCommand {
var feeCommands []ERC20Command
for key, value := range fees {
for _, supportToken := range supportedERC20Tokens {
if key.String() == supportToken.Symbol {
feeCommands = append(feeCommands, ERC20Command{
Token: key,
Value: value,
Decimal: supportToken.Decimal,
})
break
}
}
}
return WalletCommand{
AccountId: accountId,
AssetType: ERC1155AssetType,
ERC20Commands: nil,
ERC1155Command: ERC1155Command{ids, values},
BusinessModule: businessModule,
CommandSource: commandSource,
ActionType: actionType,
FeeCommands: feeCommands,
}
}