@ref-finance/ref-sdk 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1723 -0
- package/dist/constant.d.ts +44 -0
- package/dist/dcl-swap/dcl-pool.d.ts +24 -0
- package/dist/dcl-swap/limit-order.d.ts +32 -0
- package/dist/dcl-swap/swap.d.ts +46 -0
- package/dist/error.d.ts +20 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +8 -0
- package/dist/indexer.d.ts +3 -0
- package/dist/metaIcons.d.ts +4 -0
- package/dist/near.d.ts +16 -0
- package/dist/ref-sdk.cjs.development.css +296 -0
- package/dist/ref-sdk.cjs.development.js +8893 -0
- package/dist/ref-sdk.cjs.development.js.map +1 -0
- package/dist/ref-sdk.cjs.production.min.js +2 -0
- package/dist/ref-sdk.cjs.production.min.js.map +1 -0
- package/dist/ref-sdk.esm.js +8771 -0
- package/dist/ref-sdk.esm.js.map +1 -0
- package/dist/ref-sdk.umd.development.js +8908 -0
- package/dist/ref-sdk.umd.development.js.map +1 -0
- package/dist/ref-sdk.umd.production.min.js +2 -0
- package/dist/ref-sdk.umd.production.min.js.map +1 -0
- package/dist/ref.d.ts +19 -0
- package/dist/stable-swap.d.ts +5 -0
- package/dist/swap-widget/components.d.ts +90 -0
- package/dist/swap-widget/constant.d.ts +25 -0
- package/dist/swap-widget/defaultTokenList.d.ts +120 -0
- package/dist/swap-widget/index.d.ts +4 -0
- package/dist/swap-widget/state.d.ts +49 -0
- package/dist/swap-widget/types.d.ts +40 -0
- package/dist/types.d.ts +129 -0
- package/dist/utils.d.ts +92 -0
- package/dist/v1-swap/instantSwap.d.ts +9 -0
- package/dist/v1-swap/parallelSwapLogic.d.ts +84 -0
- package/dist/v1-swap/pool.d.ts +16 -0
- package/dist/v1-swap/smartRoutingLogic.d.ts +4 -0
- package/dist/v1-swap/swap.d.ts +239 -0
- package/package.json +81 -0
package/dist/ref.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Near } from 'near-api-js';
|
|
2
|
+
import { TokenMetadata, FTStorageBalance, RefFiViewFunctionOptions } from './types';
|
|
3
|
+
import { Transaction } from './types';
|
|
4
|
+
export declare const REPLACE_TOKENS: string[];
|
|
5
|
+
export declare const near: Near;
|
|
6
|
+
export declare const refFiViewFunction: ({ methodName, args, }: RefFiViewFunctionOptions) => Promise<any>;
|
|
7
|
+
export declare const ftViewFunction: (tokenId: string, { methodName, args }: RefFiViewFunctionOptions) => Promise<any>;
|
|
8
|
+
export declare const ftGetStorageBalance: (tokenId: string, AccountId: string) => Promise<FTStorageBalance | null>;
|
|
9
|
+
export declare const ftGetBalance: (tokenId: string, AccountId: string) => Promise<any>;
|
|
10
|
+
export declare const getTotalPools: () => Promise<any>;
|
|
11
|
+
export declare const ftGetTokenMetadata: (id: string, tag?: string | undefined) => Promise<TokenMetadata>;
|
|
12
|
+
export declare const ftGetTokensMetadata: (tokenIds: string[], allTokens: Record<string, TokenMetadata>) => Promise<Record<string, TokenMetadata>>;
|
|
13
|
+
export declare const getGlobalWhitelist: () => Promise<string[]>;
|
|
14
|
+
export declare const getUserRegisteredTokens: (AccountId?: string | undefined) => Promise<string[]>;
|
|
15
|
+
export declare const getAccountNearBalance: (accountId: string) => Promise<string>;
|
|
16
|
+
export declare const nearDepositTransaction: (amount: string) => Transaction;
|
|
17
|
+
export declare const nearWithdrawTransaction: (amount: string) => Transaction;
|
|
18
|
+
export declare const refDCLSwapViewFunction: ({ methodName, args, }: RefFiViewFunctionOptions) => Promise<any>;
|
|
19
|
+
export declare const DCLSwapGetStorageBalance: (tokenId: string, AccountId: string) => Promise<any>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StablePool } from './types';
|
|
2
|
+
export declare const calc_d: (amp: number, c_amounts: number[]) => number;
|
|
3
|
+
export declare const calc_y: (amp: number, x_c_amount: number, current_c_amounts: number[], index_x: number, index_y: number) => number;
|
|
4
|
+
export declare const calc_swap: (amp: number, in_token_idx: number, in_c_amount: number, out_token_idx: number, old_c_amounts: number[], trade_fee: number) => number[];
|
|
5
|
+
export declare const getSwappedAmount: (tokenInId: string, tokenOutId: string, amountIn: string, stablePool: StablePool, STABLE_LP_TOKEN_DECIMALS: number) => number[];
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { TokenMetadata, EstimateSwapView, Pool } from '../types';
|
|
3
|
+
import './style.css';
|
|
4
|
+
interface TokenAmountProps {
|
|
5
|
+
balance?: string;
|
|
6
|
+
reloading?: JSX.Element;
|
|
7
|
+
token?: TokenMetadata;
|
|
8
|
+
onSelectToken: () => void;
|
|
9
|
+
amount: string;
|
|
10
|
+
onChangeAmount?: (amount: string) => void;
|
|
11
|
+
price?: string;
|
|
12
|
+
onForceUpdate?: () => void;
|
|
13
|
+
poolFetchingState?: 'loading' | 'end';
|
|
14
|
+
}
|
|
15
|
+
export declare const getPriceImpact: (value: string, tokenIn: TokenMetadata, tokenInAmount: string) => JSX.Element;
|
|
16
|
+
export declare const SmartRouteV2: ({ tokens: tokensRaw, p, pools, }: {
|
|
17
|
+
tokens: TokenMetadata[];
|
|
18
|
+
p: string;
|
|
19
|
+
pools: Pool[];
|
|
20
|
+
}) => JSX.Element | null;
|
|
21
|
+
export declare const DetailView: ({ tokenIn, tokenOut, rate, fee, minReceived, amountIn, amountOut, priceImpact, estimates, }: {
|
|
22
|
+
tokenIn: TokenMetadata | undefined;
|
|
23
|
+
tokenOut: TokenMetadata | undefined;
|
|
24
|
+
rate: string;
|
|
25
|
+
fee: number;
|
|
26
|
+
minReceived: string;
|
|
27
|
+
amountIn: string;
|
|
28
|
+
amountOut: string;
|
|
29
|
+
priceImpact: string;
|
|
30
|
+
estimates: EstimateSwapView[];
|
|
31
|
+
}) => JSX.Element | null;
|
|
32
|
+
export declare const HalfAndMaxAmount: ({ max, onChangeAmount, token, amount, }: {
|
|
33
|
+
max: string;
|
|
34
|
+
token: TokenMetadata;
|
|
35
|
+
onChangeAmount: (amount: string) => void;
|
|
36
|
+
amount: string;
|
|
37
|
+
}) => JSX.Element;
|
|
38
|
+
export declare const TokenAmount: (props: TokenAmountProps) => JSX.Element;
|
|
39
|
+
export declare const SlippageSelector: ({ slippageTolerance, onChangeSlippageTolerance, showSlip, setShowSlip, }: {
|
|
40
|
+
slippageTolerance: number;
|
|
41
|
+
onChangeSlippageTolerance: (slippageTolerance: number) => void;
|
|
42
|
+
showSlip: boolean;
|
|
43
|
+
setShowSlip: (showSlip: boolean) => void;
|
|
44
|
+
}) => JSX.Element | null;
|
|
45
|
+
interface TokenListProps {
|
|
46
|
+
tokens: TokenMetadata[];
|
|
47
|
+
onClick: (token: TokenMetadata) => void;
|
|
48
|
+
balances: {
|
|
49
|
+
[tokenId: string]: string;
|
|
50
|
+
};
|
|
51
|
+
tokenPriceList: Record<string, any> | null;
|
|
52
|
+
starList: string[];
|
|
53
|
+
setStarList: (starList: string[]) => void;
|
|
54
|
+
onDelete: (token: TokenMetadata) => void;
|
|
55
|
+
}
|
|
56
|
+
export declare const TokenListTable: ({ tokens, onClick, balances, tokenPriceList, starList, setStarList, onDelete, }: TokenListProps) => JSX.Element | null;
|
|
57
|
+
export declare const TokenSelector: ({ onSelect, width, tokens, onClose, AccountId, balances, className, }: {
|
|
58
|
+
onSelect: (token: TokenMetadata) => void;
|
|
59
|
+
width: string;
|
|
60
|
+
tokens: TokenMetadata[];
|
|
61
|
+
onClose: () => void;
|
|
62
|
+
AccountId: string;
|
|
63
|
+
balances: {
|
|
64
|
+
[tokenId: string]: string;
|
|
65
|
+
};
|
|
66
|
+
className?: string | undefined;
|
|
67
|
+
}) => JSX.Element;
|
|
68
|
+
export declare const Slider: ({ showSlip, setShowSlip, }: {
|
|
69
|
+
showSlip: boolean;
|
|
70
|
+
setShowSlip: (show: boolean) => void;
|
|
71
|
+
}) => JSX.Element;
|
|
72
|
+
export declare const RefIcon: (props: any) => JSX.Element;
|
|
73
|
+
export declare const Loading: () => JSX.Element;
|
|
74
|
+
export declare const Warning: () => JSX.Element;
|
|
75
|
+
export declare const Success: () => JSX.Element;
|
|
76
|
+
export declare const RouterIcon: () => JSX.Element;
|
|
77
|
+
export declare const Notification: ({ state, tx, detail, open, setOpen, setState, }: {
|
|
78
|
+
state?: "success" | "fail" | null | undefined;
|
|
79
|
+
setState?: ((state: 'success' | 'fail' | null) => void) | undefined;
|
|
80
|
+
tx?: string | undefined;
|
|
81
|
+
detail?: string | undefined;
|
|
82
|
+
open: boolean;
|
|
83
|
+
setOpen: (open: boolean) => void;
|
|
84
|
+
}) => JSX.Element | null;
|
|
85
|
+
export declare const ArrowRight: () => JSX.Element;
|
|
86
|
+
export declare const AccountButton: ({ AccountId, onDisConnect, }: {
|
|
87
|
+
AccountId: string;
|
|
88
|
+
onDisConnect: () => void;
|
|
89
|
+
}) => JSX.Element | null;
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const REF_WIDGET_STAR_TOKEN_LIST_KEY = "REF_WIDGET_STAR_TOKEN_LIST_VALUE";
|
|
2
|
+
export declare const REF_WIDGET_ALL_TOKENS_LIST_KEY = "REF_WIDGET_ALL_TOKENS_LIST_VALUE";
|
|
3
|
+
export declare const REF_WIDGET_SWAP_IN_KEY = "REF_WIDGET_SWAP_IN_VALUE";
|
|
4
|
+
export declare const REF_WIDGET_SWAP_OUT_KEY = "REF_WIDGET_SWAP_OUT_VALUE";
|
|
5
|
+
export declare const REF_WIDGET_SWAP_DETAIL_KEY = "REF_WIDGET_SWAP_DETAIL_VALUE";
|
|
6
|
+
export declare const DEFAULT_START_TOKEN_LIST_TESTNET: string[];
|
|
7
|
+
export declare const DEFAULT_START_TOKEN_LIST_MAINNET: string[];
|
|
8
|
+
export declare const DEFAULT_START_TOKEN_LIST: string[];
|
|
9
|
+
export interface Theme {
|
|
10
|
+
container: string;
|
|
11
|
+
buttonBg: string;
|
|
12
|
+
primary: string;
|
|
13
|
+
secondary: string;
|
|
14
|
+
borderRadius: string;
|
|
15
|
+
fontFamily: string;
|
|
16
|
+
hover: string;
|
|
17
|
+
active: string;
|
|
18
|
+
secondaryBg: string;
|
|
19
|
+
borderColor: string;
|
|
20
|
+
iconDefault: string;
|
|
21
|
+
iconHover: string;
|
|
22
|
+
refIcon?: string;
|
|
23
|
+
}
|
|
24
|
+
export declare const defaultTheme: Theme;
|
|
25
|
+
export declare const defaultDarkModeTheme: Theme;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export declare const DefaultTestnetTokenList: ({
|
|
2
|
+
spec: string;
|
|
3
|
+
name: string;
|
|
4
|
+
symbol: string;
|
|
5
|
+
icon: null;
|
|
6
|
+
reference: null;
|
|
7
|
+
reference_hash: null;
|
|
8
|
+
decimals: number;
|
|
9
|
+
id: string;
|
|
10
|
+
} | {
|
|
11
|
+
spec: string;
|
|
12
|
+
name: string;
|
|
13
|
+
symbol: string;
|
|
14
|
+
icon: string;
|
|
15
|
+
reference: null;
|
|
16
|
+
reference_hash: null;
|
|
17
|
+
decimals: number;
|
|
18
|
+
id: string;
|
|
19
|
+
} | {
|
|
20
|
+
spec: string;
|
|
21
|
+
name: string;
|
|
22
|
+
symbol: string;
|
|
23
|
+
icon: string;
|
|
24
|
+
reference: string;
|
|
25
|
+
reference_hash: null;
|
|
26
|
+
decimals: number;
|
|
27
|
+
id: string;
|
|
28
|
+
})[];
|
|
29
|
+
export declare const DefaultMainnetTokenList: ({
|
|
30
|
+
spec: string;
|
|
31
|
+
name: string;
|
|
32
|
+
symbol: string;
|
|
33
|
+
icon: null;
|
|
34
|
+
reference: null;
|
|
35
|
+
reference_hash: null;
|
|
36
|
+
decimals: number;
|
|
37
|
+
id: string;
|
|
38
|
+
} | {
|
|
39
|
+
spec: string;
|
|
40
|
+
name: string;
|
|
41
|
+
symbol: string;
|
|
42
|
+
icon: string;
|
|
43
|
+
reference: string;
|
|
44
|
+
reference_hash: string;
|
|
45
|
+
decimals: number;
|
|
46
|
+
id: string;
|
|
47
|
+
} | {
|
|
48
|
+
spec: string;
|
|
49
|
+
name: string;
|
|
50
|
+
symbol: string;
|
|
51
|
+
icon: null;
|
|
52
|
+
reference: string;
|
|
53
|
+
reference_hash: string;
|
|
54
|
+
decimals: number;
|
|
55
|
+
id: string;
|
|
56
|
+
} | {
|
|
57
|
+
spec: string;
|
|
58
|
+
name: string;
|
|
59
|
+
symbol: string;
|
|
60
|
+
icon: string;
|
|
61
|
+
reference: null;
|
|
62
|
+
reference_hash: null;
|
|
63
|
+
decimals: number;
|
|
64
|
+
id: string;
|
|
65
|
+
} | {
|
|
66
|
+
spec: string;
|
|
67
|
+
name: string;
|
|
68
|
+
symbol: string;
|
|
69
|
+
icon: string;
|
|
70
|
+
reference: string;
|
|
71
|
+
reference_hash: null;
|
|
72
|
+
decimals: number;
|
|
73
|
+
id: string;
|
|
74
|
+
})[];
|
|
75
|
+
export declare function getDefaultTokenList(env?: string | undefined): ({
|
|
76
|
+
spec: string;
|
|
77
|
+
name: string;
|
|
78
|
+
symbol: string;
|
|
79
|
+
icon: null;
|
|
80
|
+
reference: null;
|
|
81
|
+
reference_hash: null;
|
|
82
|
+
decimals: number;
|
|
83
|
+
id: string;
|
|
84
|
+
} | {
|
|
85
|
+
spec: string;
|
|
86
|
+
name: string;
|
|
87
|
+
symbol: string;
|
|
88
|
+
icon: string;
|
|
89
|
+
reference: string;
|
|
90
|
+
reference_hash: string;
|
|
91
|
+
decimals: number;
|
|
92
|
+
id: string;
|
|
93
|
+
} | {
|
|
94
|
+
spec: string;
|
|
95
|
+
name: string;
|
|
96
|
+
symbol: string;
|
|
97
|
+
icon: null;
|
|
98
|
+
reference: string;
|
|
99
|
+
reference_hash: string;
|
|
100
|
+
decimals: number;
|
|
101
|
+
id: string;
|
|
102
|
+
} | {
|
|
103
|
+
spec: string;
|
|
104
|
+
name: string;
|
|
105
|
+
symbol: string;
|
|
106
|
+
icon: string;
|
|
107
|
+
reference: null;
|
|
108
|
+
reference_hash: null;
|
|
109
|
+
decimals: number;
|
|
110
|
+
id: string;
|
|
111
|
+
} | {
|
|
112
|
+
spec: string;
|
|
113
|
+
name: string;
|
|
114
|
+
symbol: string;
|
|
115
|
+
icon: string;
|
|
116
|
+
reference: string;
|
|
117
|
+
reference_hash: null;
|
|
118
|
+
decimals: number;
|
|
119
|
+
id: string;
|
|
120
|
+
})[];
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EstimateSwapView, Pool, StablePool, TokenMetadata, Transaction } from '../types';
|
|
3
|
+
import { SwapOptions } from '../v1-swap/swap';
|
|
4
|
+
import { SwapOutParams } from './types';
|
|
5
|
+
import { Theme } from './constant';
|
|
6
|
+
export declare const ThemeContext: React.Context<Theme>;
|
|
7
|
+
export declare const ThemeContextProvider: React.FC<{
|
|
8
|
+
customTheme: Theme | undefined;
|
|
9
|
+
}>;
|
|
10
|
+
export declare const estimateValidator: (swapTodos: EstimateSwapView[], tokenIn: TokenMetadata, parsedAmountIn: string, tokenOut: TokenMetadata) => boolean;
|
|
11
|
+
export declare const useAllTokens: ({ reload }: {
|
|
12
|
+
reload?: boolean | undefined;
|
|
13
|
+
}) => {
|
|
14
|
+
tokens: Record<string, TokenMetadata> | undefined;
|
|
15
|
+
tokensLoading: boolean;
|
|
16
|
+
};
|
|
17
|
+
export declare const useTokensIndexer: ({ defaultTokenList, AccountId, }: {
|
|
18
|
+
defaultTokenList?: TokenMetadata[] | undefined;
|
|
19
|
+
AccountId?: string | undefined;
|
|
20
|
+
}) => {
|
|
21
|
+
tokens: TokenMetadata[];
|
|
22
|
+
tokenLoading: boolean;
|
|
23
|
+
};
|
|
24
|
+
export declare const useRefPools: (refreshTrigger: boolean) => {
|
|
25
|
+
allPools: {
|
|
26
|
+
simplePools: Pool[];
|
|
27
|
+
ratedPools: Pool[];
|
|
28
|
+
unRatedPools: Pool[];
|
|
29
|
+
};
|
|
30
|
+
allStablePools: StablePool[];
|
|
31
|
+
poolFetchingState: "loading" | "end";
|
|
32
|
+
};
|
|
33
|
+
export declare const useSwap: (params: {
|
|
34
|
+
tokenIn?: TokenMetadata | undefined;
|
|
35
|
+
tokenOut?: TokenMetadata | undefined;
|
|
36
|
+
amountIn: string;
|
|
37
|
+
simplePools: Pool[];
|
|
38
|
+
options?: SwapOptions | undefined;
|
|
39
|
+
} & {
|
|
40
|
+
slippageTolerance: number;
|
|
41
|
+
refreshTrigger: boolean;
|
|
42
|
+
onSwap: (transactionsRef: Transaction[]) => void;
|
|
43
|
+
AccountId?: string | undefined;
|
|
44
|
+
poolFetchingState?: "loading" | "end" | undefined;
|
|
45
|
+
}) => SwapOutParams;
|
|
46
|
+
export declare const TokenPriceContext: React.Context<Record<string, any> | null>;
|
|
47
|
+
export declare const useTokenPriceList: () => Record<string, any>;
|
|
48
|
+
export declare const TokenPriceContextProvider: React.FC;
|
|
49
|
+
export declare const useTokenBalnces: (tokens: TokenMetadata[], AccountId: string) => Record<string, string>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { EstimateSwapView } from '../types';
|
|
2
|
+
import { Transaction } from '../types';
|
|
3
|
+
import { TokenMetadata } from '../../dist/types';
|
|
4
|
+
import { Theme } from './constant';
|
|
5
|
+
export interface SwapWidgetProps {
|
|
6
|
+
theme?: Theme;
|
|
7
|
+
defaultTokenList?: TokenMetadata[];
|
|
8
|
+
onSwap: (transactionsRef: Transaction[]) => void;
|
|
9
|
+
onDisConnect: () => void;
|
|
10
|
+
width: string;
|
|
11
|
+
height?: string;
|
|
12
|
+
enableSmartRouting?: boolean;
|
|
13
|
+
className?: string;
|
|
14
|
+
darkMode?: boolean;
|
|
15
|
+
connection: {
|
|
16
|
+
AccountId: string;
|
|
17
|
+
isSignedIn: boolean;
|
|
18
|
+
};
|
|
19
|
+
defaultTokenIn?: string;
|
|
20
|
+
defaultTokenOut?: string;
|
|
21
|
+
transactionState?: {
|
|
22
|
+
state: 'success' | 'fail' | null;
|
|
23
|
+
setState: (state: 'success' | 'fail' | null) => void;
|
|
24
|
+
tx?: string;
|
|
25
|
+
detail?: string;
|
|
26
|
+
};
|
|
27
|
+
onConnect: () => void;
|
|
28
|
+
}
|
|
29
|
+
export interface SwapOutParams {
|
|
30
|
+
amountOut: string;
|
|
31
|
+
minAmountOut: string;
|
|
32
|
+
rate: string;
|
|
33
|
+
fee: number;
|
|
34
|
+
estimates: EstimateSwapView[];
|
|
35
|
+
makeSwap: () => void;
|
|
36
|
+
canSwap: boolean;
|
|
37
|
+
swapError: Error | null;
|
|
38
|
+
setAmountOut: (amount: string) => void;
|
|
39
|
+
isEstimating: boolean;
|
|
40
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { PoolMode } from './v1-swap/swap';
|
|
3
|
+
export interface TokenMetadata {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
symbol: string;
|
|
7
|
+
decimals: number;
|
|
8
|
+
icon: string;
|
|
9
|
+
}
|
|
10
|
+
export declare type PoolKind = 'SIMPLE_POOL' | 'STABLE_SWAP' | 'RATED_SWAP';
|
|
11
|
+
export declare type StablePoolKind = 'STABLE_SWAP' | 'RATED_SWAP';
|
|
12
|
+
export interface PoolRPCView {
|
|
13
|
+
id: number;
|
|
14
|
+
token_account_ids: string[];
|
|
15
|
+
token_symbols: string[];
|
|
16
|
+
amounts: string[];
|
|
17
|
+
total_fee: number;
|
|
18
|
+
shares_total_supply: string;
|
|
19
|
+
tvl: number;
|
|
20
|
+
token0_ref_price: string;
|
|
21
|
+
share: string;
|
|
22
|
+
decimalsHandled?: boolean;
|
|
23
|
+
tokens_meta_data?: TokenMetadata[];
|
|
24
|
+
pool_kind?: PoolKind;
|
|
25
|
+
}
|
|
26
|
+
export interface Pool {
|
|
27
|
+
id: number;
|
|
28
|
+
tokenIds: string[];
|
|
29
|
+
supplies: {
|
|
30
|
+
[key: string]: string;
|
|
31
|
+
};
|
|
32
|
+
fee: number;
|
|
33
|
+
total_fee?: number;
|
|
34
|
+
shareSupply: string;
|
|
35
|
+
tvl: number;
|
|
36
|
+
token0_ref_price: string;
|
|
37
|
+
partialAmountIn?: string;
|
|
38
|
+
Dex?: string;
|
|
39
|
+
pool_kind?: PoolKind;
|
|
40
|
+
rates?: {
|
|
41
|
+
[id: string]: string;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export interface StablePool {
|
|
45
|
+
id: number;
|
|
46
|
+
token_account_ids: string[];
|
|
47
|
+
decimals: number[];
|
|
48
|
+
amounts: string[];
|
|
49
|
+
c_amounts: string[];
|
|
50
|
+
total_fee: number;
|
|
51
|
+
shares_total_supply: string;
|
|
52
|
+
amp: number;
|
|
53
|
+
rates: string[];
|
|
54
|
+
pool_kind: StablePoolKind;
|
|
55
|
+
partialAmountIn?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface SmartRoutingInputPool {
|
|
58
|
+
id: number;
|
|
59
|
+
token1Id: string;
|
|
60
|
+
token2Id: string;
|
|
61
|
+
token1Supply: string;
|
|
62
|
+
token2Supply: string;
|
|
63
|
+
fee: number;
|
|
64
|
+
shares: string;
|
|
65
|
+
token0_price: string;
|
|
66
|
+
}
|
|
67
|
+
export interface RoutePool {
|
|
68
|
+
amounts: string[];
|
|
69
|
+
fee: number;
|
|
70
|
+
id: number;
|
|
71
|
+
shares: string;
|
|
72
|
+
token0_ref_price: string;
|
|
73
|
+
token1Id: string;
|
|
74
|
+
token1Supply: string;
|
|
75
|
+
token2Id: string;
|
|
76
|
+
token2Supply: string;
|
|
77
|
+
updateTime: number;
|
|
78
|
+
partialAmountIn?: string | number | Big;
|
|
79
|
+
gamma_bps?: Big;
|
|
80
|
+
tokenIds?: string[];
|
|
81
|
+
x?: string;
|
|
82
|
+
y?: string;
|
|
83
|
+
}
|
|
84
|
+
export interface EstimateSwapView {
|
|
85
|
+
estimate: string;
|
|
86
|
+
pool: Pool;
|
|
87
|
+
intl?: any;
|
|
88
|
+
dy?: string;
|
|
89
|
+
status?: PoolMode;
|
|
90
|
+
noFeeAmountOut?: string;
|
|
91
|
+
inputToken?: string;
|
|
92
|
+
outputToken?: string;
|
|
93
|
+
nodeRoute?: string[];
|
|
94
|
+
tokens?: TokenMetadata[];
|
|
95
|
+
routeInputToken?: string;
|
|
96
|
+
route?: RoutePool[];
|
|
97
|
+
allRoutes?: RoutePool[][];
|
|
98
|
+
allNodeRoutes?: string[][];
|
|
99
|
+
totalInputAmount?: string;
|
|
100
|
+
}
|
|
101
|
+
export interface RefFiViewFunctionOptions {
|
|
102
|
+
methodName: string;
|
|
103
|
+
args?: object;
|
|
104
|
+
}
|
|
105
|
+
export interface RefFiFunctionCallOptions extends RefFiViewFunctionOptions {
|
|
106
|
+
gas?: string;
|
|
107
|
+
amount?: string;
|
|
108
|
+
}
|
|
109
|
+
export interface Transaction {
|
|
110
|
+
receiverId: string;
|
|
111
|
+
functionCalls: RefFiFunctionCallOptions[];
|
|
112
|
+
}
|
|
113
|
+
export interface TransformedTransaction {
|
|
114
|
+
signerId: string;
|
|
115
|
+
receiverId: string;
|
|
116
|
+
actions: {
|
|
117
|
+
type: string;
|
|
118
|
+
params: {
|
|
119
|
+
methodName: string;
|
|
120
|
+
args: object;
|
|
121
|
+
gas: string;
|
|
122
|
+
deposit: string;
|
|
123
|
+
};
|
|
124
|
+
}[];
|
|
125
|
+
}
|
|
126
|
+
export interface FTStorageBalance {
|
|
127
|
+
total: string;
|
|
128
|
+
available: string;
|
|
129
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Pool, PoolRPCView, StablePool, SmartRoutingInputPool, Transaction, EstimateSwapView } from './types';
|
|
2
|
+
import BN from 'bn.js';
|
|
3
|
+
import * as math from 'mathjs';
|
|
4
|
+
import Big from 'big.js';
|
|
5
|
+
import { SignAndSendTransactionsParams } from '@near-wallet-selector/core/lib/wallet';
|
|
6
|
+
import { TokenMetadata } from './types';
|
|
7
|
+
export declare const parsePool: (pool: PoolRPCView, id?: number | undefined) => Pool;
|
|
8
|
+
export declare const poolFormatter: (pool: Pool) => SmartRoutingInputPool;
|
|
9
|
+
export declare const isStablePoolToken: (stablePools: StablePool[], tokenId: string | Number) => boolean;
|
|
10
|
+
export declare const isStablePool: (stablePools: StablePool[], poolId: string | number) => boolean;
|
|
11
|
+
export declare const getStablePoolDecimal: (stablePool: StablePool) => 18 | 24;
|
|
12
|
+
export declare const round: (decimals: number, minAmountOut: string) => string;
|
|
13
|
+
export declare const convertToPercentDecimal: (percent: number) => number;
|
|
14
|
+
export declare const percentOf: (percent: number, num: number | string) => any;
|
|
15
|
+
export declare const percentLess: (percent: number, num: number | string) => string;
|
|
16
|
+
export declare const getGas: (gas: string | undefined) => BN;
|
|
17
|
+
export declare const getAmount: (amount: string) => BN;
|
|
18
|
+
export declare const ONLY_ZEROS: RegExp;
|
|
19
|
+
export declare const toReadableNumber: (decimals: number, number?: string) => string;
|
|
20
|
+
export declare const toNonDivisibleNumber: (decimals: number, number: string) => string;
|
|
21
|
+
export declare const scientificNotationToString: (strParam: string) => string;
|
|
22
|
+
export declare const formatWithCommas: (value: string) => string;
|
|
23
|
+
export declare const toPrecision: (number: string, precision: number, withCommas?: boolean, atLeastOne?: boolean) => string;
|
|
24
|
+
export declare const transformTransactions: (transactions: Transaction[], AccountId: string) => {
|
|
25
|
+
signerId: string;
|
|
26
|
+
receiverId: string;
|
|
27
|
+
actions: {
|
|
28
|
+
type: string;
|
|
29
|
+
params: {
|
|
30
|
+
methodName: string;
|
|
31
|
+
args: object;
|
|
32
|
+
gas: string;
|
|
33
|
+
deposit: string;
|
|
34
|
+
};
|
|
35
|
+
}[];
|
|
36
|
+
}[];
|
|
37
|
+
export declare const WalletSelectorTransactions: (transactions: Transaction[], AccountId: string) => SignAndSendTransactionsParams;
|
|
38
|
+
export declare const separateRoutes: (actions: EstimateSwapView[], outputToken: string) => EstimateSwapView[][];
|
|
39
|
+
export declare const calculateExchangeRate: (from: string, to: string, precision?: number | undefined) => string;
|
|
40
|
+
export declare const getAvgFee: (estimates: EstimateSwapView[], outputToken: string, parsedAmountIn: string) => number;
|
|
41
|
+
export declare const getAccountName: (AccountId: string) => string;
|
|
42
|
+
export declare const symbolsArr: string[];
|
|
43
|
+
export declare const multiply: (factor1: string, factor2: string) => string;
|
|
44
|
+
export declare const toInternationalCurrencySystemLongString: (labelValue: string, percent?: number | undefined) => string;
|
|
45
|
+
export declare const percentOfBigNumber: (percent: number, num: number | string, precision: number) => string;
|
|
46
|
+
export declare const toRealSymbol: (symbol: string) => string;
|
|
47
|
+
export declare const calculateFeeCharge: (fee: number, total: string) => number | number[] | number[][] | math.Matrix | math.Fraction | math.BigNumber | math.Complex;
|
|
48
|
+
export declare const calculateFeePercent: (fee: number) => number;
|
|
49
|
+
export declare function getExpectedOutputFromSwapTodos(estimates: EstimateSwapView[], outputToken: string): Big;
|
|
50
|
+
export declare const calculateAmountReceived: (pool: Pool, amountIn: string, tokenIn: TokenMetadata, tokenOut: TokenMetadata) => import("decimal.js").default;
|
|
51
|
+
export declare const calculateMarketPrice: (pool: Pool, tokenIn: TokenMetadata, tokenOut: TokenMetadata) => any;
|
|
52
|
+
export declare const calculateSmartRoutingPriceImpact: (tokenInAmount: string, swapTodos: EstimateSwapView[], tokenIn: TokenMetadata, tokenMid: TokenMetadata, tokenOut: TokenMetadata, stablePools: StablePool[]) => string;
|
|
53
|
+
export declare const percent: (numerator: string, denominator: string) => any;
|
|
54
|
+
export declare const calcStableSwapPriceImpact: (from: string, to: string, marketPrice?: string) => string;
|
|
55
|
+
export declare const calculatePriceImpact: (pools: Pool[], tokenIn: TokenMetadata, tokenOut: TokenMetadata, tokenInAmount: string) => string;
|
|
56
|
+
export declare function calculateSmartRoutesV2PriceImpact(actions: any, outputToken: string, tokenInPara: TokenMetadata, stablePools: StablePool[]): string;
|
|
57
|
+
export declare const getPriceImpact: ({ estimates, tokenIn, tokenOut, amountIn, amountOut, stablePools, }: {
|
|
58
|
+
estimates: EstimateSwapView[];
|
|
59
|
+
tokenIn: TokenMetadata;
|
|
60
|
+
tokenOut: TokenMetadata;
|
|
61
|
+
amountIn: string;
|
|
62
|
+
amountOut: string;
|
|
63
|
+
stablePools: StablePool[];
|
|
64
|
+
}) => string;
|
|
65
|
+
export declare const subtraction: (initialValue: string, toBeSubtract: string) => string;
|
|
66
|
+
export declare function getPoolAllocationPercents(pools: Pool[]): string[];
|
|
67
|
+
export declare const isMobile: () => boolean;
|
|
68
|
+
export declare function divide(numerator: string, denominator: string): string;
|
|
69
|
+
export declare const getMax: (id: string, amount: string) => string;
|
|
70
|
+
export declare function getPointByPrice(pointDelta: number, price: string, decimalRate: number, noNeedSlot?: boolean): number;
|
|
71
|
+
export declare const feeToPointDelta: (fee: number) => 1 | 8 | 40 | 200;
|
|
72
|
+
export declare const priceToPoint: ({ tokenA, tokenB, amountA, amountB, fee, }: {
|
|
73
|
+
tokenA: TokenMetadata;
|
|
74
|
+
tokenB: TokenMetadata;
|
|
75
|
+
amountA: string;
|
|
76
|
+
amountB: string;
|
|
77
|
+
fee: number;
|
|
78
|
+
}) => number;
|
|
79
|
+
export declare const pointToPrice: ({ tokenA, tokenB, point, }: {
|
|
80
|
+
tokenA: TokenMetadata;
|
|
81
|
+
tokenB: TokenMetadata;
|
|
82
|
+
point: number;
|
|
83
|
+
}) => string;
|
|
84
|
+
export declare const registerAccountOnToken: (AccountId: string) => {
|
|
85
|
+
methodName: string;
|
|
86
|
+
args: {
|
|
87
|
+
registration_only: boolean;
|
|
88
|
+
account_id: string;
|
|
89
|
+
};
|
|
90
|
+
gas: string;
|
|
91
|
+
amount: string;
|
|
92
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { TokenMetadata, EstimateSwapView, Transaction } from '../types';
|
|
2
|
+
export declare const instantSwap: ({ tokenIn, tokenOut, amountIn, slippageTolerance, swapTodos, AccountId, }: {
|
|
3
|
+
tokenIn: TokenMetadata;
|
|
4
|
+
tokenOut: TokenMetadata;
|
|
5
|
+
amountIn: string;
|
|
6
|
+
slippageTolerance: number;
|
|
7
|
+
swapTodos: EstimateSwapView[];
|
|
8
|
+
AccountId: string;
|
|
9
|
+
}) => Promise<Transaction[]>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import { Pool } from '../types';
|
|
3
|
+
interface FormatedPool extends Pool {
|
|
4
|
+
x?: string;
|
|
5
|
+
y?: string;
|
|
6
|
+
gamma_bps?: Big;
|
|
7
|
+
}
|
|
8
|
+
/** formatPoolNew
|
|
9
|
+
* This function appends to the existing standard Pool struct and adds attributes that simplify the parallel swap algorithms.
|
|
10
|
+
* Adds attributes "x" (for input token reserves in pool), "y" (for output token reserves in pool), and "gamma_bps" (for 1- fee in bps)
|
|
11
|
+
* Our convention for our algorithm has been to use "x" as the input token and "y" as the output token.
|
|
12
|
+
* @param pool AMM structure containing reserves of inputToken and outputToken
|
|
13
|
+
* @param inputToken the name of the inputToken being traded in.
|
|
14
|
+
* @param outputToken the name of the outputToken being traded out.
|
|
15
|
+
* @returns newFormatPool
|
|
16
|
+
*/
|
|
17
|
+
export declare function formatPoolNew(pool: Pool, inputToken: string, outputToken: string): FormatedPool;
|
|
18
|
+
/** solveForMuFloat
|
|
19
|
+
* This function takes the set of token pools, the total input of inputToken, and the names of inputToken and outputToken and
|
|
20
|
+
* solves for the Lagrange Multiplier "mu". Note that mu must be allowed to be aritrary precision floating point number. Mu will
|
|
21
|
+
* be used in subsequent function calls to determine the best allocations of intputToken to be made per pool.
|
|
22
|
+
* For more detailed math on how this function was derived, please see the white paper:
|
|
23
|
+
* https://github.com/giddyphysicist/ParallelSwapForRefFinance/blob/main/ParallelSwapWhitePaper.pdf
|
|
24
|
+
* @param pools list of pools that contain inputToken and outputToken
|
|
25
|
+
* @param totalDeltaX total allocation (among all pools) being input of inputToken
|
|
26
|
+
* @param inputToken the name of the inputToken being traded in.
|
|
27
|
+
* @param outputToken the name of the outputToken being traded out.
|
|
28
|
+
* @returns mu the lagrange multiplier value calculated for a set of pools and inputToken amount.
|
|
29
|
+
*/
|
|
30
|
+
export declare function solveForMuFloat(pools: Pool[], totalDeltaX: string, inputToken: string, outputToken: string): number | Big;
|
|
31
|
+
/** calculate_dx_float
|
|
32
|
+
* Once mu has been calculated for a set of pools and total input amount, the next step is
|
|
33
|
+
* determining the total allocation per pool. This function evaluates the amount of input Token to be
|
|
34
|
+
* allocated to the given pool. Note, in our original algorithmic convention, the 'x' variable was for the input token,
|
|
35
|
+
* and the 'y' variable was for the output token. Here, the value dx is the part of the full amount of input token X.
|
|
36
|
+
* Again, the detailed formulae for these operations can be found in the white paper referenced above.
|
|
37
|
+
* @param mu the lagrange multiplier value calculated for a set of pools and inputToken amount.
|
|
38
|
+
* @param pool AMM structure containing reserves of inputToken and outputToken
|
|
39
|
+
* @param inputToken the name of the inputToken being traded in.
|
|
40
|
+
* @param outputToken the name of the outputToken being traded out.
|
|
41
|
+
* @returns dxFloat the allocation amount determined for the given pool
|
|
42
|
+
*/
|
|
43
|
+
export declare function calculate_dx_float(mu: number | Big, pool: FormatedPool, inputToken: string, outputToken: string): Big;
|
|
44
|
+
/** calculate_dy_float
|
|
45
|
+
* Once you have an allocation amount for a given pool, you can use the AMM constant-product formula to determine
|
|
46
|
+
* the expected output amount of output Token.
|
|
47
|
+
* Note, here, as earlier, our algorithmic convention uses "y" as the output token, and so "dy" is the fraction of
|
|
48
|
+
* the total output of output Token, assuming there could be dy contributions from other parallel pools as well.
|
|
49
|
+
* @param dx_float input allocation amount of inputToken for the given pool
|
|
50
|
+
* @param pool a structure representing the reserves and fees for a given pool.
|
|
51
|
+
* @param inputToken the name of the inputToken being traded in.
|
|
52
|
+
* @param outputToken the name of the outputToken being traded out.
|
|
53
|
+
* @returns dyFloat the expected trade out amount out of outputToken
|
|
54
|
+
*/
|
|
55
|
+
export declare function calculate_dy_float(dx_float: number, pool: FormatedPool, inputToken: string, outputToken: string): Big;
|
|
56
|
+
/** calculateOptimalOutput
|
|
57
|
+
* This is the main function, which calculates optimal values of inputToken to swap into each pool.
|
|
58
|
+
* @param pools list of relevant AMM pools containing inputToken and outputToken
|
|
59
|
+
* @param inputAmount the numeric total amount of inputToken to be traded into the group of swap pools.
|
|
60
|
+
* @param inputToken the name of the inputToken being traded in.
|
|
61
|
+
* @param outputToken the name of the outputToken being traded out.
|
|
62
|
+
* @returns normalizedDxArray an array containing the amount allocations of inputToken per pool in the list of pools.
|
|
63
|
+
*/
|
|
64
|
+
export declare function calculateOptimalOutput(pools: Pool[], inputAmount: string, inputToken: string, outputToken: string): bigint[];
|
|
65
|
+
/** reducePools
|
|
66
|
+
* This function is used to implement part of the non-linear slack variables in the lagrange - multiplier
|
|
67
|
+
* solution for parallel swap. Part of what comes out of the math is that sometimes, the optimal allocation for a pool
|
|
68
|
+
* can be negative, which makes no physical sense. When this occurs, that particular pool needs to be flagged and the
|
|
69
|
+
* lagrange constraint applied to force the allocation to be zero.
|
|
70
|
+
* This function takes an already-solved set of pools, input allocation per pool, the total input amount, and the
|
|
71
|
+
* inputToken name and outputToken name, and determines which, if any, allocations need to be set to zero.
|
|
72
|
+
* However, when this occurs, and a pool is essentially ignored from the list, then the calculation for mu must be re-done.
|
|
73
|
+
* So the calculateOptimalOutput function is then called on the reduced set of pools, and if no negative allocation values remain,
|
|
74
|
+
* then the allocations on the reduced set is determined, and values of zero are put in for the 'failed' pools.
|
|
75
|
+
* @param pools list of pools that contain inputToken and outputToken
|
|
76
|
+
* @param dxArray list of input allocation per pool
|
|
77
|
+
* @param inputAmount total amount of inputToken to be traded among the pools
|
|
78
|
+
* @param inputToken the name of the inputToken
|
|
79
|
+
* @param outputToken the name of the outputToken
|
|
80
|
+
* @returns newFullDxVec the new full list of input allocations the same length as dxArray, containing zeros for failed pools.
|
|
81
|
+
*/
|
|
82
|
+
export declare function reducePools(pools: Pool[], dxArray: Big[], inputAmount: string, inputToken: string, outputToken: string): any[];
|
|
83
|
+
export declare function checkIntegerSumOfAllocations(allocations: Big[] | string[] | BigInt[], totalInput: Big | string | BigInt): any[];
|
|
84
|
+
export {};
|