github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/simulation/decoder.go (about)

     1  package simulation
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/cosmos/cosmos-sdk/codec"
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	"github.com/cosmos/cosmos-sdk/types/kv"
    10  
    11  	"github.com/gravity-devs/liquidity/x/liquidity/types"
    12  )
    13  
    14  // NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
    15  // Value to the corresponding liquidity type.
    16  func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
    17  	return func(kvA, kvB kv.Pair) string {
    18  		switch {
    19  		case bytes.Equal(kvA.Key[:1], types.PoolKeyPrefix):
    20  			var poolA, poolB types.Pool
    21  			cdc.MustUnmarshal(kvA.Value, &poolA)
    22  			cdc.MustUnmarshal(kvB.Value, &poolB)
    23  			return fmt.Sprintf("%v\n%v", poolA, poolB)
    24  
    25  		case bytes.Equal(kvA.Key[:1], types.PoolByReserveAccIndexKeyPrefix):
    26  			return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value))
    27  
    28  		case bytes.Equal(kvA.Key[:1], types.PoolBatchKeyPrefix):
    29  			var batchA, batchB types.PoolBatch
    30  			cdc.MustUnmarshal(kvA.Value, &batchA)
    31  			cdc.MustUnmarshal(kvB.Value, &batchB)
    32  			return fmt.Sprintf("%v\n%v", batchA, batchB)
    33  
    34  		case bytes.Equal(kvA.Key[:1], types.PoolBatchDepositMsgStateIndexKeyPrefix):
    35  			var msgStateA, msgStateB types.DepositMsgState
    36  			cdc.MustUnmarshal(kvA.Value, &msgStateA)
    37  			cdc.MustUnmarshal(kvB.Value, &msgStateB)
    38  			return fmt.Sprintf("%v\n%v", msgStateA, msgStateB)
    39  
    40  		case bytes.Equal(kvA.Key[:1], types.PoolBatchWithdrawMsgStateIndexKeyPrefix):
    41  			var msgStateA, msgStateB types.WithdrawMsgState
    42  			cdc.MustUnmarshal(kvA.Value, &msgStateA)
    43  			cdc.MustUnmarshal(kvB.Value, &msgStateB)
    44  			return fmt.Sprintf("%v\n%v", msgStateA, msgStateB)
    45  
    46  		case bytes.Equal(kvA.Key[:1], types.PoolBatchSwapMsgStateIndexKeyPrefix):
    47  			var msgStateA, msgStateB types.SwapMsgState
    48  			cdc.MustUnmarshal(kvA.Value, &msgStateA)
    49  			cdc.MustUnmarshal(kvB.Value, &msgStateB)
    50  			return fmt.Sprintf("%v\n%v", msgStateA, msgStateB)
    51  
    52  		default:
    53  			panic(fmt.Sprintf("invalid liquidity key prefix %X", kvA.Key[:1]))
    54  		}
    55  	}
    56  }