@mytonswap/sdk
Version:
MyTonSwap Dex Aggregator SDK
985 lines (970 loc) • 24.9 kB
TypeScript
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { IAxiosRetryConfig } from 'axios-retry';
import { TonClient } from '@ton/ton';
type MyTonSwapClientOptions = {
apiKey?: string;
tonCenterApiKey?: string;
baseUrl?: string;
headers?: Record<string, string>;
};
type MyTonSwapResponse<Tdata = any, Terror = any> = {
statusCode: number;
error: boolean;
errorData: Terror;
data: Tdata;
};
declare class Request {
private client;
private axiosInstance;
attemptOptions: Partial<IAxiosRetryConfig>;
constructor(client: MyTonSwapClient);
/**
* send
*/
send<T = any>(userOptions: AxiosRequestConfig): Promise<T>;
private handleErrors;
/**
* faultTolerantRequest
*/
faultTolerantRequest<T>(options: AxiosRequestConfig): Promise<AxiosResponse<T> | undefined>;
/**
* getDefaultHeaders
*/
getDefaultHeaders(): {
stats_id: string;
};
}
declare abstract class Services {
protected client: MyTonSwapClient;
constructor(client: MyTonSwapClient);
}
interface PaginatedAssets {
assets: Asset[];
meta: Meta;
}
interface Asset {
id: number;
createdAt: string;
updatedAt: string;
address: string;
symbol: string;
name: string;
image: string;
decimal: number;
disabled: boolean;
warning: boolean;
verify_tag: boolean;
liquidity: number;
liquidity_text: string;
}
interface Meta {
perPage: number;
currentPage: number;
nextPage: string;
totalPage: number;
previousPage: number;
isLastPage: boolean;
}
declare class Assets extends Services {
/**
* Retrieves an exact asset from the server.
*
* @param {string} asset - The token address you want.
* @returns {Promise<Asset | null>} A promise that resolves to the token if found, or null if not found.
*/
getExactAsset(token_address: string): Promise<Asset | null>;
/**
* Retrieves a list of assets from the server based on the provided asset addresses.
*
* @param {string[]} assetsAddress - An array of asset addresses to fetch.
* @returns {Promise<Asset[]>} A promise that resolves to an array of assets.
*/
getAssets(assetsAddress: string[]): Promise<Asset[]>;
/**
* Retrieves a paginated list of assets from the server.
*
* @param {number} [page=1] page - The page number to retrieve. Defaults to 1.
* @param {boolean} [warning=false] warning - A boolean indicating whether to include warnings. Defaults to false.
* @param {string} [phrase=''] phrase - An optional search phrase to filter the assets.
* @returns {Promise<PaginatedAssets>} A promise that resolves to a paginated list of assets.
*/
getPaginatedAssets(page?: number, warning?: boolean, phrase?: string): Promise<PaginatedAssets>;
/**
* Retrieves a paginated list of asset pairs from the server.
*
* @param {string} assetAddress - The address of the asset to fetch pairs for.
* @param {number} [page=1] - The page number to retrieve. Defaults to 1.
* @param {boolean} [warning=false] - A boolean indicating whether to include warnings. Defaults to false.
* @param {string} [searchPhrase=''] - An optional search phrase to filter the asset pairs.
* @returns {Promise<PaginatedAssets>} A promise that resolves to a paginated list of asset pairs.
*/
getPairs(assetAddress: string, page?: number, warning?: boolean, searchPhrase?: string): Promise<PaginatedAssets>;
}
type Dex = 'stonfi' | 'dedust' | 'tonco';
interface BestRoute {
selected_pool: SelectedPool;
pool_data: PoolData;
}
interface SelectedPool {
router_address: string;
dex: string;
dex_details: DexDetails;
reserve0: string;
reserve1: string;
token0_address: string;
token1_address: string;
fee: number;
}
interface DexDetails {
name: string;
icon_url: string;
}
interface PoolData {
router_address: string;
pay: string;
receive: string;
priceImpact: number;
minimumReceive: string;
innerMinimumReceive: string;
blockchainFee: string;
status: boolean;
message: string;
route: string[];
receive_show: number;
minimumReceive_show: number;
route_view: string[];
}
declare class Router$1 extends Services {
/**
* Finds the best route for a given input and output asset address, pay amount, and optional slippage and DEX.
*
* @param {string} inputAssetAddress - The address of the input asset.
* @param {string} outputAssetAddress - The address of the output asset.
* @param {bigint} payAmount - The amount to be paid.
* @param {number} [slippage] - Optional slippage percentage.
* @param {Dex} [forceDex] - Optional DEX to force the route through.
* @returns {Promise<BestRoute>} A promise that resolves to the best route.
*
* @todo Add validation for address and slippage.
* @todo If the user doesn't input an address, get the address by asset service.
*/
findBestRoute(inputAssetAddress: string, outputAssetAddress: string, payAmount: bigint, slippage?: number, forceDex?: Dex): Promise<BestRoute>;
}
interface Balance {
balance: string;
price?: Price$1;
wallet_address: WalletAddress;
jetton: Jetton$1;
extensions?: string[];
lock?: Lock;
}
interface Jetton$1 {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
prices?: Prices;
custom_payload_api_uri?: string;
}
interface CustomPayload {
custom_payload: string;
state_init: string;
}
interface WalletAssets {
balances: Balance[];
}
interface IJettonsRate {
rates: Rates;
}
interface Rates {
[address: string]: Price$1;
}
interface Price$1 {
prices: Prices;
diff_24h: Diff24h;
diff_7d: Diff7d;
diff_30d: Diff30d;
}
interface Prices {
USD: number;
[key: string]: number;
}
interface Diff24h {
[key: string]: number;
}
interface Diff7d {
[key: string]: number;
}
interface Diff30d {
TON: string;
}
interface WalletAddress {
address: string;
name?: string;
is_scam: boolean;
icon?: string;
is_wallet: boolean;
}
interface Lock {
amount: string;
till: number;
}
interface WalletInfo {
address: string;
balance: number;
last_activity: number;
status: string;
interfaces: string[];
get_methods: string[];
is_wallet: boolean;
}
interface SwapResponse {
to: string;
value: string;
body: string;
query_id: string;
init: {
code: string;
data: string;
};
}
interface TransactionEvent {
event_id: string;
timestamp: number;
actions: Action[];
value_flow: ValueFlow[];
is_scam: boolean;
lt: number;
in_progress: boolean;
}
interface Action {
type: string;
status: string;
TonTransfer: TonTransfer;
ContractDeploy: ContractDeploy;
JettonTransfer: JettonTransfer;
JettonBurn: JettonBurn;
JettonMint: JettonMint;
NftItemTransfer: NftItemTransfer;
Subscribe: Subscribe;
UnSubscribe: UnSubscribe;
AuctionBid: AuctionBid;
NftPurchase: NftPurchase;
DepositStake: DepositStake;
WithdrawStake: WithdrawStake;
WithdrawStakeRequest: WithdrawStakeRequest;
ElectionsDepositStake: ElectionsDepositStake;
ElectionsRecoverStake: ElectionsRecoverStake;
JettonSwap: JettonSwap;
SmartContractExec: SmartContractExec;
DomainRenew: DomainRenew;
InscriptionTransfer: InscriptionTransfer;
InscriptionMint: InscriptionMint;
simple_preview: SimplePreview;
base_transactions: string[];
}
interface TonTransfer {
sender: Sender;
recipient: Recipient;
amount: number;
comment: string;
encrypted_comment: EncryptedComment;
refund: Refund;
}
interface Sender {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Recipient {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface EncryptedComment {
encryption_type: string;
cipher_text: string;
}
interface Refund {
type: string;
origin: string;
}
interface ContractDeploy {
address: string;
interfaces: string[];
}
interface JettonTransfer {
sender: Sender2;
recipient: Recipient2;
senders_wallet: string;
recipients_wallet: string;
amount: string;
comment: string;
encrypted_comment: EncryptedComment2;
refund: Refund2;
jetton: Jetton;
}
interface Sender2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Recipient2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface EncryptedComment2 {
encryption_type: string;
cipher_text: string;
}
interface Refund2 {
type: string;
origin: string;
}
interface Jetton {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
custom_payload_api_uri: string;
}
interface JettonBurn {
sender: Sender3;
senders_wallet: string;
amount: string;
jetton: Jetton2;
}
interface Sender3 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Jetton2 {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
custom_payload_api_uri: string;
}
interface JettonMint {
recipient: Recipient3;
recipients_wallet: string;
amount: string;
jetton: Jetton3;
}
interface Recipient3 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Jetton3 {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
custom_payload_api_uri: string;
}
interface NftItemTransfer {
sender: Sender4;
recipient: Recipient4;
nft: string;
comment: string;
encrypted_comment: EncryptedComment3;
payload: string;
refund: Refund3;
}
interface Sender4 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Recipient4 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface EncryptedComment3 {
encryption_type: string;
cipher_text: string;
}
interface Refund3 {
type: string;
origin: string;
}
interface Subscribe {
subscriber: Subscriber;
subscription: string;
beneficiary: Beneficiary;
amount: number;
initial: boolean;
}
interface Subscriber {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Beneficiary {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface UnSubscribe {
subscriber: Subscriber2;
subscription: string;
beneficiary: Beneficiary2;
}
interface Subscriber2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Beneficiary2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface AuctionBid {
auction_type: string;
amount: Amount;
nft: Nft;
bidder: Bidder;
auction: Auction;
}
interface Amount {
value: string;
token_name: string;
}
interface Nft {
address: string;
index: number;
owner: Owner;
collection: Collection;
verified: boolean;
metadata: Metadata;
sale: Sale;
previews: Preview[];
dns: string;
approved_by: string[];
include_cnft: boolean;
trust: string;
}
interface Owner {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Collection {
address: string;
name: string;
description: string;
}
interface Metadata {
}
interface Sale {
address: string;
market: Market;
owner: Owner2;
price: Price;
}
interface Market {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Owner2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Price {
value: string;
token_name: string;
}
interface Preview {
resolution: string;
url: string;
}
interface Bidder {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Auction {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface NftPurchase {
auction_type: string;
amount: Amount2;
nft: Nft2;
seller: Seller;
buyer: Buyer;
}
interface Amount2 {
value: string;
token_name: string;
}
interface Nft2 {
address: string;
index: number;
owner: Owner3;
collection: Collection2;
verified: boolean;
metadata: Metadata2;
sale: Sale2;
previews: Preview2[];
dns: string;
approved_by: string[];
include_cnft: boolean;
trust: string;
}
interface Owner3 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Collection2 {
address: string;
name: string;
description: string;
}
interface Metadata2 {
}
interface Sale2 {
address: string;
market: Market2;
owner: Owner4;
price: Price2;
}
interface Market2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Owner4 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Price2 {
value: string;
token_name: string;
}
interface Preview2 {
resolution: string;
url: string;
}
interface Seller {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Buyer {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface DepositStake {
amount: number;
staker: Staker;
pool: Pool;
implementation: string;
}
interface Staker {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Pool {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface WithdrawStake {
amount: number;
staker: Staker2;
pool: Pool2;
implementation: string;
}
interface Staker2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Pool2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface WithdrawStakeRequest {
amount: number;
staker: Staker3;
pool: Pool3;
implementation: string;
}
interface Staker3 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Pool3 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface ElectionsDepositStake {
amount: number;
staker: Staker4;
}
interface Staker4 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface ElectionsRecoverStake {
amount: number;
staker: Staker5;
}
interface Staker5 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface JettonSwap {
dex: string;
amount_in: string;
amount_out: string;
ton_in: number;
ton_out: number;
user_wallet: UserWallet;
router: Router;
jetton_master_in: JettonMasterIn;
jetton_master_out: JettonMasterOut;
}
interface UserWallet {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Router {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface JettonMasterIn {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
custom_payload_api_uri: string;
}
interface JettonMasterOut {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
custom_payload_api_uri: string;
}
interface SmartContractExec {
executor: Executor;
contract: Contract;
ton_attached: number;
operation: string;
payload: string;
refund: Refund4;
}
interface Executor {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Contract {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Refund4 {
type: string;
origin: string;
}
interface DomainRenew {
domain: string;
contract_address: string;
renewer: Renewer;
}
interface Renewer {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface InscriptionTransfer {
sender: Sender5;
recipient: Recipient5;
amount: string;
comment: string;
type: string;
ticker: string;
decimals: number;
}
interface Sender5 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Recipient5 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface InscriptionMint {
recipient: Recipient6;
amount: string;
type: string;
ticker: string;
decimals: number;
}
interface Recipient6 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface SimplePreview {
name: string;
description: string;
action_image: string;
value: string;
value_image: string;
accounts: Account[];
}
interface Account {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface ValueFlow {
account: Account2;
ton: number;
fees: number;
jettons: Jetton4[];
}
interface Account2 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Jetton4 {
account: Account3;
jetton: Jetton5;
quantity: number;
}
interface Account3 {
address: string;
name: string;
is_scam: boolean;
icon: string;
is_wallet: boolean;
}
interface Jetton5 {
address: string;
name: string;
symbol: string;
decimals: number;
image: string;
verification: string;
custom_payload_api_uri: string;
}
declare class TonApi extends Services {
/**
* Fetches the jetton data for a given wallet address and jetton address.
*
* @param {string} walletAddr - The wallet address to fetch the jetton data for.
* @param {string} jettonAddress - The jetton address to fetch the data from.
* @returns {Promise<Balance>} A promise that resolves to the balance data.
*/
getJettonData(walletAddr: string, jettonAddress: string): Promise<Balance>;
/**
* Retrieves a custom payload for a specific wallet and jetton address.
*
* @param {string} walletAddr - The address of the wallet.
* @param {string} jettonAddress - The address of the jetton.
* @returns {Promise<CustomPayload>} A promise that resolves to the custom payload.
*/
getCustomPayload(walletAddr: string, jettonAddress: string): Promise<CustomPayload>;
/**
* Retrieves wallet assets for a given wallet address, including balances and rates for jettons.
*
* @param {string} walletAddress - The address of the wallet to retrieve assets for.
* @param {string[]} [currencies=['usd']] - An array of currency codes to retrieve rates for.
* @param {boolean} [custom_payload=true] - Whether to include custom payload in the request.
* @returns {Promise<Map<string, Balance>>} A promise that resolves to a map of balances keyed by jetton addresses.
*/
getWalletAssets(walletAddress: string, currencies?: string[], custom_payload?: boolean): Promise<Map<string, Balance>>;
/**
* Fetches the rates of specified assets from the TON API.
*
* @param {string[]} assetsAddresses - An array of asset addresses to fetch rates for.
* @returns {Promise<Map<string, Prices>>} A promise that resolves to a map where the keys are user-friendly asset addresses and the values are their corresponding prices.
*/
getAssetsRates(assetsAddresses: string[]): Promise<Map<string, Prices>>;
/**
* waitForTransactionResult
*/
/**
* Waits for a transaction result by periodically checking the transaction status.
*
* @param {string} hash - The hash of the transaction to wait for.
* @param {number} [period_ms=3000] - The period in milliseconds to wait between checks.
* @param {number} [maxRetry=30] - The maximum number of retries before giving up.
* @returns {Promise<TransactionEvent>} - A promise that resolves with the transaction result when complete.
* @throws {Error} - Throws an error if the maximum number of retries is reached.
*/
waitForTransactionResult(hash: string, period_ms?: number, maxRetry?: number): Promise<TransactionEvent>;
/**
* Fetches a transaction event from the TON API using the provided hash.
*
* @param {string} hash - The hash of the transaction event to retrieve.
* @returns {Promise<TransactionEvent>} A promise that resolves to the transaction event.
*/
getTransactionEvent(hash: string): Promise<TransactionEvent>;
/**
* Checks if all transactions in the given event are complete.
*
* @param {TransactionEvent} event - The transaction event to check.
* @returns {string} - Returns `ok` if all transactions are complete and successful, otherwise `failed` or `inprogress`.
* @throws {Error} - Throws an error if any transaction action has a status other than 'ok'.
*/
allTransactionComplete(event: TransactionEvent): "inprogress" | "failed" | "ok";
}
declare class Swap extends Services {
/**
* Creates a swap request using the provided user wallet address and best route.
*
* @param {string} userWalletAddress - The address of the user's wallet.
* @param {BestRoute} bestRoute - The best route from router.
* @param {string} [app_id] - Optional application ID to include in the request headers.
* @returns {Promise<SwapResponse>} A promise that resolves to the swap response.
*/
createSwap(userWalletAddress: string, bestRoute: BestRoute, app_id?: string): Promise<SwapResponse>;
}
declare class MyTonSwapClient {
options: MyTonSwapClientOptions | undefined;
request: Request;
assets: Assets;
router: Router$1;
tonapi: TonApi;
swap: Swap;
tonClient: TonClient;
constructor(options?: MyTonSwapClientOptions);
}
/**
* Converts a given number, string, or bigint to a bigint with the specified number of decimals.
*
* @param {number | string | bigint} src - The source value to convert. It can be a number, string, or bigint.
* @param {number} [decimals=9] - The number of decimal places to consider for the conversion. Defaults to 9.
* @returns {bigint} - The converted value as a bigint.
* @throws {Error} - Throws an error if the input number is not finite, if the input string is invalid, or if the input number does not have enough precision.
*/
declare function toNano(src: number | string | bigint, decimals?: number): bigint;
/**
* Converts a value from nano units to a string representation with the specified number of decimals.
*
* @param {bigint | number | string} src - The source value in nano units. It can be a bigint, number, or string.
* @param {number} [decimals=9] - The number of decimal places to include in the output string. Defaults to 9.
* @returns {string} The converted value as a string with the specified number of decimals.
*/
declare function fromNano(src: bigint | number | string, decimals?: number): string;
export { type Asset, type Balance, type BestRoute, type CustomPayload, type Dex, type DexDetails, type Diff24h, type Diff30d, type Diff7d, type IJettonsRate, type Jetton$1 as Jetton, type Lock, type Meta, MyTonSwapClient, type MyTonSwapClientOptions, type MyTonSwapResponse, type PaginatedAssets, type PoolData, type Price$1 as Price, type Prices, type Rates, type SelectedPool, type SwapResponse, type WalletAddress, type WalletAssets, type WalletInfo, fromNano, toNano };