-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize_share_record.go
150 lines (116 loc) · 4.92 KB
/
tokenize_share_record.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gogotypes "github.com/gogo/protobuf/types"
"github.com/incubus-network/fanfury-sdk/v2/x/lsnative/staking/types"
)
func (k Keeper) GetLastTokenizeShareRecordId(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bytes := store.Get(types.LastTokenizeShareRecordIdKey)
if bytes == nil {
return 0
}
return sdk.BigEndianToUint64(bytes)
}
func (k Keeper) SetLastTokenizeShareRecordId(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.LastTokenizeShareRecordIdKey, sdk.Uint64ToBigEndian(id))
}
func (k Keeper) GetTokenizeShareRecord(ctx sdk.Context, id uint64) (tokenizeShareRecord types.TokenizeShareRecord, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTokenizeShareRecordByIndexKey(id))
if bz == nil {
return tokenizeShareRecord, sdkerrors.Wrap(types.ErrTokenizeShareRecordNotExists, fmt.Sprintf("tokenizeShareRecord %d does not exist", id))
}
k.cdc.MustUnmarshal(bz, &tokenizeShareRecord)
return tokenizeShareRecord, nil
}
func (k Keeper) GetTokenizeShareRecordsByOwner(ctx sdk.Context, owner sdk.AccAddress) (tokenizeShareRecords []types.TokenizeShareRecord) {
store := ctx.KVStore(k.storeKey)
var it sdk.Iterator = sdk.KVStorePrefixIterator(store, types.GetTokenizeShareRecordIdsByOwnerPrefix(owner))
defer it.Close()
for ; it.Valid(); it.Next() {
var id gogotypes.UInt64Value
k.cdc.MustUnmarshal(it.Value(), &id)
tokenizeShareRecord, err := k.GetTokenizeShareRecord(ctx, id.Value)
if err != nil {
continue
}
tokenizeShareRecords = append(tokenizeShareRecords, tokenizeShareRecord)
}
return
}
func (k Keeper) GetTokenizeShareRecordByDenom(ctx sdk.Context, denom string) (types.TokenizeShareRecord, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTokenizeShareRecordIdByDenomKey(denom))
if bz == nil {
return types.TokenizeShareRecord{}, fmt.Errorf("tokenize share record not found from denom: %s", denom)
}
var id gogotypes.UInt64Value
k.cdc.MustUnmarshal(bz, &id)
return k.GetTokenizeShareRecord(ctx, id.Value)
}
func (k Keeper) GetAllTokenizeShareRecords(ctx sdk.Context) (tokenizeShareRecords []types.TokenizeShareRecord) {
store := ctx.KVStore(k.storeKey)
var it sdk.Iterator = sdk.KVStorePrefixIterator(store, types.TokenizeShareRecordPrefix)
defer it.Close()
for ; it.Valid(); it.Next() {
var tokenizeShareRecord types.TokenizeShareRecord
k.cdc.MustUnmarshal(it.Value(), &tokenizeShareRecord)
tokenizeShareRecords = append(tokenizeShareRecords, tokenizeShareRecord)
}
return
}
func (k Keeper) AddTokenizeShareRecord(ctx sdk.Context, tokenizeShareRecord types.TokenizeShareRecord) error {
if k.hasTokenizeShareRecord(ctx, tokenizeShareRecord.Id) {
return sdkerrors.Wrapf(types.ErrTokenizeShareRecordAlreadyExists, "TokenizeShareRecord already exists: %d", tokenizeShareRecord.Id)
}
k.setTokenizeShareRecord(ctx, tokenizeShareRecord)
owner, err := sdk.AccAddressFromBech32(tokenizeShareRecord.Owner)
if err != nil {
return err
}
k.setTokenizeShareRecordWithOwner(ctx, owner, tokenizeShareRecord.Id)
k.setTokenizeShareRecordWithDenom(ctx, tokenizeShareRecord.GetShareTokenDenom(), tokenizeShareRecord.Id)
return nil
}
func (k Keeper) DeleteTokenizeShareRecord(ctx sdk.Context, recordId uint64) error {
record, err := k.GetTokenizeShareRecord(ctx, recordId)
if err != nil {
return err
}
owner, err := sdk.AccAddressFromBech32(record.Owner)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetTokenizeShareRecordByIndexKey(recordId))
store.Delete(types.GetTokenizeShareRecordIdByOwnerAndIdKey(owner, recordId))
store.Delete(types.GetTokenizeShareRecordIdByDenomKey(record.GetShareTokenDenom()))
return nil
}
func (k Keeper) hasTokenizeShareRecord(ctx sdk.Context, id uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetTokenizeShareRecordByIndexKey(id))
}
func (k Keeper) setTokenizeShareRecord(ctx sdk.Context, tokenizeShareRecord types.TokenizeShareRecord) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&tokenizeShareRecord)
store.Set(types.GetTokenizeShareRecordByIndexKey(tokenizeShareRecord.Id), bz)
}
func (k Keeper) setTokenizeShareRecordWithOwner(ctx sdk.Context, owner sdk.AccAddress, id uint64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.UInt64Value{Value: id})
store.Set(types.GetTokenizeShareRecordIdByOwnerAndIdKey(owner, id), bz)
}
func (k Keeper) deleteTokenizeShareRecordWithOwner(ctx sdk.Context, owner sdk.AccAddress, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetTokenizeShareRecordIdByOwnerAndIdKey(owner, id))
}
func (k Keeper) setTokenizeShareRecordWithDenom(ctx sdk.Context, denom string, id uint64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.UInt64Value{Value: id})
store.Set(types.GetTokenizeShareRecordIdByDenomKey(denom), bz)
}