@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
package/src/const.ts CHANGED
@@ -1,95 +1,5 @@
1
1
  import BN from "bn.js"
2
2
 
3
- /**
4
- * The address representing the clock in the system.
5
- */
6
- export const CLOCK_ADDRESS =
7
- "0x0000000000000000000000000000000000000000000000000000000000000006"
8
-
9
- /**
10
- * The address for CoinInfo module.
11
- */
12
- export const CoinInfoAddress = "0x1::coin::CoinInfo"
13
-
14
- /**
15
- * The address for CoinStore module.
16
- */
17
- export const CoinStoreAddress = "0x1::coin::CoinStore"
18
-
19
- export const SuiZeroCoinFn = "0x2::coin::zero"
20
-
21
- // Dex names
22
- export const AGGREGATOR = "aggregator"
23
- export const CETUS_DEX = "CETUS"
24
- export const DEEPBOOK_DEX = "DEEPBOOK"
25
- export const KRIYA_DEX = "KRIYA"
26
- export const FLOWX_AMM = "FLOWX"
27
- export const TURBOS_DEX = "TURBOS"
28
- export const AFTERMATH_AMM = "AFTERMATH"
29
-
30
- export const INTEGRATE = "integrate"
31
-
32
- // Module names
33
- export const CETUS_MODULE = "cetus"
34
- export const DEEPBOOK_MODULE = "deepbook"
35
- export const KRIYA_MODULE = "kriya"
36
- export const UTILS_MODULE = "utils"
37
- export const POOL_MODULT = "pool"
38
- export const PAY_MODULE = "pay"
39
- export const FLOWX_AMM_MODULE = "flowx_amm"
40
- export const TURBOS_MODULE = "turbos"
41
- export const AFTERMATH_MODULE = "aftermath"
42
-
43
- export const DEEPBOOK_CUSTODIAN_V2_MODULE = "custodian_v2"
44
- export const DEEPBOOK_CLOB_V2_MODULE = "clob_v2"
45
-
46
- // Function names
47
- export const FlashSwapFunc = "flash_swap"
48
- export const FlashSwapWithPartnerFunc = "flash_swap_with_partner"
49
- export const RepayFalshSwapFunc = "repay_flash_swap"
50
- export const RepayFlashSwapWithPartnerFunc = "repay_flash_swap_with_partner"
51
-
52
- export const FlashSwapA2BFunc = "flash_swap_a2b"
53
- export const FlashSwapB2AFunc = "flash_swap_b2a"
54
-
55
- export const FlashSwapWithPartnerA2BFunc = "flash_swap_with_partner_a2b"
56
- export const FlashSwapWithPartnerB2AFunc = "flash_swap_with_partner_b2a"
57
-
58
- export const REPAY_FLASH_SWAP_A2B_FUNC = "repay_flash_swap_a2b"
59
- export const REPAY_FLASH_SWAP_B2A_FUNC = "repay_flash_swap_b2a"
60
-
61
- export const REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC =
62
- "repay_flash_swap_with_partner_a2b"
63
- export const REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC =
64
- "repay_flash_swap_with_partner_b2a"
65
-
66
- export const SWAP_A2B_FUNC = "swap_a2b"
67
- export const SWAP_B2A_FUNC = "swap_b2a"
68
-
69
- export const TRANSFER_OR_DESTORY_COIN_FUNC = "transfer_or_destroy_coin"
70
- export const CHECK_COINS_THRESHOLD_FUNC = "check_coins_threshold"
71
-
72
- export const JOIN_FUNC = "join_vec"
73
-
74
- export const TRANSFER_ACCOUNT_CAP = "transfer_account_cap"
75
-
76
- // Package IDs
77
- export const DEEPBOOK_PACKAGE_ID =
78
- "0x000000000000000000000000000000000000000000000000000000000000dee9"
79
- export const DEEPBOOK_PUBLISHED_AT =
80
- "0x000000000000000000000000000000000000000000000000000000000000dee9"
81
-
82
- export const CETUS_PUBLISHED_AT =
83
- "0x70968826ad1b4ba895753f634b0aea68d0672908ca1075a2abdf0fc9e0b2fc6a"
84
-
85
- // Mainnet Cetus objects IDs
86
- export const MAINNET_CETUS_GLOBAL_CONFIG_ID =
87
- "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f"
88
-
89
- // Testnet Cetus objects IDs
90
- export const TESTNET_CETUS_GLOBAL_CONFIG_ID =
91
- "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a"
92
-
93
3
  export const ZERO = new BN(0)
94
4
 
95
5
  export const ONE = new BN(1)
@@ -103,26 +13,3 @@ export const U64_MAX_BN = new BN("18446744073709551615")
103
13
  export const U64_MAX = "18446744073709551615"
104
14
 
105
15
  export const TEN_POW_NINE = 1000000000
106
-
107
- export const MAINNET_FLOWX_AMM_CONTAINER_ID =
108
- "0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511"
109
- export const TESTNET_FLOWX_AMM_CONTAINER_ID = ""
110
-
111
- export const TURBOS_VERSIONED =
112
- "0xf1cf0e81048df168ebeb1b8030fad24b3e0b53ae827c25053fff0779c1445b6f"
113
-
114
- export const MAINNET_AFTERMATH_REGISTRY_ID =
115
- "0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae"
116
- export const TESTNET_AFTERMATH_REGISTRY_ID = ""
117
- export const MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID =
118
- "0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4"
119
- export const TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = ""
120
- export const MAINNET_AFTERMATH_TREASURY_ID =
121
- "0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce"
122
- export const TESTNET_AFTERMATH_TREASURY_ID = ""
123
- export const MAINNET_AFTERMATH_INSURANCE_FUND_ID =
124
- "0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b"
125
- export const TESTNET_AFTERMATH_INSURANCE_FUND_ID = ""
126
- export const MAINNET_AFTERMATH_REFERRAL_VAULT_ID =
127
- "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278"
128
- export const TESTNET_AFTERMATH_REFERRAL_VAULT_ID = ""
package/src/index.ts CHANGED
@@ -1,5 +1,10 @@
1
- export * from './client'
2
- export * from './transaction'
3
- export * from './config'
4
- export * from './utils'
5
- export * from './const'
1
+ export * from "./client"
2
+ export * from "./transaction"
3
+ export * from "./utils"
4
+ export * from "./const"
5
+ export * from "./api"
6
+
7
+ export enum Env {
8
+ Mainnet,
9
+ Testnet,
10
+ }
@@ -0,0 +1,60 @@
1
+ import {
2
+ Transaction,
3
+ TransactionObjectArgument,
4
+ } from "@mysten/sui/transactions"
5
+ import { AggregatorClient, Dex, Env, Path } from ".."
6
+ import { SUI_SYSTEM_ADDRESS } from "@mysten/sui/utils"
7
+
8
+ export class Afsui implements Dex {
9
+ private stakedSuiVault: string
10
+ private safe: string
11
+ private referVault: string
12
+ private validator: string
13
+
14
+ constructor(env: Env) {
15
+ if (env !== Env.Mainnet) {
16
+ throw new Error("Afsui only supported on mainnet")
17
+ }
18
+
19
+ this.stakedSuiVault =
20
+ "0x2f8f6d5da7f13ea37daa397724280483ed062769813b6f31e9788e59cc88994d"
21
+ this.safe =
22
+ "0xeb685899830dd5837b47007809c76d91a098d52aabbf61e8ac467c59e5cc4610"
23
+ this.referVault =
24
+ "0x4ce9a19b594599536c53edb25d22532f82f18038dc8ef618afd00fbbfb9845ef"
25
+ this.validator =
26
+ "0xd30018ec3f5ff1a3c75656abf927a87d7f0529e6dc89c7ddd1bd27ecb05e3db2"
27
+ }
28
+
29
+ async swap(
30
+ client: AggregatorClient,
31
+ txb: Transaction,
32
+ path: Path,
33
+ inputCoin: TransactionObjectArgument
34
+ ): Promise<TransactionObjectArgument> {
35
+ const { direction } = path
36
+
37
+ if (!direction) {
38
+ throw new Error("Afsui not support b2a swap")
39
+ }
40
+
41
+ const func = "swap_a2b"
42
+
43
+ const args = [
44
+ txb.object(this.stakedSuiVault),
45
+ txb.object(this.safe),
46
+ txb.object("0x5"),
47
+ txb.object(this.referVault),
48
+ txb.object(this.validator),
49
+ inputCoin,
50
+ ]
51
+
52
+ const res = txb.moveCall({
53
+ target: `${client.publishedAt()}::afsui::${func}`,
54
+ typeArguments: [],
55
+ arguments: args,
56
+ }) as TransactionObjectArgument
57
+
58
+ return res
59
+ }
60
+ }
@@ -1,142 +1,89 @@
1
1
  import {
2
- TransactionArgument,
3
2
  Transaction,
3
+ TransactionArgument,
4
4
  TransactionObjectArgument,
5
+ TransactionResult,
5
6
  } from "@mysten/sui/transactions"
6
- import { AggregatorConfig, ENV } from "../config"
7
- import {
8
- AFTERMATH_MODULE,
9
- AGGREGATOR,
10
- MAINNET_AFTERMATH_INSURANCE_FUND_ID,
11
- MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID,
12
- MAINNET_AFTERMATH_REFERRAL_VAULT_ID,
13
- MAINNET_AFTERMATH_REGISTRY_ID,
14
- MAINNET_AFTERMATH_TREASURY_ID,
15
- SWAP_A2B_FUNC,
16
- SWAP_B2A_FUNC,
17
- TESTNET_AFTERMATH_INSURANCE_FUND_ID,
18
- TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID,
19
- TESTNET_AFTERMATH_REFERRAL_VAULT_ID,
20
- TESTNET_AFTERMATH_REGISTRY_ID,
21
- TESTNET_AFTERMATH_TREASURY_ID,
22
- } from "../const"
23
- import { ConfigErrorCode, TransactionErrorCode } from "../errors"
24
- import { createTarget } from "../utils"
7
+ import { AggregatorClient, Dex, Env, Path } from ".."
8
+ import BN from "bn.js"
25
9
 
26
- export type AftermathSwapParams = {
27
- poolId: string
28
- amount: TransactionArgument
29
- amountOut: number
30
- amountLimit: number
31
- a2b: boolean
32
- byAmountIn: boolean
33
- coinA?: TransactionObjectArgument
34
- coinB?: TransactionObjectArgument
35
- useFullInputCoinAmount: boolean
36
- coinAType: string
37
- coinBType: string
38
- slippage: number
39
- lpSupplyType: string
40
- }
10
+ export class Aftermath implements Dex {
11
+ private slippage: string
12
+ private poolRegistry: string
13
+ private protocolFeeVault: string
14
+ private treasury: string
15
+ private insuranceFund: string
16
+ private referrealVault: string
41
17
 
42
- export type AftermathSwapResult = {
43
- targetCoin: TransactionObjectArgument
44
- amountIn: TransactionArgument
45
- amountOut: TransactionArgument
46
- txb: Transaction
47
- }
18
+ constructor(env: Env) {
19
+ if (env !== Env.Mainnet) {
20
+ throw new Error("Aftermath only supported on mainnet")
21
+ }
48
22
 
49
- export async function AftermathAmmSwapMovecall(
50
- swapParams: AftermathSwapParams,
51
- txb: Transaction,
52
- config: AggregatorConfig
53
- ): Promise<AftermathSwapResult> {
54
- const aggregatorPackage = config.getPackage(AGGREGATOR)
55
- if (aggregatorPackage == null) {
56
- throw new AggregateError(
57
- "Aggregator package not set",
58
- ConfigErrorCode.MissAggregatorPackage
59
- )
23
+ this.slippage = "900000000000000000"
24
+ this.poolRegistry =
25
+ "0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae"
26
+ this.protocolFeeVault =
27
+ "0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4"
28
+ this.treasury =
29
+ "0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce"
30
+ this.insuranceFund =
31
+ "0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b"
32
+ this.referrealVault =
33
+ "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278"
60
34
  }
61
- const aggregatorPublishedAt = aggregatorPackage.publishedAt
62
35
 
63
- if (swapParams.a2b) {
64
- if (swapParams.coinA == null) {
65
- throw new AggregateError(
66
- "coinA is required",
67
- TransactionErrorCode.MissCoinA
68
- )
69
- }
70
- } else {
71
- if (swapParams.coinB == null) {
72
- throw new AggregateError(
73
- "coinB is required",
74
- TransactionErrorCode.MissCoinB
75
- )
76
- }
36
+ amountLimit(exportAmountOut: number): number {
37
+ return Number(
38
+ new BN(exportAmountOut)
39
+ .mul(new BN(this.slippage))
40
+ .div(new BN("1000000000000000000"))
41
+ .toString()
42
+ )
77
43
  }
78
44
 
79
- const poolRegistryID =
80
- config.getENV() === ENV.MAINNET
81
- ? MAINNET_AFTERMATH_REGISTRY_ID
82
- : TESTNET_AFTERMATH_REGISTRY_ID
83
-
84
- const vaultID =
85
- config.getENV() === ENV.MAINNET
86
- ? MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID
87
- : TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID
88
-
89
- const treasuryID =
90
- config.getENV() === ENV.MAINNET
91
- ? MAINNET_AFTERMATH_TREASURY_ID
92
- : TESTNET_AFTERMATH_TREASURY_ID
45
+ async swap(
46
+ client: AggregatorClient,
47
+ txb: Transaction,
48
+ path: Path,
49
+ inputCoin: TransactionObjectArgument
50
+ ): Promise<TransactionObjectArgument> {
51
+ const { direction, from, target } = path
93
52
 
94
- const insuranceFundID =
95
- config.getENV() === ENV.MAINNET
96
- ? MAINNET_AFTERMATH_INSURANCE_FUND_ID
97
- : TESTNET_AFTERMATH_INSURANCE_FUND_ID
53
+ const [func, coinAType, coinBType] = direction
54
+ ? ["swap_a2b", from, target]
55
+ : ["swap_b2a", target, from]
98
56
 
99
- const referrealVaultID =
100
- config.getENV() === ENV.MAINNET
101
- ? MAINNET_AFTERMATH_REFERRAL_VAULT_ID
102
- : TESTNET_AFTERMATH_REFERRAL_VAULT_ID
103
-
104
- const swapCoin = swapParams.a2b ? swapParams.coinA! : swapParams.coinB!
105
-
106
- const slippageArg = (1 - swapParams.slippage) * 1000000000000000000
107
-
108
- const args = [
109
- txb.object(swapParams.poolId),
110
- txb.object(poolRegistryID),
111
- txb.object(vaultID),
112
- txb.object(treasuryID),
113
- txb.object(insuranceFundID),
114
- txb.object(referrealVaultID),
115
- swapParams.amount,
116
- txb.pure.u64(swapParams.amountLimit),
117
- txb.pure.u64(swapParams.amountOut),
118
- txb.pure.u64(slippageArg),
119
- swapCoin,
120
- txb.pure.bool(swapParams.useFullInputCoinAmount),
121
- ]
57
+ if (path.extendedDetails == null) {
58
+ throw new Error("Extended details not supported")
59
+ } else {
60
+ if (path.extendedDetails.aftermathLpSupplyType == null) {
61
+ throw new Error("LP supply type not supported")
62
+ }
63
+ }
122
64
 
123
- const func = swapParams.a2b ? SWAP_A2B_FUNC : SWAP_B2A_FUNC
65
+ const args = [
66
+ txb.object(path.id),
67
+ txb.object(this.poolRegistry),
68
+ txb.object(this.protocolFeeVault),
69
+ txb.object(this.treasury),
70
+ txb.object(this.insuranceFund),
71
+ txb.object(this.referrealVault),
72
+ txb.pure.u64(this.amountLimit(path.amountOut)),
73
+ txb.pure.u64(this.slippage),
74
+ inputCoin,
75
+ ]
124
76
 
125
- const target = createTarget(aggregatorPublishedAt, AFTERMATH_MODULE, func)
77
+ const res = txb.moveCall({
78
+ target: `${client.publishedAt()}::aftermath::${func}`,
79
+ typeArguments: [
80
+ coinAType,
81
+ coinBType,
82
+ path.extendedDetails.aftermathLpSupplyType,
83
+ ],
84
+ arguments: args,
85
+ }) as TransactionObjectArgument
126
86
 
127
- const res = txb.moveCall({
128
- target,
129
- typeArguments: [
130
- swapParams.coinAType,
131
- swapParams.coinBType,
132
- swapParams.lpSupplyType,
133
- ],
134
- arguments: args,
135
- })
136
- return {
137
- targetCoin: res[0],
138
- amountIn: res[1],
139
- amountOut: res[2],
140
- txb,
87
+ return res
141
88
  }
142
89
  }