-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathallowance.go
163 lines (135 loc) · 4.09 KB
/
allowance.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
package keeper
import (
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
"github.com/ethereum/go-ethereum/common"
"github.com/cosmos/evm/x/erc20/types"
)
// GetAllowance returns the allowance of the given owner and spender
// on the given erc20 precompile address.
func (k Keeper) GetAllowance(
ctx sdk.Context,
erc20 common.Address,
owner common.Address,
spender common.Address,
) (*big.Int, error) {
allowanceKey := types.AllowanceKey(erc20, owner, spender)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixAllowance)
var allowance types.Allowance
bz := store.Get(allowanceKey)
if bz == nil {
return big.NewInt(0), nil
}
k.cdc.MustUnmarshal(bz, &allowance)
return allowance.Value.BigInt(), nil
}
// SetAllowance sets the allowance of the given owner and spender
// on the given erc20 precompile address.
func (k Keeper) SetAllowance(
ctx sdk.Context,
erc20 common.Address,
owner common.Address,
spender common.Address,
value *big.Int,
) error {
return k.setAllowance(ctx, erc20, owner, spender, value)
}
// DeleteAllowance deletes the allowance of the given owner and spender
// on the given erc20 precompile address.
func (k Keeper) DeleteAllowance(
ctx sdk.Context,
erc20 common.Address,
owner common.Address,
spender common.Address,
) error {
return k.setAllowance(ctx, erc20, owner, spender, common.Big0)
}
func (k Keeper) setAllowance(
ctx sdk.Context,
erc20 common.Address,
owner common.Address,
spender common.Address,
value *big.Int,
) error {
if err := k.checkAddressesNonZero(erc20, owner, spender); err != nil {
return err
}
if err := k.checkTokenPair(ctx, erc20); err != nil {
return err
}
allowanceKey := types.AllowanceKey(erc20, owner, spender)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixAllowance)
switch {
case value == nil || value.Sign() == 0:
store.Delete(allowanceKey)
case value.Sign() < 0:
return errorsmod.Wrapf(types.ErrInvalidAllowance, "value '%s' is less than zero", value)
default:
allowance := types.NewAllowance(erc20, owner, spender, value)
bz := k.cdc.MustMarshal(&allowance)
store.Set(allowanceKey, bz)
}
return nil
}
// GetAllowances returns all allowances stored on the given erc20 precompile address.
func (k Keeper) GetAllowances(
ctx sdk.Context,
) []types.Allowance {
allowances := []types.Allowance{}
k.IterateAllowances(ctx, func(allowance types.Allowance) (stop bool) {
allowances = append(allowances, allowance)
return false
})
return allowances
}
// IterateAllowances iterates through all allowances stored on the given erc20 precompile address.
func (k Keeper) IterateAllowances(
ctx sdk.Context,
cb func(allowance types.Allowance) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.KeyPrefixAllowance)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var allowance types.Allowance
k.cdc.MustUnmarshal(iterator.Value(), &allowance)
if cb(allowance) {
break
}
}
}
func (k Keeper) checkAddressesNonZero(
erc20 common.Address,
owner common.Address,
spender common.Address,
) error {
if erc20 == (common.Address{}) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid erc20 address: '%s'", erc20)
}
if owner == (common.Address{}) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid owner address: '%s'", owner)
}
if spender == (common.Address{}) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid spender address: '%s'", spender)
}
return nil
}
func (k Keeper) checkTokenPair(ctx sdk.Context, erc20 common.Address) error {
tokenPairID := k.GetERC20Map(ctx, erc20)
tokenPair, found := k.GetTokenPair(ctx, tokenPairID)
if !found {
return errorsmod.Wrapf(
types.ErrTokenPairNotFound, "token pair for address '%s' not registered", erc20,
)
}
if !tokenPair.Enabled {
return errorsmod.Wrapf(
types.ErrERC20TokenPairDisabled, "token pair for address '%s' is disabled", erc20,
)
}
return nil
}