@cetusprotocol/aggregator-sdk 0.0.7 → 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 -1556
- package/dist/index.mjs +1215 -1543
- 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 -31
- 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 +127 -81
- 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 -174
- 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 -341
- package/src/utils/account_cap.ts +0 -62
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AGGREGATOR,
|
|
3
|
-
CHECK_COINS_THRESHOLD_FUNC,
|
|
4
|
-
JOIN_FUNC,
|
|
5
|
-
PAY_MODULE,
|
|
6
|
-
SuiZeroCoinFn,
|
|
7
|
-
TRANSFER_OR_DESTORY_COIN_FUNC,
|
|
8
|
-
UTILS_MODULE,
|
|
9
|
-
} from "../const"
|
|
10
|
-
import { CoinAsset } from "../types/sui"
|
|
11
|
-
import { CoinUtils } from "../types/CoinAssist"
|
|
12
|
-
import { ConfigErrorCode, TransactionErrorCode } from "../errors"
|
|
13
|
-
import { createTarget } from "../utils"
|
|
14
|
-
import { AggregatorConfig } from "../config"
|
|
15
|
-
import {
|
|
16
|
-
Transaction,
|
|
17
|
-
TransactionArgument,
|
|
18
|
-
TransactionObjectArgument,
|
|
19
|
-
} from "@mysten/sui/transactions"
|
|
20
|
-
import { SUI_FRAMEWORK_ADDRESS } from "@mysten/sui/utils"
|
|
21
|
-
|
|
22
|
-
export function mintZeroCoin(
|
|
23
|
-
txb: Transaction,
|
|
24
|
-
coinType: string
|
|
25
|
-
): TransactionObjectArgument {
|
|
26
|
-
return txb.moveCall({
|
|
27
|
-
target: SuiZeroCoinFn,
|
|
28
|
-
typeArguments: [coinType],
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type BuildCoinResult = {
|
|
33
|
-
targetCoin: TransactionObjectArgument
|
|
34
|
-
isMintZeroCoin: boolean
|
|
35
|
-
targetCoinAmount: number
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function buildInputCoin(
|
|
39
|
-
txb: Transaction,
|
|
40
|
-
allCoins: CoinAsset[],
|
|
41
|
-
amount: bigint,
|
|
42
|
-
coinType: string
|
|
43
|
-
): BuildCoinResult {
|
|
44
|
-
const usedCoinAsests = CoinUtils.getCoinAssets(coinType, allCoins)
|
|
45
|
-
if (amount === BigInt(0) && usedCoinAsests.length === 0) {
|
|
46
|
-
const zeroCoin = mintZeroCoin(txb, coinType)
|
|
47
|
-
return {
|
|
48
|
-
targetCoin: zeroCoin,
|
|
49
|
-
isMintZeroCoin: true,
|
|
50
|
-
targetCoinAmount: 0,
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
console.log("used coin asests", usedCoinAsests)
|
|
55
|
-
|
|
56
|
-
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests)
|
|
57
|
-
|
|
58
|
-
console.log("totalCoinBalance", totalCoinBalance)
|
|
59
|
-
console.log("amount", amount)
|
|
60
|
-
if (totalCoinBalance < amount) {
|
|
61
|
-
throw new AggregateError(
|
|
62
|
-
"Insufficient balance when build merge coin",
|
|
63
|
-
TransactionErrorCode.InsufficientBalance
|
|
64
|
-
)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (CoinUtils.isSuiCoin(coinType) && amount <= BigInt(950000000000)) {
|
|
68
|
-
const resultCoin = txb.splitCoins(txb.gas, [
|
|
69
|
-
txb.pure.u64(amount.toString()),
|
|
70
|
-
])
|
|
71
|
-
return {
|
|
72
|
-
targetCoin: resultCoin,
|
|
73
|
-
isMintZeroCoin: true,
|
|
74
|
-
targetCoinAmount: Number(amount.toString()),
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// sort used coin by amount, asc
|
|
79
|
-
let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests)
|
|
80
|
-
|
|
81
|
-
// find first three coin if greater than amount
|
|
82
|
-
let totalThreeCoinBalance = sortCoinAssets
|
|
83
|
-
.slice(0, 3)
|
|
84
|
-
.reduce((acc, coin) => acc + coin.balance, BigInt(0))
|
|
85
|
-
if (totalThreeCoinBalance < BigInt(amount)) {
|
|
86
|
-
sortCoinAssets = CoinUtils.sortByBalanceDes(usedCoinAsests)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let selectedCoinResult = CoinUtils.selectCoinObjectIdGreaterThanOrEqual(
|
|
90
|
-
sortCoinAssets,
|
|
91
|
-
amount
|
|
92
|
-
)
|
|
93
|
-
const [masterCoin, ...mergedCoin] = selectedCoinResult.objectArray
|
|
94
|
-
|
|
95
|
-
if (mergedCoin.length > 0) {
|
|
96
|
-
txb.mergeCoins(
|
|
97
|
-
masterCoin,
|
|
98
|
-
mergedCoin.map((coin) => txb.object(coin))
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
targetCoin: txb.object(masterCoin),
|
|
104
|
-
isMintZeroCoin: false,
|
|
105
|
-
targetCoinAmount: Number(amount.toString()),
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function transferOrDestoryCoin(
|
|
110
|
-
txb: Transaction,
|
|
111
|
-
coinObject: TransactionObjectArgument,
|
|
112
|
-
coinType: string,
|
|
113
|
-
config: AggregatorConfig
|
|
114
|
-
) {
|
|
115
|
-
const aggregatorPackage = config.getPackage(AGGREGATOR)
|
|
116
|
-
if (aggregatorPackage == null) {
|
|
117
|
-
throw new AggregateError(
|
|
118
|
-
"Aggregator package not set",
|
|
119
|
-
ConfigErrorCode.MissAggregatorPackage
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
const aggregatorPublishedAt = aggregatorPackage.publishedAt
|
|
123
|
-
|
|
124
|
-
txb.moveCall({
|
|
125
|
-
target: createTarget(
|
|
126
|
-
aggregatorPublishedAt,
|
|
127
|
-
UTILS_MODULE,
|
|
128
|
-
TRANSFER_OR_DESTORY_COIN_FUNC
|
|
129
|
-
),
|
|
130
|
-
typeArguments: [coinType],
|
|
131
|
-
arguments: [coinObject],
|
|
132
|
-
})
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export function checkCoinThresholdAndMergeCoin(
|
|
136
|
-
txb: Transaction,
|
|
137
|
-
coins: TransactionObjectArgument[],
|
|
138
|
-
coinType: string,
|
|
139
|
-
amountLimit: number,
|
|
140
|
-
config: AggregatorConfig
|
|
141
|
-
): TransactionArgument {
|
|
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 vec = txb.makeMoveVec({
|
|
152
|
-
elements: coins,
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
txb.moveCall({
|
|
156
|
-
target: createTarget(
|
|
157
|
-
aggregatorPublishedAt,
|
|
158
|
-
UTILS_MODULE,
|
|
159
|
-
CHECK_COINS_THRESHOLD_FUNC
|
|
160
|
-
),
|
|
161
|
-
typeArguments: [coinType],
|
|
162
|
-
arguments: [vec, txb.pure.u64(amountLimit)],
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
const zeroCoin = mintZeroCoin(txb, coinType)
|
|
166
|
-
|
|
167
|
-
txb.moveCall({
|
|
168
|
-
target: createTarget(SUI_FRAMEWORK_ADDRESS, PAY_MODULE, JOIN_FUNC),
|
|
169
|
-
typeArguments: [coinType],
|
|
170
|
-
arguments: [zeroCoin, vec],
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
return zeroCoin
|
|
174
|
-
}
|
|
@@ -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
|
-
}
|
package/src/transaction/flowx.ts
DELETED
|
@@ -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
|
-
}
|
package/src/transaction/kriya.ts
DELETED
|
@@ -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
|
-
}
|