-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathaccount.go
103 lines (91 loc) · 2.58 KB
/
account.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
/*
* Copyright © 2021 ZkBNB Protocol
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package types
import (
"encoding/json"
"math/big"
)
const (
FungibleAssetType = 1
NftAssetType = 2
CollectionNonceAssetType = 3
BuyOfferType = 0
SellOfferType = 1
)
type AccountAsset struct {
AssetId int64
Balance *big.Int
OfferCanceledOrFinalized *big.Int
}
func (asset *AccountAsset) DeepCopy() *AccountAsset {
return &AccountAsset{
AssetId: asset.AssetId,
Balance: big.NewInt(0).Set(asset.Balance),
OfferCanceledOrFinalized: big.NewInt(0).Set(asset.OfferCanceledOrFinalized),
}
}
func ConstructAccountAsset(assetId int64, balance *big.Int, offerCanceledOrFinalized *big.Int) *AccountAsset {
return &AccountAsset{
assetId,
balance,
offerCanceledOrFinalized,
}
}
func ParseAccountAsset(balance string) (asset *AccountAsset, err error) {
err = json.Unmarshal([]byte(balance), &asset)
if err != nil {
return nil, JsonErrUnmarshal
}
return asset, nil
}
func (asset *AccountAsset) String() (info string) {
infoBytes, _ := json.Marshal(asset)
return string(infoBytes)
}
type AccountInfo struct {
AccountId uint
AccountIndex int64
AccountName string
PublicKey string
AccountNameHash string
L1Address string
Nonce int64
CollectionNonce int64
AssetInfo map[int64]*AccountAsset // key: index, value: balance
AssetRoot string
Status int
}
func (ai *AccountInfo) DeepCopy() *AccountInfo {
assetInfo := make(map[int64]*AccountAsset)
for assetId, asset := range ai.AssetInfo {
assetInfo[assetId] = asset.DeepCopy()
}
newAccountInfo := &AccountInfo{
AccountId: ai.AccountId,
AccountIndex: ai.AccountIndex,
AccountName: ai.AccountName,
PublicKey: ai.PublicKey,
AccountNameHash: ai.AccountNameHash,
L1Address: ai.L1Address,
Nonce: ai.Nonce,
CollectionNonce: ai.CollectionNonce,
AssetInfo: assetInfo,
AssetRoot: ai.AssetRoot,
Status: ai.Status,
}
return newAccountInfo
}