Skip to content

Commit

Permalink
add btc refund
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Feb 7, 2024
1 parent e5e1bbd commit a35fe2d
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 101 deletions.
1 change: 1 addition & 0 deletions proto/crosschain/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ message MsgAbortStuckCCTXResponse {}
message MsgRefundAbortedCCTX {
string creator = 1;
string cctx_index = 2;
string receiver_btc_refund = 3;
}

message MsgRefundAbortedCCTXResponse {}
14 changes: 11 additions & 3 deletions x/crosschain/keeper/msg_server_refund_aborted_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ func (k msgServer) RefundAbortedCCTX(goCtx context.Context, msg *types.MsgRefund
}

// refund the amount
err := k.RefundAbortedAmountOnZetaChain(ctx, cctx)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnableProcessRefund, err.Error())
if common.IsEVMChain(cctx.InboundTxParams.SenderChainId) {
err := k.RefundAbortedAmountOnZetaChainForEvmChain(ctx, cctx)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnableProcessRefund, err.Error())
}
} else if common.IsBitcoinChain(cctx.InboundTxParams.SenderChainId) {
err := k.RefundAbortedAmountOnZetaChainForBitcoinChain(ctx, cctx, msg.ReceiverBtcRefund)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnableProcessRefund, err.Error())
}
}

cctx.IsRefunded = true
k.SetCrossChainTx(ctx, cctx)
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_Zeta {
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/msg_server_vote_inbound_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
// gas payment for erc20 type might fail because no liquidity pool is defined to swap the zrc20 token into the gas token
// in this gas we should refund the sender on ZetaChain
if cctx.InboundTxParams.CoinType == common.CoinType_ERC20 {
if err := k.RefundAbortedAmountOnZetaChain(ctx, cctx); err != nil {
if err := k.RefundAbortedAmountOnZetaChainForEvmChain(ctx, cctx); err != nil {
// log the error
k.Logger(ctx).Error("failed to refund amount of aborted cctx on ZetaChain",
"error", err,
Expand Down
12 changes: 11 additions & 1 deletion x/crosschain/keeper/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
zetaObserverTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

func (k Keeper) RefundAbortedAmountOnZetaChain(ctx sdk.Context, cctx types.CrossChainTx) error {
func (k Keeper) RefundAbortedAmountOnZetaChainForEvmChain(ctx sdk.Context, cctx types.CrossChainTx) error {
coinType := cctx.InboundTxParams.CoinType
switch coinType {
case common.CoinType_Gas:
Expand All @@ -26,6 +26,16 @@ func (k Keeper) RefundAbortedAmountOnZetaChain(ctx sdk.Context, cctx types.Cross
}
}

func (k Keeper) RefundAbortedAmountOnZetaChainForBitcoinChain(ctx sdk.Context, cctx types.CrossChainTx, evmAddressForBtcRefund string) error {
refundTo := ethcommon.HexToAddress(evmAddressForBtcRefund)
if refundTo == (ethcommon.Address{}) {
return errors.New("invalid address for refund")
}
// Set TxOrigin to the supplied address so that the refund is made to the evm address
cctx.InboundTxParams.TxOrigin = refundTo.String()
return k.RefundAmountOnZetaChainGas(ctx, cctx)
}

// RefundAmountOnZetaChainGas refunds the amount of the cctx on ZetaChain in case of aborted cctx with cointype gas
func (k Keeper) RefundAmountOnZetaChainGas(ctx sdk.Context, cctx types.CrossChainTx) error {
// refund in gas token of a sender chain to the tx origin
Expand Down
Loading

0 comments on commit a35fe2d

Please sign in to comment.