Skip to content

Commit f863c11

Browse files
authored
Merge pull request #1731 from balancednetwork/fix/fix-legacy-routing
Fix bnUSD related routing issue
2 parents 1413d3b + 47bf619 commit f863c11

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

apps/web/src/constants/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export const FUNDING_TOKENS_LIST = FUNDING_TOKENS[NETWORK_ID];
204204

205205
export const ORACLE_PRICED_TOKENS = ['USDC', 'USDT'];
206206

207-
export const UNTRADEABLE_TOKENS = ['JitoSOL', 'SOL', 'bnUSD', 'wBTC', 'tBTC', 'cbBTC', 'wETH', 'S', 'USDT', 'USDC'];
207+
export const UNTRADEABLE_TOKENS = ['JitoSOL', 'SOL', 'bnUSD', 'wBTC', 'tBTC', 'cbBTC', 'wETH', 'S', 'USDT'];
208208

209209
//search for Pyth ID here https://hermes.pyth.network/docs/#/rest/price_feeds_metadata
210210
export const PYTH_PRICED_TOKENS: { symbol: string; pythId: string }[] = [

packages/v1-sdk/src/entities/pair.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import {
1919
} from '../constants';
2020
import { InsufficientInputAmountError, InsufficientReservesError } from '../errors';
2121

22+
// Helper function to check if a token is bnUSD (handles both 'bnUSD' and 'bnUSD(old)' symbols)
23+
function isBnUSD(token: Token): boolean {
24+
return token.symbol === 'bnUSD' || token.symbol === 'bnUSD(old)';
25+
}
26+
2227
export const computePairAddress = ({
2328
factoryAddress,
2429
tokenA,
@@ -203,8 +208,8 @@ export class Pair {
203208
invariant(this.involvesToken(inputAmount.currency), 'TOKEN');
204209

205210
if (this.type === PairType.STABILITY_FUND) {
206-
// this.token1 is always bnUSD
207-
if (inputAmount.currency.symbol === 'bnUSD') {
211+
// this.token1 is always bnUSD (or bnUSD(old))
212+
if (isBnUSD(inputAmount.currency)) {
208213
// bnUSD -> USDC
209214
// apply fee 0.1%
210215
const outputAmount = CurrencyAmount.fromRawAmount(
@@ -262,8 +267,8 @@ export class Pair {
262267
public getInputAmount(outputAmount: CurrencyAmount<Token>): [CurrencyAmount<Token>, Pair] {
263268
invariant(this.involvesToken(outputAmount.currency), 'TOKEN');
264269
if (this.type === PairType.STABILITY_FUND) {
265-
// this.token1 is always bnUSD
266-
if (outputAmount.currency.symbol === 'bnUSD') {
270+
// this.token1 is always bnUSD (or bnUSD(old))
271+
if (isBnUSD(outputAmount.currency)) {
267272
// USDC -> bnUSD
268273
return [
269274
CurrencyAmount.fromRawAmount(

packages/v1-sdk/src/entities/route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { Currency, Price, Token } from '@balancednetwork/sdk-core';
44

55
import { Pair, PairType } from './pair';
66

7+
// Helper function to check if a token is bnUSD (handles both 'bnUSD' and 'bnUSD(old)' symbols)
8+
function isBnUSD(token: Token): boolean {
9+
return token.symbol === 'bnUSD' || token.symbol === 'bnUSD(old)';
10+
}
11+
712
export type RouteAction = {
813
type: number;
914
address: string | null;
@@ -52,8 +57,8 @@ export class Route<TInput extends Currency, TOutput extends Currency> {
5257
for (const [i, pair] of this.pairs.entries()) {
5358
if (pair.type === PairType.STABILITY_FUND) {
5459
let price;
55-
// pair.token1 is always bnUSD
56-
if (this.path[i].symbol === 'bnUSD') {
60+
// pair.token1 is always bnUSD (or bnUSD(old))
61+
if (isBnUSD(this.path[i])) {
5762
// bnUSD -> USDC
5863
price = new Price(
5964
pair.token1,

packages/v1-sdk/src/entities/trade.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import { ONE, STABILITY_FUND_FRACTION, ZERO } from '../constants';
1616
import { Pair, PairType } from './pair';
1717
import { Route } from './route';
1818

19+
// Helper function to check if a token is bnUSD (handles both 'bnUSD' and 'bnUSD(old)' symbols)
20+
function isBnUSD(token: Token): boolean {
21+
return token.symbol === 'bnUSD' || token.symbol === 'bnUSD(old)';
22+
}
23+
1924
// minimal interface so the input output comparator may be shared across types
2025
interface InputOutput<TInput extends Currency, TOutput extends Currency> {
2126
readonly inputAmount: CurrencyAmount<TInput>;
@@ -451,11 +456,10 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
451456
let result = new Fraction(ONE);
452457
for (let i = 0; i < this.route.path.length - 1; i++) {
453458
const pair = this.route.pairs[i];
454-
const inputCurrencySymbol = this.route.path[i].symbol;
455459
const outputCurrencySymbol = this.route.path[i + 1].symbol;
456460

457461
if (pair.type === PairType.STABILITY_FUND) {
458-
if (inputCurrencySymbol === 'bnUSD') {
462+
if (isBnUSD(this.route.path[i])) {
459463
result = result.multiply(STABILITY_FUND_FRACTION);
460464
}
461465
} else if (pair.type === PairType.STAKING) {

0 commit comments

Comments
 (0)