mirror of
https://github.com/Brotocol-xyz/bro-sdk.git
synced 2026-04-30 04:15:13 +08:00
refactor: extract getAllStacksTokens.ts file
This commit is contained in:
69
src/stacksUtils/apiHelpers/getAllStacksTokens.ts
Normal file
69
src/stacksUtils/apiHelpers/getAllStacksTokens.ts
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user