-
Notifications
You must be signed in to change notification settings - Fork 204
/
deposit_record.go
114 lines (93 loc) · 3.57 KB
/
deposit_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
package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Stride-Labs/stride/v16/x/records/types"
)
// GetDepositRecordCount get the total number of depositRecord
func (k Keeper) GetDepositRecordCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.DepositRecordCountKey)
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetDepositRecordCount set the total number of depositRecord
func (k Keeper) SetDepositRecordCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.DepositRecordCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// AppendDepositRecord appends a depositRecord in the store with a new id and update the count
func (k Keeper) AppendDepositRecord(
ctx sdk.Context,
depositRecord types.DepositRecord,
) uint64 {
// Create the depositRecord
count := k.GetDepositRecordCount(ctx)
// Set the ID of the appended value
depositRecord.Id = count
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DepositRecordKey))
appendedValue := k.Cdc.MustMarshal(&depositRecord)
store.Set(GetDepositRecordIDBytes(depositRecord.Id), appendedValue)
// Update depositRecord count
k.SetDepositRecordCount(ctx, count+1)
return count
}
// SetDepositRecord set a specific depositRecord in the store
func (k Keeper) SetDepositRecord(ctx sdk.Context, depositRecord types.DepositRecord) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DepositRecordKey))
b := k.Cdc.MustMarshal(&depositRecord)
store.Set(GetDepositRecordIDBytes(depositRecord.Id), b)
}
// GetDepositRecord returns a depositRecord from its id
func (k Keeper) GetDepositRecord(ctx sdk.Context, id uint64) (val types.DepositRecord, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DepositRecordKey))
b := store.Get(GetDepositRecordIDBytes(id))
if b == nil {
return val, false
}
k.Cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveDepositRecord removes a depositRecord from the store
func (k Keeper) RemoveDepositRecord(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DepositRecordKey))
store.Delete(GetDepositRecordIDBytes(id))
}
// GetAllDepositRecord returns all depositRecord
func (k Keeper) GetAllDepositRecord(ctx sdk.Context) (list []types.DepositRecord) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DepositRecordKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.DepositRecord
k.Cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetDepositRecordIDBytes returns the byte representation of the ID
func GetDepositRecordIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
func (k Keeper) GetTransferDepositRecordByEpochAndChain(ctx sdk.Context, epochNumber uint64, chainId string) (val *types.DepositRecord, found bool) {
records := k.GetAllDepositRecord(ctx)
for _, depositRecord := range records {
if depositRecord.DepositEpochNumber == epochNumber &&
depositRecord.HostZoneId == chainId &&
depositRecord.Status == types.DepositRecord_TRANSFER_QUEUE {
return &depositRecord, true
}
}
return nil, false
}