@cetusprotocol/aggregator-sdk 0.0.8 → 0.1.1

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.
Files changed (57) hide show
  1. package/dist/index.d.mts +98 -161
  2. package/dist/index.d.ts +98 -161
  3. package/dist/index.js +1128 -1550
  4. package/dist/index.mjs +1116 -1488
  5. package/dist/src/api.d.ts +53 -0
  6. package/dist/src/client.d.ts +38 -65
  7. package/dist/src/const.d.ts +0 -68
  8. package/dist/src/index.d.ts +9 -5
  9. package/dist/src/transaction/afsui.d.ts +10 -0
  10. package/dist/src/transaction/aftermath.d.ts +13 -24
  11. package/dist/src/transaction/cetus.d.ts +9 -33
  12. package/dist/src/transaction/deepbook_v2.d.ts +14 -0
  13. package/dist/src/transaction/flowx_v2.d.ts +7 -0
  14. package/dist/src/transaction/haedal.d.ts +6 -0
  15. package/dist/src/transaction/index.d.ts +6 -1
  16. package/dist/src/transaction/kriya_v2.d.ts +6 -0
  17. package/dist/src/transaction/kriya_v3.d.ts +7 -0
  18. package/dist/src/transaction/swap.d.ts +1 -2
  19. package/dist/src/transaction/turbos.d.ts +7 -22
  20. package/dist/src/transaction/volo.d.ts +8 -0
  21. package/dist/src/utils/coin.d.ts +9 -0
  22. package/dist/{src → tests}/test_data.test.d.ts +1 -0
  23. package/package.json +2 -2
  24. package/src/api.ts +144 -0
  25. package/src/client.ts +295 -239
  26. package/src/const.ts +0 -113
  27. package/src/index.ts +10 -5
  28. package/src/transaction/afsui.ts +60 -0
  29. package/src/transaction/aftermath.ts +71 -124
  30. package/src/transaction/cetus.ts +87 -258
  31. package/src/transaction/deepbook_v2.ts +122 -0
  32. package/src/transaction/flowx_v2.ts +42 -0
  33. package/src/transaction/haedal.ts +41 -0
  34. package/src/transaction/index.ts +17 -1
  35. package/src/transaction/kriya_v2.ts +38 -0
  36. package/src/transaction/kriya_v3.ts +46 -0
  37. package/src/transaction/swap.ts +17 -24
  38. package/src/transaction/turbos.ts +40 -99
  39. package/src/transaction/volo.ts +52 -0
  40. package/src/utils/coin.ts +91 -6
  41. package/src/utils/transaction.ts +1 -1
  42. package/tests/router.test.ts +123 -73
  43. package/{src → tests}/test_data.test.ts +2 -0
  44. package/dist/src/config.d.ts +0 -26
  45. package/dist/src/transaction/common.d.ts +0 -12
  46. package/dist/src/transaction/deepbook.d.ts +0 -21
  47. package/dist/src/transaction/flowx.d.ts +0 -20
  48. package/dist/src/transaction/kriya.d.ts +0 -21
  49. package/dist/src/transaction/router.d.ts +0 -6
  50. package/dist/src/utils/account_cap.d.ts +0 -7
  51. package/src/config.ts +0 -65
  52. package/src/transaction/common.ts +0 -169
  53. package/src/transaction/deepbook.ts +0 -126
  54. package/src/transaction/flowx.ts +0 -97
  55. package/src/transaction/kriya.ts +0 -77
  56. package/src/transaction/router.ts +0 -339
  57. package/src/utils/account_cap.ts +0 -62
@@ -1,126 +0,0 @@
1
- import {
2
- TransactionArgument,
3
- Transaction,
4
- TransactionObjectArgument,
5
- } from "@mysten/sui/transactions"
6
- import { AggregatorConfig } from "../config"
7
- import {
8
- AGGREGATOR,
9
- CLOCK_ADDRESS,
10
- DEEPBOOK_MODULE,
11
- SWAP_A2B_FUNC,
12
- SWAP_B2A_FUNC,
13
- TRANSFER_ACCOUNT_CAP,
14
- } from "../const"
15
- import { ConfigErrorCode, TransactionErrorCode } from "../errors"
16
- import { createTarget } from "../utils"
17
- import { getOrCreateAccountCap } from "../utils/account_cap"
18
- import { SuiClient } from "@mysten/sui/client"
19
-
20
- export type DeepbookSwapParams = {
21
- poolId: string
22
- a2b: boolean
23
- amount: TransactionArgument
24
- amountLimit: number
25
- coinA?: TransactionObjectArgument
26
- coinB?: TransactionObjectArgument
27
- useFullInputCoinAmount: boolean
28
- coinAType: string
29
- coinBType: string
30
- }
31
-
32
- export type DeepbookSwapResult = {
33
- targetCoin: TransactionObjectArgument
34
- amountIn: TransactionArgument
35
- amountOut: TransactionArgument
36
- txb: Transaction
37
- }
38
-
39
- export async function deepbookSwapMovecall(
40
- swapParams: DeepbookSwapParams,
41
- client: SuiClient,
42
- txb: Transaction,
43
- config: AggregatorConfig
44
- ): Promise<DeepbookSwapResult> {
45
- const accountCapRes = await getOrCreateAccountCap(
46
- txb,
47
- client,
48
- config.getWallet()
49
- )
50
- const accountCap = accountCapRes.accountCap
51
-
52
- const aggregatorPackage = config.getPackage(AGGREGATOR)
53
- if (aggregatorPackage == null) {
54
- throw new AggregateError(
55
- "Aggregator package not set",
56
- ConfigErrorCode.MissAggregatorPackage
57
- )
58
- }
59
- const aggregatorPublishedAt = aggregatorPackage.publishedAt
60
-
61
- if (swapParams.a2b) {
62
- if (swapParams.coinA == null) {
63
- throw new AggregateError(
64
- "coinA is required",
65
- TransactionErrorCode.MissCoinA
66
- )
67
- }
68
- } else {
69
- if (swapParams.coinB == null) {
70
- throw new AggregateError(
71
- "coinB is required",
72
- TransactionErrorCode.MissCoinB
73
- )
74
- }
75
- }
76
-
77
- const args = swapParams.a2b
78
- ? [
79
- txb.object(swapParams.poolId),
80
- swapParams.amount,
81
- txb.pure.u64(swapParams.amountLimit),
82
- swapParams.coinA!,
83
- accountCap,
84
- txb.pure.bool(swapParams.useFullInputCoinAmount),
85
- txb.object(CLOCK_ADDRESS),
86
- ]
87
- : [
88
- txb.object(swapParams.poolId),
89
- swapParams.amount,
90
- txb.pure.u64(swapParams.amountLimit),
91
- swapParams.coinB!,
92
- accountCap,
93
- txb.pure.bool(swapParams.useFullInputCoinAmount),
94
- txb.object(CLOCK_ADDRESS),
95
- ]
96
-
97
- let func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC
98
- const target = createTarget(aggregatorPublishedAt, DEEPBOOK_MODULE, func)
99
-
100
- const res = txb.moveCall({
101
- target,
102
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
103
- arguments: args,
104
- })
105
-
106
- if (accountCapRes.isCreate) {
107
- const target = createTarget(
108
- aggregatorPublishedAt,
109
- DEEPBOOK_MODULE,
110
- TRANSFER_ACCOUNT_CAP
111
- )
112
-
113
- const res = txb.moveCall({
114
- target,
115
- typeArguments: [],
116
- arguments: [accountCap],
117
- })
118
- }
119
-
120
- return {
121
- targetCoin: res[0],
122
- amountIn: res[1],
123
- amountOut: res[2],
124
- txb,
125
- }
126
- }
@@ -1,97 +0,0 @@
1
- import {
2
- TransactionArgument,
3
- Transaction,
4
- TransactionObjectArgument,
5
- } from "@mysten/sui/transactions"
6
- import { AggregatorConfig, ENV } from "../config"
7
- import {
8
- AGGREGATOR,
9
- FLOWX_AMM_MODULE,
10
- MAINNET_FLOWX_AMM_CONTAINER_ID,
11
- SWAP_A2B_FUNC,
12
- SWAP_B2A_FUNC,
13
- TESTNET_FLOWX_AMM_CONTAINER_ID,
14
- } from "../const"
15
- import { ConfigErrorCode, TransactionErrorCode } from "../errors"
16
- import { createTarget } from "../utils"
17
-
18
- export type FlowxSwapParams = {
19
- amount: TransactionArgument
20
- amountLimit: number
21
- a2b: boolean
22
- byAmountIn: boolean
23
- coinA?: TransactionObjectArgument
24
- coinB?: TransactionObjectArgument
25
- useFullInputCoinAmount: boolean
26
- coinAType: string
27
- coinBType: string
28
- }
29
-
30
- export type FlowxSwapResult = {
31
- targetCoin: TransactionObjectArgument
32
- amountIn: TransactionArgument
33
- amountOut: TransactionArgument
34
- txb: Transaction
35
- }
36
-
37
- export async function flowxAmmSwapMovecall(
38
- swapParams: FlowxSwapParams,
39
- txb: Transaction,
40
- config: AggregatorConfig
41
- ): Promise<FlowxSwapResult> {
42
- const aggregatorPackage = config.getPackage(AGGREGATOR)
43
- if (aggregatorPackage == null) {
44
- throw new AggregateError(
45
- "Aggregator package not set",
46
- ConfigErrorCode.MissAggregatorPackage
47
- )
48
- }
49
- const aggregatorPublishedAt = aggregatorPackage.publishedAt
50
-
51
- if (swapParams.a2b) {
52
- if (swapParams.coinA == null) {
53
- throw new AggregateError(
54
- "coinA is required",
55
- TransactionErrorCode.MissCoinA
56
- )
57
- }
58
- } else {
59
- if (swapParams.coinB == null) {
60
- throw new AggregateError(
61
- "coinB is required",
62
- TransactionErrorCode.MissCoinB
63
- )
64
- }
65
- }
66
-
67
- const containerID =
68
- config.getENV() === ENV.MAINNET
69
- ? MAINNET_FLOWX_AMM_CONTAINER_ID
70
- : TESTNET_FLOWX_AMM_CONTAINER_ID
71
-
72
- const swapCoin = swapParams.a2b ? swapParams.coinA! : swapParams.coinB!
73
-
74
- const args = [
75
- txb.object(containerID),
76
- swapParams.amount,
77
- txb.pure.u64(swapParams.amountLimit),
78
- swapCoin,
79
- txb.pure.bool(swapParams.useFullInputCoinAmount),
80
- ]
81
-
82
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC
83
-
84
- const target = createTarget(aggregatorPublishedAt, FLOWX_AMM_MODULE, func)
85
-
86
- const res = txb.moveCall({
87
- target,
88
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
89
- arguments: args,
90
- })
91
- return {
92
- targetCoin: res[0],
93
- amountIn: res[1],
94
- amountOut: res[2],
95
- txb,
96
- }
97
- }
@@ -1,77 +0,0 @@
1
- import { TransactionArgument, Transaction, TransactionObjectArgument } from "@mysten/sui/transactions"
2
- import { AggregatorConfig } from "../config"
3
- import { AGGREGATOR, KRIYA_MODULE, SWAP_A2B_FUNC, SWAP_B2A_FUNC } from "../const"
4
- import { ConfigErrorCode, TransactionErrorCode } from "../errors"
5
- import { createTarget } from "../utils"
6
-
7
- export type KriyaSwapParams = {
8
- poolId: string
9
- amount: TransactionArgument
10
- amountLimit: number
11
- a2b: boolean
12
- byAmountIn: boolean
13
- coinA?: TransactionObjectArgument
14
- coinB?: TransactionObjectArgument
15
- useFullInputCoinAmount: boolean
16
- coinAType: string
17
- coinBType: string
18
- }
19
-
20
- export type KriyaSwapResult = {
21
- targetCoin: TransactionObjectArgument
22
- amountIn: TransactionArgument
23
- amountOut: TransactionArgument
24
- txb: Transaction
25
- }
26
-
27
- export async function kriyaSwapMovecall(
28
- swapParams: KriyaSwapParams,
29
- txb: Transaction,
30
- config: AggregatorConfig,
31
- ): Promise<KriyaSwapResult> {
32
- const aggregatorPackage = config.getPackage(AGGREGATOR)
33
- if (aggregatorPackage == null) {
34
- throw new AggregateError("Aggregator package not set", ConfigErrorCode.MissAggregatorPackage)
35
- }
36
- const aggregatorPublishedAt = aggregatorPackage.publishedAt
37
-
38
- if (swapParams.a2b) {
39
- if (swapParams.coinA == null) {
40
- throw new AggregateError("coinA is required", TransactionErrorCode.MissCoinA)
41
- }
42
- } else {
43
- if (swapParams.coinB == null) {
44
- throw new AggregateError("coinB is required", TransactionErrorCode.MissCoinB)
45
- }
46
- }
47
-
48
- const args = swapParams.a2b ? [
49
- txb.object(swapParams.poolId),
50
- swapParams.amount,
51
- txb.pure.u64(swapParams.amountLimit),
52
- swapParams.coinA!,
53
- txb.pure.bool(swapParams.useFullInputCoinAmount)
54
- ] : [
55
- txb.object(swapParams.poolId),
56
- swapParams.amount,
57
- txb.pure.u64(swapParams.amountLimit),
58
- swapParams.coinB!,
59
- txb.pure.bool(swapParams.useFullInputCoinAmount)
60
- ]
61
-
62
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC
63
-
64
- const target = createTarget(aggregatorPublishedAt, KRIYA_MODULE, func)
65
-
66
- const res = txb.moveCall({
67
- target,
68
- typeArguments: [swapParams.coinAType, swapParams.coinBType],
69
- arguments: args,
70
- })
71
- return {
72
- targetCoin: res[0],
73
- amountIn: res[1],
74
- amountOut: res[2],
75
- txb
76
- }
77
- }
@@ -1,339 +0,0 @@
1
- import {
2
- TransactionArgument,
3
- Transaction,
4
- TransactionObjectArgument,
5
- } from "@mysten/sui/transactions"
6
- import type { BuildRouterSwapParams } from "../client"
7
- import { AggregatorConfig } from "../config"
8
- import {
9
- AFTERMATH_AMM,
10
- CETUS_DEX,
11
- DEEPBOOK_DEX,
12
- FLOWX_AMM,
13
- KRIYA_DEX,
14
- TURBOS_DEX,
15
- U64_MAX,
16
- } from "../const"
17
- import { deepbookSwapMovecall } from "./deepbook"
18
- import { SuiClient } from "@mysten/sui/client"
19
- import { kriyaSwapMovecall } from "./kriya"
20
- import {
21
- cetusFlashSwapMovecall,
22
- cetusRepayFlashSwapMovecall,
23
- cetusSwapWithOutLimit,
24
- } from "./cetus"
25
- import { transferOrDestoryCoin } from "./common"
26
- import { GetDefaultSqrtPriceLimit } from "../math"
27
- import { flowxAmmSwapMovecall } from "./flowx"
28
- import { turbosClmmSwapMovecall } from "./turbos"
29
- import { AftermathAmmSwapMovecall } from "./aftermath"
30
- import { TransactionErrorCode } from "~/errors"
31
-
32
- export async function expectInputRouterSwap(
33
- client: SuiClient,
34
- params: BuildRouterSwapParams,
35
- txb: Transaction,
36
- fromCoin: TransactionObjectArgument,
37
- config: AggregatorConfig,
38
- partner?: string
39
- ): Promise<TransactionObjectArgument[]> {
40
- const splitAmounts = params.routers.map((router) =>
41
- router.amountIn.toString()
42
- )
43
- const targetCoins = []
44
- const fromCoins = txb.splitCoins(fromCoin, splitAmounts)
45
-
46
- for (let i = 0; i < params.routers.length; i++) {
47
- const router = params.routers[i]
48
- let intermediateTargetCoin: TransactionObjectArgument
49
- let nextFromCoin = fromCoins[i] as TransactionObjectArgument
50
- let nextFlashAmount: TransactionArgument = txb.pure.u64(splitAmounts[i])
51
-
52
- for (let j = 0; j < router.path.length; j++) {
53
- const firstPathPool = j === 0
54
- const path = router.path[j]
55
-
56
- if (path.provider === CETUS_DEX) {
57
- const swapParams = {
58
- poolId: path.id,
59
- amount: nextFlashAmount,
60
- amountLimit: "0",
61
- a2b: path.a2b,
62
- byAmountIn: true,
63
- sqrtPriceLimit: GetDefaultSqrtPriceLimit(path.a2b),
64
- partner: partner!,
65
- coinAType: path.a2b ? path.from : path.target,
66
- coinBType: path.a2b ? path.target : path.from,
67
- }
68
-
69
- const swapResult = await cetusSwapWithOutLimit(
70
- swapParams,
71
- nextFromCoin,
72
- txb,
73
- config
74
- )
75
-
76
- transferOrDestoryCoin(
77
- txb,
78
- swapResult.repayTargetCoin,
79
- path.from,
80
- config
81
- )
82
-
83
- intermediateTargetCoin = swapResult.flashTargetCoin!
84
- nextFromCoin = intermediateTargetCoin
85
- nextFlashAmount = swapResult.nextInputAmount!
86
- }
87
-
88
- if (path.provider === DEEPBOOK_DEX) {
89
- const coinA = path.a2b ? nextFromCoin : undefined
90
- const coinB = path.a2b ? undefined : nextFromCoin
91
-
92
- const swapParams = {
93
- poolId: path.id,
94
- a2b: path.a2b,
95
- amount: nextFlashAmount,
96
- amountLimit: 0,
97
- coinA,
98
- coinB,
99
- useFullInputCoinAmount: !firstPathPool,
100
- coinAType: path.a2b ? path.from : path.target,
101
- coinBType: path.a2b ? params.targetCoinType : path.from,
102
- }
103
- const deepbookSwapResult = await deepbookSwapMovecall(
104
- swapParams,
105
- client,
106
- txb,
107
- config
108
- )
109
-
110
- intermediateTargetCoin = deepbookSwapResult.targetCoin
111
- nextFromCoin = intermediateTargetCoin
112
- nextFlashAmount = deepbookSwapResult.amountOut
113
- }
114
-
115
- if (path.provider === KRIYA_DEX) {
116
- const coinA = path.a2b ? nextFromCoin : undefined
117
- const coinB = path.a2b ? undefined : nextFromCoin
118
-
119
- const swapParams = {
120
- poolId: path.id,
121
- amount: nextFlashAmount,
122
- amountLimit: 0,
123
- a2b: path.a2b,
124
- byAmountIn: true,
125
- coinA,
126
- coinB,
127
- useFullInputCoinAmount: !firstPathPool,
128
- coinAType: path.a2b ? path.from : path.target,
129
- coinBType: path.a2b ? path.target : path.from,
130
- }
131
-
132
- const swapResult = await kriyaSwapMovecall(swapParams, txb, config)
133
-
134
- intermediateTargetCoin = swapResult.targetCoin
135
- nextFromCoin = intermediateTargetCoin
136
- nextFlashAmount = swapResult.amountOut
137
- }
138
-
139
- if (path.provider === FLOWX_AMM) {
140
- const coinA = path.a2b ? nextFromCoin : undefined
141
- const coinB = path.a2b ? undefined : nextFromCoin
142
-
143
- const swapParams = {
144
- amount: nextFlashAmount,
145
- amountLimit: 0,
146
- a2b: path.a2b,
147
- byAmountIn: true,
148
- coinA,
149
- coinB,
150
- useFullInputCoinAmount: !firstPathPool,
151
- coinAType: path.a2b ? path.from : path.target,
152
- coinBType: path.a2b ? path.target : path.from,
153
- }
154
-
155
- const swapResult = await flowxAmmSwapMovecall(swapParams, txb, config)
156
-
157
- intermediateTargetCoin = swapResult.targetCoin
158
- nextFromCoin = intermediateTargetCoin
159
- nextFlashAmount = swapResult.amountOut
160
- }
161
-
162
- if (path.provider === TURBOS_DEX) {
163
- const coinA = path.a2b ? nextFromCoin : undefined
164
- const coinB = path.a2b ? undefined : nextFromCoin
165
-
166
- let feeType = ""
167
- if (
168
- path.extendedDetails != null &&
169
- path.extendedDetails.turbosFeeType != null
170
- ) {
171
- feeType = path.extendedDetails.turbosFeeType
172
- } else {
173
- throw new AggregateError(
174
- "Build turbos swap movecall error: ",
175
- TransactionErrorCode.MissTurbosFeeType
176
- )
177
- }
178
-
179
- const swapParams = {
180
- poolId: path.id,
181
- amount: nextFlashAmount,
182
- amountLimit: 0,
183
- a2b: path.a2b,
184
- byAmountIn: true,
185
- coinA,
186
- coinB,
187
- useFullInputCoinAmount: !firstPathPool,
188
- coinAType: path.a2b ? path.from : path.target,
189
- coinBType: path.a2b ? path.target : path.from,
190
- feeType,
191
- }
192
-
193
- const swapResult = await turbosClmmSwapMovecall(swapParams, txb, config)
194
-
195
- intermediateTargetCoin = swapResult.targetCoin
196
- nextFromCoin = intermediateTargetCoin
197
- nextFlashAmount = swapResult.amountOut
198
- }
199
-
200
- if (path.provider === AFTERMATH_AMM) {
201
- const coinA = path.a2b ? nextFromCoin : undefined
202
- const coinB = path.a2b ? undefined : nextFromCoin
203
-
204
- let lpSupplyType = ""
205
-
206
- if (
207
- path.extendedDetails != null &&
208
- path.extendedDetails.aftermathLpSupplyType != null
209
- ) {
210
- lpSupplyType = path.extendedDetails.aftermathLpSupplyType
211
- } else {
212
- throw new AggregateError(
213
- "Build aftermath swap movecall error: ",
214
- TransactionErrorCode.MissAftermathLpSupplyType
215
- )
216
- }
217
-
218
- const swapParams = {
219
- poolId: path.id,
220
- amount: nextFlashAmount,
221
- amountOut: path.amountOut,
222
- amountLimit: 0,
223
- a2b: path.a2b,
224
- byAmountIn: true,
225
- coinA,
226
- coinB,
227
- useFullInputCoinAmount: !firstPathPool,
228
- coinAType: path.a2b ? path.from : path.target,
229
- coinBType: path.a2b ? path.target : path.from,
230
- slippage: params.slippage,
231
- lpSupplyType,
232
- }
233
-
234
- const swapResult = await AftermathAmmSwapMovecall(
235
- swapParams,
236
- txb,
237
- config
238
- )
239
-
240
- intermediateTargetCoin = swapResult.targetCoin
241
- nextFromCoin = intermediateTargetCoin
242
- nextFlashAmount = swapResult.amountOut
243
- }
244
- }
245
- targetCoins.push(nextFromCoin)
246
- }
247
-
248
- transferOrDestoryCoin(txb, fromCoin, params.fromCoinType, config)
249
-
250
- return targetCoins
251
- }
252
-
253
- export async function expectOutputRouterSwap(
254
- params: BuildRouterSwapParams,
255
- txb: Transaction,
256
- fromCoin: TransactionObjectArgument,
257
- config: AggregatorConfig,
258
- partner?: string
259
- ): Promise<TransactionObjectArgument[]> {
260
- const splitAmounts = params.routers.map((router) =>
261
- router.amountOut.toString()
262
- )
263
-
264
- const returnCoins: TransactionObjectArgument[] = []
265
- const receipts: TransactionObjectArgument[] = []
266
-
267
- const targetCoins = []
268
-
269
- for (let i = 0; i < params.routers.length; i++) {
270
- const router = params.routers[i]
271
-
272
- let nextFlashAmount: TransactionArgument = txb.pure.u64(splitAmounts[i])
273
- for (let j = router.path.length - 1; j >= 0; j--) {
274
- const path = router.path[j]
275
-
276
- const coinAType = path.a2b ? path.from : path.target
277
- const coinBType = path.a2b ? path.target : path.from
278
-
279
- const swapParams = {
280
- poolId: path.id,
281
- amount: nextFlashAmount,
282
- amountLimit: U64_MAX,
283
- a2b: path.a2b,
284
- byAmountIn: false,
285
- sqrtPriceLimit: GetDefaultSqrtPriceLimit(path.a2b), //
286
- partner,
287
- coinAType,
288
- coinBType,
289
- }
290
-
291
- const flashSwapResult = cetusFlashSwapMovecall(swapParams, txb, config)
292
- nextFlashAmount = flashSwapResult.payAmount
293
- returnCoins.unshift(flashSwapResult.targetCoin)
294
- receipts.unshift(flashSwapResult.flashReceipt)
295
- }
296
-
297
- let nextRepayCoin = fromCoin
298
- for (let j = 0; j < router.path.length; j++) {
299
- const path = router.path[j]
300
-
301
- const coinAType = path.a2b ? path.from : path.target
302
- const coinBType = path.a2b ? path.target : path.from
303
-
304
- const repayParams = {
305
- poolId: path.id,
306
- a2b: path.a2b,
307
- partner: partner,
308
- coinA: path.a2b ? nextRepayCoin : undefined,
309
- coinB: path.a2b ? undefined : nextRepayCoin,
310
- receipt: receipts[j],
311
- coinAType,
312
- coinBType,
313
- }
314
- const repayResult = await cetusRepayFlashSwapMovecall(
315
- repayParams,
316
- txb,
317
- config
318
- )
319
- nextRepayCoin = returnCoins[j]
320
- if (j === 0) {
321
- fromCoin = repayResult.repayTargetCoin
322
- } else {
323
- transferOrDestoryCoin(
324
- txb,
325
- repayResult.repayTargetCoin,
326
- path.from,
327
- config
328
- )
329
- }
330
- if (j === router.path.length - 1) {
331
- targetCoins.push(nextRepayCoin)
332
- }
333
- }
334
- }
335
-
336
- transferOrDestoryCoin(txb, fromCoin, params.fromCoinType, config)
337
-
338
- return targetCoins
339
- }
@@ -1,62 +0,0 @@
1
- import { SuiClient } from "@mysten/sui/client"
2
- import { DEEPBOOK_CLOB_V2_MODULE, DEEPBOOK_CUSTODIAN_V2_MODULE, DEEPBOOK_PACKAGE_ID, DEEPBOOK_PUBLISHED_AT } from "../const"
3
- import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions"
4
-
5
- export type GetOrCreateAccountCapResult = {
6
- accountCap: TransactionObjectArgument,
7
- isCreate: boolean
8
- }
9
-
10
- export async function getOrCreateAccountCap(txb: Transaction, client: SuiClient, owner: string): Promise<GetOrCreateAccountCapResult> {
11
- let accountCapStr = await getAccountCap(client, owner)
12
- if (accountCapStr !== null) {
13
- return {
14
- accountCap: txb.object(accountCapStr),
15
- isCreate: false,
16
- }
17
- }
18
-
19
- const accountCap = txb.moveCall({
20
- target: `${DEEPBOOK_PUBLISHED_AT}::${DEEPBOOK_CLOB_V2_MODULE}::create_account`,
21
- typeArguments: [],
22
- arguments: [],
23
- })
24
-
25
- return {
26
- accountCap,
27
- isCreate: true,
28
- }
29
- }
30
-
31
- async function getAccountCap(client: SuiClient, owner: string): Promise<string | null> {
32
- let limit = 50;
33
- let cursor = null;
34
-
35
- while (true) {
36
- const ownedObjects: any = client.getOwnedObjects({
37
- owner,
38
- cursor,
39
- limit,
40
- filter: {
41
- MoveModule: {
42
- package: DEEPBOOK_PACKAGE_ID,
43
- module: DEEPBOOK_CUSTODIAN_V2_MODULE,
44
- }
45
- }
46
- })
47
-
48
- if (ownedObjects != null && ownedObjects.data != null) {
49
- if (ownedObjects.data.length !== 0) {
50
- return ownedObjects.data[0].data.objectId
51
- }
52
-
53
- if (ownedObjects.data.length < 50) {
54
- break
55
- }
56
- } else {
57
- break
58
- }
59
- }
60
-
61
- return null
62
- }