-
Notifications
You must be signed in to change notification settings - Fork 13
/
dao.go
156 lines (129 loc) · 3.99 KB
/
dao.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
package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/gitopia/gitopia/v2/x/gitopia/types"
)
// GetDaoCount get the total number of Dao
func (k Keeper) GetDaoCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DaoCountKey))
byteKey := types.KeyPrefix(types.DaoCountKey)
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetDaoCount set the total number of Dao
func (k Keeper) SetDaoCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DaoCountKey))
byteKey := types.KeyPrefix(types.DaoCountKey)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// AppendDao appends a Dao in the store with a new id and update the count
func (k Keeper) AppendDao(
ctx sdk.Context,
dao types.Dao,
) uint64 {
// Create the Dao
count := k.GetDaoCount(ctx)
// Set the ID of the appended value
dao.Id = count
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DaoKey))
appendedValue := k.cdc.MustMarshal(&dao)
store.Set([]byte(dao.Address), appendedValue)
// Update Dao count
k.SetDaoCount(ctx, count+1)
return count
}
func (k Keeper) SetUserDao(
ctx sdk.Context,
userDao types.UserDao,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.GetUserDaoKeyForUserAddress(userDao.UserAddress)))
appendedValue := k.cdc.MustMarshal(&userDao)
store.Set([]byte(userDao.DaoAddress), appendedValue)
}
// SetDao set a specific Dao in the store
func (k Keeper) SetDao(ctx sdk.Context, dao types.Dao) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.DaoKey),
)
b := k.cdc.MustMarshal(&dao)
store.Set([]byte(dao.Address), b)
}
// GetDao returns a Dao from its address
func (k Keeper) GetDao(ctx sdk.Context, address string) (val types.Dao, found bool) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.DaoKey),
)
b := store.Get([]byte(address))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveDao removes a Dao from the store
func (k Keeper) RemoveDao(ctx sdk.Context, address string) {
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.DaoKey),
)
store.Delete([]byte(address))
}
// GetAllDao returns all Dao
func (k Keeper) GetAllDao(ctx sdk.Context) (list []types.Dao) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DaoKey))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Dao
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllUserDao returns all user
func (k Keeper) GetAllUserDao(ctx sdk.Context, userAddress string) ([]types.Dao, error) {
var list []types.Dao
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.KeyPrefix(types.GetUserDaoKeyForUserAddress(userAddress)),
)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var userDao types.UserDao
if err := k.cdc.Unmarshal(iterator.Value(), &userDao); err != nil {
return []types.Dao{}, err
}
dao, found := k.GetDao(ctx, userDao.DaoAddress)
if found {
list = append(list, dao)
}
}
return list, nil
}
func NewDaoAddress(daoId uint64) sdk.AccAddress {
key := append([]byte("dao"), sdk.Uint64ToBigEndian(daoId)...)
return address.Module(types.ModuleName, key)
}
// GetDaoIDBytes returns the byte representation of the ID
func GetDaoIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
// GetDaoIDFromBytes returns ID in uint64 format from a byte array
func GetDaoIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}