/
keeper.go
232 lines (203 loc) · 6.8 KB
/
keeper.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
package keeper
import (
"fmt"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/incubus-network/fury/x/evmutil/types"
)
// Keeper of the evmutil store.
// This keeper stores additional data related to evm accounts.
type Keeper struct {
cdc codec.Codec
storeKey storetypes.StoreKey
paramSubspace paramtypes.Subspace
bankKeeper types.BankKeeper
evmKeeper types.EvmKeeper
accountKeeper types.AccountKeeper
}
// NewKeeper creates an evmutil keeper.
func NewKeeper(
cdc codec.Codec,
storeKey storetypes.StoreKey,
params paramtypes.Subspace,
bk types.BankKeeper,
ak types.AccountKeeper,
) Keeper {
if !params.HasKeyTable() {
params = params.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSubspace: params,
bankKeeper: bk,
accountKeeper: ak,
}
}
func (k *Keeper) SetEvmKeeper(evmKeeper types.EvmKeeper) {
k.evmKeeper = evmKeeper
}
// GetAllAccounts returns all accounts.
func (k Keeper) GetAllAccounts(ctx sdk.Context) (accounts []types.Account) {
k.IterateAllAccounts(ctx, func(account types.Account) bool {
accounts = append(accounts, account)
return false
})
return accounts
}
// IterateAllAccounts iterates over all accounts. If true is returned from the
// callback, iteration is halted.
func (k Keeper) IterateAllAccounts(ctx sdk.Context, cb func(types.Account) bool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.AccountStoreKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var acc types.Account
if err := k.cdc.Unmarshal(iterator.Value(), &acc); err != nil {
panic(err)
}
if cb(acc) {
break
}
}
}
// GetAccount returns the account for a given address.
func (k Keeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) *types.Account {
store := ctx.KVStore(k.storeKey)
var account types.Account
bz := store.Get(types.AccountStoreKey(addr))
if bz == nil {
return nil
}
if err := account.Unmarshal(bz); err != nil {
panic(err)
}
return &account
}
// SetAccount sets the account for a given address.
func (k Keeper) SetAccount(ctx sdk.Context, account types.Account) error {
if err := account.Validate(); err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
accountKey := types.AccountStoreKey(account.Address)
// make sure we remove accounts with zero balance
if !account.Balance.IsPositive() {
if store.Has(accountKey) {
store.Delete(accountKey)
}
return nil
}
bz, err := k.cdc.Marshal(&account)
if err != nil {
panic(err)
}
store.Set(accountKey, bz)
return nil
}
// GetBalance returns the total balance of afury for a given account by address.
func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress) sdkmath.Int {
account := k.GetAccount(ctx, addr)
if account == nil {
return sdk.ZeroInt()
}
return account.Balance
}
// SetBalance sets the total balance of afury for a given account by address.
func (k Keeper) SetBalance(ctx sdk.Context, addr sdk.AccAddress, bal sdkmath.Int) error {
account := k.GetAccount(ctx, addr)
if account == nil {
account = types.NewAccount(addr, bal)
} else {
account.Balance = bal
}
if err := account.Validate(); err != nil {
return err
}
return k.SetAccount(ctx, *account)
}
// SendBalance transfers the afury balance from sender addr to recipient addr.
func (k Keeper) SendBalance(ctx sdk.Context, senderAddr sdk.AccAddress, recipientAddr sdk.AccAddress, amt sdkmath.Int) error {
if amt.IsNegative() {
return fmt.Errorf("cannot send a negative amount of afury: %d", amt)
}
if amt.IsZero() {
return nil
}
senderBal := k.GetBalance(ctx, senderAddr)
if senderBal.LT(amt) {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient funds to send %s", amt.String())
}
if err := k.SetBalance(ctx, senderAddr, senderBal.Sub(amt)); err != nil {
return err
}
receiverBal := k.GetBalance(ctx, recipientAddr).Add(amt)
return k.SetBalance(ctx, recipientAddr, receiverBal)
}
// AddBalance increments the afury balance of an address.
func (k Keeper) AddBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdkmath.Int) error {
bal := k.GetBalance(ctx, addr)
return k.SetBalance(ctx, addr, amt.Add(bal))
}
// RemoveBalance decrements the afury balance of an address.
func (k Keeper) RemoveBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdkmath.Int) error {
if amt.IsNegative() {
return fmt.Errorf("cannot remove a negative amount from balance: %d", amt)
}
if amt.IsZero() {
return nil
}
bal := k.GetBalance(ctx, addr)
finalBal := bal.Sub(amt)
if finalBal.IsNegative() {
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient funds to send %s", amt.String())
}
return k.SetBalance(ctx, addr, finalBal)
}
// SetDeployedCosmosCoinContract stores a single deployed ERC20FuryWrappedCosmosCoin contract address
func (k *Keeper) SetDeployedCosmosCoinContract(ctx sdk.Context, cosmosDenom string, contractAddress types.InternalEVMAddress) error {
if err := sdk.ValidateDenom(cosmosDenom); err != nil {
return errorsmod.Wrap(types.ErrInvalidCosmosDenom, cosmosDenom)
}
if contractAddress.IsNil() {
return errorsmod.Wrapf(
sdkerrors.ErrInvalidAddress,
"attempting to register empty contract address for denom '%s'",
cosmosDenom,
)
}
store := ctx.KVStore(k.storeKey)
storeKey := types.DeployedCosmosCoinContractKey(cosmosDenom)
store.Set(storeKey, contractAddress.Bytes())
return nil
}
// SetDeployedCosmosCoinContract gets a deployed ERC20FuryWrappedCosmosCoin contract address by cosmos denom
// Returns the stored address and a bool indicating if it was found or not
func (k *Keeper) GetDeployedCosmosCoinContract(ctx sdk.Context, cosmosDenom string) (types.InternalEVMAddress, bool) {
store := ctx.KVStore(k.storeKey)
storeKey := types.DeployedCosmosCoinContractKey(cosmosDenom)
bz := store.Get(storeKey)
found := len(bz) != 0
return types.BytesToInternalEVMAddress(bz), found
}
// IterateAllDeployedCosmosCoinContracts iterates through all the deployed ERC20 contracts representing
// cosmos-sdk coins. If true is returned from the callback, iteration is halted.
func (k Keeper) IterateAllDeployedCosmosCoinContracts(ctx sdk.Context, cb func(types.DeployedCosmosCoinContract) bool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.DeployedCosmosCoinContractKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
contract := types.NewDeployedCosmosCoinContract(
types.DenomFromDeployedCosmosCoinContractKey(iterator.Key()),
types.BytesToInternalEVMAddress(iterator.Value()),
)
if cb(contract) {
break
}
}
}