Skip to content

Commit

Permalink
refactor: rename and add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Jul 25, 2023
1 parent aac8a5f commit 58ca9c0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
tmpCtx, commit := ctx.CacheContext()
err = func() error {
cctx.InboundTxParams.InboundTxFinalizedZetaHeight = uint64(ctx.BlockHeader().Height)
err := k.UpdatePrices(tmpCtx, receiverChain.ChainId, &cctx)
err := k.PayGasInZetaAndUpdateCctx(tmpCtx, receiverChain.ChainId, &cctx)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
//}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ 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(tmpCtx, cctx.InboundTxParams.SenderChainId, &cctx)
err := k.PayGasInZetaAndUpdateCctx(tmpCtx, cctx.InboundTxParams.SenderChainId, &cctx)
if err != nil {
return err
}
Expand Down
19 changes: 11 additions & 8 deletions x/crosschain/keeper/keeper_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func (k Keeper) IsAuthorizedNodeAccount(ctx sdk.Context, address string) bool {
return false
}

// UpdatePrices 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 in the ZETA/gas uniswapv2 pool
// 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) UpdatePrices(ctx sdk.Context, chainID int64, cctx *types.CrossChainTx) error {
func (k Keeper) PayGasInZetaAndUpdateCctx(ctx sdk.Context, chainID int64, cctx *types.CrossChainTx) error {

chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(chainID)
if chain == nil {
Expand All @@ -42,11 +43,11 @@ 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))

Expand All @@ -63,23 +64,25 @@ func (k Keeper) UpdatePrices(ctx sdk.Context, chainID int64, cctx *types.CrossCh
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")
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, "UpdatePrices: unable to CallUniswapv2RouterSwapExactETHForToken")
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, "UpdatePrices: unable to CallZRC20Burn")
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 {
Expand Down

0 comments on commit 58ca9c0

Please sign in to comment.