This documentation provides information about the extensions shipped with the SDK.
Note: The PriceStorageExtension and TokensListExtension are required for the SDK to work correctly.
An extension that adds the ability to fetch and update token prices.
import { PriceStorageExtension } from "@safeblock/exchange-sdk/extensions"
new PriceStorageExtension(env.sdk, env.eventBus, {
// Optional. Token prices auto-update interval
updateInterval: 6_000,
// Optional. Minimal timeout between force refetch
// calls in milliseconds
forceRefetchTimeout: 200
})-
onPriceStorageInitialLoadFinished()
Called when the initial token price update is complete -
onPriceStoragePricesUpdated()
Called each time the token prices are updated -
onPriceStorageForceRefetch()
Called each time theforceRefetchmethod is invoked
-
waitInitialFetch(pollingInterval?): Promise<void>
Creates aPromiseobject that resolves only after the initial price loading has finished.
Arguments:pollingInterval?: number— determines how frequently the extension polls its internal state to see if the initial loading has finished
-
forceRefetch(): Promise<void>
Manually refreshes the token prices -
getPrice(tokenOrNetwork, address?): Amount
Retrieves the price of a token.
Arguments:tokenOrNetwork: BasicToken | Network— a token object or a network pointeraddress?: Address— the token address (used only iftokenOrNetworkis a network pointer)
An extension that adds a managed token list to the SDK.
import { TokensListExtension } from "@safeblock/exchange-sdk/extensions"
new TokensListExtension(
env.sdk,
env.eventBus,
// Optional, initial tokens list in one of the following types:
// Record<string, BasicToken[]> | Map<string, BasicToken[]> | [string, BasicToken[]][]
initialTokensList
)-
onTokenAdded(token: BasicToken)
Called when a new token is added -
onTokenRemoved(token: BasicToken)
Called when a token is removed
-
exist(token: BasicToken): boolean
Checks if the specified token is already in the token list -
get(network: Network, address: Address): BasicToken | null
Retrieves a token by its network pointer and address. ReturnsBasicTokenornullif the token is not found -
add(token: BasicToken): this
Adds a token to the list -
remove(token: BasicToken): this
Removes a token from the list -
get tokensList(): BasicToken[]
Returns all tokens that have been added to the list -
list(network: Network): BasicToken[]
Returns all tokens in the list that belong to the specified network -
get networks(): Network[]
Returns a list of network pointers for which tokens exist in the list
An extension for interacting with SafeBlock token APIs.
import { TokensExtension } from "@safeblock/exchange-sdk/extensions"
new TokensExtension(env.sdk, env.config)Does not declare any events
-
reset()
Resets the current balance update task and clears all stored user balances -
balanceOf(of: Address, token: BasicToken): Amount
Returns the stored balance of thetokenfor the specified address -
findTokens(query, options?): Promise<BasicToken[] | null>
Searches for tokens by name, symbol, or address using the SafeBlock API
Arguments:query: string— a part or full name, symbol, or address of the token to searchoptions?: FindTokenOptions— additional search parameters
-
fetchBalances(of: Address): Promise<void>
Initiates an update procedure to retrieve the token balances for a specific address -
as(of: Address)
Returns an object that provides thebalanceOfandfetchBalancesmethods without requiring the address as the first argument. Similar to usingbind.
interface FindTokensOptions {
// A list of networks to search on.
// If not specified, the search will be performed on all available networks
networks?: Network[]
// The maximum number of tokens in the response
maxTokensPerRequest?: number
}