forked from evmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
45 lines (42 loc) · 1.51 KB
/
decoder.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
package simulation
import (
"bytes"
"fmt"
"github.com/adminoid/cosmos-sdk/codec"
sdk "github.com/adminoid/cosmos-sdk/types"
"github.com/adminoid/cosmos-sdk/types/kv"
"github.com/adminoid/cosmos-sdk/x/nft"
"github.com/adminoid/cosmos-sdk/x/nft/keeper"
)
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding nft type.
func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], keeper.ClassKey):
var classA, classB nft.Class
cdc.MustUnmarshal(kvA.Value, &classA)
cdc.MustUnmarshal(kvB.Value, &classB)
return fmt.Sprintf("%v\n%v", classA, classB)
case bytes.Equal(kvA.Key[:1], keeper.NFTKey):
var nftA, nftB nft.NFT
cdc.MustUnmarshal(kvA.Value, &nftA)
cdc.MustUnmarshal(kvB.Value, &nftB)
return fmt.Sprintf("%v\n%v", nftA, nftB)
case bytes.Equal(kvA.Key[:1], keeper.NFTOfClassByOwnerKey):
return fmt.Sprintf("%v\n%v", kvA.Value, kvB.Value)
case bytes.Equal(kvA.Key[:1], keeper.OwnerKey):
var ownerA, ownerB sdk.AccAddress
ownerA = sdk.AccAddress(kvA.Value)
ownerB = sdk.AccAddress(kvB.Value)
return fmt.Sprintf("%v\n%v", ownerA, ownerB)
case bytes.Equal(kvA.Key[:1], keeper.ClassTotalSupply):
var supplyA, supplyB uint64
supplyA = sdk.BigEndianToUint64(kvA.Value)
supplyB = sdk.BigEndianToUint64(kvB.Value)
return fmt.Sprintf("%v\n%v", supplyA, supplyB)
default:
panic(fmt.Sprintf("invalid nft key %X", kvA.Key))
}
}
}