Skip to content

Commit

Permalink
chore: adding a fix to addNativeGasMethod with overestimation (#253)
Browse files Browse the repository at this point in the history
* chore: adding a fix to addNativeGasMethod with overestimation

* chore: code cleanup
  • Loading branch information
canhtrinh authored Mar 15, 2023
1 parent 9076dee commit 54e145d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
37 changes: 33 additions & 4 deletions src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from "../types";
import {
AxelarRecoveryApi,
BatchedCommandsAxelarscanResponse,
ExecuteParams,
GMPStatus,
GMPStatusResponse,
Expand Down Expand Up @@ -476,6 +475,19 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
);
return txStatus?.status === GMPStatus.DEST_EXECUTED;
}
/**
* Check if given transaction is already confirmed.
* @param txHash string - transaction hash
* @returns Promise<boolean> - true if transaction is already confirmed
*/
public async isConfirmed(txHash: string): Promise<boolean> {
const txStatus: GMPStatusResponse | undefined = await this.queryTransactionStatus(txHash).catch(
() => undefined
);
return [GMPStatus.SRC_GATEWAY_CONFIRMED, GMPStatus.DEST_GATEWAY_APPROVED].includes(
this.parseGMPStatus(txStatus?.status) as GMPStatus
);
}

/**
* Calculate the gas fee in native token for executing a transaction at the destination chain using the source chain's gas price.
Expand All @@ -498,6 +510,8 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
const provider = options.provider || getDefaultProvider(sourceChain, this.environment);
const receipt = await provider.getTransactionReceipt(txHash);
const paidGasFee = getNativeGasAmountFromTxReceipt(receipt) || "0";
const hasTxBeenConfirmed = (await this.isConfirmed(txHash)) || false;
options.shouldSubtractBaseFee = hasTxBeenConfirmed;

return this.subtractGasFee(sourceChain, destinationChain, gasTokenSymbol, paidGasFee, options);
}
Expand Down Expand Up @@ -578,7 +592,11 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
chain,
destinationChain,
nativeGasTokenSymbol,
{ estimatedGas: options?.estimatedGasUsed, provider: evmWalletDetails.provider }
{
estimatedGas: options?.estimatedGasUsed,
gasMultipler: options?.gasMultipler,
provider: evmWalletDetails.provider,
}
).catch(() => undefined);
}

Expand Down Expand Up @@ -781,10 +799,21 @@ export class AxelarGMPRecoveryAPI extends AxelarRecoveryApi {
sourceChain,
destinationChain,
gasTokenSymbol,
options.estimatedGas
options.estimatedGas,
options.gasMultipler,
undefined,
false
);

const topupGasAmount = ethers.BigNumber.from(totalGasFee);
let topupGasAmount = ethers.BigNumber.from(totalGasFee);
if (options.shouldSubtractBaseFee) {
const response = await this.axelarQueryApi
.getNativeGasBaseFee(sourceChain, destinationChain, gasTokenSymbol as GasToken)
.catch(() => undefined);
if (response && response.baseFee) {
topupGasAmount = topupGasAmount.sub(response.baseFee);
}
}
return topupGasAmount.gt(0) ? topupGasAmount.toString() : "0";
}

Expand Down
3 changes: 2 additions & 1 deletion src/libs/TransactionRecoveryApi/AxelarRecoveryApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum GMPStatus {
INSUFFICIENT_FEE = "insufficient_fee",
UNKNOWN_ERROR = "unknown_error",
CANNOT_FETCH_STATUS = "cannot_fetch_status",
SRC_GATEWAY_CONFIRMED = "confirmed",
}

export enum GasPaidStatus {
Expand Down Expand Up @@ -188,7 +189,7 @@ export class AxelarRecoveryApi {
);
}

private parseGMPStatus(response: any): GMPStatus | string {
public parseGMPStatus(response: any): GMPStatus | string {
const { error, status } = response;

if (status === "error" && error) return GMPStatus.DEST_EXECUTE_ERROR;
Expand Down
3 changes: 3 additions & 0 deletions src/libs/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export interface AddGasOptions {
amount?: string;
refundAddress?: string;
estimatedGasUsed?: number;
gasMultipler?: number;
evmWalletDetails?: EvmWalletDetails;
}

Expand Down Expand Up @@ -177,6 +178,8 @@ export interface TxResult {
export interface QueryGasFeeOptions {
provider?: ethers.providers.JsonRpcProvider;
estimatedGas?: number;
gasMultipler?: number;
shouldSubtractBaseFee?: boolean;
}

export interface QueryTransferOptions {
Expand Down

0 comments on commit 54e145d

Please sign in to comment.