@cetusprotocol/aggregator-sdk 0.0.8 → 0.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/dist/index.d.mts +98 -102
- package/dist/index.d.ts +98 -102
- package/dist/index.js +1228 -1552
- package/dist/index.mjs +1215 -1539
- package/dist/src/api.d.ts +53 -0
- package/dist/src/client.d.ts +38 -65
- package/dist/src/const.d.ts +0 -9
- package/dist/src/index.d.ts +9 -5
- package/dist/src/transaction/afsui.d.ts +10 -0
- package/dist/src/transaction/aftermath.d.ts +13 -24
- package/dist/src/transaction/cetus.d.ts +9 -33
- package/dist/src/transaction/deepbook_v2.d.ts +14 -0
- package/dist/src/transaction/flowx_v2.d.ts +7 -0
- package/dist/src/transaction/haedal.d.ts +6 -0
- package/dist/src/transaction/index.d.ts +6 -1
- package/dist/src/transaction/kriya_v2.d.ts +6 -0
- package/dist/src/transaction/kriya_v3.d.ts +7 -0
- package/dist/src/transaction/swap.d.ts +1 -2
- package/dist/src/transaction/turbos.d.ts +7 -22
- package/dist/src/transaction/volo.d.ts +8 -0
- package/dist/src/utils/coin.d.ts +9 -0
- package/dist/{src → tests}/test_data.test.d.ts +1 -0
- package/package.json +2 -2
- package/src/api.ts +144 -0
- package/src/client.ts +295 -239
- package/src/const.ts +0 -13
- package/src/index.ts +10 -5
- package/src/transaction/afsui.ts +60 -0
- package/src/transaction/aftermath.ts +71 -124
- package/src/transaction/cetus.ts +87 -258
- package/src/transaction/deepbook_v2.ts +122 -0
- package/src/transaction/flowx_v2.ts +42 -0
- package/src/transaction/haedal.ts +41 -0
- package/src/transaction/index.ts +17 -1
- package/src/transaction/kriya_v2.ts +38 -0
- package/src/transaction/kriya_v3.ts +46 -0
- package/src/transaction/swap.ts +17 -24
- package/src/transaction/turbos.ts +40 -99
- package/src/transaction/volo.ts +52 -0
- package/src/utils/coin.ts +91 -6
- package/src/utils/transaction.ts +1 -1
- package/tests/router.test.ts +123 -73
- package/{src → tests}/test_data.test.ts +2 -0
- package/dist/src/config.d.ts +0 -26
- package/dist/src/transaction/common.d.ts +0 -12
- package/dist/src/transaction/deepbook.d.ts +0 -21
- package/dist/src/transaction/flowx.d.ts +0 -20
- package/dist/src/transaction/kriya.d.ts +0 -21
- package/dist/src/transaction/router.d.ts +0 -6
- package/dist/src/utils/account_cap.d.ts +0 -7
- package/src/config.ts +0 -65
- package/src/transaction/common.ts +0 -169
- package/src/transaction/deepbook.ts +0 -126
- package/src/transaction/flowx.ts +0 -97
- package/src/transaction/kriya.ts +0 -77
- package/src/transaction/router.ts +0 -339
- package/src/utils/account_cap.ts +0 -62
package/src/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
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 {
|
|
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
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
package/src/transaction/cetus.ts
CHANGED
|
@@ -3,279 +3,108 @@ import {
|
|
|
3
3
|
TransactionArgument,
|
|
4
4
|
TransactionObjectArgument,
|
|
5
5
|
} from "@mysten/sui/transactions"
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
AGGREGATOR,
|
|
9
|
-
CLOCK_ADDRESS,
|
|
10
|
-
CETUS_MODULE,
|
|
11
|
-
FlashSwapA2BFunc,
|
|
12
|
-
MAINNET_CETUS_GLOBAL_CONFIG_ID,
|
|
13
|
-
TESTNET_CETUS_GLOBAL_CONFIG_ID,
|
|
14
|
-
FlashSwapWithPartnerA2BFunc,
|
|
15
|
-
FlashSwapWithPartnerB2AFunc,
|
|
16
|
-
FlashSwapB2AFunc,
|
|
17
|
-
REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC,
|
|
18
|
-
REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC,
|
|
19
|
-
REPAY_FLASH_SWAP_A2B_FUNC,
|
|
20
|
-
REPAY_FLASH_SWAP_B2A_FUNC,
|
|
21
|
-
} from "../const"
|
|
22
|
-
import { ConfigErrorCode, TransactionErrorCode } from "../errors"
|
|
23
|
-
import BN from "bn.js"
|
|
24
|
-
import { createTarget } from "../utils"
|
|
25
|
-
|
|
26
|
-
export type CetusSwapParams = {
|
|
27
|
-
poolId: string
|
|
28
|
-
amount: TransactionArgument
|
|
29
|
-
amountLimit: string
|
|
30
|
-
a2b: boolean
|
|
31
|
-
byAmountIn: boolean
|
|
32
|
-
sqrtPriceLimit: BN
|
|
33
|
-
partner?: string
|
|
34
|
-
coinAType: string
|
|
35
|
-
coinBType: string
|
|
36
|
-
}
|
|
6
|
+
import { AggregatorClient, CLOCK_ADDRESS, Dex, Env, Path } from ".."
|
|
37
7
|
|
|
38
8
|
export type CetusFlashSwapResult = {
|
|
39
9
|
targetCoin: TransactionObjectArgument
|
|
40
10
|
flashReceipt: TransactionObjectArgument
|
|
41
11
|
payAmount: TransactionArgument
|
|
42
|
-
swapedAmount: TransactionArgument
|
|
43
|
-
txb: Transaction
|
|
44
12
|
}
|
|
45
13
|
|
|
46
|
-
export
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
config: AggregatorConfig
|
|
50
|
-
): CetusFlashSwapResult {
|
|
51
|
-
const aggregatorPackage = config.getPackage(AGGREGATOR)
|
|
52
|
-
if (aggregatorPackage == null) {
|
|
53
|
-
throw new AggregateError(
|
|
54
|
-
"Aggregator package not set",
|
|
55
|
-
ConfigErrorCode.MissAggregatorPackage
|
|
56
|
-
)
|
|
57
|
-
}
|
|
58
|
-
const aggregatorPublishedAt = aggregatorPackage.publishedAt
|
|
59
|
-
|
|
60
|
-
const globalConfigId =
|
|
61
|
-
config.getENV() === ENV.MAINNET
|
|
62
|
-
? MAINNET_CETUS_GLOBAL_CONFIG_ID
|
|
63
|
-
: TESTNET_CETUS_GLOBAL_CONFIG_ID
|
|
64
|
-
|
|
65
|
-
const hasPartner = swapParams.partner != null && swapParams.partner.length > 0
|
|
14
|
+
export class Cetus implements Dex {
|
|
15
|
+
private globalConfig: string
|
|
16
|
+
private partner: string
|
|
66
17
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
txb.pure.u64(swapParams.amountLimit),
|
|
73
|
-
txb.pure.bool(swapParams.byAmountIn),
|
|
74
|
-
txb.pure.u128(swapParams.sqrtPriceLimit.toString()),
|
|
75
|
-
txb.object(swapParams.partner!.toString()),
|
|
76
|
-
txb.object(CLOCK_ADDRESS),
|
|
77
|
-
]
|
|
78
|
-
: [
|
|
79
|
-
txb.object(globalConfigId),
|
|
80
|
-
txb.object(swapParams.poolId),
|
|
81
|
-
swapParams.amount,
|
|
82
|
-
txb.pure.u64(swapParams.amountLimit),
|
|
83
|
-
txb.pure.bool(swapParams.byAmountIn),
|
|
84
|
-
txb.pure.u128(swapParams.sqrtPriceLimit.toString()),
|
|
85
|
-
txb.object(CLOCK_ADDRESS),
|
|
86
|
-
]
|
|
18
|
+
constructor(env: Env, partner?: string) {
|
|
19
|
+
this.globalConfig =
|
|
20
|
+
env === Env.Mainnet
|
|
21
|
+
? "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f"
|
|
22
|
+
: "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a"
|
|
87
23
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
func = FlashSwapWithPartnerA2BFunc
|
|
92
|
-
} else {
|
|
93
|
-
func = FlashSwapWithPartnerB2AFunc
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
if (swapParams.a2b) {
|
|
97
|
-
func = FlashSwapA2BFunc
|
|
98
|
-
} else {
|
|
99
|
-
func = FlashSwapB2AFunc
|
|
100
|
-
}
|
|
24
|
+
this.partner =
|
|
25
|
+
partner ??
|
|
26
|
+
"0x639b5e433da31739e800cd085f356e64cae222966d0f1b11bd9dc76b322ff58b"
|
|
101
27
|
}
|
|
102
28
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
export type SwapResult = {
|
|
132
|
-
repayTargetCoin: TransactionObjectArgument
|
|
133
|
-
nextInputAmount?: TransactionArgument
|
|
134
|
-
flashTargetCoin?: TransactionObjectArgument
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export async function cetusRepayFlashSwapMovecall(
|
|
138
|
-
repayParams: repayParams,
|
|
139
|
-
txb: Transaction,
|
|
140
|
-
config: AggregatorConfig
|
|
141
|
-
): Promise<SwapResult> {
|
|
142
|
-
const aggregatorPackage = config.getPackage(AGGREGATOR)
|
|
143
|
-
if (aggregatorPackage == null) {
|
|
144
|
-
throw new AggregateError(
|
|
145
|
-
"Aggregator package not set",
|
|
146
|
-
ConfigErrorCode.MissAggregatorPackage
|
|
147
|
-
)
|
|
148
|
-
}
|
|
149
|
-
const aggregatorPublishedAt = aggregatorPackage.publishedAt
|
|
150
|
-
|
|
151
|
-
const globalConfigId =
|
|
152
|
-
config.getENV() === ENV.MAINNET
|
|
153
|
-
? MAINNET_CETUS_GLOBAL_CONFIG_ID
|
|
154
|
-
: TESTNET_CETUS_GLOBAL_CONFIG_ID
|
|
155
|
-
|
|
156
|
-
const hasPartner =
|
|
157
|
-
repayParams.partner != null && repayParams.partner.length > 0
|
|
158
|
-
|
|
159
|
-
if (repayParams.a2b) {
|
|
160
|
-
if (repayParams.coinA == null) {
|
|
161
|
-
throw new AggregateError(
|
|
162
|
-
"coinA is required",
|
|
163
|
-
TransactionErrorCode.MissCoinA
|
|
164
|
-
)
|
|
29
|
+
flash_swap(
|
|
30
|
+
client: AggregatorClient,
|
|
31
|
+
txb: Transaction,
|
|
32
|
+
path: Path,
|
|
33
|
+
by_amount_in: boolean
|
|
34
|
+
): CetusFlashSwapResult {
|
|
35
|
+
const { direction, from, target } = path
|
|
36
|
+
const [func, coinAType, coinBType] = direction
|
|
37
|
+
? ["flash_swap_a2b", from, target]
|
|
38
|
+
: ["flash_swap_b2a", target, from]
|
|
39
|
+
let amount = by_amount_in ? path.amountIn : path.amountOut
|
|
40
|
+
const args = [
|
|
41
|
+
txb.object(this.globalConfig),
|
|
42
|
+
txb.object(path.id),
|
|
43
|
+
txb.object(this.partner),
|
|
44
|
+
txb.pure.u64(amount),
|
|
45
|
+
txb.pure.bool(by_amount_in),
|
|
46
|
+
txb.object(CLOCK_ADDRESS),
|
|
47
|
+
]
|
|
48
|
+
const res: TransactionObjectArgument[] = txb.moveCall({
|
|
49
|
+
target: `${client.publishedAt()}::cetus::${func}`,
|
|
50
|
+
typeArguments: [coinAType, coinBType],
|
|
51
|
+
arguments: args,
|
|
52
|
+
})
|
|
53
|
+
return {
|
|
54
|
+
targetCoin: res[0],
|
|
55
|
+
flashReceipt: res[1],
|
|
56
|
+
payAmount: res[2],
|
|
165
57
|
}
|
|
166
|
-
} else {
|
|
167
|
-
if (repayParams.coinB == null) {
|
|
168
|
-
throw new AggregateError(
|
|
169
|
-
"coinB is required",
|
|
170
|
-
TransactionErrorCode.MissCoinB
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
let args
|
|
176
|
-
if (hasPartner) {
|
|
177
|
-
if (repayParams.a2b) {
|
|
178
|
-
args = [
|
|
179
|
-
txb.object(globalConfigId),
|
|
180
|
-
txb.object(repayParams.poolId),
|
|
181
|
-
repayParams.coinA!,
|
|
182
|
-
repayParams.receipt,
|
|
183
|
-
txb.object(repayParams.partner!),
|
|
184
|
-
]
|
|
185
|
-
} else {
|
|
186
|
-
args = [
|
|
187
|
-
txb.object(globalConfigId),
|
|
188
|
-
txb.object(repayParams.poolId),
|
|
189
|
-
repayParams.coinB!,
|
|
190
|
-
repayParams.receipt,
|
|
191
|
-
txb.object(repayParams.partner!),
|
|
192
|
-
]
|
|
193
|
-
}
|
|
194
|
-
} else {
|
|
195
|
-
if (repayParams.a2b) {
|
|
196
|
-
args = [
|
|
197
|
-
txb.object(globalConfigId),
|
|
198
|
-
txb.object(repayParams.poolId),
|
|
199
|
-
repayParams.coinA!,
|
|
200
|
-
repayParams.receipt,
|
|
201
|
-
]
|
|
202
|
-
} else {
|
|
203
|
-
args = [
|
|
204
|
-
txb.object(globalConfigId),
|
|
205
|
-
txb.object(repayParams.poolId),
|
|
206
|
-
repayParams.coinB!,
|
|
207
|
-
repayParams.receipt,
|
|
208
|
-
]
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
let func
|
|
213
|
-
if (hasPartner) {
|
|
214
|
-
if (repayParams.a2b) {
|
|
215
|
-
func = REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC
|
|
216
|
-
} else {
|
|
217
|
-
func = REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC
|
|
218
|
-
}
|
|
219
|
-
} else {
|
|
220
|
-
if (repayParams.a2b) {
|
|
221
|
-
func = REPAY_FLASH_SWAP_A2B_FUNC
|
|
222
|
-
} else {
|
|
223
|
-
func = REPAY_FLASH_SWAP_B2A_FUNC
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const target = createTarget(aggregatorPublishedAt, CETUS_MODULE, func)
|
|
228
|
-
|
|
229
|
-
const res: TransactionObjectArgument[] = txb.moveCall({
|
|
230
|
-
target,
|
|
231
|
-
typeArguments: [repayParams.coinAType, repayParams.coinBType],
|
|
232
|
-
arguments: args,
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
return {
|
|
236
|
-
repayTargetCoin: res[0],
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export async function cetusSwapWithOutLimit(
|
|
241
|
-
swapParams: CetusSwapParams,
|
|
242
|
-
fromCoin: TransactionObjectArgument,
|
|
243
|
-
txb: Transaction,
|
|
244
|
-
config: AggregatorConfig
|
|
245
|
-
): Promise<SwapResult> {
|
|
246
|
-
const flashResult = cetusFlashSwapMovecall(swapParams, txb, config)
|
|
247
|
-
|
|
248
|
-
const repayCoinA = swapParams.a2b ? fromCoin : undefined
|
|
249
|
-
const repayCoinB = swapParams.a2b ? undefined : fromCoin
|
|
250
|
-
|
|
251
|
-
const repayParams = {
|
|
252
|
-
poolId: swapParams.poolId,
|
|
253
|
-
a2b: swapParams.a2b,
|
|
254
|
-
coinA: repayCoinA,
|
|
255
|
-
coinB: repayCoinB,
|
|
256
|
-
receipt: flashResult.flashReceipt,
|
|
257
|
-
coinAType: swapParams.coinAType,
|
|
258
|
-
coinBType: swapParams.coinBType,
|
|
259
|
-
partner: swapParams.partner,
|
|
260
58
|
}
|
|
261
59
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
60
|
+
repay_flash_swap(
|
|
61
|
+
client: AggregatorClient,
|
|
62
|
+
txb: Transaction,
|
|
63
|
+
path: Path,
|
|
64
|
+
inputCoin: TransactionObjectArgument,
|
|
65
|
+
receipt: TransactionArgument
|
|
66
|
+
): TransactionObjectArgument {
|
|
67
|
+
const { direction, from, target } = path
|
|
68
|
+
const [func, coinAType, coinBType] = direction
|
|
69
|
+
? ["repay_flash_swap_a2b", from, target]
|
|
70
|
+
: ["repay_flash_swap_b2a", target, from]
|
|
71
|
+
const args = [
|
|
72
|
+
txb.object(this.globalConfig),
|
|
73
|
+
txb.object(path.id),
|
|
74
|
+
txb.object(this.partner),
|
|
75
|
+
inputCoin,
|
|
76
|
+
receipt,
|
|
77
|
+
]
|
|
78
|
+
const res = txb.moveCall({
|
|
79
|
+
target: `${client.publishedAt()}::cetus::${func}`,
|
|
80
|
+
typeArguments: [coinAType, coinBType],
|
|
81
|
+
arguments: args,
|
|
82
|
+
})
|
|
83
|
+
return res[0] as TransactionObjectArgument
|
|
268
84
|
}
|
|
269
85
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
txb,
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
86
|
+
async swap(
|
|
87
|
+
client: AggregatorClient,
|
|
88
|
+
txb: Transaction,
|
|
89
|
+
path: Path,
|
|
90
|
+
inputCoin: TransactionObjectArgument
|
|
91
|
+
): Promise<TransactionObjectArgument> {
|
|
92
|
+
const { direction, from, target } = path
|
|
93
|
+
const [func, coinAType, coinBType] = direction
|
|
94
|
+
? ["swap_a2b", from, target]
|
|
95
|
+
: ["swap_b2a", target, from]
|
|
96
|
+
const args = [
|
|
97
|
+
txb.object(this.globalConfig),
|
|
98
|
+
txb.object(path.id),
|
|
99
|
+
txb.object(this.partner),
|
|
100
|
+
inputCoin,
|
|
101
|
+
txb.object(CLOCK_ADDRESS),
|
|
102
|
+
]
|
|
103
|
+
const res = txb.moveCall({
|
|
104
|
+
target: `${client.publishedAt()}::cetus::${func}`,
|
|
105
|
+
typeArguments: [coinAType, coinBType],
|
|
106
|
+
arguments: args,
|
|
107
|
+
}) as TransactionArgument
|
|
108
|
+
return res
|
|
280
109
|
}
|
|
281
110
|
}
|