Skip to content

Commit

Permalink
test: extend token factory query and execute
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Aug 30, 2023
1 parent d0f5c9e commit df5c6d2
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 27 deletions.
146 changes: 140 additions & 6 deletions x/tokenfactory/bindings/custom_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,140 @@ func TestCreateDenomMsg(t *testing.T) {
},
}
resp := bindings.FullDenomResponse{}
queryCustom(t, ctx, app, reflect, query, &resp)
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, fmt.Sprintf("factory/%s/SUN", reflect.String()))
}

func TestSetMetadata(t *testing.T) {
creator := RandomAccountAddress()
app, ctx := SetupCustomApp(t, creator)

lucky := RandomAccountAddress()
reflect := instantiateReflectContract(t, ctx, app, lucky)
require.NotEmpty(t, reflect)

// Fund reflect contract with 100 base denom creation fees
reflectAmount := sdk.NewCoins(sdk.NewCoin(types.DefaultParams().DenomCreationFee[0].Denom, types.DefaultParams().DenomCreationFee[0].Amount.MulRaw(100)))
fundAccount(t, ctx, app, reflect, reflectAmount)
// create denom
msg := bindings.TokenMsg{CreateDenom: &bindings.CreateDenom{
Subdenom: "SUN",
Metadata: &bindings.Metadata{
Description: "SUN is a stablecoin pegged to the value of the sun",
Display: "SUN",
DenomUnits: []bindings.DenomUnit{
{
Denom: "factory/cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr/SUN",
Exponent: 0,
Aliases: []string{"SUN"},
},
{
Denom: "SUN",
Exponent: 2,
Aliases: []string{"SUN"},
},
},
Base: "factory/cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr/SUN",
Name: "SUN",
Symbol: "SUN",
},
}}
err := executeCustom(t, ctx, app, reflect, lucky, msg, sdk.Coin{})
require.NoError(t, err)

// Set Metadata
setMetadataMsg := bindings.TokenMsg{SetMetadata: &bindings.SetMetadata{
Denom: "factory/cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr/SUN",
Metadata: bindings.Metadata{
Description: "SUN is a stablecoin pegged to the value of the sun",
Display: "SUN",
DenomUnits: []bindings.DenomUnit{
{
Denom: "factory/cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr/SUN",
Exponent: 0,
Aliases: []string{"SUN"},
},
{
Denom: "SUN",
Exponent: 2,
Aliases: []string{"SUN"},
},
},
Base: "factory/cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr/SUN",
Name: "SUN",
Symbol: "SUN",
},
}}
err = executeCustom(t, ctx, app, reflect, lucky, setMetadataMsg, sdk.Coin{})
require.NoError(t, err)

// query the denom and see if it matches
query := bindings.TokenQuery{
FullDenom: &bindings.FullDenom{
CreatorAddr: reflect.String(),
Subdenom: "SUN",
},
}
resp := bindings.FullDenomResponse{}
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, fmt.Sprintf("factory/%s/SUN", reflect.String()))
}
func TestChangeAdminMsg(t *testing.T) {
creator := RandomAccountAddress()
app, ctx := SetupCustomApp(t, creator)

lucky := RandomAccountAddress()
reflect := instantiateReflectContract(t, ctx, app, lucky)
require.NotEmpty(t, reflect)

// Fund reflect contract with 100 base denom creation fees
reflectAmount := sdk.NewCoins(sdk.NewCoin(types.DefaultParams().DenomCreationFee[0].Denom, types.DefaultParams().DenomCreationFee[0].Amount.MulRaw(100)))
fundAccount(t, ctx, app, reflect, reflectAmount)

// Create the SUN denom
msg := bindings.TokenMsg{CreateDenom: &bindings.CreateDenom{
Subdenom: "SUN",
}}
err := executeCustom(t, ctx, app, reflect, lucky, msg, sdk.Coin{})
require.NoError(t, err)

// Change admin to creator
msg = bindings.TokenMsg{ChangeAdmin: &bindings.ChangeAdmin{
Denom: fmt.Sprintf("factory/%s/SUN", reflect.String()),
NewAdminAddress: creator.String(),
}}
err = executeCustom(t, ctx, app, reflect, lucky, msg, sdk.Coin{})
require.NoError(t, err)

// Query denomm admin
query := bindings.TokenQuery{
Admin: &bindings.DenomAdmin{
Denom: fmt.Sprintf("factory/%s/SUN", reflect.String()),
},
}
resp := bindings.AdminResponse{}
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)
require.Equal(t, creator.String(), resp.Admin)

// query the denom and see if it matches
query = bindings.TokenQuery{
FullDenom: &bindings.FullDenom{
CreatorAddr: reflect.String(),
Subdenom: "SUN",
},
}
fullDenomRes := bindings.FullDenomResponse{}
err = queryCustom(t, ctx, app, reflect, query, &fullDenomRes)
require.NoError(t, err)

require.Equal(t, fullDenomRes.Denom, fmt.Sprintf("factory/%s/SUN", reflect.String()))
}

func TestCreateDenomWithMetadataMsg(t *testing.T) {
creator := RandomAccountAddress()
app, ctx := SetupCustomApp(t, creator)
Expand Down Expand Up @@ -92,7 +221,8 @@ func TestCreateDenomWithMetadataMsg(t *testing.T) {
},
}
resp := bindings.FullDenomResponse{}
queryCustom(t, ctx, app, reflect, query, &resp)
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, fmt.Sprintf("factory/%s/SUN", reflect.String()))
}
Expand Down Expand Up @@ -145,7 +275,8 @@ func TestMintMsg(t *testing.T) {
},
}
resp := bindings.FullDenomResponse{}
queryCustom(t, ctx, app, reflect, query, &resp)
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, coin.Denom)

Expand All @@ -167,7 +298,8 @@ func TestMintMsg(t *testing.T) {
},
}
resp = bindings.FullDenomResponse{}
queryCustom(t, ctx, app, reflect, query, &resp)
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, coin.Denom)

Expand Down Expand Up @@ -203,7 +335,8 @@ func TestMintMsg(t *testing.T) {
},
}
resp = bindings.FullDenomResponse{}
queryCustom(t, ctx, app, reflect, query, &resp)
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, coin.Denom)

Expand All @@ -220,7 +353,8 @@ func TestMintMsg(t *testing.T) {
},
}
resp = bindings.FullDenomResponse{}
queryCustom(t, ctx, app, reflect, query, &resp)
err = queryCustom(t, ctx, app, reflect, query, &resp)
require.NoError(t, err)

require.Equal(t, resp.Denom, coin.Denom)
}
Expand Down
141 changes: 122 additions & 19 deletions x/tokenfactory/bindings/custom_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,116 @@ import (
bindings "github.com/terra-money/core/v2/x/tokenfactory/bindings/types"
)

func TestQueryFullDenom(t *testing.T) {
actor := RandomAccountAddress()
tokenz, ctx := SetupCustomApp(t, actor)

reflect := instantiateReflectContract(t, ctx, tokenz, actor)
func TestQuery(t *testing.T) {
// Setup the environment and fund the user accounts
user := RandomAccountAddress()
app, ctx := SetupCustomApp(t, user)
reflect := instantiateReflectContract(t, ctx, app, user)
require.NotEmpty(t, reflect)
fundAccount(t, ctx, app, reflect, sdk.Coins{sdk.NewInt64Coin("uluna", 100_000_000_000)})

// query full denom
// Create ustart and ustart2 denoms thoguht the smart contract to
// query and validate the query binding are working as expected
msg := bindings.TokenMsg{CreateDenom: &bindings.CreateDenom{
Subdenom: "ustart",
}}
err := executeCustom(t, ctx, app, reflect, user, msg, sdk.Coin{})
require.NoError(t, err)
msg = bindings.TokenMsg{CreateDenom: &bindings.CreateDenom{
Subdenom: "ustart2",
}}
err = executeCustom(t, ctx, app, reflect, user, msg, sdk.Coin{})
require.NoError(t, err)

// Query params info
query := bindings.TokenQuery{
Params: &bindings.GetParams{},
}
paramsRes := bindings.ParamsResponse{}
err = queryCustom(t, ctx, app, reflect, query, &paramsRes)
require.NoError(t, err)

require.EqualValues(t, bindings.ParamsResponse{
Params: bindings.Params{
DenomCreationFee: []wasmvmtypes.Coin{
{
Denom: "uluna",
Amount: "10000000",
},
},
},
}, paramsRes)

// Query full denom name throught wasm binding
query = bindings.TokenQuery{
FullDenom: &bindings.FullDenom{
CreatorAddr: reflect.String(),
Subdenom: "ustart",
},
}
resp := bindings.FullDenomResponse{}
queryCustom(t, ctx, tokenz, reflect, query, &resp)
fulldenomresp := bindings.FullDenomResponse{}
err = queryCustom(t, ctx, app, reflect, query, &fulldenomresp)
require.NoError(t, err)

require.EqualValues(t,
fmt.Sprintf("factory/%s/ustart", reflect.String()),
fulldenomresp.Denom,
)

// Query metadata thoguht wasm binding
query = bindings.TokenQuery{
Metadata: &bindings.GetMetadata{
Denom: fmt.Sprintf("factory/%s/ustart", reflect.String()),
},
}
metadataRes := bindings.MetadataResponse{}
err = queryCustom(t, ctx, app, reflect, query, &metadataRes)
require.NoError(t, err)

require.EqualValues(t, bindings.MetadataResponse{
Metadata: &bindings.Metadata{
Description: "",
Base: fmt.Sprintf("factory/%s/ustart", reflect.String()),
Display: "",
Name: "",
Symbol: "",
DenomUnits: []bindings.DenomUnit{
{
Denom: fmt.Sprintf("factory/%s/ustart", reflect.String()),
Exponent: 0,
Aliases: nil,
},
},
},
}, metadataRes)

// Query denom admin thoguht wasm binding
query = bindings.TokenQuery{
Admin: &bindings.DenomAdmin{
Denom: fmt.Sprintf("factory/%s/ustart", reflect.String()),
},
}
adminresp := bindings.AdminResponse{}
err = queryCustom(t, ctx, app, reflect, query, &adminresp)
require.NoError(t, err)

require.EqualValues(t, reflect.String(), adminresp.Admin)

// Query all denoms by user thoguht wasm binding
query = bindings.TokenQuery{
DenomsByCreator: &bindings.DenomsByCreator{
Creator: reflect.String(),
},
}
denomsbycreator := bindings.DenomsByCreatorResponse{}
err = queryCustom(t, ctx, app, reflect, query, &denomsbycreator)
require.NoError(t, err)

expected := fmt.Sprintf("factory/%s/ustart", reflect.String())
require.EqualValues(t, expected, resp.Denom)
expected := []string{
fmt.Sprintf("factory/%s/ustart", reflect.String()),
fmt.Sprintf("factory/%s/ustart2", reflect.String()),
}
require.EqualValues(t, expected, denomsbycreator.Denoms)
}

type ReflectQuery struct {
Expand All @@ -47,29 +138,41 @@ type ChainResponse struct {
Data []byte `json:"data"`
}

func queryCustom(t *testing.T, ctx sdk.Context, tokenz *app.TerraApp, contract sdk.AccAddress, request bindings.TokenQuery, response interface{}) {
func queryCustom(t *testing.T, ctx sdk.Context, app *app.TerraApp, contract sdk.AccAddress, request bindings.TokenQuery, response interface{}) error {
t.Helper()
wrapped := bindings.TokenFactoryQuery{
Token: &request,
}
msgBz, err := json.Marshal(wrapped)
require.NoError(t, err)
fmt.Println(string(msgBz))
if err != nil {
return err
}

query := ReflectQuery{
Chain: &ChainRequest{
Request: wasmvmtypes.QueryRequest{Custom: msgBz},
},
}
queryBz, err := json.Marshal(query)
require.NoError(t, err)
fmt.Println(string(queryBz))
if err != nil {
return err
}

resBz, err := app.WasmKeeper.QuerySmart(ctx, contract, queryBz)
if err != nil {
return err
}

resBz, err := tokenz.WasmKeeper.QuerySmart(ctx, contract, queryBz)
require.NoError(t, err)
var resp ChainResponse
err = json.Unmarshal(resBz, &resp)
require.NoError(t, err)
if err != nil {
return err
}

err = json.Unmarshal(resp.Data, response)
require.NoError(t, err)
if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions x/tokenfactory/bindings/query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
var contractQuery bindingstypes.TokenFactoryQuery
if err := json.Unmarshal(request, &contractQuery); err != nil {
return nil, errorsmod.Wrap(err, "osmosis query")
return nil, errorsmod.Wrap(err, "failed query")
}
if contractQuery.Token == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "nil token field")
Expand All @@ -31,7 +31,7 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag

fullDenom, err := GetFullDenom(creator, subdenom)
if err != nil {
return nil, errorsmod.Wrap(err, "osmo full denom query")
return nil, errorsmod.Wrap(err, "full denom query")
}

res := bindingstypes.FullDenomResponse{
Expand Down

0 comments on commit df5c6d2

Please sign in to comment.