Skip to content

Commit

Permalink
feat: add fetchSwappableCurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang1992 committed Sep 6, 2023
1 parent 095c343 commit 8f44578
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
12 changes: 10 additions & 2 deletions src/alexSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
broadcastSponsoredTx,
isSponsoredSwapEnabled,
} from './utils/sponsoredTx';
import { fetchTokenList, TokenInfo } from './utils/tokenlist';
import {
fetchSwappableCurrency,
fetchTokenList,
TokenInfo,
} from './utils/tokenlist';
export { SponsoredTxError, SponsoredTxErrorCode } from './utils/sponsoredTx';

export class AlexSDK {
Expand Down Expand Up @@ -112,6 +116,10 @@ export class AlexSDK {
}

fetchTokenList(): Promise<TokenInfo[]> {
return fetchTokenList()
return fetchTokenList();
}

fetchSwappableCurrency(): Promise<TokenInfo[]> {
return fetchSwappableCurrency();
}
}
34 changes: 22 additions & 12 deletions src/utils/tokenlist.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { Currency } from '../currency';

export type TokenInfo = {
type: 'fungibleToken',
id: string,
name: string
displayPrecision: number,
icon: string,
availableInSwap: boolean,
contractAddress: string,
decimals: number
}
type: 'fungibleToken';
id: string;
name: string;
displayPrecision: number;
icon: string;
availableInSwap: boolean;
contractAddress: string;
decimals: number;
};

export function fetchTokenList(): Promise<TokenInfo[]> {
return fetch('https://token-list.alexlab.co', {
mode: 'cors'
mode: 'cors',
})
.then(res => res.json())
.then(data => data.tokens)
.then((res) => res.json())
.then((data) => data.tokens);
}

export async function fetchSwappableCurrency(): Promise<TokenInfo[]> {
const tokens = await fetchTokenList();
return tokens.filter(
(x) =>
x.availableInSwap && Object.values(Currency).includes(x.id as Currency)
);
}

0 comments on commit 8f44578

Please sign in to comment.