refactor: extract getAllStacksTokens.ts file

This commit is contained in:
c4605
2025-02-06 10:52:45 +01:00
parent 526028a21e
commit 1ba8888d4c
3 changed files with 72 additions and 66 deletions

View File

@@ -0,0 +1,69 @@
import { requestAPI } from "../../utils/apiHelpers"
import {
KnownChainId,
createStacksToken,
KnownTokenId,
} from "../../utils/types/knownIds"
import { StacksContractAddress } from "../../xlinkSdkUtils/types"
import { SDKGlobalContext } from "../../xlinkSdkUtils/types.internal"
export interface StacksTokenInfo {
stacksTokenId: KnownTokenId.StacksToken
contractAddress: StacksContractAddress
decimals: number
underlyingToken?: {
contractAddress: StacksContractAddress
decimals: number
}
}
export const getAllStacksTokens = (
ctx: SDKGlobalContext,
chain: KnownChainId.StacksChain,
): Promise<StacksTokenInfo[]> => {
const cache = ctx.stacks.tokensCache
if (cache == null) {
return getAllStacksTokensImpl(ctx, chain)
}
const cached = cache.get(chain)
if (cached == null) {
const promise = getAllStacksTokensImpl(ctx, chain).catch(e => {
if (cache.get(chain) === promise) {
cache.delete(chain)
}
throw e
})
cache.set(chain, promise)
}
return cache.get(chain)!
}
const getAllStacksTokensImpl = async (
ctx: SDKGlobalContext,
chain: KnownChainId.StacksChain,
): Promise<StacksTokenInfo[]> => {
const res = await requestAPI<{ tokens: StacksTokenFromAPI[] }>(ctx, {
method: "GET",
path: "/2024-10-01/stacks/tokens",
query: {
network: chain === KnownChainId.Stacks.Mainnet ? "mainnet" : "testnet",
},
})
return res.tokens.map(info => ({
stacksTokenId: createStacksToken(info.id),
contractAddress: info.contractAddress,
decimals: info.decimals,
underlyingToken: info.underlyingToken,
}))
}
interface StacksTokenFromAPI {
id: string
contractAddress: StacksContractAddress
decimals: number
underlyingToken?: {
contractAddress: StacksContractAddress
decimals: number
}
}

View File

@@ -7,13 +7,8 @@ import {
} from "clarity-codegen"
import { xlinkContracts } from "../../generated/smartContract/contracts_xlink"
import { STACKS_MAINNET, STACKS_TESTNET } from "../config"
import { requestAPI } from "../utils/apiHelpers"
import { BigNumber, BigNumberSource } from "../utils/BigNumber"
import {
createStacksToken,
KnownChainId,
KnownTokenId,
} from "../utils/types/knownIds"
import { KnownChainId, KnownTokenId } from "../utils/types/knownIds"
import {
isStacksContractAddressEqual,
StacksContractAddress,
@@ -24,6 +19,7 @@ import {
stxContractAddresses,
stxTokenContractAddresses_legacy,
} from "./stxContractAddresses"
import { getAllStacksTokens } from "./apiHelpers/getAllStacksTokens"
const CONTRACT_COMMON_NUMBER_SCALE = 8
export const numberFromStacksContractNumber = (
@@ -160,62 +156,3 @@ export async function getStacksToken(
return
}
const getAllStacksTokens = (
ctx: SDKGlobalContext,
chain: KnownChainId.StacksChain,
): Promise<StacksTokenInfo[]> => {
const cache = ctx.stacks.tokensCache
if (cache == null) {
return getAllStacksTokensImpl(ctx, chain)
}
const cached = cache.get(chain)
if (cached == null) {
const promise = getAllStacksTokensImpl(ctx, chain).catch(e => {
if (cache.get(chain) === promise) {
cache.delete(chain)
}
throw e
})
cache.set(chain, promise)
}
return cache.get(chain)!
}
const getAllStacksTokensImpl = async (
ctx: SDKGlobalContext,
chain: KnownChainId.StacksChain,
): Promise<StacksTokenInfo[]> => {
const res = await requestAPI<{ tokens: StacksTokenFromAPI[] }>(ctx, {
method: "GET",
path: "/2024-10-01/stacks/tokens",
query: {
network: chain === KnownChainId.Stacks.Mainnet ? "mainnet" : "testnet",
},
})
return res.tokens.map(info => ({
stacksTokenId: createStacksToken(info.id),
contractAddress: info.contractAddress,
decimals: info.decimals,
underlyingToken: info.underlyingToken,
}))
}
export interface StacksTokenInfo {
stacksTokenId: KnownTokenId.StacksToken
contractAddress: StacksContractAddress
decimals: number
underlyingToken?: {
contractAddress: StacksContractAddress
decimals: number
}
}
interface StacksTokenFromAPI {
id: string
contractAddress: StacksContractAddress
decimals: number
underlyingToken?: {
contractAddress: StacksContractAddress
decimals: number
}
}

View File

@@ -2,7 +2,7 @@ import { Client } from "viem"
import { EVMOnChainAddresses } from "../evmUtils/evmContractAddresses"
import type { RunesSupportedRoute } from "../metaUtils/apiHelpers/getRunesSupportedRoutes"
import type { BRC20SupportedRoute } from "../metaUtils/apiHelpers/getBRC20SupportedRoutes"
import { StacksTokenInfo } from "../stacksUtils/xlinkContractHelpers"
import { StacksTokenInfo } from "../stacksUtils/apiHelpers/getAllStacksTokens"
import { KnownChainId } from "../utils/types/knownIds"
import { EVMAddress } from "./types"
import { EVMSupportedRoute } from "../evmUtils/apiHelpers/getEVMSupportedRoutes"