diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index 5fd158dbeb..10423c780d 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -1404,3 +1404,29 @@ def query_grant(self, granter, grantee): home=self.data_dir, ) ) + + def query_batches(self): + "query all gravity batches" + return json.loads( + self.raw( + "query", + "gravity", + "batch-txs", + home=self.data_dir, + ) + ) + + def turn_bridge(self, enable, **kwargs): + kwargs.setdefault("gas_prices", DEFAULT_GAS_PRICE) + kwargs.setdefault("gas", DEFAULT_GAS) + return json.loads( + self.raw( + "tx", + "cronos", + "turn-bridge", + enable, + "-y", + home=self.data_dir, + **kwargs, + ) + ) diff --git a/integration_tests/test_gravity.py b/integration_tests/test_gravity.py index 998a97afce..fca8adf0a5 100644 --- a/integration_tests/test_gravity.py +++ b/integration_tests/test_gravity.py @@ -795,3 +795,74 @@ def check(): gravity.contract.functions.redeemVoucher( old_nonce, ADDRS["signer2"] ).build_transaction({"from": ADDRS["signer1"]}) + + +def test_gravity_turn_bridge(gravity): + geth = gravity.geth + cli = gravity.cronos.cosmos_cli() + cronos_w3 = gravity.cronos.w3 + + # deploy test erc20 contract + erc20 = deploy_contract( + geth, + CONTRACTS["TestERC20A"], + ) + + balance = erc20.caller.balanceOf(ADDRS["validator"]) + assert balance == 100000000000000000000000000 + amount = 1000 + + print("send to cronos crc20") + recipient = HexBytes(ADDRS["community"]) + txreceipt = send_to_cosmos( + gravity.contract, erc20, geth, recipient, amount, KEYS["validator"] + ) + assert txreceipt.status == 1, "should success" + assert erc20.caller.balanceOf(ADDRS["validator"]) == balance - amount + + denom = f"gravity{erc20.address}" + + def check_gravity_native_tokens(): + "check the balance of gravity native token" + return cli.balance(eth_to_bech32(recipient), denom=denom) == amount + + if gravity.cronos.enable_auto_deployment: + crc21_contract = None + + def local_check_auto_deployment(): + nonlocal crc21_contract + crc21_contract = check_auto_deployment( + cli, denom, cronos_w3, recipient, amount + ) + return crc21_contract + + wait_for_fn("send-to-crc21", local_check_auto_deployment) + else: + wait_for_fn("send-to-gravity-native", check_gravity_native_tokens) + + # turn off bridge + rsp = cli.turn_bridge("false", from_="community") + assert rsp["code"] != 0, "should not have the permission" + + rsp = cli.turn_bridge("false", from_="validator") + assert rsp["code"] == 0, rsp["raw_log"] + wait_for_new_blocks(cli, 1) + + if gravity.cronos.enable_auto_deployment: + # send it back to erc20, should fail + tx = crc21_contract.functions.send_to_evm_chain( + ADDRS["validator"], amount, 1, 0, b"" + ).build_transaction({"from": ADDRS["community"]}) + txreceipt = send_transaction(cronos_w3, tx, KEYS["community"]) + assert txreceipt.status == 0, "should fail" + else: + # send back the gravity native tokens, should fail + rsp = cli.send_to_ethereum( + ADDRS["validator"], f"{amount}{denom}", f"0{denom}", from_="community" + ) + assert rsp["code"] == 3, rsp["raw_log"] + + wait_for_new_blocks(cli, 10) + # check no new batch is created + rsp = cli.query_batches() + assert len(rsp["batches"]) == 0 diff --git a/x/cronos/client/cli/tx.go b/x/cronos/client/cli/tx.go index c047b8c80a..5ffe969698 100644 --- a/x/cronos/client/cli/tx.go +++ b/x/cronos/client/cli/tx.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "strconv" "strings" "github.com/cosmos/cosmos-sdk/client/flags" @@ -251,28 +252,22 @@ func CmdUpdateTokenMapping() *cobra.Command { return cmd } -// FlagEnable TurnBridgeCmd flags -const ( - FlagEnable = "enable" -) - // CmdTurnBridge returns a CLI command handler for enable or disable the bridge func CmdTurnBridge() *cobra.Command { cmd := &cobra.Command{ - Use: "turn-bridge", + Use: "turn-bridge [true/false]", Short: "Turn Bridge", - Args: cobra.ExactArgs(0), + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } - enable, err := cmd.Flags().GetBool(FlagEnable) + enable, err := strconv.ParseBool(args[0]) if err != nil { return err } - msg := types.NewMsgTurnBridge(clientCtx.GetFromAddress().String(), enable) if err := msg.ValidateBasic(); err != nil { return err @@ -282,7 +277,5 @@ func CmdTurnBridge() *cobra.Command { } flags.AddTxFlagsToCmd(cmd) - cmd.Flags().Bool(FlagEnable, true, "") - return cmd }