Skip to content

Commit

Permalink
get error optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
marxeille committed Jul 5, 2024
1 parent ed6c1bc commit 88d5183
Showing 1 changed file with 80 additions and 69 deletions.
149 changes: 80 additions & 69 deletions packages/hooks/src/tx/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ export class FeeConfig extends TxChainSetter implements IFeeConfig {

getError(): Error | undefined {
try {
if (this.gasConfig.getError()) {
return this.gasConfig.getError();
const gasError = this.checkGasError();
if (gasError) {
return gasError;
}

if (this.disableBalanceCheck) {
Expand All @@ -289,82 +290,92 @@ export class FeeConfig extends TxChainSetter implements IFeeConfig {
}

const amount = this.amountConfig.getAmountPrimitive();

let need: Coin;
if (this.additionAmountToNeedFee && fee && fee.denom === amount.denom) {
need = new Coin(
fee.denom,
new Int(fee.amount).add(new Int(amount.amount))
);
} else {
need = new Coin(fee.denom, new Int(fee.amount));
}
const need = this.calculateNeed(fee, amount);

if (need.amount.gt(new Int(0))) {
// if (this.chainInfo.networkType === 'evm') {
// const balance = this.queryEvmBalances.getQueryBalance(this._senderEvm).balance;
// if (!balance) return new InsufficientFeeError('insufficient fee');
// else if (
// balance
// .toDec()
// .mul(DecUtils.getTenExponentNInPrecisionRange(balance.currency.coinDecimals))
// .truncate()
// .lt(need.amount)
// )
// return new InsufficientFeeError('insufficient fee');
// } else
if (this.chainInfo.networkType === "bitcoin") {
const balance = this.queryBtcBalances.getQueryBalance(
this._sender
)?.balance;
if (!balance) return new InsufficientFeeError("insufficient fee");
else if (
balance
.toDec()
.mul(
DecUtils.getTenExponentNInPrecisionRange(
balance.currency.coinDecimals
)
)
.truncate()
.lt(need.amount)
) {
return new InsufficientFeeError("insufficient fee");
}
}
const bal = this.queryBalances
.getQueryBech32Address(this._sender)
.balances.find((bal) => {
return bal.currency.coinMinimalDenom === need.denom;
});

if (!bal) {
return new InsufficientFeeError("insufficient fee");
} else if (!bal.response && !bal.error) {
// If fetching balance doesn't have the response nor error,
// assume it is not loaded from KVStore(cache).
return new NotLoadedFeeError(
`${bal.currency.coinDenom} is not loaded yet`
);
} else if (
bal.balance
.toDec()
.mul(
DecUtils.getTenExponentNInPrecisionRange(
bal.currency.coinDecimals
)
)
.truncate()
.lt(need.amount)
) {
return new InsufficientFeeError("insufficient fee");
const insufficientFeeError = this.checkInsufficientFee(need);
if (insufficientFeeError) {
return insufficientFeeError;
}
}
} catch (error) {
console.log("Error on get fees: ", error);
}
}

private checkGasError(): Error | undefined {
if (this.gasConfig.getError()) {
return this.gasConfig.getError();
}
return undefined;
}

private calculateNeed(fee: CoinPrimitive, amount: CoinPrimitive): Coin {
if (this.additionAmountToNeedFee && fee.denom === amount.denom) {
return new Coin(
fee.denom,
new Int(fee.amount).add(new Int(amount.amount))
);
} else {
return new Coin(fee.denom, new Int(fee.amount));
}
}

private checkInsufficientFee(need: Coin): Error | undefined {
if (this.chainInfo.networkType === "bitcoin") {
return this.checkBitcoinInsufficientFee(need);
} else {
return this.checkOtherInsufficientFee(need);
}
}

private checkBitcoinInsufficientFee(need: Coin): Error | undefined {
const balance = this.queryBtcBalances.getQueryBalance(
this._sender
)?.balance;
if (!balance) {
return new InsufficientFeeError("insufficient fee");
} else if (
balance
.toDec()
.mul(
DecUtils.getTenExponentNInPrecisionRange(
balance.currency.coinDecimals
)
)
.truncate()
.lt(need.amount)
) {
return new InsufficientFeeError("insufficient fee");
}
return undefined;
}

private checkOtherInsufficientFee(need: Coin): Error | undefined {
const bal = this.queryBalances
.getQueryBech32Address(this._sender)
.balances.find((bal) => bal.currency.coinMinimalDenom === need.denom);

if (!bal) {
return new InsufficientFeeError("insufficient fee");
} else if (!bal.response && !bal.error) {
return new NotLoadedFeeError(
`${bal.currency.coinDenom} is not loaded yet`
);
} else if (
bal.balance
.toDec()
.mul(
DecUtils.getTenExponentNInPrecisionRange(bal.currency.coinDecimals)
)
.truncate()
.lt(need.amount)
) {
return new InsufficientFeeError("insufficient fee");
}
return undefined;
}

@action
setDisableBalanceCheck(bool: boolean) {
this._disableBalanceCheck = bool;
Expand Down

0 comments on commit 88d5183

Please sign in to comment.