Skip to content

Commit

Permalink
Tokenfactory Query Wasmbindings (sei-protocol#1002)
Browse files Browse the repository at this point in the history
* Tokenfactory Query Wasmbindings

* Update errors

* Add tfk wasmbinding tests
  • Loading branch information
Kbhat1 authored Jul 31, 2023
1 parent 31465d7 commit 28ad1eb
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

const TestContract = "TEST"
const TestUser = "sei1jdppe6fnj2q7hjsepty5crxtrryzhuqsjrj95y"

type TestTx struct {
msgs []sdk.Msg
Expand Down
32 changes: 31 additions & 1 deletion wasmbinding/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
oraclebindings "github.com/sei-protocol/sei-chain/x/oracle/client/wasm/bindings"
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
tokenfactorywasm "github.com/sei-protocol/sei-chain/x/tokenfactory/client/wasm"
tokenfactorybindings "github.com/sei-protocol/sei-chain/x/tokenfactory/client/wasm/bindings"
tokenfactorytypes "github.com/sei-protocol/sei-chain/x/tokenfactory/types"
)

Expand Down Expand Up @@ -156,5 +157,34 @@ func (qp QueryPlugin) HandleEpochQuery(ctx sdk.Context, queryData json.RawMessag
}

func (qp QueryPlugin) HandleTokenFactoryQuery(ctx sdk.Context, queryData json.RawMessage) ([]byte, error) {
return nil, tokenfactorytypes.ErrUnknownSeiTokenFactoryQuery
var parsedQuery tokenfactorybindings.SeiTokenFactoryQuery
if err := json.Unmarshal(queryData, &parsedQuery); err != nil {
return nil, tokenfactorytypes.ErrParsingSeiTokenFactoryQuery
}
switch {
case parsedQuery.DenomAuthorityMetadata != nil:
res, err := qp.tokenfactoryHandler.GetDenomAuthorityMetadata(ctx, parsedQuery.DenomAuthorityMetadata)
if err != nil {
return nil, err
}
bz, err := json.Marshal(res)
if err != nil {
return nil, tokenfactorytypes.ErrEncodingDenomAuthorityMetadata
}

return bz, nil
case parsedQuery.DenomsFromCreator != nil:
res, err := qp.tokenfactoryHandler.GetDenomsFromCreator(ctx, parsedQuery.DenomsFromCreator)
if err != nil {
return nil, err
}
bz, err := json.Marshal(res)
if err != nil {
return nil, tokenfactorytypes.ErrEncodingDenomsFromCreator
}

return bz, nil
default:
return nil, tokenfactorytypes.ErrUnknownSeiTokenFactoryQuery
}
}
80 changes: 80 additions & 0 deletions wasmbinding/test/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wasmbinding
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"

Expand All @@ -26,6 +27,8 @@ import (
oracletypes "github.com/sei-protocol/sei-chain/x/oracle/types"
oracleutils "github.com/sei-protocol/sei-chain/x/oracle/utils"
tokenfactorywasm "github.com/sei-protocol/sei-chain/x/tokenfactory/client/wasm"
tokenfactorybinding "github.com/sei-protocol/sei-chain/x/tokenfactory/client/wasm/bindings"
tokenfactorytypes "github.com/sei-protocol/sei-chain/x/tokenfactory/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -321,6 +324,83 @@ func TestWasmGetEpoch(t *testing.T) {
require.Equal(t, int64(40), epoch.CurrentEpochHeight)
}

func TestWasmGetDenomAuthorityMetadata(t *testing.T) {
testWrapper, customQuerier := SetupWasmbindingTest(t)

denom := fmt.Sprintf("factory/%s/test", app.TestUser)
testWrapper.Ctx = testWrapper.Ctx.WithBlockHeight(11).WithBlockTime(time.Unix(3600, 0))
// Create denom
testWrapper.App.TokenFactoryKeeper.CreateDenom(testWrapper.Ctx, app.TestUser, "test")
authorityMetadata := tokenfactorytypes.DenomAuthorityMetadata{
Admin: app.TestUser,
}

// Setup tfk query
req := tokenfactorybinding.SeiTokenFactoryQuery{DenomAuthorityMetadata: &tokenfactorytypes.QueryDenomAuthorityMetadataRequest{Denom: denom}}
queryData, err := json.Marshal(req)
require.NoError(t, err)
query := wasmbinding.SeiQueryWrapper{Route: wasmbinding.TokenFactoryRoute, QueryData: queryData}

rawQuery, err := json.Marshal(query)
require.NoError(t, err)

res, err := customQuerier(testWrapper.Ctx, rawQuery)
require.NoError(t, err)

var parsedRes tokenfactorytypes.QueryDenomAuthorityMetadataResponse
err = json.Unmarshal(res, &parsedRes)
require.NoError(t, err)
require.Equal(t, tokenfactorytypes.QueryDenomAuthorityMetadataResponse{AuthorityMetadata: authorityMetadata}, parsedRes)
}

func TestWasmGetDenomsFromCreator(t *testing.T) {
testWrapper, customQuerier := SetupWasmbindingTest(t)

denom1 := fmt.Sprintf("factory/%s/test1", app.TestUser)
denom2 := fmt.Sprintf("factory/%s/test2", app.TestUser)
testWrapper.Ctx = testWrapper.Ctx.WithBlockHeight(11).WithBlockTime(time.Unix(3600, 0))

// No denoms created initially
req := tokenfactorybinding.SeiTokenFactoryQuery{DenomsFromCreator: &tokenfactorytypes.QueryDenomsFromCreatorRequest{Creator: app.TestUser}}
queryData, err := json.Marshal(req)
require.NoError(t, err)
query := wasmbinding.SeiQueryWrapper{Route: wasmbinding.TokenFactoryRoute, QueryData: queryData}

rawQuery, err := json.Marshal(query)
require.NoError(t, err)

res, err := customQuerier(testWrapper.Ctx, rawQuery)
require.NoError(t, err)

var parsedRes tokenfactorytypes.QueryDenomsFromCreatorResponse
err = json.Unmarshal(res, &parsedRes)
require.NoError(t, err)
require.Equal(t, tokenfactorytypes.QueryDenomsFromCreatorResponse{Denoms: nil}, parsedRes)

// Add first denom
testWrapper.App.TokenFactoryKeeper.CreateDenom(testWrapper.Ctx, app.TestUser, "test1")

res, err = customQuerier(testWrapper.Ctx, rawQuery)
require.NoError(t, err)

var parsedRes2 tokenfactorytypes.QueryDenomsFromCreatorResponse
err = json.Unmarshal(res, &parsedRes2)
require.NoError(t, err)
require.Equal(t, tokenfactorytypes.QueryDenomsFromCreatorResponse{Denoms: []string{denom1}}, parsedRes2)

// Add second denom
testWrapper.App.TokenFactoryKeeper.CreateDenom(testWrapper.Ctx, app.TestUser, "test2")

res, err = customQuerier(testWrapper.Ctx, rawQuery)
require.NoError(t, err)

var parsedRes3 tokenfactorytypes.QueryDenomsFromCreatorResponse
err = json.Unmarshal(res, &parsedRes3)
require.NoError(t, err)
require.Equal(t, tokenfactorytypes.QueryDenomsFromCreatorResponse{Denoms: []string{denom1, denom2}}, parsedRes3)

}

func MockQueryPlugins() wasmkeeper.QueryPlugins {
return wasmkeeper.QueryPlugins{
Bank: func(ctx sdk.Context, request *wasmvmtypes.BankQuery) ([]byte, error) { return []byte{}, nil },
Expand Down
10 changes: 10 additions & 0 deletions x/tokenfactory/client/wasm/bindings/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bindings

import "github.com/sei-protocol/sei-chain/x/tokenfactory/types"

type SeiTokenFactoryQuery struct {
// queries the tokenfactory authority metadata
DenomAuthorityMetadata *types.QueryDenomAuthorityMetadataRequest `json:"denom_authority_metadata,omitempty"`
// queries the tokenfactory denoms from a creator
DenomsFromCreator *types.QueryDenomsFromCreatorRequest `json:"denoms_from_creator,omitempty"`
}
12 changes: 12 additions & 0 deletions x/tokenfactory/client/wasm/query.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package wasm

import (
sdk "github.com/cosmos/cosmos-sdk/types"
tokenfactorykeeper "github.com/sei-protocol/sei-chain/x/tokenfactory/keeper"
"github.com/sei-protocol/sei-chain/x/tokenfactory/types"
)

type TokenFactoryWasmQueryHandler struct {
Expand All @@ -13,3 +15,13 @@ func NewTokenFactoryWasmQueryHandler(keeper *tokenfactorykeeper.Keeper) *TokenFa
tokenfactoryKeeper: *keeper,
}
}

func (handler TokenFactoryWasmQueryHandler) GetDenomAuthorityMetadata(ctx sdk.Context, req *types.QueryDenomAuthorityMetadataRequest) (*types.QueryDenomAuthorityMetadataResponse, error) {
c := sdk.WrapSDKContext(ctx)
return handler.tokenfactoryKeeper.DenomAuthorityMetadata(c, req)
}

func (handler TokenFactoryWasmQueryHandler) GetDenomsFromCreator(ctx sdk.Context, req *types.QueryDenomsFromCreatorRequest) (*types.QueryDenomsFromCreatorResponse, error) {
c := sdk.WrapSDKContext(ctx)
return handler.tokenfactoryKeeper.DenomsFromCreator(c, req)
}
36 changes: 19 additions & 17 deletions x/tokenfactory/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ import (

// x/tokenfactory module sentinel errors
var (
ErrDenomExists = sdkerrors.Register(ModuleName, 2, "attempting to create a denom that already exists (has bank metadata)")
ErrUnauthorized = sdkerrors.Register(ModuleName, 3, "unauthorized account")
ErrInvalidDenom = sdkerrors.Register(ModuleName, 4, "invalid denom")
ErrInvalidCreator = sdkerrors.Register(ModuleName, 5, "invalid creator")
ErrInvalidAuthorityMetadata = sdkerrors.Register(ModuleName, 6, "invalid authority metadata")
ErrInvalidGenesis = sdkerrors.Register(ModuleName, 7, "invalid genesis")
ErrSubdenomTooLong = sdkerrors.Register(ModuleName, 8, fmt.Sprintf("subdenom too long, max length is %d bytes", MaxSubdenomLength))
ErrCreatorTooLong = sdkerrors.Register(ModuleName, 9, fmt.Sprintf("creator too long, max length is %d bytes", MaxCreatorLength))
ErrDenomDoesNotExist = sdkerrors.Register(ModuleName, 10, "denom does not exist")
ErrEncodeTokenFactoryCreateDenom = sdkerrors.Register(ModuleName, 11, "Error while encoding tokenfactory create denom msg in wasmd")
ErrEncodeTokenFactoryMint = sdkerrors.Register(ModuleName, 12, "Error while encoding tokenfactory mint denom msg in wasmd")
ErrEncodeTokenFactoryBurn = sdkerrors.Register(ModuleName, 13, "Error while encoding tokenfactory burn denom msg in wasmd")
ErrEncodeTokenFactoryChangeAdmin = sdkerrors.Register(ModuleName, 14, "Error while encoding tokenfactory change admin msg in wasmd")
ErrParsingSeiTokenFactoryQuery = sdkerrors.Register(ModuleName, 15, "Error parsing SeiTokenFactoryQuery")
ErrAdminAlreadyExists = sdkerrors.Register(ModuleName, 16, "attempting to create a new admin that already exists for the denom")
ErrEncodeTokenFactorySetMetadata = sdkerrors.Register(ModuleName, 17, "Error while encoding tokenfactory set metadata msg in wasmd")
ErrUnknownSeiTokenFactoryQuery = sdkerrors.Register(ModuleName, 23, "Error unknown sei token factory query")
ErrDenomExists = sdkerrors.Register(ModuleName, 2, "attempting to create a denom that already exists (has bank metadata)")
ErrUnauthorized = sdkerrors.Register(ModuleName, 3, "unauthorized account")
ErrInvalidDenom = sdkerrors.Register(ModuleName, 4, "invalid denom")
ErrInvalidCreator = sdkerrors.Register(ModuleName, 5, "invalid creator")
ErrInvalidAuthorityMetadata = sdkerrors.Register(ModuleName, 6, "invalid authority metadata")
ErrInvalidGenesis = sdkerrors.Register(ModuleName, 7, "invalid genesis")
ErrSubdenomTooLong = sdkerrors.Register(ModuleName, 8, fmt.Sprintf("subdenom too long, max length is %d bytes", MaxSubdenomLength))
ErrCreatorTooLong = sdkerrors.Register(ModuleName, 9, fmt.Sprintf("creator too long, max length is %d bytes", MaxCreatorLength))
ErrDenomDoesNotExist = sdkerrors.Register(ModuleName, 10, "denom does not exist")
ErrEncodeTokenFactoryCreateDenom = sdkerrors.Register(ModuleName, 11, "Error while encoding tokenfactory create denom msg in wasmd")
ErrEncodeTokenFactoryMint = sdkerrors.Register(ModuleName, 12, "Error while encoding tokenfactory mint denom msg in wasmd")
ErrEncodeTokenFactoryBurn = sdkerrors.Register(ModuleName, 13, "Error while encoding tokenfactory burn denom msg in wasmd")
ErrEncodeTokenFactoryChangeAdmin = sdkerrors.Register(ModuleName, 14, "Error while encoding tokenfactory change admin msg in wasmd")
ErrParsingSeiTokenFactoryQuery = sdkerrors.Register(ModuleName, 15, "Error parsing SeiTokenFactoryQuery")
ErrAdminAlreadyExists = sdkerrors.Register(ModuleName, 16, "attempting to create a new admin that already exists for the denom")
ErrEncodeTokenFactorySetMetadata = sdkerrors.Register(ModuleName, 17, "Error while encoding tokenfactory set metadata msg in wasmd")
ErrEncodingDenomAuthorityMetadata = sdkerrors.Register(ModuleName, 18, "Error encoding denom authority metadata as JSON")
ErrEncodingDenomsFromCreator = sdkerrors.Register(ModuleName, 19, "Error encoding denoms from creator as JSON")
ErrUnknownSeiTokenFactoryQuery = sdkerrors.Register(ModuleName, 23, "Error unknown sei token factory query")
)

0 comments on commit 28ad1eb

Please sign in to comment.