diff --git a/contrib/localnet/orchestrator/smoketest/main.go b/contrib/localnet/orchestrator/smoketest/main.go index 6ec49e908d..a54381ebc4 100644 --- a/contrib/localnet/orchestrator/smoketest/main.go +++ b/contrib/localnet/orchestrator/smoketest/main.go @@ -7,6 +7,8 @@ import ( "context" "fmt" "github.com/btcsuite/btcutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/spf13/cobra" "github.com/zeta-chain/zetacore/contrib/localnet/orchestrator/smoketest/contracts/zevmswap" "github.com/zeta-chain/zetacore/zetaclient/config" @@ -52,11 +54,15 @@ var ( ) type SmokeTest struct { - zevmClient *ethclient.Client - goerliClient *ethclient.Client - cctxClient types.QueryClient - btcRPCClient *rpcclient.Client - fungibleClient fungibletypes.QueryClient + zevmClient *ethclient.Client + goerliClient *ethclient.Client + btcRPCClient *rpcclient.Client + + cctxClient types.QueryClient + fungibleClient fungibletypes.QueryClient + authClient authtypes.QueryClient + bankClient banktypes.QueryClient + wg sync.WaitGroup ZetaEth *zetaeth.ZetaEth ZetaEthAddr ethcommon.Address @@ -104,6 +110,7 @@ func init() { func NewSmokeTest(goerliClient *ethclient.Client, zevmClient *ethclient.Client, cctxClient types.QueryClient, fungibleClient fungibletypes.QueryClient, + authClient authtypes.QueryClient, bankClient banktypes.QueryClient, goerliAuth *bind.TransactOpts, zevmAuth *bind.TransactOpts, btcRPCClient *rpcclient.Client) *SmokeTest { // query system contract address @@ -140,6 +147,8 @@ func NewSmokeTest(goerliClient *ethclient.Client, zevmClient *ethclient.Client, goerliClient: goerliClient, cctxClient: cctxClient, fungibleClient: fungibleClient, + authClient: authClient, + bankClient: bankClient, wg: sync.WaitGroup{}, goerliAuth: goerliAuth, zevmAuth: zevmAuth, @@ -204,6 +213,8 @@ func LocalSmokeTest(_ *cobra.Command, _ []string) { cctxClient := types.NewQueryClient(grpcConn) fungibleClient := fungibletypes.NewQueryClient(grpcConn) + authClient := authtypes.NewQueryClient(grpcConn) + bankClient := banktypes.NewQueryClient(grpcConn) // Wait for Genesis and keygen to be completed. ~ height 30 time.Sleep(20 * time.Second) @@ -240,7 +251,7 @@ func LocalSmokeTest(_ *cobra.Command, _ []string) { panic(err) } - smokeTest := NewSmokeTest(goerliClient, zevmClient, cctxClient, fungibleClient, goerliAuth, zevmAuth, btcRPCClient) + smokeTest := NewSmokeTest(goerliClient, zevmClient, cctxClient, fungibleClient, authClient, bankClient, goerliAuth, zevmAuth, btcRPCClient) // The following deployment must happen here and in this order, please do not change // ==================== Deploying contracts ==================== diff --git a/contrib/localnet/orchestrator/smoketest/test_message_passing.go b/contrib/localnet/orchestrator/smoketest/test_message_passing.go index 3488e1db25..62335e4ce8 100644 --- a/contrib/localnet/orchestrator/smoketest/test_message_passing.go +++ b/contrib/localnet/orchestrator/smoketest/test_message_passing.go @@ -6,6 +6,7 @@ package main import ( "context" "fmt" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "math/big" "time" @@ -171,6 +172,15 @@ func (sm *SmokeTest) TestMessagePassingRevertSuccess() { if err != nil { panic(err) } + + res2, err := sm.bankClient.SupplyOf(context.Background(), &banktypes.QuerySupplyOfRequest{ + Denom: "azeta", + }) + if err != nil { + panic(err) + } + fmt.Printf("$$$ Before: SUPPLY OF AZETA: %d\n", res2.Amount.Amount) + tx, err = testDApp.SendHelloWorld(auth, sm.TestDAppAddr, big.NewInt(1337), amount, true) if err != nil { panic(err) @@ -198,4 +208,12 @@ func (sm *SmokeTest) TestMessagePassingRevertSuccess() { fmt.Printf(" Message: %x\n", event.Message) } } + res3, err := sm.bankClient.SupplyOf(context.Background(), &banktypes.QuerySupplyOfRequest{ + Denom: "azeta", + }) + if err != nil { + panic(err) + } + fmt.Printf("$$$ After: SUPPLY OF AZETA: %d\n", res3.Amount.Amount.BigInt()) + fmt.Printf("$$$ Diff: SUPPLY OF AZETA: %d\n", res3.Amount.Amount.Sub(res2.Amount.Amount).BigInt()) } diff --git a/contrib/localnet/orchestrator/smoketest/test_stress.go b/contrib/localnet/orchestrator/smoketest/test_stress.go index ffa7708b0f..3a0b648280 100644 --- a/contrib/localnet/orchestrator/smoketest/test_stress.go +++ b/contrib/localnet/orchestrator/smoketest/test_stress.go @@ -6,6 +6,8 @@ package main import ( "context" "fmt" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/ethereum/go-ethereum/accounts/abi/bind" ethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -100,6 +102,8 @@ func StressTest(_ *cobra.Command, _ []string) { cctxClient := types.NewQueryClient(grpcConn) fungibleClient := fungibletypes.NewQueryClient(grpcConn) + bankClient := banktypes.NewQueryClient(grpcConn) + authClient := authtypes.NewQueryClient(grpcConn) // Wait for Genesis and keygen to be completed. ~ height 30 time.Sleep(20 * time.Second) @@ -136,7 +140,7 @@ func StressTest(_ *cobra.Command, _ []string) { panic(err) } - smokeTest := NewSmokeTest(goerliClient, zevmClient, cctxClient, fungibleClient, goerliAuth, zevmAuth, nil) + smokeTest := NewSmokeTest(goerliClient, zevmClient, cctxClient, fungibleClient, authClient, bankClient, goerliAuth, zevmAuth, nil) // If stress test is running on local docker environment if stressTestArgs.local { diff --git a/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx.go b/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx.go index c94ee0807a..8b3a35f38c 100644 --- a/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx.go +++ b/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx.go @@ -96,18 +96,18 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg tmpCtx, commit := ctx.CacheContext() isContractReverted, err := k.HandleEVMDeposit(tmpCtx, &cctx, *msg, observationChain) if err != nil && !isContractReverted { // exceptional case; internal error; should abort CCTX - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, err.Error(), cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, err.Error()) return &types.MsgVoteOnObservedInboundTxResponse{}, nil } else if err != nil && isContractReverted { // contract call reverted; should refund revertMessage := err.Error() chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(cctx.InboundTxParams.SenderChainId) if chain == nil { - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, "invalid sender chain", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, "invalid sender chain") return &types.MsgVoteOnObservedInboundTxResponse{}, nil } medianGasPrice, isFound := k.GetMedianGasPriceInUint(ctx, chain.ChainId) if !isFound { - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, "cannot find gas price", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, "cannot find gas price") return &types.MsgVoteOnObservedInboundTxResponse{}, nil } // create new OutboundTxParams for the revert @@ -120,35 +120,38 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg OutboundTxGasPrice: medianGasPrice.MulUint64(2).String(), }) if err = k.UpdateNonce(ctx, chain.ChainId, &cctx); err != nil { - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, err.Error(), cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, err.Error()) return &types.MsgVoteOnObservedInboundTxResponse{}, nil } // do not commit() here; - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_PendingRevert, revertMessage, cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_PendingRevert, revertMessage) return &types.MsgVoteOnObservedInboundTxResponse{}, nil } else { // successful HandleEVMDeposit; commit() - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_OutboundMined, "Remote omnichain contract call completed", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_OutboundMined, "Remote omnichain contract call completed") return &types.MsgVoteOnObservedInboundTxResponse{}, nil } } else { // Cross Chain SWAP + tmpCtx, commit := ctx.CacheContext() err = func() error { cctx.InboundTxParams.InboundTxFinalizedZetaHeight = uint64(ctx.BlockHeader().Height) - err := k.UpdatePrices(ctx, receiverChain.ChainId, &cctx) + err := k.PayGasInZetaAndUpdateCctx(tmpCtx, receiverChain.ChainId, &cctx) if err != nil { return err } - err = k.UpdateNonce(ctx, receiverChain.ChainId, &cctx) + err = k.UpdateNonce(tmpCtx, receiverChain.ChainId, &cctx) if err != nil { return err } return nil }() if err != nil { - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, err.Error(), cctx.LogIdentifierForCCTX()) + // do not commit anything here as the CCTX should be aborted + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, err.Error()) return &types.MsgVoteOnObservedInboundTxResponse{}, nil } - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_PendingOutbound, "", cctx.LogIdentifierForCCTX()) + commit() + cctx.CctxStatus.ChangeStatus(types.CctxStatus_PendingOutbound, "") return &types.MsgVoteOnObservedInboundTxResponse{}, nil } } diff --git a/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx_test.go b/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx_test.go index 0ecf339650..7801d2a30b 100644 --- a/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx_test.go +++ b/x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx_test.go @@ -55,7 +55,7 @@ import ( // // NativeTokenSymbol: "", // // MedianIndex: 0, // //}) -// err := keeper.UpdatePrices(ctx, cctx[0].OutboundTxParams.ReceiverChain, &cctx[0]) +// err := keeper.PayGasInZetaAndUpdateCctx(ctx, cctx[0].OutboundTxParams.ReceiverChain, &cctx[0]) // assert.NoError(t, err) // fmt.Println(cctx[0].String()) //} @@ -94,11 +94,11 @@ func TestStatus_StatusTransition(t *testing.T) { IsErr: false, }, } - _, ctx := setupKeeper(t) + _, _ = setupKeeper(t) for _, test := range tt { test := test t.Run(test.Name, func(t *testing.T) { - test.Status.ChangeStatus(&ctx, test.NonErrStatus, test.Msg, "") + test.Status.ChangeStatus(test.NonErrStatus, test.Msg) if test.IsErr { assert.Equal(t, test.ErrStatus, test.Status.Status) } else { diff --git a/x/crosschain/keeper/keeper_cross_chain_tx_vote_outbound_tx.go b/x/crosschain/keeper/keeper_cross_chain_tx_vote_outbound_tx.go index b6450ca7c9..be469262f9 100644 --- a/x/crosschain/keeper/keeper_cross_chain_tx_vote_outbound_tx.go +++ b/x/crosschain/keeper/keeper_cross_chain_tx_vote_outbound_tx.go @@ -4,11 +4,9 @@ import ( "context" "fmt" - "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/rs/zerolog/log" - "github.com/zeta-chain/zetacore/common" "github.com/zeta-chain/zetacore/x/crosschain/types" observerKeeper "github.com/zeta-chain/zetacore/x/observer/keeper" observerTypes "github.com/zeta-chain/zetacore/x/observer/types" @@ -105,32 +103,20 @@ func (k msgServer) VoteOnObservedOutboundTx(goCtx context.Context, msg *types.Ms // FinalizeOutbound sets final status for a successful vote // FinalizeOutbound updates CCTX Prices and Nonce for a revert - err = func() error { //err = FinalizeOutbound(k, ctx, &cctx, msg, ballot.BallotStatus) // remove this line + tmpCtx, commit := ctx.CacheContext() + err = func() error { //err = FinalizeOutbound(k, ctx, &cctx, msg, ballot.BallotStatus) cctx.GetCurrentOutTxParam().OutboundTxObservedExternalHeight = msg.ObservedOutTxBlockHeight - zetaBurnt := cctx.InboundTxParams.Amount - zetaMinted := cctx.GetCurrentOutTxParam().Amount oldStatus := cctx.CctxStatus.Status switch ballot.BallotStatus { case observerTypes.BallotStatus_BallotFinalized_SuccessObservation: switch oldStatus { case types.CctxStatus_PendingRevert: - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Reverted, "", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Reverted, "") case types.CctxStatus_PendingOutbound: - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_OutboundMined, "", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_OutboundMined, "") } - newStatus := cctx.CctxStatus.Status.String() - if zetaBurnt.LT(zetaMinted) { - // TODO :Handle Error ? - } - balanceAmount := zetaBurnt.Sub(zetaMinted) - if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_Zeta { // TODO : Handle Fee for other coins - err := HandleFeeBalances(k, ctx, balanceAmount) - if err != nil { - return err - } - } - EmitOutboundSuccess(ctx, msg, oldStatus.String(), newStatus, cctx) + EmitOutboundSuccess(tmpCtx, msg, oldStatus.String(), newStatus, cctx) case observerTypes.BallotStatus_BallotFinalized_FailureObservation: switch oldStatus { case types.CctxStatus_PendingOutbound: @@ -142,17 +128,17 @@ func (k msgServer) VoteOnObservedOutboundTx(goCtx context.Context, msg *types.Ms CoinType: cctx.InboundTxParams.CoinType, OutboundTxGasLimit: cctx.OutboundTxParams[0].OutboundTxGasLimit, // NOTE(pwu): revert gas limit = initial outbound gas limit set by user; }) - err := k.UpdatePrices(ctx, cctx.InboundTxParams.SenderChainId, &cctx) + err := k.PayGasInZetaAndUpdateCctx(tmpCtx, cctx.InboundTxParams.SenderChainId, &cctx) if err != nil { return err } - err = k.UpdateNonce(ctx, cctx.InboundTxParams.SenderChainId, &cctx) + err = k.UpdateNonce(tmpCtx, cctx.InboundTxParams.SenderChainId, &cctx) if err != nil { return err } - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_PendingRevert, "Outbound failed, start revert", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_PendingRevert, "Outbound failed, start revert") case types.CctxStatus_PendingRevert: - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, "Outbound failed: revert failed; abort TX", cctx.LogIdentifierForCCTX()) + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, "Outbound failed: revert failed; abort TX") } newStatus := cctx.CctxStatus.Status.String() EmitOutboundFailure(ctx, msg, oldStatus.String(), newStatus, cctx) @@ -160,7 +146,8 @@ func (k msgServer) VoteOnObservedOutboundTx(goCtx context.Context, msg *types.Ms return nil }() if err != nil { - cctx.CctxStatus.ChangeStatus(&ctx, types.CctxStatus_Aborted, err.Error(), cctx.LogIdentifierForCCTX()) + // do not commit tmpCtx + cctx.CctxStatus.ChangeStatus(types.CctxStatus_Aborted, err.Error()) ctx.Logger().Error(err.Error()) k.RemoveOutTxTracker(ctx, msg.OutTxChain, msg.OutTxTssNonce) k.RemoveFromPendingNonces(ctx, tss.TssPubkey, msg.OutTxChain, int64(msg.OutTxTssNonce)) @@ -168,6 +155,7 @@ func (k msgServer) VoteOnObservedOutboundTx(goCtx context.Context, msg *types.Ms k.SetCctxAndNonceToCctxAndInTxHashToCctx(ctx, cctx) return &types.MsgVoteOnObservedOutboundTxResponse{}, nil } + commit() // Set the ballot index to the finalized ballot cctx.GetCurrentOutTxParam().OutboundTxBallotIndex = ballotIndex k.RemoveFromPendingNonces(ctx, tss.TssPubkey, msg.OutTxChain, int64(msg.OutTxTssNonce)) @@ -175,12 +163,3 @@ func (k msgServer) VoteOnObservedOutboundTx(goCtx context.Context, msg *types.Ms k.SetCctxAndNonceToCctxAndInTxHashToCctx(ctx, cctx) return &types.MsgVoteOnObservedOutboundTxResponse{}, nil } - -func HandleFeeBalances(k msgServer, ctx sdk.Context, balanceAmount math.Uint) error { - err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(common.ZETADenom, sdk.NewIntFromBigInt(balanceAmount.BigInt())))) - if err != nil { - log.Error().Msgf("ReceiveConfirmation: failed to mint coins: %s", err.Error()) - return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("failed to mint coins: %s", err.Error())) - } - return nil -} diff --git a/x/crosschain/keeper/keeper_utils.go b/x/crosschain/keeper/keeper_utils.go index 9d43a15f81..12ed8987e8 100644 --- a/x/crosschain/keeper/keeper_utils.go +++ b/x/crosschain/keeper/keeper_utils.go @@ -21,7 +21,12 @@ func (k Keeper) IsAuthorizedNodeAccount(ctx sdk.Context, address string) bool { return false } -func (k Keeper) UpdatePrices(ctx sdk.Context, chainID int64, cctx *types.CrossChainTx) error { +// PayGasInZetaAndUpdateCctx updates parameter cctx with the gas price and gas fee for the outbound tx; +// it also makes a trade to fulfill the outbound tx gas fee in ZETA by swapping ZETA for some gas ZRC20 balances +// The gas ZRC20 balance is subsequently burned to account for the expense of TSS address gas fee payment in the outbound tx. +// **Caller should feed temporary ctx into this function** +func (k Keeper) PayGasInZetaAndUpdateCctx(ctx sdk.Context, chainID int64, cctx *types.CrossChainTx) error { + chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(chainID) if chain == nil { return zetaObserverTypes.ErrSupportedChains @@ -38,31 +43,14 @@ func (k Keeper) UpdatePrices(ctx sdk.Context, chainID int64, cctx *types.CrossCh // the following logic computes outbound tx gas fee, and convert into ZETA using system uniswapv2 pool wzeta/gasZRC20 gasZRC20, err := k.fungibleKeeper.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chain.ChainId)) if err != nil { - return sdkerrors.Wrap(err, "UpdatePrices: unable to get system contract gas coin") + return sdkerrors.Wrap(err, "PayGasInZetaAndUpdateCctx: unable to get system contract gas coin") } outTxGasFeeInZeta, err := k.fungibleKeeper.QueryUniswapv2RouterGetAmountsIn(ctx, outTxGasFee.BigInt(), gasZRC20) if err != nil { - return sdkerrors.Wrap(err, "UpdatePrices: unable to QueryUniswapv2RouterGetAmountsIn") + return sdkerrors.Wrap(err, "PayGasInZetaAndUpdateCctx: unable to QueryUniswapv2RouterGetAmountsIn") } feeInZeta := types.GetProtocolFee().Add(math.NewUintFromBigInt(outTxGasFeeInZeta)) - // swap the outTxGasFeeInZeta portion of zeta to the real gas ZRC20 and burn it - coins := sdk.NewCoins(sdk.NewCoin(config.BaseDenom, sdk.NewIntFromBigInt(feeInZeta.BigInt()))) - err = k.bankKeeper.MintCoins(ctx, types.ModuleName, coins) - if err != nil { - return sdkerrors.Wrap(err, "UpdatePrices: unable to mint coins") - } - amounts, err := k.fungibleKeeper.CallUniswapv2RouterSwapExactETHForToken(ctx, types.ModuleAddressEVM, types.ModuleAddressEVM, outTxGasFeeInZeta, gasZRC20) - if err != nil { - return sdkerrors.Wrap(err, "UpdatePrices: unable to CallUniswapv2RouterSwapExactETHForToken") - } - ctx.Logger().Info("gas fee", "outTxGasFee", outTxGasFee, "outTxGasFeeInZeta", outTxGasFeeInZeta) - ctx.Logger().Info("CallUniswapv2RouterSwapExactETHForToken", "zetaAmountIn", amounts[0], "zrc20AmountOut", amounts[1]) - err = k.fungibleKeeper.CallZRC20Burn(ctx, types.ModuleAddressEVM, gasZRC20, amounts[1]) - if err != nil { - return sdkerrors.Wrap(err, "UpdatePrices: unable to CallZRC20Burn") - } - cctx.ZetaFees = cctx.ZetaFees.Add(feeInZeta) if cctx.ZetaFees.GT(cctx.InboundTxParams.Amount) && cctx.InboundTxParams.CoinType == common.CoinType_Zeta { @@ -70,9 +58,31 @@ func (k Keeper) UpdatePrices(ctx sdk.Context, chainID int64, cctx *types.CrossCh } cctx.GetCurrentOutTxParam().Amount = cctx.InboundTxParams.Amount.Sub(cctx.ZetaFees) + // ** The following logic converts the outTxGasFeeInZeta into gasZRC20 and burns it ** + // swap the outTxGasFeeInZeta portion of zeta to the real gas ZRC20 and burn it, in a temporary context. + { + coins := sdk.NewCoins(sdk.NewCoin(config.BaseDenom, sdk.NewIntFromBigInt(feeInZeta.BigInt()))) + err = k.bankKeeper.MintCoins(ctx, types.ModuleName, coins) + if err != nil { + return sdkerrors.Wrap(err, "PayGasInZetaAndUpdateCctx: unable to mint coins") + } + amounts, err := k.fungibleKeeper.CallUniswapv2RouterSwapExactETHForToken(ctx, types.ModuleAddressEVM, types.ModuleAddressEVM, outTxGasFeeInZeta, gasZRC20) + if err != nil { + return sdkerrors.Wrap(err, "PayGasInZetaAndUpdateCctx: unable to CallUniswapv2RouterSwapExactETHForToken") + } + ctx.Logger().Info("gas fee", "outTxGasFee", outTxGasFee, "outTxGasFeeInZeta", outTxGasFeeInZeta) + ctx.Logger().Info("CallUniswapv2RouterSwapExactETHForToken", "zetaAmountIn", amounts[0], "zrc20AmountOut", amounts[1]) + err = k.fungibleKeeper.CallZRC20Burn(ctx, types.ModuleAddressEVM, gasZRC20, amounts[1]) + if err != nil { + return sdkerrors.Wrap(err, "PayGasInZetaAndUpdateCctx: unable to CallZRC20Burn") + } + } + return nil } +// UpdateNonce sets the CCTX outbound nonce to the next nonce, and updates the nonce of blockchain state. +// It also updates the PendingNonces that is used to track the unfulfilled outbound txs. func (k Keeper) UpdateNonce(ctx sdk.Context, receiveChainID int64, cctx *types.CrossChainTx) error { chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(receiveChainID) if chain == nil { @@ -106,8 +116,3 @@ func (k Keeper) UpdateNonce(ctx sdk.Context, receiveChainID int64, cctx *types.C k.SetPendingNonces(ctx, p) return nil } -func CalculateFee(price, gasLimit, rate sdk.Uint) sdk.Uint { - gasFee := price.Mul(gasLimit).Mul(rate) - gasFee = reducePrecision(gasFee) - return gasFee.Add(types.GetProtocolFee()) -} diff --git a/x/crosschain/keeper/utils.go b/x/crosschain/keeper/utils.go deleted file mode 100644 index d17dac9ba7..0000000000 --- a/x/crosschain/keeper/utils.go +++ /dev/null @@ -1,33 +0,0 @@ -package keeper - -import ( - "bytes" - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -func IsBondedValidator(creator string, validators []stakingtypes.Validator) bool { - creatorAddr, err := sdk.AccAddressFromBech32(creator) - if err != nil { - return false - } - for _, v := range validators { - valAddr, err := sdk.ValAddressFromBech32(v.OperatorAddress) - if err != nil { - continue - } - //TODO: How about Jailed? - if v.IsBonded() && bytes.Compare(creatorAddr.Bytes(), valAddr.Bytes()) == 0 { - return true - } - } - return false -} - -// func increasePrecision(i sdk.Uint) sdk.Uint { -// return i.Mul(sdk.NewUintFromString("1000000000000000000")) -// } -func reducePrecision(i sdk.Uint) sdk.Uint { - return i.Quo(math.NewUintFromString("1000000000000000000")) -} diff --git a/x/crosschain/types/status.go b/x/crosschain/types/status.go index 625bf9fce4..1364e5946c 100644 --- a/x/crosschain/types/status.go +++ b/x/crosschain/types/status.go @@ -2,22 +2,10 @@ package types import ( "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" ) -func AllStatus() []CctxStatus { - return []CctxStatus{ - CctxStatus_PendingInbound, - CctxStatus_PendingOutbound, - CctxStatus_OutboundMined, - CctxStatus_PendingRevert, - CctxStatus_Reverted, - CctxStatus_Aborted, - } -} - // empty msg does not overwrite old status message -func (m *Status) ChangeStatus(ctx *sdk.Context, newStatus CctxStatus, msg, logIdentifier string) { +func (m *Status) ChangeStatus(newStatus CctxStatus, msg string) { if len(msg) > 0 { m.StatusMessage = msg }