feat: add fetchSwappableCurrency

This commit is contained in:
Kyle Fang
2023-09-06 10:21:00 +08:00
parent 095c343488
commit 8f4457811a
2 changed files with 32 additions and 14 deletions

View File

@@ -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 {
@@ -112,6 +116,10 @@ export class AlexSDK {
}
fetchTokenList(): Promise<TokenInfo[]> {
return fetchTokenList()
return fetchTokenList();
}
fetchSwappableCurrency(): Promise<TokenInfo[]> {
return fetchSwappableCurrency();
}
}

View File

@@ -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)
);
}