Skip to content

Commit

Permalink
process withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Jan 14, 2025
1 parent c992cfd commit 9410dc0
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions x/crosschain/keeper/cctx_orchestrator_validate_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"

"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/coin"
"github.com/zeta-chain/node/x/crosschain/types"
fungiblekeeper "github.com/zeta-chain/node/x/fungible/keeper"
fungibletypes "github.com/zeta-chain/node/x/fungible/types"
observertypes "github.com/zeta-chain/node/x/observer/types"
)

Expand Down Expand Up @@ -352,21 +354,55 @@ func (k Keeper) processFailedOutboundV2(ctx sdk.Context, cctx *types.CrossChainT
// update status
cctx.SetPendingRevert("", "outbound failed")

// use a temporary context to not commit any state change in case of error
tmpCtx, commit := ctx.CacheContext()

// process the revert on ZEVM
if err := k.fungibleKeeper.ProcessV2RevertDeposit(
ctx,
to := ethcommon.HexToAddress(cctx.GetCurrentOutboundParam().Receiver)
evmTxResponse, err := k.fungibleKeeper.ProcessV2RevertDeposit(
tmpCtx,
cctx.InboundParams.Sender,
cctx.GetCurrentOutboundParam().Amount.BigInt(),
chainID,
cctx.InboundParams.CoinType,
cctx.InboundParams.Asset,
ethcommon.HexToAddress(cctx.GetCurrentOutboundParam().Receiver),
to,
cctx.RevertOptions.CallOnRevert,
cctx.RevertOptions.RevertMessage,
); err != nil {
return errors.Wrap(err, "failed ProcessV2RevertDeposit")
)
if fungibletypes.IsContractReverted(evmTxResponse, err) {
// this is a contract revert, we commit the state to save the emitted logs related to revert
commit()
return errors.Wrap(err, "revert transaction reverted")
} else if err != nil {
// this should not happen and we don't commit the state to avoid inconsistent state
return errors.Wrap(err, "revert transaction couldn't be processed")
}

// a withdrawal event in the logs could generate cctxs for outbound transactions.
if !evmTxResponse.Failed() {
logs := evmtypes.LogsToEthereum(evmTxResponse.Logs)
if len(logs) > 0 {
tmpCtx = tmpCtx.WithValue(InCCTXIndexKey, cctx.Index)
txOrigin := cctx.InboundParams.TxOrigin
if txOrigin == "" {
txOrigin = cctx.InboundParams.Sender
}

// process logs to process cctx events initiated during the contract call
err = k.ProcessLogs(tmpCtx, logs, to, txOrigin)
if err != nil {
// this happens if the cctx events are not processed correctly with invalid withdrawals
// in this situation we want the CCTX to be reverted, we don't commit the state so the contract call is not persisted
// the contract call is considered as reverted
return errors.Wrap(types.ErrCannotProcessWithdrawal, err.Error())
}
}
}

// commit state change from the deposit and eventual cctx events
commit()

// tx is reverted
cctx.SetReverted("", "outbound failed")

Expand Down

0 comments on commit 9410dc0

Please sign in to comment.