Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Jan 6, 2025
1 parent 88d4599 commit a9393e7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
4 changes: 3 additions & 1 deletion demo/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,9 @@ func NewConsumerApp(
bbnkeeper.NewDefaultCustomMsgHandler(app.BabylonKeeper),
)
})
wasmOpts = append(wasmOpts, messageHandler,
wasmOpts = append(wasmOpts,
// add support for the custom message handlers
messageHandler,
// add support for the custom queries
wasmkeeper.WithQueryHandlerDecorator(bbnkeeper.NewQueryDecorator(app.BabylonKeeper)),
)
Expand Down
20 changes: 12 additions & 8 deletions x/babylon/contract/query.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package contract

// CustomQuery is a query request from a smart contract to the Babylon module
// TODO: implement
type CustomQuery struct {
Test *TestQuery `json:"test,omitempty"`
Params *ParamsQuery `json:"params,omitempty"`
}

type TestQuery struct {
Placeholder string `json:"placeholder,omitempty"`
}
// ParamsQuery requests the current module parameters
type ParamsQuery struct{}

type TestResponse struct {
// MaxCap is the max cap limit
Placeholder2 string `json:"placeholder2"`
// ParamsResponse contains the current module parameters
type ParamsResponse struct {
BabylonContractCodeId uint64 `json:"babylon_contract_code_id,omitempty"`
BtcStakingContractCodeId uint64 `json:"btc_staking_contract_code_id,omitempty"`
BtcFinalityContractCodeId uint64 `json:"btc_finality_contract_code_id,omitempty"`
BabylonContractAddress string `json:"babylon_contract_address,omitempty"`
BtcStakingContractAddress string `json:"btc_staking_contract_address,omitempty"`
BtcFinalityContractAddress string `json:"btc_finality_contract_address,omitempty"`
MaxGasBeginBlocker uint32 `json:"max_gas_begin_blocker,omitempty"`
}
24 changes: 16 additions & 8 deletions x/babylon/keeper/query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/babylonlabs-io/babylon-sdk/x/babylon/contract"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
)

type (
// abstract query keeper
ViewKeeper interface {
GetTest(ctx sdk.Context, actor sdk.AccAddress) string
GetParams(ctx sdk.Context) types.Params
}
)

Expand Down Expand Up @@ -52,15 +53,22 @@ func ChainedCustomQuerier(k ViewKeeper, next wasmkeeper.WasmVMQueryHandler) wasm
if err := json.Unmarshal(request.Custom, &contractQuery); err != nil {
return nil, errorsmod.Wrap(err, "babylon query")
}
query := contractQuery.Test
if query == nil {
switch {
case contractQuery.Params != nil:
params := k.GetParams(ctx)
res := contract.ParamsResponse{
BabylonContractCodeId: params.BabylonContractCodeId,
BtcStakingContractCodeId: params.BtcStakingContractCodeId,
BtcFinalityContractCodeId: params.BtcFinalityContractCodeId,
BabylonContractAddress: params.BabylonContractAddress,
BtcStakingContractAddress: params.BtcStakingContractAddress,
BtcFinalityContractAddress: params.BtcFinalityContractAddress,
MaxGasBeginBlocker: params.MaxGasBeginBlocker,
}
return json.Marshal(res)
default:
return next.HandleQuery(ctx, caller, request)
}

res := contract.TestResponse{
Placeholder2: "hello world",
}
return json.Marshal(res)
})
}

Expand Down
33 changes: 28 additions & 5 deletions x/babylon/keeper/query_plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package keeper_test

import (
"encoding/json"
"testing"

wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/contract"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/keeper"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
"github.com/cometbft/cometbft/libs/rand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -16,6 +19,18 @@ func TestChainedCustomQuerier(t *testing.T) {
myContractAddr := sdk.AccAddress(rand.Bytes(32))
keepers := NewTestKeepers(t)

defaultParams := contract.ParamsResponse{
BabylonContractCodeId: 0,
BtcStakingContractCodeId: 0,
BtcFinalityContractCodeId: 0,
BabylonContractAddress: "",
BtcStakingContractAddress: "",
BtcFinalityContractAddress: "",
MaxGasBeginBlocker: 500_000,
}
expData, err := json.Marshal(defaultParams)
require.NoError(t, err)

specs := map[string]struct {
src wasmvmtypes.QueryRequest
viewKeeper keeper.ViewKeeper
Expand All @@ -30,13 +45,21 @@ func TestChainedCustomQuerier(t *testing.T) {
viewKeeper: keepers.BabylonKeeper,
expNextCalled: true,
},
"custom non babylon query": {
"unexpected babylon query": {
src: wasmvmtypes.QueryRequest{
Custom: []byte(`{"foo":{}}`),
},
viewKeeper: keepers.BabylonKeeper,
expNextCalled: true,
},
"expected babylon query": {
src: wasmvmtypes.QueryRequest{
Custom: []byte(`{"params":{}}`),
},
viewKeeper: keepers.BabylonKeeper,
expNextCalled: false,
expData: expData,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
Expand All @@ -62,12 +85,12 @@ func TestChainedCustomQuerier(t *testing.T) {
var _ keeper.ViewKeeper = &MockViewKeeper{}

type MockViewKeeper struct {
GetTestFn func(ctx sdk.Context, actor sdk.AccAddress) string
GetParamsFn func(ctx sdk.Context) types.Params
}

func (m MockViewKeeper) GetTest(ctx sdk.Context, actor sdk.AccAddress) string {
if m.GetTestFn == nil {
func (m MockViewKeeper) GetParams(ctx sdk.Context) types.Params {
if m.GetParamsFn == nil {
panic("not expected to be called")
}
return m.GetTestFn(ctx, actor)
return m.GetParamsFn(ctx)
}

0 comments on commit a9393e7

Please sign in to comment.