Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swap] fix token switch #835

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/views/Swap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import type {
QuotePayload,
PrimaryMarketsEnabledAssets,
LPRewardsInfo,
SwapResult,
} from '@sora-substrate/liquidity-proxy/build/types';
import type { DexQuoteData } from '@/store/swap/types';

Expand Down Expand Up @@ -423,8 +424,8 @@ export default class Swap extends Mixins(

try {
// TODO: [ARCH] Asset -> Asset | AccountAsset
const results = dexes.map(({ dexId }) => {
return api.swap.getResult(
const results = dexes.reduce<{ [dexId: number]: SwapResult }>((buffer, { dexId }) => {
const swapResult = api.swap.getResult(
this.tokenFrom as Asset,
this.tokenTo as Asset,
value,
Expand All @@ -434,31 +435,33 @@ export default class Swap extends Mixins(
this.dexQuoteData[dexId].payload as QuotePayload,
dexId as DexId
);
});

const bestDexIndex = results.reduce((bestIdx, result, index, res) => {
const currAmount = FPNumber.fromCodecValue(result.amount);
const bestAmount = FPNumber.fromCodecValue(res[bestIdx].amount);
return { ...buffer, [dexId]: swapResult };
}, {});

let bestDexId: number = DexId.XOR;

for (const currentDexId in results) {
const currAmount = FPNumber.fromCodecValue(results[currentDexId].amount);
const bestAmount = FPNumber.fromCodecValue(results[bestDexId].amount);

if (currAmount.isZero()) return bestIdx;
if (currAmount.isZero()) continue;

if (
(FPNumber.isLessThan(currAmount, bestAmount) && this.isExchangeB) ||
(FPNumber.isLessThan(bestAmount, currAmount) && !this.isExchangeB)
) {
return index;
bestDexId = +currentDexId;
}
}

return bestIdx;
}, 0);

const { amount, amountWithoutImpact, fee, rewards } = results[bestDexIndex];
const { amount, amountWithoutImpact, fee, rewards } = results[bestDexId];

setOppositeValue(this.getStringFromCodec(amount, oppositeToken.decimals));
this.setAmountWithoutImpact(amountWithoutImpact as string);
this.setLiquidityProviderFee(fee);
this.setRewards(rewards);
this.selectDexId(dexes[bestDexIndex].dexId);
this.selectDexId(bestDexId);
} catch (error: any) {
console.error(error);
resetOppositeValue();
Expand Down