-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_account.go
209 lines (176 loc) · 5.54 KB
/
base_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
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
package types
import (
"fmt"
"github.com/ci123chain/ci123chain/pkg/abci/types"
"github.com/ci123chain/ci123chain/pkg/account/exported"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto"
"time"
)
var _ exported.Account = (*BaseAccount)(nil)
const (
EvmContractType string = "evm"
WasmContractType string = "wasm"
)
func ProtoBaseAccount() exported.Account {
return &BaseAccount{}
}
type BaseAccount struct {
Address types.AccAddress `json:"address" yaml:"address"`
Coins types.Coins `json:"coins" yaml:"coins"`
Sequence uint64 `json:"sequence_number" yaml:"sequence_number"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
PubKey crypto.PubKey `json:"pub_key" yaml:"pub_key"`
ContractList []string `json:"contract_list" yaml:"contract_list"`
ContractType string `json:"contract_type" yaml:"contract_type"`
CodeHash []byte `json:"code_hash" yaml:"code_hash"`
IsModule bool `json:"is_module"`
}
func (acc *BaseAccount) SetCodeHash(bz []byte) {
acc.CodeHash = bz
}
func (acc *BaseAccount) GetCodeHash() []byte {
return acc.CodeHash
}
// NewBaseAccount creates a new BaseAccount object
func NewBaseAccount(address types.AccAddress, coin types.Coins,
pubKey crypto.PubKey, accountNumber uint64, sequence uint64) *BaseAccount {
return &BaseAccount{
Address: address,
Coins: coin,
PubKey: pubKey,
AccountNumber: accountNumber,
Sequence: sequence,
}
}
func NewBaseAccountFromExportAccount(exportAcc exported.Account) *BaseAccount {
return &BaseAccount{
Address: exportAcc.GetAddress(),
Coins: exportAcc.GetCoins(),
Sequence: exportAcc.GetSequence(),
AccountNumber: exportAcc.GetAccountNumber(),
PubKey: exportAcc.GetPubKey(),
ContractType: exportAcc.GetContractType(),
CodeHash: exportAcc.GetCodeHash(),
IsModule: exportAcc.GetIsModule(),
}
}
// NewBaseAccountWithAddress - returns a new base account with a given address
func NewBaseAccountWithAddress(addr types.AccAddress) BaseAccount {
return BaseAccount{
Address: addr,
}
}
//func (acc *BaseAccount) AddContract(contractAddress types.AccAddress) {
// contractAddrStr := contractAddress.String()
// for _,v := range acc.ContractList {
// if v == contractAddrStr {
// return
// }
// }
// acc.ContractList = append(acc.ContractList, contractAddrStr)
//}
//
//func (acc BaseAccount) GetContractList() []string {
// return acc.ContractList
//}
// GetAddress - Implements sdk.Account.
func (acc *BaseAccount) GetAddress() types.AccAddress {
return acc.Address
}
// SetAddress - Implements sdk.Account.
func (acc *BaseAccount) SetAddress(addr types.AccAddress) error {
if !acc.Address.Empty(){
return errors.New("cannot override BaseAccount address")
}
acc.Address = addr
return nil
}
// GetPubKey - Implements sdk.Account.
func (acc *BaseAccount) GetPubKey() crypto.PubKey {
return acc.PubKey
}
// SetPubKey - Implements sdk.Account.
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
acc.PubKey = pubKey
return nil
}
// GetCoins - Implements sdk.Account.
func (acc *BaseAccount) GetCoins() types.Coins {
return acc.Coins
}
// SetCoins - Implements sdk.Account.
func (acc *BaseAccount) SetCoins(coin types.Coins) error {
if coin == nil {
coin = types.Coins{types.NewChainCoin(types.NewInt(0))}
}
acc.Coins = coin
return nil
}
// GetAccountNumber - Implements Account
func (acc *BaseAccount) GetAccountNumber() uint64 {
return acc.AccountNumber
}
// SetAccountNumber - Implements Account
func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error {
acc.AccountNumber = accNumber
return nil
}
// GetSequence - Implements sdk.Account.
func (acc *BaseAccount) GetSequence() uint64 {
return acc.Sequence
}
// SetSequence - Implements sdk.Account.
func (acc *BaseAccount) SetSequence(seq uint64) error {
acc.Sequence = seq
return nil
}
// SpendableCoins returns the total set of spendable coins. For a base account,
// this is simply the base coins.
func (acc *BaseAccount) SpendableCoins(_ time.Time) types.Coins{
return acc.GetCoins()
}
func (acc *BaseAccount) String() string {
return fmt.Sprintf(`Vesting Account:
Address: %s
Pubkey: %s
Coins: %v
AccountNumber: %d
Sequence: %d`,
acc.Address, acc.PubKey, acc.Coins, acc.AccountNumber, acc.Sequence,
)
}
// EthAddress returns the account address ethereum format.
//func (acc *BaseAccount) EthAddress() ethcmn.Address {
// return ethcmn.BytesToAddress(acc.Address.Bytes())
//}
// Balance returns the balance of an account.
//func (acc *BaseAccount) Balance(denom string) types.Int {
// return acc.GetCoins().AmountOf(denom)
//}
// SetBalance sets an account's balance of the given coin denomination.
//
// CONTRACT: assumes the denomination is valid.
//func (acc *BaseAccount) SetBalance(denom string, amt types.Int) {
// newCoin := types.NewChainCoin(amt)
// if err := acc.SetCoins(types.NewCoins(newCoin)); err != nil {
// panic(fmt.Errorf("could not set %s coins for address %s: %w", denom, acc.GetAddress().String(), err))
// }
//}
func (acc *BaseAccount) SetContractType(contractType string) error {
if contractType != EvmContractType && contractType != WasmContractType {
return errors.New("error contractType")
}
acc.ContractType = contractType
return nil
}
func (acc *BaseAccount) GetContractType() string {
return acc.ContractType
}
func (acc *BaseAccount) SetIsModule(flag bool) error {
acc.IsModule = flag
return nil
}
func (acc *BaseAccount) GetIsModule() bool {
return acc.IsModule
}