Skip to content

Commit

Permalink
Added back error when is not enough balance for destination fee
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Jul 15, 2024
1 parent 5ca1a42 commit dc4d80c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
32 changes: 10 additions & 22 deletions packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,18 @@ export class Xtokens implements TransferContractInterface {
return 0n;
}

/**
* Contract can throw an error if user balance is smaller than fee.
* Or if you try to send 0 as amount.
*/
try {
const gas = await this.#publicClient.estimateContractGas({
abi: XTOKENS_ABI,
account: address as Address,
address: this.address as Address,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: this.#config.args as any,
functionName: this.#config.func as 'transfer',
});
const gas = await this.#publicClient.estimateContractGas({
abi: XTOKENS_ABI,
account: address as Address,
address: this.address as Address,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: this.#config.args as any,
functionName: this.#config.func as 'transfer',
});

const gasPrice = await this.getGasPrice();
const gasPrice = await this.getGasPrice();

return gas * gasPrice;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
throw new Error(
"Can't get a fee. Make sure that you have enough balance!",
);
}
return gas * gasPrice;
}

private async getGasPrice() {
Expand Down
18 changes: 16 additions & 2 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export async function getSourceData({
balance,
chain,
contract,
destinationFee,
extrinsic,
feeBalance,
feeConfig: config.fee,
Expand Down Expand Up @@ -122,6 +123,7 @@ export interface GetFeeParams {
feeBalance: AssetAmount;
contract?: ContractConfig;
chain: AnyChain;
destinationFee: AssetAmount;
extrinsic?: ExtrinsicConfig;
feeConfig?: FeeAssetConfig;
polkadot: PolkadotService;
Expand All @@ -133,6 +135,7 @@ export async function getFee({
feeBalance,
chain,
contract: contractConfig,
destinationFee,
extrinsic,
feeConfig,
polkadot,
Expand All @@ -147,9 +150,20 @@ export async function getFee({
chain,
contractConfig,
) as TransferContractInterface;
const fee = await contract.getFee(balance.amount, sourceAddress);
try {
const fee = await contract.getFee(balance.amount, sourceAddress);

return feeBalance.copyWith({ amount: fee });
return feeBalance.copyWith({ amount: fee });
} catch (error) {
/**
* Contract can throw an error if user balance is smaller than fee.
* Or if you try to send 0 as amount.
*/
throw new Error(
`Can't get a fee, make sure you have ${destinationFee.toDecimal()} ${destinationFee.getSymbol()} in your source balance, needed for destination fees`,
{ cause: error },
);
}
}

const fee = await getExtrinsicFee(
Expand Down

0 comments on commit dc4d80c

Please sign in to comment.