Skip to content

Commit

Permalink
Merge pull request #172 from alexgao001/add-crosschain-gov-example
Browse files Browse the repository at this point in the history
docs: add crosschain gov example
  • Loading branch information
alexgao001 authored Sep 6, 2023
2 parents c9ee26a + 1dd8e8d commit fba42a1
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions examples/crosschain_gov.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"context"
"cosmossdk.io/math"
"github.com/bnb-chain/greenfield-go-sdk/client"
"github.com/bnb-chain/greenfield-go-sdk/types"
gnfdSdkTypes "github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"log"
)

func main() {
account, _ := types.NewAccountFromPrivateKey("proposer", privateKey)
cli, _ := client.New(chainId, rpcAddr, client.Option{DefaultAccount: account})
ctx := context.Background()

// The example below is for parameter change, each proposal can have only 1 msg, either change parameter or upgrade contract
proposalID, txHash, err := cli.SubmitProposal(
ctx,
[]sdk.Msg{parameterChange()}, // or upgradeContract() for upgrading contract
math.NewIntWithDecimal(1000, gnfdSdkTypes.DecimalBNB), // deposit, various from different env
"Change BSC contract parameter",
"Change BSC contract parameter",
types.SubmitProposalOptions{TxOption: gnfdSdkTypes.TxOption{}},
)
if err != nil {
log.Fatalf("unable to submit proposal , %v", err)
}
cli.WaitForTx(ctx, txHash)

// Have validators to vote for the proposal
// there should be enough validators to vote for the proposal
validatorPrivKey := "0x..."
validatorAcct, _ := types.NewAccountFromPrivateKey("validator", validatorPrivKey)
cli.SetDefaultAccount(validatorAcct)
voteTxHash, err := cli.VoteProposal(ctx, proposalID, govv1.OptionYes, types.VoteProposalOptions{})
cli.WaitForTx(ctx, voteTxHash)
}

// Suppose we want to modify a parameter of contract 0x40eC91B82D7aCAA065d54B08D751505D479b0E43, fillin CrossChainParamsChange as below
// note: Values if a slice of hex representation of the value you want to modify to(must not include 0x prefix), Targets defines the target contract address(es).
func parameterChange() sdk.Msg {
govAcctAddress := authtypes.NewModuleAddress(govtypes.ModuleName).String()
msgUpdateParams := &govv1.MsgUpdateCrossChainParams{
Authority: govAcctAddress,
Params: govv1.CrossChainParamsChange{
Key: "batchSizeForOracle", // The parameter name.
Values: []string{"0000000000000000000000000000000000000000000000000000000000000034"}, // the value in hex format. The length might vary depend on the exact parameter you want to change.
Targets: []string{"0x40eC91B82D7aCAA065d54B08D751505D479b0E43"}, // the contract's address
},
DestChainId: 97, // Dest BSC chain ID
}
return msgUpdateParams
}

// Suppose the current bucketHub contract is 0x111568F484E4b8759a3aeC6aF11EA17BC18479A8, objectHub 0x2F0cf555a0E1dAE8CDacef66D8244E49Ee72Ad2D, grouphub 0x40eC91B82D7aCAA065d54B08D751505D479b0E43.
// respectively, we want to upgrade to 0x82CDc0BDb92Af93F301332Ed05F4F844c7c74FD6, 0xd00137EABe7CC9434EA70Cde29f9DB5f65a335f7, 0xc11bFABfFE9e1A4A1557f1494cb74Cc86AB69441.
// fill this the MsgUpdateCrossChainParams as below
func upgradeContract() sdk.Msg {
govAcctAddress := authtypes.NewModuleAddress(govtypes.ModuleName).String()
msgUpdateParams := &govv1.MsgUpdateCrossChainParams{
Authority: govAcctAddress,
Params: govv1.CrossChainParamsChange{
Key: "upgrade", // The key specify the purpose of the governance. It must be ""upgrade" for upgrade contract
Values: []string{"0x82CDc0BDb92Af93F301332Ed05F4F844c7c74FD6", "0xd00137EABe7CC9434EA70Cde29f9DB5f65a335f7", "0xc11bFABfFE9e1A4A1557f1494cb74Cc86AB69441"},
Targets: []string{"0x111568F484E4b8759a3aeC6aF11EA17BC18479A8", "0x2F0cf555a0E1dAE8CDacef66D8244E49Ee72Ad2D", "0x40eC91B82D7aCAA065d54B08D751505D479b0E43"},
},
DestChainId: 97, // Dest BSC chain ID
}
return msgUpdateParams
}

0 comments on commit fba42a1

Please sign in to comment.