-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
130 lines (114 loc) · 3.55 KB
/
pair.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
package keeper
import (
"encoding/binary"
"fmt"
"github.com/QuadrateOrg/core/x/stable/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
)
func (k Keeper) GetPairCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.PairCountKey)
bz := store.Get(byteKey)
if bz == nil {
return 0
}
return binary.BigEndian.Uint64(bz)
}
func (k Keeper) SetPairCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.PairCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
func (k Keeper) AppendPair(
ctx sdk.Context,
pair types.Pair,
) uint64 {
count := k.GetPairCount(ctx)
pair.Id = count
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PairKey))
appendedValue := k.cdc.MustMarshal(&pair)
store.Set(GetPairIDBytes(pair.Id), appendedValue)
k.SetPairCount(ctx, count+1)
return count
}
func (k Keeper) SetPair(ctx sdk.Context, Pair types.Pair) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PairKey))
b := k.cdc.MustMarshal(&Pair)
store.Set(GetPairIDBytes(Pair.Id), b)
}
func (k Keeper) RemovePair(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PairKey))
store.Delete(GetPairIDBytes(id))
}
func (k Keeper) GetAllPair(ctx sdk.Context) (list []types.Pair) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PairKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Pair
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func GetPairIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
func GetPairIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}
func (k Keeper) GetPairByPairID(ctx sdk.Context, pairId string) (val types.Pair, found bool) {
allPair := k.GetAllPair(ctx)
for _, v := range allPair {
if v.PairId == pairId {
return v, true
}
}
return val, false
}
func (k Keeper) GetPairByID(ctx sdk.Context, id uint64) (val types.Pair, found bool) {
allPair := k.GetAllPair(ctx)
for _, v := range allPair {
if v.Id == id {
return v, true
}
}
return val, false
}
//nolint:all
func (k Keeper) GeneratePairIdHash(denom1 string, denom2 string) string {
return fmt.Sprintf("%x", crypto.Sha256(append([]byte(denom1+denom2))))
}
func (k Keeper) GetPairIdMint(amountInt string, denom2 string) (string, error) {
amountIntCoins, err := sdk.ParseCoinsNormalized(amountInt)
if err != nil {
return "", err
}
return k.GeneratePairIdHash(amountIntCoins.GetDenomByIndex(0), denom2), nil
}
func (k Keeper) GetPairIdBurn(amountInt string, denom2 string) (string, error) {
amountIntCoins, err := sdk.ParseCoinsNormalized(amountInt)
if err != nil {
return "", err
}
return k.GeneratePairIdHash(denom2, amountIntCoins.GetDenomByIndex(0)), nil
}
func (k Keeper) RegisterPair(ctx sdk.Context, p types.Pair) error {
pair := types.Pair{
PairId: k.GeneratePairIdHash(p.AmountInMetadata.Base, p.AmountOutMetadata.Base),
AmountInMetadata: p.AmountInMetadata,
AmountOutMetadata: p.AmountOutMetadata,
Qm: sdk.NewInt(0),
Ar: sdk.NewInt(0),
MinAmountIn: p.MinAmountIn,
Model: p.Model,
}
_ = k.AppendPair(ctx, pair)
return nil
}