/
account.go
101 lines (88 loc) · 2.87 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
package types
import (
sdk "github.com/aximchain/axc-cosmos-sdk/types"
"github.com/aximchain/axc-cosmos-sdk/x/auth"
"github.com/aximchain/flash-node/wire"
)
var _ sdk.Account = (NamedAccount)(nil)
// TODO: maybe need to move GetFrozenCoins to the base interface
type NamedAccount interface {
sdk.Account
GetName() string
SetName(string)
GetFrozenCoins() sdk.Coins
SetFrozenCoins(sdk.Coins)
//TODO: this should merge into Coin
GetLockedCoins() sdk.Coins
SetLockedCoins(sdk.Coins)
GetFlags() uint64
SetFlags(uint64)
}
// Custom extensions for this application. This is just an example of
// extending auth.BaseAccount with custom fields.
//
// This is compatible with the stock auth.AccountStore, since
// auth.AccountStore uses the flexible go-amino library.
var _ NamedAccount = (*AppAccount)(nil)
type AppAccount struct {
auth.BaseAccount `json:"base"`
Name string `json:"name"`
FrozenCoins sdk.Coins `json:"frozen"`
LockedCoins sdk.Coins `json:"locked"`
Flags uint64 `json:"flags"`
}
// nolint
func (acc AppAccount) GetName() string { return acc.Name }
func (acc *AppAccount) SetName(name string) { acc.Name = name }
func (acc AppAccount) GetFrozenCoins() sdk.Coins { return acc.FrozenCoins }
func (acc *AppAccount) SetFrozenCoins(frozen sdk.Coins) { acc.FrozenCoins = frozen }
func (acc AppAccount) GetLockedCoins() sdk.Coins { return acc.LockedCoins }
func (acc *AppAccount) SetLockedCoins(frozen sdk.Coins) { acc.LockedCoins = frozen }
func (acc *AppAccount) GetFlags() uint64 { return acc.Flags }
func (acc *AppAccount) SetFlags(flags uint64) { acc.Flags = flags }
func (acc *AppAccount) Clone() sdk.Account {
baseAcc := acc.BaseAccount.Clone().(*auth.BaseAccount)
clonedAcc := &AppAccount{
BaseAccount: *baseAcc,
Name: acc.Name,
Flags: acc.Flags,
}
if acc.FrozenCoins == nil {
clonedAcc.FrozenCoins = nil
} else {
coins := sdk.Coins{}
for _, coin := range acc.FrozenCoins {
coins = append(coins, sdk.Coin{Denom: coin.Denom, Amount: coin.Amount})
}
clonedAcc.FrozenCoins = coins
}
if acc.LockedCoins == nil {
clonedAcc.LockedCoins = nil
} else {
coins := sdk.Coins{}
for _, coin := range acc.LockedCoins {
coins = append(coins, sdk.Coin{Denom: coin.Denom, Amount: coin.Amount})
}
clonedAcc.LockedCoins = coins
}
return clonedAcc
}
// Get the AccountDecoder function for the custom AppAccount
func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder {
return func(accBytes []byte) (res sdk.Account, err error) {
if len(accBytes) == 0 {
return nil, sdk.ErrTxDecode("accBytes are empty")
}
acct := new(AppAccount)
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
if err != nil {
panic(err)
}
return acct, err
}
}
// Prototype function for AppAccount
func ProtoAppAccount() sdk.Account {
aa := AppAccount{}
return &aa
}