Skip to content

Commit

Permalink
try fix instantiate
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Jan 9, 2025
1 parent 7bac6f0 commit 63c3c5c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 32 deletions.
12 changes: 7 additions & 5 deletions demo/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"fmt"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -99,6 +98,7 @@ func TestInstantiateBabylonContracts(t *testing.T) {
ctx = ctx.WithBlockHeader(cmtproto.Header{Time: time.Now()})
babylonKeeper := consumerApp.BabylonKeeper
babylonMsgServer := babylonkeeper.NewMsgServer(babylonKeeper)
babylonQueryServer := babylonkeeper.NewQuerier(babylonKeeper)
wasmKeeper := consumerApp.WasmKeeper
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&wasmKeeper)

Expand All @@ -124,9 +124,9 @@ func TestInstantiateBabylonContracts(t *testing.T) {
require.NoError(t, err)

msg, err := cli.ParseInstantiateArgs([]string{
strconv.FormatUint(babylonContractCodeID, 10),
strconv.FormatUint(btcStakingContractCodeID, 10),
strconv.FormatUint(btcFinalityContractCodeID, 10),
fmt.Sprintf("%d", babylonContractCodeID),
fmt.Sprintf("%d", btcStakingContractCodeID),
fmt.Sprintf("%d", btcFinalityContractCodeID),
"regtest",
"01020304",
"1",
Expand All @@ -143,7 +143,9 @@ func TestInstantiateBabylonContracts(t *testing.T) {
require.NoError(t, err)

// get params and verify contract addresses are set correctly
params := babylonKeeper.GetParams(ctx)
paramsResp, err := babylonQueryServer.Params(ctx, &types.QueryParamsRequest{})
require.NoError(t, err)
params := paramsResp.Params
require.NotEmpty(t, params.BabylonContractAddress)
require.NotEmpty(t, params.BtcStakingContractAddress)
require.NotEmpty(t, params.BtcFinalityContractAddress)
Expand Down
71 changes: 56 additions & 15 deletions tests/e2e/types/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package types

import (
"fmt"
"strconv"
"testing"

"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/babylonlabs-io/babylon-sdk/demo/app"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/client/cli"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
bbntypes "github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -99,37 +98,79 @@ func (p *TestConsumerClient) GetSender() sdk.AccAddress {
return p.Chain.SenderAccount.GetAddress()
}

// TODO(babylon): deploy Babylon contracts
// deploy Babylon contracts
// TODO: use InstantiateBabylonContracts instead
func (p *TestConsumerClient) BootstrapContracts() (*ConsumerContract, error) {
babylonContractWasmId := p.Chain.StoreCodeFile("../testdata/babylon_contract.wasm").CodeID
btcStakingContractWasmId := p.Chain.StoreCodeFile("../testdata/btc_staking.wasm").CodeID
btcFinalityContractWasmId := p.Chain.StoreCodeFile("../testdata/btc_finality.wasm").CodeID

// instantiate Babylon contracts
msgInstantiate, err := cli.ParseInstantiateArgs([]string{
strconv.FormatUint(babylonContractWasmId, 10),
strconv.FormatUint(btcStakingContractWasmId, 10),
strconv.FormatUint(btcFinalityContractWasmId, 10),
initMsg, err := types.NewInitMsg(
"regtest",
"01020304",
"1",
"2",
"false",
1,
2,
false,
"test-consumer",
"test-consumer-description",
p.GetSender().String(),
}, p.GetSender().String())
)
resp, err := p.Chain.SendMsgs(&wasmtypes.MsgInstantiateContract{
Sender: p.GetSender().String(),
CodeID: babylonContractWasmId,
Label: "v0.0.1",
Msg: initMsg,
Funds: []sdk.Coin{},
Admin: p.GetSender().String(),
})
if err != nil {
return nil, err
}
babylonAddr := sdk.MustAccAddressFromBech32(resp.Events[4].Attributes[0].Value)

_, err = p.Chain.SendMsgs(msgInstantiate)
btcStakingInitMsg, err := types.NewBTCStakingInitMsg(
p.GetSender().String(),
)
resp, err = p.Chain.SendMsgs(&wasmtypes.MsgInstantiateContract{
Sender: p.GetSender().String(),
CodeID: btcStakingContractWasmId,
Label: "v0.0.1",
Msg: btcStakingInitMsg,
Funds: []sdk.Coin{},
Admin: p.GetSender().String(),
})
if err != nil {
return nil, err
}
btcStakingAddr := sdk.MustAccAddressFromBech32(resp.Events[4].Attributes[0].Value)

params := p.App.BabylonKeeper.GetParams(p.Chain.GetContext())
babylonAddr, btcStakingAddr, btcFinalityAddr, err := params.GetContractAddresses()
btcFinalityInitMsg, err := types.NewBTCFinalityInitMsg(
p.GetSender().String(),
)
if err != nil {
return nil, err
}
resp, err = p.Chain.SendMsgs(&wasmtypes.MsgInstantiateContract{
Sender: p.GetSender().String(),
CodeID: btcFinalityContractWasmId,
Label: "v0.0.1",
Msg: btcFinalityInitMsg,
Funds: []sdk.Coin{},
Admin: p.GetSender().String(),
})
if err != nil {
return nil, err
}
btcFinalityAddr := sdk.MustAccAddressFromBech32(resp.Events[4].Attributes[0].Value)

err = p.App.BabylonKeeper.SetParams(p.Chain.GetContext(), types.Params{
BabylonContractCodeId: babylonContractWasmId,
BtcStakingContractCodeId: btcStakingContractWasmId,
BtcFinalityContractCodeId: btcFinalityContractWasmId,
BabylonContractAddress: babylonAddr.String(),
BtcStakingContractAddress: btcStakingAddr.String(),
BtcFinalityContractAddress: btcFinalityAddr.String(),
})
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions x/babylon/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (ms msgServer) InstantiateBabylonContracts(goCtx context.Context, req *type
params.BabylonContractAddress = babylonContractAddr
params.BtcStakingContractAddress = btcStakingContractAddr
params.BtcFinalityContractAddress = btcFinalityContractAddr

if err := ms.k.SetParams(ctx, params); err != nil {
panic(err)
}
Expand All @@ -75,9 +76,10 @@ func (ms msgServer) InstantiateBabylonContracts(goCtx context.Context, req *type

// UpdateParams updates the params.
func (ms msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.k.authority != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.k.authority, req.Authority)
}
// TODO: remove this once determining gov gated contracts
// if ms.k.authority != req.Authority {
// return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", ms.k.authority, req.Authority)
// }
if err := req.Params.ValidateBasic(); err != nil {
return nil, govtypes.ErrInvalidProposalMsg.Wrapf("invalid parameter: %v", err)
}
Expand Down
8 changes: 3 additions & 5 deletions x/babylon/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
Expand All @@ -12,13 +11,12 @@ import (
var _ types.QueryServer = &querier{}

type querier struct {
cdc codec.Codec
k *Keeper
k *Keeper
}

// NewQuerier constructor
func NewQuerier(cdc codec.Codec, k *Keeper) *querier {
return &querier{cdc: cdc, k: k}
func NewQuerier(k *Keeper) *querier {
return &querier{k: k}
}

// Params implements the gRPC service handler for querying the babylon parameters.
Expand Down
7 changes: 4 additions & 3 deletions x/babylon/keeper/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/contract"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -32,19 +33,19 @@ func (k Keeper) InstantiateBabylonContracts(
// instantiate Babylon contract
babylonContractAddr, _, err := contractKeeper.Instantiate(ctx, babylonContractCodeId, govAddr, govAddr, babylonInitMsg, "Babylon contract", nil)
if err != nil {
return "", "", "", err
return "", "", "", types.ErrInvalid.Wrapf("failed to instantiate Babylon contract: %v", err)
}

// instantiate BTC staking contract
btcStakingContractAddr, _, err := contractKeeper.Instantiate(ctx, btcStakingContractCodeId, govAddr, govAddr, btcStakingInitMsg, "BTC staking contract", nil)
if err != nil {
return "", "", "", err
return "", "", "", types.ErrInvalid.Wrapf("failed to instantiate BTC staking contract: %v", err)
}

// instantiate BTC finality contract
btcFinalityContractAddr, _, err := contractKeeper.Instantiate(ctx, btcFinalityContractCodeId, govAddr, govAddr, btcFinalityInitMsg, "BTC finality contract", nil)
if err != nil {
return "", "", "", err
return "", "", "", types.ErrInvalid.Wrapf("failed to instantiate BTC finality contract: %v", err)
}

return babylonContractAddr.String(), btcStakingContractAddr.String(), btcFinalityContractAddr.String(), nil
Expand Down
2 changes: 1 addition & 1 deletion x/babylon/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (AppModule) Name() string { return types.ModuleName }
// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.k))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.cdc, am.k))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.k))
}

// RegisterInvariants registers the module's invariants.
Expand Down

0 comments on commit 63c3c5c

Please sign in to comment.