Skip to content

Commit

Permalink
try catch check balance when channel not found
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed Sep 5, 2023
1 parent 976e902 commit ed52f37
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions src/pages/UniversalSwap/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,31 +290,37 @@ export class UniversalSwapHandler {
async checkBalanceChannelIbc(ibcInfo: IBCInfo, toToken: TokenItemType) {
const ics20Contract =
this.cwIcs20LatestClient ?? new CwIcs20LatestQueryClient(window.client, process.env.REACT_APP_IBC_WASM_CONTRACT);
const { balances } = await ics20Contract.channel({
forward: false,
id: ibcInfo.channel
});

for (let balance of balances) {
if ('native' in balance) {
let pairKey = this.buildIbcWasmPairKey(ibcInfo.source, ibcInfo.channel, toToken.denom);
if (toToken.prefix && toToken.contractAddress) {
pairKey = this.buildIbcWasmPairKey(
ibcInfo.source,
ibcInfo.channel,
`${toToken.prefix}${toToken.contractAddress}`
);
}
if (pairKey !== balance.native.denom) continue;
const pairMapping = await ics20Contract.pairMapping({ key: pairKey });
const trueBalance = toDisplay(balance.native.amount, pairMapping.pair_mapping.remote_decimals);
const _toAmount = toDisplay(this.simulateAmount, toToken.decimals);
if (trueBalance < _toAmount) {
throw generateError(`pair key is not enough balance!`);
try {
const { balances } = await ics20Contract.channel({
forward: false,
id: ibcInfo.channel
});

for (let balance of balances) {
if ('native' in balance) {
let pairKey = this.buildIbcWasmPairKey(ibcInfo.source, ibcInfo.channel, toToken.denom);
if (toToken.prefix && toToken.contractAddress) {
pairKey = this.buildIbcWasmPairKey(
ibcInfo.source,
ibcInfo.channel,
`${toToken.prefix}${toToken.contractAddress}`
);
}
if (pairKey !== balance.native.denom) continue;
const pairMapping = await ics20Contract.pairMapping({ key: pairKey });
const trueBalance = toDisplay(balance.native.amount, pairMapping.pair_mapping.remote_decimals);
const _toAmount = toDisplay(this.simulateAmount, toToken.decimals);
if (trueBalance < _toAmount) {
throw generateError(`pair key is not enough balance!`);
}
} else {
// do nothing because currently we dont have any cw20 balance in the channel
}
} else {
// do nothing because currently we dont have any cw20 balance in the channel
}
} catch (error) {
console.log({ CheckBalanceChannelIbcErrors: error });
return;
}
}

Expand Down

0 comments on commit ed52f37

Please sign in to comment.