-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_type.go
142 lines (121 loc) · 3.65 KB
/
asset_type.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
package keeper
import (
"encoding/binary"
"fmt"
"github.com/QuadrateOrg/core/x/grow/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
)
func (k Keeper) GetAssetCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.AssetCountKey)
bz := store.Get(byteKey)
if bz == nil {
return 0
}
return binary.BigEndian.Uint64(bz)
}
func (k Keeper) SetAssetCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.AssetCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
func (k Keeper) AppendAsset(
ctx sdk.Context,
Asset types.Asset,
) uint64 {
count := k.GetAssetCount(ctx)
Asset.Id = count
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
appendedValue := k.cdc.MustMarshal(&Asset)
store.Set(GetAssetIDBytes(Asset.Id), appendedValue)
k.SetAssetCount(ctx, count+1)
return count
}
func (k Keeper) SetAsset(ctx sdk.Context, Asset types.Asset) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
b := k.cdc.MustMarshal(&Asset)
store.Set(GetAssetIDBytes(Asset.Id), b)
}
func (k Keeper) RemoveAsset(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
store.Delete(GetAssetIDBytes(id))
}
func (k Keeper) GetAllAsset(ctx sdk.Context) (list []types.Asset) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Asset
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func GetAssetIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
func GetAssetIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}
//nolint:all
func (k Keeper) GenerateAssetIdHash(denom1 string) string {
return fmt.Sprintf("%x", crypto.Sha256(append([]byte(denom1))))
}
// func for gov proposal
func (k Keeper) RegisterAsset(ctx sdk.Context, p types.Asset) error {
Asset := types.Asset{
AssetId: k.GenerateAssetIdHash(p.AssetMetadata.Base),
AssetMetadata: p.AssetMetadata,
OracleAssetId: p.OracleAssetId,
}
_ = k.AppendAsset(ctx, Asset)
return nil
}
/*
functions for get lend asset
*/
func (k Keeper) GetAssetIdByCoins(amountIn string) (string, error) {
amountInCoins, err := sdk.ParseCoinsNormalized(amountIn)
if err != nil {
return "", err
}
return k.GenerateAssetIdHash(amountInCoins.GetDenomByIndex(0)), nil
}
func (k Keeper) GetAssetByDenom(ctx sdk.Context, denom string) (types.Asset, bool) {
assetId := k.GenerateAssetIdHash(denom)
asset, found := k.GetAssetByAssetId(ctx, assetId)
return asset, found
}
func (k Keeper) GetAssetByOracleAssetId(ctx sdk.Context, oracleAssetId string) (val types.Asset, err error) {
allAsset := k.GetAllAsset(ctx)
for _, v := range allAsset {
if v.OracleAssetId == oracleAssetId {
return v, nil
}
}
return val, types.ErrAssetNotFound
}
func (k Keeper) GetAssetByAssetId(ctx sdk.Context, AssetId string) (val types.Asset, found bool) {
allAsset := k.GetAllAsset(ctx)
for _, v := range allAsset {
if v.AssetId == AssetId {
return v, true
}
}
return val, false
}
func (k Keeper) GetAssetByID(ctx sdk.Context, id uint64) (val types.Asset, found bool) {
allAsset := k.GetAllAsset(ctx)
for _, v := range allAsset {
if v.Id == id {
return v, true
}
}
return val, false
}