@scallop-io/scallop-swap-sdk 3.0.0 → 4.1.0
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/README.md +13 -5
- package/dist/{base-DhVMdOkK.d.ts → base-D9ApraXl.d.cts} +2 -1
- package/dist/base-D9ApraXl.d.ts +217 -0
- package/dist/chunk-3CBW5I4K.cjs +2 -0
- package/dist/chunk-5N4RDKGD.js +1 -0
- package/dist/chunk-PUC4ABCH.js +2 -0
- package/dist/chunk-YKJ7DP7U.cjs +1 -0
- package/dist/index.cjs +1 -38
- package/dist/index.d.cts +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +1 -1
- package/dist/sdk/7k.cjs +1 -10
- package/dist/sdk/7k.d.cts +1 -36
- package/dist/sdk/7k.d.ts +1 -36
- package/dist/sdk/7k.js +0 -8
- package/dist/sdk/aftermath.cjs +1 -12
- package/dist/sdk/aftermath.d.cts +7 -6
- package/dist/sdk/aftermath.d.ts +7 -6
- package/dist/sdk/aftermath.js +1 -10
- package/dist/sdk/cetus.cjs +2 -17
- package/dist/sdk/cetus.d.cts +8 -4
- package/dist/sdk/cetus.d.ts +8 -4
- package/dist/sdk/cetus.js +2 -11
- package/dist/sdk/flowx.cjs +1 -13
- package/dist/sdk/flowx.d.cts +2 -2
- package/dist/sdk/flowx.d.ts +2 -2
- package/dist/sdk/flowx.js +1 -11
- package/package.json +34 -35
- package/src/class/aggregator.ts +4 -0
- package/src/class/base.ts +3 -1
- package/src/example/index.ts +2 -2
- package/src/index.ts +1 -2
- package/src/sdk/7k.ts +224 -224
- package/src/sdk/aftermath.ts +30 -12
- package/src/sdk/cetus.ts +22 -8
- package/src/sdk/flowx.ts +1 -1
- package/src/sdk/index.ts +2 -3
- package/src/sdk/util.ts +10 -4
- package/dist/chunk-DOMXI4CB.js +0 -3
- package/dist/chunk-RJTK6ARK.cjs +0 -5
- package/dist/chunk-SOD66EHR.cjs +0 -18
- package/dist/chunk-YXTM7AVE.js +0 -9
package/README.md
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
### Scallop Swap Meta Aggregator SDK
|
|
2
2
|
|
|
3
|
-
A unified SDK that aggregates existing swap aggregators on Sui, including Aftermath, Cetus,
|
|
3
|
+
A unified SDK that aggregates existing swap aggregators on Sui, including Aftermath, Cetus, and FlowX and provides a generic `SwapSdkBase` class for seamless integration and extension with future swap aggregators.
|
|
4
4
|
|
|
5
5
|
#### Adding a Custom SDK Integration
|
|
6
6
|
|
|
7
7
|
Extend `SwapSdkBase` with your own SDK-specific generic types:
|
|
8
8
|
|
|
9
9
|
```typescript
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import {
|
|
11
|
+
SwapSdkBase,
|
|
12
|
+
SwapSdkConstructorParams,
|
|
13
|
+
} from '@scallop-io/scallop-swap-sdk';
|
|
14
|
+
|
|
15
|
+
class MyDexSwap extends SwapSdkBase<
|
|
16
|
+
MyPoolType,
|
|
17
|
+
MyRawRoute,
|
|
18
|
+
MyFetchSettings,
|
|
19
|
+
MyClient
|
|
20
|
+
> {
|
|
13
21
|
constructor(params: SwapSdkConstructorParams<MyFetchSettings>) {
|
|
14
22
|
super('mydex', params);
|
|
15
23
|
this.client = new MyClient(/* ... */);
|
|
16
24
|
}
|
|
17
25
|
|
|
18
|
-
// Implement abstract methods: fetchRoute, buildSwapTransaction, setFetchRouteSettings, calcSlippage
|
|
26
|
+
// Implement abstract methods: fetchRoute, buildSwapTransaction, setFetchRouteSettings, calcSlippage, ensureClient
|
|
19
27
|
}
|
|
20
28
|
```
|
|
21
29
|
|
|
@@ -181,6 +181,7 @@ declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any,
|
|
|
181
181
|
fetchSettings?: FetchRouteSettings;
|
|
182
182
|
poolsMap: Map<PoolType, boolean>;
|
|
183
183
|
constructor(name: string, params: SwapSdkConstructorParams<FetchRouteSettings>);
|
|
184
|
+
abstract ensureClient(): Promise<SdkClient>;
|
|
184
185
|
get pools(): PoolType[];
|
|
185
186
|
protected validateWalletAddress(): asserts this is typeof SwapSdkBase & {
|
|
186
187
|
walletAddress: string;
|
|
@@ -213,4 +214,4 @@ declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any,
|
|
|
213
214
|
}>;
|
|
214
215
|
}
|
|
215
216
|
|
|
216
|
-
export { type AggregatorEvents as A, type Coin as C, type FetchRouteParams as F, SwapSdkBase as S, TypedEventEmitter as T, type
|
|
217
|
+
export { type AggregatorEvents as A, type Coin as C, type FetchRouteParams as F, SwapSdkBase as S, TypedEventEmitter as T, type SwapSdkConstructorParams as a, type FetchRouteResult as b, type SwapParams as c, type FormattedRouteResult as d, type SwapRoute as e, type FetchRoutesOptions as f, type SwapBuildResult as g, type AggregatorEvent as h, type AggregatorStatusEvent as i, type AllSwapSdkPoolStatusEvent as j, type SlippageChangedEvent as k, type SwapCoinInfo as l, type SwapPath as m, type SwapSdkBaseInterface as n, type SwapSdkEvents as o, type SwapSdkPoolStatusEvent as p };
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import * as _mysten_sui_client from '@mysten/sui/client';
|
|
2
|
+
import { ClientWithCoreApi } from '@mysten/sui/client';
|
|
3
|
+
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
4
|
+
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
|
|
7
|
+
interface Coin {
|
|
8
|
+
type: string;
|
|
9
|
+
amount: bigint;
|
|
10
|
+
}
|
|
11
|
+
interface SwapCoinInfo {
|
|
12
|
+
coinIn: Coin;
|
|
13
|
+
coinOut: Coin;
|
|
14
|
+
}
|
|
15
|
+
interface SwapPath extends SwapCoinInfo {
|
|
16
|
+
protocolName: string;
|
|
17
|
+
}
|
|
18
|
+
interface SwapRoute extends SwapCoinInfo {
|
|
19
|
+
paths: SwapPath[];
|
|
20
|
+
splitPercentage?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Common interface for route result (for frontend display purposes)
|
|
24
|
+
*/
|
|
25
|
+
interface FormattedRouteResult {
|
|
26
|
+
routes: SwapRoute[];
|
|
27
|
+
coinIn: Coin;
|
|
28
|
+
coinOut: Coin;
|
|
29
|
+
name: string;
|
|
30
|
+
}
|
|
31
|
+
type FetchRouteResult<T> = {
|
|
32
|
+
rawRouteResult: T;
|
|
33
|
+
formattedResult: FormattedRouteResult;
|
|
34
|
+
};
|
|
35
|
+
type FetchRoutesOptions = {
|
|
36
|
+
onRouteFound?: (route: FetchRouteResult<any>) => void;
|
|
37
|
+
onError?: (error: {
|
|
38
|
+
name: string;
|
|
39
|
+
reason: unknown;
|
|
40
|
+
}) => void;
|
|
41
|
+
abortSignal?: AbortSignal;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type FetchRouteParams = {
|
|
45
|
+
coinInType: string;
|
|
46
|
+
coinOutType: string;
|
|
47
|
+
swapAmount: bigint;
|
|
48
|
+
};
|
|
49
|
+
type SwapParams<T> = {
|
|
50
|
+
route?: FetchRouteResult<T>;
|
|
51
|
+
/**
|
|
52
|
+
* In decimal, e.g. 0.01 for 1% slippage
|
|
53
|
+
*/
|
|
54
|
+
slippage: number;
|
|
55
|
+
minAmountOut?: bigint;
|
|
56
|
+
txExtensionParams?: {
|
|
57
|
+
initTx: Transaction;
|
|
58
|
+
coinIn: TransactionObjectArgument;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
type SwapBuildResult<MergeResult extends boolean> = MergeResult extends true ? {
|
|
62
|
+
tx: Transaction;
|
|
63
|
+
coinOutType: string;
|
|
64
|
+
} : {
|
|
65
|
+
tx: Transaction;
|
|
66
|
+
coinOutType: string;
|
|
67
|
+
coinOut: TransactionObjectArgument;
|
|
68
|
+
};
|
|
69
|
+
interface SwapSdkConstructorParams<T> {
|
|
70
|
+
suiClient?: ClientWithCoreApi;
|
|
71
|
+
fetchTimeoutInMs?: number;
|
|
72
|
+
walletAddress: string;
|
|
73
|
+
fetchSettings?: T;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Base interface for Swap SDK
|
|
77
|
+
*/
|
|
78
|
+
interface SwapSdkBaseInterface<PoolType = string, RawRouteResultType = any, FetchRouteSettings = any, SdkClient = ClientWithCoreApi> {
|
|
79
|
+
/**
|
|
80
|
+
* SDK specific client
|
|
81
|
+
*/
|
|
82
|
+
client?: SdkClient;
|
|
83
|
+
/**
|
|
84
|
+
* How long the route fetch request should wait before timing out (in milliseconds)
|
|
85
|
+
*/
|
|
86
|
+
fetchTimeoutInMs?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Pools or DEXes available in the SDK
|
|
89
|
+
*/
|
|
90
|
+
pools?: PoolType[];
|
|
91
|
+
/**
|
|
92
|
+
* Map of pools or DEXes available in the SDK for quick lookup
|
|
93
|
+
*/
|
|
94
|
+
poolsMap?: Map<PoolType, boolean>;
|
|
95
|
+
/**
|
|
96
|
+
* Fetch route settings based on SDK-specific settings
|
|
97
|
+
*/
|
|
98
|
+
fetchSettings?: FetchRouteSettings;
|
|
99
|
+
/**
|
|
100
|
+
* Set fetch route settings based on SDK-specific settings
|
|
101
|
+
* @param settings
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
setFetchRouteSettings: (settings: FetchRouteSettings) => void;
|
|
105
|
+
togglePoolStatus: (pool: PoolType, enabled?: boolean) => void;
|
|
106
|
+
toggleAllPoolStatus: (enabled: boolean) => void;
|
|
107
|
+
setSuiClient: (client: SuiGrpcClient) => void;
|
|
108
|
+
setWalletAddress: (address: string) => void;
|
|
109
|
+
/**
|
|
110
|
+
* Calculate slippage for the specific protocol.
|
|
111
|
+
* Each protocol may have different way to represent slippage, e.g. in decimal, in bps, etc.
|
|
112
|
+
* This function will convert a unified slippage input (in decimal) to the protocol specific slippage value.
|
|
113
|
+
* @param slippageInDecimal
|
|
114
|
+
* @returns SDK specific slippage value
|
|
115
|
+
*/
|
|
116
|
+
calcSlippage: (slippageInDecimal: number) => number;
|
|
117
|
+
fetchRoute: (params: FetchRouteParams) => Promise<FetchRouteResult<RawRouteResultType>>;
|
|
118
|
+
/**
|
|
119
|
+
* Select coinIn for swap transaction
|
|
120
|
+
* @param tx
|
|
121
|
+
* @param amount
|
|
122
|
+
* @param coinType
|
|
123
|
+
* @returns Promise<TransactionObjectArgument>
|
|
124
|
+
*/
|
|
125
|
+
selectCoinInForSwap: (tx: Transaction, amount: bigint, coinType: string) => Promise<TransactionObjectArgument>;
|
|
126
|
+
buildSwapTransaction: (params: SwapParams<RawRouteResultType>) => Promise<{
|
|
127
|
+
tx: Transaction;
|
|
128
|
+
coinOut: TransactionObjectArgument;
|
|
129
|
+
}>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
type SwapSdkPoolStatusEvent<PoolType> = {
|
|
133
|
+
name: string;
|
|
134
|
+
pool: PoolType;
|
|
135
|
+
enabled: boolean;
|
|
136
|
+
};
|
|
137
|
+
type AllSwapSdkPoolStatusEvent = {
|
|
138
|
+
name: string;
|
|
139
|
+
enabled: boolean;
|
|
140
|
+
};
|
|
141
|
+
type SwapSdkEvents<PoolType> = {
|
|
142
|
+
poolStatusChanged: SwapSdkPoolStatusEvent<PoolType>;
|
|
143
|
+
allPoolStatusChanged: AllSwapSdkPoolStatusEvent;
|
|
144
|
+
};
|
|
145
|
+
type SlippageChangedEvent = {
|
|
146
|
+
slippageInDecimal: number;
|
|
147
|
+
};
|
|
148
|
+
type AggregatorStatusEvent = {
|
|
149
|
+
name: string;
|
|
150
|
+
enabled: boolean;
|
|
151
|
+
};
|
|
152
|
+
type AggregatorEvent = {
|
|
153
|
+
name: string;
|
|
154
|
+
};
|
|
155
|
+
type AggregatorEvents<PoolType> = {
|
|
156
|
+
poolStatusChanged: SwapSdkPoolStatusEvent<PoolType>;
|
|
157
|
+
allPoolStatusChanged: AllSwapSdkPoolStatusEvent;
|
|
158
|
+
aggregatorStatusChanged: AggregatorStatusEvent;
|
|
159
|
+
aggregatorRemoved: AggregatorEvent;
|
|
160
|
+
aggregatorAdded: AggregatorEvent;
|
|
161
|
+
aggregatorSlippageChanged: SlippageChangedEvent;
|
|
162
|
+
aggregatorFetchStart: AggregatorEvent;
|
|
163
|
+
aggregatorFetchEnd: AggregatorEvent;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
declare class TypedEventEmitter<Events extends Record<string, any>> extends EventEmitter {
|
|
167
|
+
on<K extends keyof Events & (string | symbol)>(event: K, listener: (payload: Events[K]) => void): this;
|
|
168
|
+
once<K extends keyof Events & (string | symbol)>(event: K, listener: (payload: Events[K]) => void): this;
|
|
169
|
+
off<K extends keyof Events & (string | symbol)>(event: K, listener: (payload: Events[K]) => void): this;
|
|
170
|
+
emit<K extends keyof Events & (string | symbol)>(event: K, payload: Events[K]): boolean;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
declare abstract class SwapSdkBase<PoolType = string, RawRouteResultType = any, FetchRouteSettings = any, SdkClient = any> implements SwapSdkBaseInterface<PoolType, RawRouteResultType, FetchRouteSettings, SdkClient> {
|
|
174
|
+
protected rawRouteResult?: RawRouteResultType;
|
|
175
|
+
readonly name: string;
|
|
176
|
+
readonly events: TypedEventEmitter<SwapSdkEvents<PoolType>>;
|
|
177
|
+
client?: SdkClient;
|
|
178
|
+
suiClient: ClientWithCoreApi;
|
|
179
|
+
fetchTimeoutInMs?: number;
|
|
180
|
+
walletAddress: string;
|
|
181
|
+
fetchSettings?: FetchRouteSettings;
|
|
182
|
+
poolsMap: Map<PoolType, boolean>;
|
|
183
|
+
constructor(name: string, params: SwapSdkConstructorParams<FetchRouteSettings>);
|
|
184
|
+
abstract ensureClient(): Promise<SdkClient>;
|
|
185
|
+
get pools(): PoolType[];
|
|
186
|
+
protected validateWalletAddress(): asserts this is typeof SwapSdkBase & {
|
|
187
|
+
walletAddress: string;
|
|
188
|
+
};
|
|
189
|
+
protected validateClient(): asserts this is typeof SwapSdkBase & {
|
|
190
|
+
client: SdkClient;
|
|
191
|
+
};
|
|
192
|
+
setWalletAddress(address: string): void;
|
|
193
|
+
setSuiClient(client: ClientWithCoreApi): void;
|
|
194
|
+
abstract calcSlippage(slippageInDecimal: number): number;
|
|
195
|
+
togglePoolStatus(pool: PoolType, enabled?: boolean): void;
|
|
196
|
+
toggleAllPoolStatus(enabled: boolean): void;
|
|
197
|
+
getEnabledPools(): PoolType[];
|
|
198
|
+
protected normalizeFetchRouteParams(params: FetchRouteParams): FetchRouteParams;
|
|
199
|
+
protected withTimeout<T>(fn: Promise<T>, abortSignal?: AbortSignal): Promise<T>;
|
|
200
|
+
selectCoinInForSwap(tx: Transaction, amount: bigint, coinType: string): Promise<{
|
|
201
|
+
NestedResult: [number, number];
|
|
202
|
+
$kind: "NestedResult";
|
|
203
|
+
} | {
|
|
204
|
+
$kind: "Input";
|
|
205
|
+
Input: number;
|
|
206
|
+
type?: "object";
|
|
207
|
+
}>;
|
|
208
|
+
protected selectCoins(coinType: string, amount: string): Promise<_mysten_sui_client.SuiClientTypes.Coin[]>;
|
|
209
|
+
abstract setFetchRouteSettings(settings: FetchRouteSettings): void;
|
|
210
|
+
abstract fetchRoute(params: FetchRouteParams, abortSignal?: AbortSignal): Promise<FetchRouteResult<RawRouteResultType>>;
|
|
211
|
+
abstract buildSwapTransaction(params: SwapParams<RawRouteResultType>): Promise<{
|
|
212
|
+
tx: Transaction;
|
|
213
|
+
coinOut: TransactionObjectArgument;
|
|
214
|
+
}>;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export { type AggregatorEvents as A, type Coin as C, type FetchRouteParams as F, SwapSdkBase as S, TypedEventEmitter as T, type SwapSdkConstructorParams as a, type FetchRouteResult as b, type SwapParams as c, type FormattedRouteResult as d, type SwapRoute as e, type FetchRoutesOptions as f, type SwapBuildResult as g, type AggregatorEvent as h, type AggregatorStatusEvent as i, type AllSwapSdkPoolStatusEvent as j, type SlippageChangedEvent as k, type SwapCoinInfo as l, type SwapPath as m, type SwapSdkBaseInterface as n, type SwapSdkEvents as o, type SwapSdkPoolStatusEvent as p };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var utils=require('@mysten/sui/utils'),bignumber_js=require('bignumber.js'),events=require('events'),grpc=require('@mysten/sui/grpc'),suiKit=require('@scallop-io/sui-kit');var A=utils.normalizeStructTag(utils.SUI_TYPE_ARG),p=n=>utils.normalizeStructTag(n)===A,f=async(n,t,e,r)=>{let i=[],s=bignumber_js.BigNumber(e),l=null;try{do{let{objects:o,hasNextPage:a,cursor:c}=await n.core.listCoins({coinType:t,owner:r,limit:50,cursor:l});if(i.push(...o),s=s.minus(o.reduce((v,R)=>v.plus(bignumber_js.BigNumber(R.balance)),bignumber_js.BigNumber(0))),s.lte(0)||(l=c??null,!a))break}while(l)}catch(o){throw new Error(o?.message??"Failed to fetch coins")}if(i.length===0)throw new Error(`No available coin for type ${t} found in wallet ${r}`);if(s.gt(0))throw new Error(`Insufficient balance for coin type ${t} in wallet ${r}`);return i},S=async(n,t,e,r,i)=>{try{if(p(r))n.mergeCoins(n.gas,[t]);else {let{objects:s}=await e.core.listCoins({coinType:r,owner:i,limit:1});if(!s.length)throw new Error(`No existing ${r} coin found to merge with`);let l=n.objectRef({objectId:s[0].objectId,version:s[0].version,digest:s[0].digest});s.length===1?n.mergeCoins(l,[t]):n.mergeCoins(l,[t,...s.slice(1).map(o=>n.objectRef({objectId:o.objectId,version:o.version,digest:o.digest}))]);}}catch{n.transferObjects([t],n.pure.address(i));}},b=n=>n[0].toUpperCase()+n.slice(1),O=n=>n.split("_").map(b).join(" ");var u=class extends events.EventEmitter{on(t,e){return super.on(t,e)}once(t,e){return super.once(t,e)}off(t,e){return super.off(t,e)}emit(t,e){return super.emit(t,e)}};var y=class{rawRouteResult;name="";events=new u;client;suiClient;fetchTimeoutInMs;walletAddress;fetchSettings;poolsMap=new Map;constructor(t,e){this.suiClient=e.suiClient??new grpc.SuiGrpcClient({baseUrl:"https://fullnode.mainnet.sui.io:443",network:"mainnet"}).core,this.fetchTimeoutInMs=e.fetchTimeoutInMs??4e3,this.walletAddress=e.walletAddress,this.name=t.toLowerCase(),e.fetchSettings&&this.setFetchRouteSettings(e.fetchSettings);}get pools(){return Array.from(this.poolsMap.keys())}validateWalletAddress(){if(!this.walletAddress||this.walletAddress.length===0)throw new Error("Wallet address is not set")}validateClient(){if(!this.client)throw new Error("SDK client is not set")}setWalletAddress(t){this.walletAddress=t;}setSuiClient(t){this.suiClient=t;}togglePoolStatus(t,e){let r=e??!(this.poolsMap.get(t)??true);this.poolsMap.set(t,r),this.events.emit("poolStatusChanged",{name:this.name,pool:t,enabled:r});}toggleAllPoolStatus(t){this.pools.forEach(e=>{this.poolsMap.set(e,t);}),this.events.emit("allPoolStatusChanged",{name:this.name,enabled:t});}getEnabledPools(){return Array.from(this.poolsMap.entries()).filter(([t,e])=>e).map(([t,e])=>t)}normalizeFetchRouteParams(t){return {...t,coinInType:utils.normalizeStructTag(t.coinInType),coinOutType:utils.normalizeStructTag(t.coinOutType)}}async withTimeout(t,e){let r,i,s=[t,new Promise((l,o)=>{r=setTimeout(()=>{o(new Error(`${this.name} fetch route timeout`));},this.fetchTimeoutInMs);})];e&&s.push(new Promise((l,o)=>{if(e.aborted){o(e.reason??new Error(`${this.name} fetch route aborted`));return}i=()=>o(e.reason??new Error(`${this.name} fetch route aborted`)),e.addEventListener("abort",i,{once:true});}));try{return await Promise.race(s)}finally{r&&clearTimeout(r),e&&i&&e.removeEventListener("abort",i);}}async selectCoinInForSwap(t,e,r){let i=bignumber_js.BigNumber(e.toString());if(p(r)){let[s]=t.splitCoins(t.gas,[i.toString()]);return s}else {let s=await this.selectCoins(r,i.toString()),l=s.reduce((a,c)=>a.plus(bignumber_js.BigNumber(c.balance)),bignumber_js.BigNumber(0)),o=t.objectRef({objectId:s[0].objectId,version:s[0].version,digest:s[0].digest});if(s.length>1&&t.mergeCoins(o,s.slice(1).map(a=>t.objectRef({objectId:a.objectId,version:a.version,digest:a.digest}))),l.gt(i)){let[a]=t.splitCoins(o,[i.toString()]);return a}else return o}}async selectCoins(t,e){return this.validateWalletAddress(),await f(this.suiClient,t,e,this.walletAddress)}};var g=class{constructor(t,e){this.client=t;e&&e.forEach(r=>this.cache.set(r.coinType,r));}client;cache=new Map;setClient(t){this.client=t;}async getCoinMetadata(t){let e=utils.normalizeStructTag(t);if(this.cache.has(e))return this.cache.get(e);let{coinMetadata:r}=await this.client.core.getCoinMetadata({coinType:e}),i=r;if(!i)throw new Error(`Coin metadata not found for coin type: ${e}`);return this.cache.set(e,i),i}};var C=class{coinMetadata;suiKit;events=new u;_slippage=.003;signerMode=false;aggregatorMap=new Map;activeAggregator=new Map;_fetchingStatus=new Map;poolStatusForwarders=new Map;allPoolStatusForwarders=new Map;walletAddress;client;constructor(t){if(this.client=t.client??new grpc.SuiGrpcClient({baseUrl:t.fullnodeUrl||"https://fullnode.mainnet.sui.io:443",network:"mainnet"}),this.coinMetadata=new g(this.client),this.signerMode=!!(t.secretKey||t.mnemonics),t.secretKey||t.mnemonics)this.suiKit=new suiKit.SuiKit({...t,suiClients:[this.client]}),this.walletAddress=this.suiKit.currentAddress;else if(t.walletAddress!==void 0)this.walletAddress=t.walletAddress;else throw new Error("One of secretKey, mnemonics or walletAddress must be provided")}async init(){await Promise.all(this.aggregators.map(t=>t.ensureClient()));}forwardPoolStatusChangeEvent(t){this.events.emit("poolStatusChanged",t);}addAggregator(t){for(let e of t)if(!this.aggregatorMap.has(e.name)){this.aggregatorMap.set(e.name,e),this.activeAggregator.set(e.name,true),this._fetchingStatus.set(e.name,false);let r=s=>this.forwardPoolStatusChangeEvent(s);this.poolStatusForwarders.set(e.name,r),e.events.on("poolStatusChanged",r);let i=s=>this.events.emit("allPoolStatusChanged",s);this.allPoolStatusForwarders.set(e.name,i),e.events.on("allPoolStatusChanged",i),this.events.emit("aggregatorAdded",{name:e.name});}}getActiveAggregators(){return Array.from(this.activeAggregator.entries()).filter(([t,e])=>e).map(([t,e])=>t)}getAggregator(t){return this.aggregatorMap.get(t)}toggleAggregator(t,e){if(!this.activeAggregator.has(t))throw new Error(`Aggregator ${t} not found`);this.activeAggregator.set(t,e),this.events.emit("aggregatorStatusChanged",{name:t,enabled:e});}removeAggregator(t){let e=this.aggregatorMap.get(t);if(!e)throw new Error(`Aggregator ${t} not found`);let r=this.poolStatusForwarders.get(t);r&&e.events.off("poolStatusChanged",r),this.poolStatusForwarders.delete(t);let i=this.allPoolStatusForwarders.get(t);i&&e.events.off("allPoolStatusChanged",i),this.allPoolStatusForwarders.delete(t),this.aggregatorMap.delete(t),this.activeAggregator.delete(t),this._fetchingStatus.delete(t),this.events.emit("aggregatorRemoved",{name:t});}get aggregators(){return Array.from(this.aggregatorMap.values())}get fetchingStatus(){return Object.fromEntries(this._fetchingStatus)}get slippage(){return this._slippage}setSlippage(t){this._slippage=t,this.events.emit("aggregatorSlippageChanged",{slippageInDecimal:t});}setFetchTimeoutInMs(t){this.aggregators.forEach(e=>{e.fetchTimeoutInMs=t;});}#t(){if(!this.suiKit)throw new Error("SuiKit is not initialized.")}setSuiClient(t){this.aggregators.forEach(e=>{e.setSuiClient(t);}),this.coinMetadata.setClient(t),this.client=t;}setWalletAddress(t){if(this.signerMode)throw new Error("Cannot change wallet address when SuiKit is initialized with secretKey or mnemonics.");this.walletAddress=t,this.aggregators.forEach(e=>{e.setWalletAddress(t);});}async getCoinMetadata(t){return this.coinMetadata.getCoinMetadata(t)}async fetchRoutes(t,e){let r=this.aggregators.filter(({name:o})=>this.activeAggregator.get(o)===true),i=[],s=[],l=r.map(async o=>{this._fetchingStatus.set(o.name,true),this.events.emit("aggregatorFetchStart",{name:o.name});try{let a=await o.fetchRoute(t,e?.abortSignal);i.push(a),e?.onRouteFound?.(a);}catch(a){let c={name:o.name,reason:a};s.push(c),e?.onError?.(c);}finally{this._fetchingStatus.set(o.name,false),this.events.emit("aggregatorFetchEnd",{name:o.name});}});return await Promise.all(l),{routes:i.sort((o,a)=>bignumber_js.BigNumber(a.formattedResult.coinOut.amount).comparedTo(o.formattedResult.coinOut.amount)??0),errors:s}}async buildRouteTransaction(t,e={}){let{mergeResult:r=true,txExtensionParams:i}=e,s=this.aggregatorMap.get(t.formattedResult.name);if(!s)throw new Error(`Aggregator ${t.formattedResult.name} not found in the aggregator list`);let{tx:l,coinOut:o}=await s.buildSwapTransaction({route:t,slippage:e.slippage??this.slippage,txExtensionParams:i}),a=t.formattedResult.coinOut.type;return r?(await S(l,o,this.client,a,this.walletAddress),{tx:l,coinOutType:a}):{tx:l,coinOut:o,coinOutType:a}}async executeSwapTransaction(t,e=false){return this.#t(),e?(t.setSender(this.walletAddress),this.suiKit.dryRunTxn(t)):this.suiKit.signAndSendTxn(t)}};
|
|
2
|
+
exports.a=p;exports.b=f;exports.c=S;exports.d=O;exports.e=u;exports.f=y;exports.g=g;exports.h=C;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function i(n,r){let e;return async()=>{if(e)return e;try{return e=await r(),e}catch(t){throw t?.code==="ERR_MODULE_NOT_FOUND"||t?.code==="MODULE_NOT_FOUND"?new Error(`${n} is required but not installed. Install it with: pnpm add ${n}`):t}}}export{i as a};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import {normalizeStructTag,SUI_TYPE_ARG}from'@mysten/sui/utils';import {BigNumber}from'bignumber.js';import {EventEmitter}from'events';import {SuiGrpcClient}from'@mysten/sui/grpc';import {SuiKit}from'@scallop-io/sui-kit';var A=normalizeStructTag(SUI_TYPE_ARG),p=n=>normalizeStructTag(n)===A,f=async(n,t,e,r)=>{let i=[],s=BigNumber(e),l=null;try{do{let{objects:o,hasNextPage:a,cursor:c}=await n.core.listCoins({coinType:t,owner:r,limit:50,cursor:l});if(i.push(...o),s=s.minus(o.reduce((v,R)=>v.plus(BigNumber(R.balance)),BigNumber(0))),s.lte(0)||(l=c??null,!a))break}while(l)}catch(o){throw new Error(o?.message??"Failed to fetch coins")}if(i.length===0)throw new Error(`No available coin for type ${t} found in wallet ${r}`);if(s.gt(0))throw new Error(`Insufficient balance for coin type ${t} in wallet ${r}`);return i},S=async(n,t,e,r,i)=>{try{if(p(r))n.mergeCoins(n.gas,[t]);else {let{objects:s}=await e.core.listCoins({coinType:r,owner:i,limit:1});if(!s.length)throw new Error(`No existing ${r} coin found to merge with`);let l=n.objectRef({objectId:s[0].objectId,version:s[0].version,digest:s[0].digest});s.length===1?n.mergeCoins(l,[t]):n.mergeCoins(l,[t,...s.slice(1).map(o=>n.objectRef({objectId:o.objectId,version:o.version,digest:o.digest}))]);}}catch{n.transferObjects([t],n.pure.address(i));}},b=n=>n[0].toUpperCase()+n.slice(1),O=n=>n.split("_").map(b).join(" ");var u=class extends EventEmitter{on(t,e){return super.on(t,e)}once(t,e){return super.once(t,e)}off(t,e){return super.off(t,e)}emit(t,e){return super.emit(t,e)}};var y=class{rawRouteResult;name="";events=new u;client;suiClient;fetchTimeoutInMs;walletAddress;fetchSettings;poolsMap=new Map;constructor(t,e){this.suiClient=e.suiClient??new SuiGrpcClient({baseUrl:"https://fullnode.mainnet.sui.io:443",network:"mainnet"}).core,this.fetchTimeoutInMs=e.fetchTimeoutInMs??4e3,this.walletAddress=e.walletAddress,this.name=t.toLowerCase(),e.fetchSettings&&this.setFetchRouteSettings(e.fetchSettings);}get pools(){return Array.from(this.poolsMap.keys())}validateWalletAddress(){if(!this.walletAddress||this.walletAddress.length===0)throw new Error("Wallet address is not set")}validateClient(){if(!this.client)throw new Error("SDK client is not set")}setWalletAddress(t){this.walletAddress=t;}setSuiClient(t){this.suiClient=t;}togglePoolStatus(t,e){let r=e??!(this.poolsMap.get(t)??true);this.poolsMap.set(t,r),this.events.emit("poolStatusChanged",{name:this.name,pool:t,enabled:r});}toggleAllPoolStatus(t){this.pools.forEach(e=>{this.poolsMap.set(e,t);}),this.events.emit("allPoolStatusChanged",{name:this.name,enabled:t});}getEnabledPools(){return Array.from(this.poolsMap.entries()).filter(([t,e])=>e).map(([t,e])=>t)}normalizeFetchRouteParams(t){return {...t,coinInType:normalizeStructTag(t.coinInType),coinOutType:normalizeStructTag(t.coinOutType)}}async withTimeout(t,e){let r,i,s=[t,new Promise((l,o)=>{r=setTimeout(()=>{o(new Error(`${this.name} fetch route timeout`));},this.fetchTimeoutInMs);})];e&&s.push(new Promise((l,o)=>{if(e.aborted){o(e.reason??new Error(`${this.name} fetch route aborted`));return}i=()=>o(e.reason??new Error(`${this.name} fetch route aborted`)),e.addEventListener("abort",i,{once:true});}));try{return await Promise.race(s)}finally{r&&clearTimeout(r),e&&i&&e.removeEventListener("abort",i);}}async selectCoinInForSwap(t,e,r){let i=BigNumber(e.toString());if(p(r)){let[s]=t.splitCoins(t.gas,[i.toString()]);return s}else {let s=await this.selectCoins(r,i.toString()),l=s.reduce((a,c)=>a.plus(BigNumber(c.balance)),BigNumber(0)),o=t.objectRef({objectId:s[0].objectId,version:s[0].version,digest:s[0].digest});if(s.length>1&&t.mergeCoins(o,s.slice(1).map(a=>t.objectRef({objectId:a.objectId,version:a.version,digest:a.digest}))),l.gt(i)){let[a]=t.splitCoins(o,[i.toString()]);return a}else return o}}async selectCoins(t,e){return this.validateWalletAddress(),await f(this.suiClient,t,e,this.walletAddress)}};var g=class{constructor(t,e){this.client=t;e&&e.forEach(r=>this.cache.set(r.coinType,r));}client;cache=new Map;setClient(t){this.client=t;}async getCoinMetadata(t){let e=normalizeStructTag(t);if(this.cache.has(e))return this.cache.get(e);let{coinMetadata:r}=await this.client.core.getCoinMetadata({coinType:e}),i=r;if(!i)throw new Error(`Coin metadata not found for coin type: ${e}`);return this.cache.set(e,i),i}};var C=class{coinMetadata;suiKit;events=new u;_slippage=.003;signerMode=false;aggregatorMap=new Map;activeAggregator=new Map;_fetchingStatus=new Map;poolStatusForwarders=new Map;allPoolStatusForwarders=new Map;walletAddress;client;constructor(t){if(this.client=t.client??new SuiGrpcClient({baseUrl:t.fullnodeUrl||"https://fullnode.mainnet.sui.io:443",network:"mainnet"}),this.coinMetadata=new g(this.client),this.signerMode=!!(t.secretKey||t.mnemonics),t.secretKey||t.mnemonics)this.suiKit=new SuiKit({...t,suiClients:[this.client]}),this.walletAddress=this.suiKit.currentAddress;else if(t.walletAddress!==void 0)this.walletAddress=t.walletAddress;else throw new Error("One of secretKey, mnemonics or walletAddress must be provided")}async init(){await Promise.all(this.aggregators.map(t=>t.ensureClient()));}forwardPoolStatusChangeEvent(t){this.events.emit("poolStatusChanged",t);}addAggregator(t){for(let e of t)if(!this.aggregatorMap.has(e.name)){this.aggregatorMap.set(e.name,e),this.activeAggregator.set(e.name,true),this._fetchingStatus.set(e.name,false);let r=s=>this.forwardPoolStatusChangeEvent(s);this.poolStatusForwarders.set(e.name,r),e.events.on("poolStatusChanged",r);let i=s=>this.events.emit("allPoolStatusChanged",s);this.allPoolStatusForwarders.set(e.name,i),e.events.on("allPoolStatusChanged",i),this.events.emit("aggregatorAdded",{name:e.name});}}getActiveAggregators(){return Array.from(this.activeAggregator.entries()).filter(([t,e])=>e).map(([t,e])=>t)}getAggregator(t){return this.aggregatorMap.get(t)}toggleAggregator(t,e){if(!this.activeAggregator.has(t))throw new Error(`Aggregator ${t} not found`);this.activeAggregator.set(t,e),this.events.emit("aggregatorStatusChanged",{name:t,enabled:e});}removeAggregator(t){let e=this.aggregatorMap.get(t);if(!e)throw new Error(`Aggregator ${t} not found`);let r=this.poolStatusForwarders.get(t);r&&e.events.off("poolStatusChanged",r),this.poolStatusForwarders.delete(t);let i=this.allPoolStatusForwarders.get(t);i&&e.events.off("allPoolStatusChanged",i),this.allPoolStatusForwarders.delete(t),this.aggregatorMap.delete(t),this.activeAggregator.delete(t),this._fetchingStatus.delete(t),this.events.emit("aggregatorRemoved",{name:t});}get aggregators(){return Array.from(this.aggregatorMap.values())}get fetchingStatus(){return Object.fromEntries(this._fetchingStatus)}get slippage(){return this._slippage}setSlippage(t){this._slippage=t,this.events.emit("aggregatorSlippageChanged",{slippageInDecimal:t});}setFetchTimeoutInMs(t){this.aggregators.forEach(e=>{e.fetchTimeoutInMs=t;});}#t(){if(!this.suiKit)throw new Error("SuiKit is not initialized.")}setSuiClient(t){this.aggregators.forEach(e=>{e.setSuiClient(t);}),this.coinMetadata.setClient(t),this.client=t;}setWalletAddress(t){if(this.signerMode)throw new Error("Cannot change wallet address when SuiKit is initialized with secretKey or mnemonics.");this.walletAddress=t,this.aggregators.forEach(e=>{e.setWalletAddress(t);});}async getCoinMetadata(t){return this.coinMetadata.getCoinMetadata(t)}async fetchRoutes(t,e){let r=this.aggregators.filter(({name:o})=>this.activeAggregator.get(o)===true),i=[],s=[],l=r.map(async o=>{this._fetchingStatus.set(o.name,true),this.events.emit("aggregatorFetchStart",{name:o.name});try{let a=await o.fetchRoute(t,e?.abortSignal);i.push(a),e?.onRouteFound?.(a);}catch(a){let c={name:o.name,reason:a};s.push(c),e?.onError?.(c);}finally{this._fetchingStatus.set(o.name,false),this.events.emit("aggregatorFetchEnd",{name:o.name});}});return await Promise.all(l),{routes:i.sort((o,a)=>BigNumber(a.formattedResult.coinOut.amount).comparedTo(o.formattedResult.coinOut.amount)??0),errors:s}}async buildRouteTransaction(t,e={}){let{mergeResult:r=true,txExtensionParams:i}=e,s=this.aggregatorMap.get(t.formattedResult.name);if(!s)throw new Error(`Aggregator ${t.formattedResult.name} not found in the aggregator list`);let{tx:l,coinOut:o}=await s.buildSwapTransaction({route:t,slippage:e.slippage??this.slippage,txExtensionParams:i}),a=t.formattedResult.coinOut.type;return r?(await S(l,o,this.client,a,this.walletAddress),{tx:l,coinOutType:a}):{tx:l,coinOut:o,coinOutType:a}}async executeSwapTransaction(t,e=false){return this.#t(),e?(t.setSender(this.walletAddress),this.suiKit.dryRunTxn(t)):this.suiKit.signAndSendTxn(t)}};
|
|
2
|
+
export{p as a,f as b,S as c,O as d,u as e,y as f,g,C as h};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';function i(n,r){let e;return async()=>{if(e)return e;try{return e=await r(),e}catch(t){throw t?.code==="ERR_MODULE_NOT_FOUND"||t?.code==="MODULE_NOT_FOUND"?new Error(`${n} is required but not installed. Install it with: pnpm add ${n}`):t}}}exports.a=i;
|
package/dist/index.cjs
CHANGED
|
@@ -1,38 +1 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkSOD66EHR_cjs = require('./chunk-SOD66EHR.cjs');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "Aggregator", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () { return chunkSOD66EHR_cjs.h; }
|
|
10
|
-
});
|
|
11
|
-
Object.defineProperty(exports, "CoinMetadataRegistry", {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: function () { return chunkSOD66EHR_cjs.g; }
|
|
14
|
-
});
|
|
15
|
-
Object.defineProperty(exports, "SwapSdkBase", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function () { return chunkSOD66EHR_cjs.f; }
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(exports, "TypedEventEmitter", {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () { return chunkSOD66EHR_cjs.e; }
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(exports, "isSuiType", {
|
|
24
|
-
enumerable: true,
|
|
25
|
-
get: function () { return chunkSOD66EHR_cjs.a; }
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(exports, "mergeWithExistingOrTransfer", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function () { return chunkSOD66EHR_cjs.c; }
|
|
30
|
-
});
|
|
31
|
-
Object.defineProperty(exports, "selectCoins", {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
get: function () { return chunkSOD66EHR_cjs.b; }
|
|
34
|
-
});
|
|
35
|
-
Object.defineProperty(exports, "transformProperCase", {
|
|
36
|
-
enumerable: true,
|
|
37
|
-
get: function () { return chunkSOD66EHR_cjs.d; }
|
|
38
|
-
});
|
|
1
|
+
'use strict';var chunk3CBW5I4K_cjs=require('./chunk-3CBW5I4K.cjs');Object.defineProperty(exports,"Aggregator",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.h}});Object.defineProperty(exports,"CoinMetadataRegistry",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.g}});Object.defineProperty(exports,"SwapSdkBase",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.f}});Object.defineProperty(exports,"TypedEventEmitter",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.e}});Object.defineProperty(exports,"isSuiType",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.a}});Object.defineProperty(exports,"mergeWithExistingOrTransfer",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.c}});Object.defineProperty(exports,"selectCoins",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.b}});Object.defineProperty(exports,"transformProperCase",{enumerable:true,get:function(){return chunk3CBW5I4K_cjs.d}});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import { T as TypedEventEmitter, A as AggregatorEvents, S as SwapSdkBase, F as FetchRouteParams,
|
|
2
|
-
export {
|
|
1
|
+
import { T as TypedEventEmitter, A as AggregatorEvents, S as SwapSdkBase, F as FetchRouteParams, f as FetchRoutesOptions, b as FetchRouteResult, g as SwapBuildResult } from './base-D9ApraXl.cjs';
|
|
2
|
+
export { h as AggregatorEvent, i as AggregatorStatusEvent, j as AllSwapSdkPoolStatusEvent, C as Coin, d as FormattedRouteResult, k as SlippageChangedEvent, l as SwapCoinInfo, c as SwapParams, m as SwapPath, e as SwapRoute, n as SwapSdkBaseInterface, a as SwapSdkConstructorParams, o as SwapSdkEvents, p as SwapSdkPoolStatusEvent } from './base-D9ApraXl.cjs';
|
|
3
3
|
import * as _mysten_sui_client from '@mysten/sui/client';
|
|
4
4
|
import { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';
|
|
5
5
|
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
6
6
|
import { SuiKitParams, AccountManagerParams, SuiKit, SuiTransactionBlockResponse, SimulateTransactionResponse } from '@scallop-io/sui-kit';
|
|
7
7
|
import { FlowXSwap } from './sdk/flowx.cjs';
|
|
8
|
-
import { _7kSwap } from './sdk/7k.cjs';
|
|
9
8
|
import { AftermathSwap } from './sdk/aftermath.cjs';
|
|
10
9
|
import { CetusSwap } from './sdk/cetus.cjs';
|
|
11
10
|
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
12
11
|
import 'events';
|
|
13
12
|
import '@flowx-finance/sdk';
|
|
14
|
-
import '@7kprotocol/sdk-ts';
|
|
15
13
|
import 'aftermath-ts-sdk';
|
|
16
14
|
import '@cetusprotocol/aggregator-sdk';
|
|
17
15
|
|
|
@@ -40,7 +38,6 @@ type AggregatorConstructorParams = Omit<SuiKitParams, keyof AccountManagerParams
|
|
|
40
38
|
interface SdkRegistry {
|
|
41
39
|
cetus: CetusSwap;
|
|
42
40
|
aftermath: AftermathSwap;
|
|
43
|
-
'7k': _7kSwap;
|
|
44
41
|
flowx: FlowXSwap;
|
|
45
42
|
}
|
|
46
43
|
type SdkName = keyof SdkRegistry;
|
|
@@ -78,6 +75,7 @@ declare class Aggregator {
|
|
|
78
75
|
walletAddress: string;
|
|
79
76
|
client: ClientWithCoreApi;
|
|
80
77
|
constructor(params: AggregatorConstructorParams);
|
|
78
|
+
init(): Promise<void>;
|
|
81
79
|
private forwardPoolStatusChangeEvent;
|
|
82
80
|
addAggregator(aggregator: SwapSdkBase[]): void;
|
|
83
81
|
getActiveAggregators(): string[];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import { T as TypedEventEmitter, A as AggregatorEvents, S as SwapSdkBase, F as FetchRouteParams,
|
|
2
|
-
export {
|
|
1
|
+
import { T as TypedEventEmitter, A as AggregatorEvents, S as SwapSdkBase, F as FetchRouteParams, f as FetchRoutesOptions, b as FetchRouteResult, g as SwapBuildResult } from './base-D9ApraXl.js';
|
|
2
|
+
export { h as AggregatorEvent, i as AggregatorStatusEvent, j as AllSwapSdkPoolStatusEvent, C as Coin, d as FormattedRouteResult, k as SlippageChangedEvent, l as SwapCoinInfo, c as SwapParams, m as SwapPath, e as SwapRoute, n as SwapSdkBaseInterface, a as SwapSdkConstructorParams, o as SwapSdkEvents, p as SwapSdkPoolStatusEvent } from './base-D9ApraXl.js';
|
|
3
3
|
import * as _mysten_sui_client from '@mysten/sui/client';
|
|
4
4
|
import { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';
|
|
5
5
|
import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions';
|
|
6
6
|
import { SuiKitParams, AccountManagerParams, SuiKit, SuiTransactionBlockResponse, SimulateTransactionResponse } from '@scallop-io/sui-kit';
|
|
7
7
|
import { FlowXSwap } from './sdk/flowx.js';
|
|
8
|
-
import { _7kSwap } from './sdk/7k.js';
|
|
9
8
|
import { AftermathSwap } from './sdk/aftermath.js';
|
|
10
9
|
import { CetusSwap } from './sdk/cetus.js';
|
|
11
10
|
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
12
11
|
import 'events';
|
|
13
12
|
import '@flowx-finance/sdk';
|
|
14
|
-
import '@7kprotocol/sdk-ts';
|
|
15
13
|
import 'aftermath-ts-sdk';
|
|
16
14
|
import '@cetusprotocol/aggregator-sdk';
|
|
17
15
|
|
|
@@ -40,7 +38,6 @@ type AggregatorConstructorParams = Omit<SuiKitParams, keyof AccountManagerParams
|
|
|
40
38
|
interface SdkRegistry {
|
|
41
39
|
cetus: CetusSwap;
|
|
42
40
|
aftermath: AftermathSwap;
|
|
43
|
-
'7k': _7kSwap;
|
|
44
41
|
flowx: FlowXSwap;
|
|
45
42
|
}
|
|
46
43
|
type SdkName = keyof SdkRegistry;
|
|
@@ -78,6 +75,7 @@ declare class Aggregator {
|
|
|
78
75
|
walletAddress: string;
|
|
79
76
|
client: ClientWithCoreApi;
|
|
80
77
|
constructor(params: AggregatorConstructorParams);
|
|
78
|
+
init(): Promise<void>;
|
|
81
79
|
private forwardPoolStatusChangeEvent;
|
|
82
80
|
addAggregator(aggregator: SwapSdkBase[]): void;
|
|
83
81
|
getActiveAggregators(): string[];
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export{h as Aggregator,g as CoinMetadataRegistry,f as SwapSdkBase,e as TypedEventEmitter,a as isSuiType,c as mergeWithExistingOrTransfer,b as selectCoins,d as transformProperCase}from'./chunk-PUC4ABCH.js';
|
package/dist/sdk/7k.cjs
CHANGED
|
@@ -1,10 +1 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkRJTK6ARK_cjs = require('../chunk-RJTK6ARK.cjs');
|
|
4
|
-
var chunkSOD66EHR_cjs = require('../chunk-SOD66EHR.cjs');
|
|
5
|
-
var suiKit = require('@scallop-io/sui-kit');
|
|
6
|
-
var jsonRpc = require('@mysten/sui/jsonRpc');
|
|
7
|
-
|
|
8
|
-
var P=chunkRJTK6ARK_cjs.a("@7kprotocol/sdk-ts",()=>import('@7kprotocol/sdk-ts')),f=["bluefin7k","cetus","flowx"],g=class extends chunkSOD66EHR_cjs.f{_constructorParams;constructor(t){super("7k",t),this._constructorParams=t,this.poolsMap=new Map(f.map(e=>[e,true]));}async ensureClient(){if(!this.client){let{MetaAg:t}=await P(),e=this.suiClient.network;this.client=new t({fullnodeUrl:jsonRpc.getJsonRpcFullnodeUrl(e),partner:this._constructorParams.commission?.partner??this.walletAddress,partnerCommissionBps:this._constructorParams.commission?.commissionBps??0,...this._constructorParams.fetchSettings});}return this.client}setSuiClient(t){super.setSuiClient(t);let e=t.network;this.client?.updateMetaAgOptions({fullnodeUrl:jsonRpc.getJsonRpcFullnodeUrl(e)});}setFetchRouteSettings(t){this.fetchSettings=t,this.client?.updateMetaAgOptions(t);}calcSlippage(t){return Math.round(t*1e4)}parseRawRouteToFormattedResult(t){return {routes:[{paths:[],coinIn:{type:t.coinTypeIn,amount:BigInt(t.amountIn)},coinOut:{type:t.coinTypeOut,amount:BigInt(t.amountOut)},splitPercentage:100}],coinIn:{type:t.coinTypeIn,amount:BigInt(t.amountIn)},coinOut:{type:t.coinTypeOut,amount:BigInt(t.amountOut)},name:this.name}}async fetchRoute(t,e){let s=await this.ensureClient(),i=this.normalizeFetchRouteParams(t),{coinInType:a,coinOutType:n,swapAmount:o}=i;this.rawRouteResult=void 0;let r=new Set(this.getEnabledPools()),u=(await this.withTimeout(s.quote({coinTypeIn:a,coinTypeOut:n,amountIn:o.toString()}),e)).filter(p=>r.has(p.provider));if(!u||u.length===0)throw new Error(`${this.name} fetch route returned empty response`);let c=u.sort((p,R)=>Number(BigInt(R.amountOut)-BigInt(p.amountOut)))[0];return this.rawRouteResult=c,{formattedResult:this.parseRawRouteToFormattedResult(c),rawRouteResult:c}}async buildSwapTransaction(t){this.validateWalletAddress();let e=await this.ensureClient(),{txExtensionParams:s,slippage:i,route:a}=t,n=a?.rawRouteResult??this.rawRouteResult;if(!n)throw new Error(`${this.name} build swap transaction failed: no route available`);let o,r;s?(o=s.initTx,r=s.coinIn):(o=new suiKit.Transaction,o.setSender(this.walletAddress),r=await this.selectCoinInForSwap(o,BigInt(n.amountIn),suiKit.normalizeStructTag(n.coinTypeIn)));let m=await e.swap({quote:n,signer:this.walletAddress,tx:o,coinIn:r},this.calcSlippage(i));return {tx:o,coinOut:m}}};
|
|
9
|
-
|
|
10
|
-
exports._7kSwap = g;
|
|
1
|
+
'use strict';
|
package/dist/sdk/7k.d.cts
CHANGED
|
@@ -1,37 +1,2 @@
|
|
|
1
|
-
import { Transaction, TransactionObjectArgument } from '@scallop-io/sui-kit';
|
|
2
|
-
import { S as SwapSdkBase, n as SwapSdkConstructorParams, F as FetchRouteParams, b as FetchRouteResult, j as SwapParams } from '../base-DhVMdOkK.js';
|
|
3
|
-
import { ClientWithCoreApi } from '@mysten/sui/client';
|
|
4
|
-
import { EProvider, MetaQuote, MetaAgOptions, MetaAg } from '@7kprotocol/sdk-ts';
|
|
5
|
-
export { MetaQuote as _7kQuoteResponse } from '@7kprotocol/sdk-ts';
|
|
6
|
-
import '@mysten/sui/transactions';
|
|
7
|
-
import '@mysten/sui/grpc';
|
|
8
|
-
import 'events';
|
|
9
1
|
|
|
10
|
-
|
|
11
|
-
providers?: MetaAgOptions['providers'];
|
|
12
|
-
hermesApi?: string;
|
|
13
|
-
slippageBps?: number;
|
|
14
|
-
tipBps?: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
declare class _7kSwap extends SwapSdkBase<EProvider, MetaQuote, FetchRouteSettings, MetaAg> {
|
|
18
|
-
private _constructorParams;
|
|
19
|
-
constructor(params: SwapSdkConstructorParams<FetchRouteSettings> & {
|
|
20
|
-
commission?: {
|
|
21
|
-
partner?: string;
|
|
22
|
-
commissionBps?: number;
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
private ensureClient;
|
|
26
|
-
setSuiClient(client: ClientWithCoreApi): void;
|
|
27
|
-
setFetchRouteSettings(settings: FetchRouteSettings): void;
|
|
28
|
-
calcSlippage(slippageInDecimal: number): number;
|
|
29
|
-
private parseRawRouteToFormattedResult;
|
|
30
|
-
fetchRoute(params: FetchRouteParams, abortSignal?: AbortSignal): Promise<FetchRouteResult<MetaQuote>>;
|
|
31
|
-
buildSwapTransaction(params: SwapParams<MetaQuote>): Promise<{
|
|
32
|
-
tx: Transaction;
|
|
33
|
-
coinOut: TransactionObjectArgument;
|
|
34
|
-
}>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { type FetchRouteSettings as _7kFetchRouteSettings, _7kSwap };
|
|
2
|
+
export { }
|
package/dist/sdk/7k.d.ts
CHANGED
|
@@ -1,37 +1,2 @@
|
|
|
1
|
-
import { Transaction, TransactionObjectArgument } from '@scallop-io/sui-kit';
|
|
2
|
-
import { S as SwapSdkBase, n as SwapSdkConstructorParams, F as FetchRouteParams, b as FetchRouteResult, j as SwapParams } from '../base-DhVMdOkK.js';
|
|
3
|
-
import { ClientWithCoreApi } from '@mysten/sui/client';
|
|
4
|
-
import { EProvider, MetaQuote, MetaAgOptions, MetaAg } from '@7kprotocol/sdk-ts';
|
|
5
|
-
export { MetaQuote as _7kQuoteResponse } from '@7kprotocol/sdk-ts';
|
|
6
|
-
import '@mysten/sui/transactions';
|
|
7
|
-
import '@mysten/sui/grpc';
|
|
8
|
-
import 'events';
|
|
9
1
|
|
|
10
|
-
|
|
11
|
-
providers?: MetaAgOptions['providers'];
|
|
12
|
-
hermesApi?: string;
|
|
13
|
-
slippageBps?: number;
|
|
14
|
-
tipBps?: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
declare class _7kSwap extends SwapSdkBase<EProvider, MetaQuote, FetchRouteSettings, MetaAg> {
|
|
18
|
-
private _constructorParams;
|
|
19
|
-
constructor(params: SwapSdkConstructorParams<FetchRouteSettings> & {
|
|
20
|
-
commission?: {
|
|
21
|
-
partner?: string;
|
|
22
|
-
commissionBps?: number;
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
private ensureClient;
|
|
26
|
-
setSuiClient(client: ClientWithCoreApi): void;
|
|
27
|
-
setFetchRouteSettings(settings: FetchRouteSettings): void;
|
|
28
|
-
calcSlippage(slippageInDecimal: number): number;
|
|
29
|
-
private parseRawRouteToFormattedResult;
|
|
30
|
-
fetchRoute(params: FetchRouteParams, abortSignal?: AbortSignal): Promise<FetchRouteResult<MetaQuote>>;
|
|
31
|
-
buildSwapTransaction(params: SwapParams<MetaQuote>): Promise<{
|
|
32
|
-
tx: Transaction;
|
|
33
|
-
coinOut: TransactionObjectArgument;
|
|
34
|
-
}>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { type FetchRouteSettings as _7kFetchRouteSettings, _7kSwap };
|
|
2
|
+
export { }
|
package/dist/sdk/7k.js
CHANGED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { a } from '../chunk-DOMXI4CB.js';
|
|
2
|
-
import { f as f$1 } from '../chunk-YXTM7AVE.js';
|
|
3
|
-
import { Transaction, normalizeStructTag } from '@scallop-io/sui-kit';
|
|
4
|
-
import { getJsonRpcFullnodeUrl } from '@mysten/sui/jsonRpc';
|
|
5
|
-
|
|
6
|
-
var P=a("@7kprotocol/sdk-ts",()=>import('@7kprotocol/sdk-ts')),f=["bluefin7k","cetus","flowx"],g=class extends f$1{_constructorParams;constructor(t){super("7k",t),this._constructorParams=t,this.poolsMap=new Map(f.map(e=>[e,true]));}async ensureClient(){if(!this.client){let{MetaAg:t}=await P(),e=this.suiClient.network;this.client=new t({fullnodeUrl:getJsonRpcFullnodeUrl(e),partner:this._constructorParams.commission?.partner??this.walletAddress,partnerCommissionBps:this._constructorParams.commission?.commissionBps??0,...this._constructorParams.fetchSettings});}return this.client}setSuiClient(t){super.setSuiClient(t);let e=t.network;this.client?.updateMetaAgOptions({fullnodeUrl:getJsonRpcFullnodeUrl(e)});}setFetchRouteSettings(t){this.fetchSettings=t,this.client?.updateMetaAgOptions(t);}calcSlippage(t){return Math.round(t*1e4)}parseRawRouteToFormattedResult(t){return {routes:[{paths:[],coinIn:{type:t.coinTypeIn,amount:BigInt(t.amountIn)},coinOut:{type:t.coinTypeOut,amount:BigInt(t.amountOut)},splitPercentage:100}],coinIn:{type:t.coinTypeIn,amount:BigInt(t.amountIn)},coinOut:{type:t.coinTypeOut,amount:BigInt(t.amountOut)},name:this.name}}async fetchRoute(t,e){let s=await this.ensureClient(),i=this.normalizeFetchRouteParams(t),{coinInType:a,coinOutType:n,swapAmount:o}=i;this.rawRouteResult=void 0;let r=new Set(this.getEnabledPools()),u=(await this.withTimeout(s.quote({coinTypeIn:a,coinTypeOut:n,amountIn:o.toString()}),e)).filter(p=>r.has(p.provider));if(!u||u.length===0)throw new Error(`${this.name} fetch route returned empty response`);let c=u.sort((p,R)=>Number(BigInt(R.amountOut)-BigInt(p.amountOut)))[0];return this.rawRouteResult=c,{formattedResult:this.parseRawRouteToFormattedResult(c),rawRouteResult:c}}async buildSwapTransaction(t){this.validateWalletAddress();let e=await this.ensureClient(),{txExtensionParams:s,slippage:i,route:a}=t,n=a?.rawRouteResult??this.rawRouteResult;if(!n)throw new Error(`${this.name} build swap transaction failed: no route available`);let o,r;s?(o=s.initTx,r=s.coinIn):(o=new Transaction,o.setSender(this.walletAddress),r=await this.selectCoinInForSwap(o,BigInt(n.amountIn),normalizeStructTag(n.coinTypeIn)));let m=await e.swap({quote:n,signer:this.walletAddress,tx:o,coinIn:r},this.calcSlippage(i));return {tx:o,coinOut:m}}};
|
|
7
|
-
|
|
8
|
-
export { g as _7kSwap };
|