@chainflip/sdk 2.3.2 → 2.3.4-dev.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.
- package/dist/swap.cjs +347 -402
- package/dist/swap.d.cts +65 -190
- package/dist/swap.d.ts +65 -190
- package/dist/swap.js +289 -344
- package/package.json +5 -6
package/dist/swap.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { getEnvironment,
|
|
1
|
+
import { getEnvironment, getEvmChainId, getAllSupplyPoolsDepth, getAllBoostPoolsDepth, assert, SUPPLY_POOL_BOOST_FEE_BPS, isNotNullish, ADDRESSES } from './chunk-6KPGKVRC.js';
|
|
2
2
|
import { unreachable } from '@chainflip/utils/assertion';
|
|
3
|
-
import { chainflipChains, assetSymbols, isValidAssetAndChain,
|
|
3
|
+
import { chainflipChains, assetSymbols, isValidAssetAndChain, getInternalAssets, assetConstants, isChainflipAsset, internalAssetToRpcAsset, getInternalAsset, chainflipNetworks, chainflipAssets, readAssetValue, anyChainConstants, anyAssetConstants } from '@chainflip/utils/chainflip';
|
|
4
4
|
export { getInternalAsset as getChainflipId } from '@chainflip/utils/chainflip';
|
|
5
5
|
import { calculateTotalEffectiveBorrowableAmount, ppmToBps } from '@chainflip/utils/lending';
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import { isValidSolanaAddress } from '@chainflip/solana';
|
|
6
|
+
import { HttpClient } from '@chainflip/rpc';
|
|
7
|
+
import { bytesToHex } from '@chainflip/utils/bytes';
|
|
9
8
|
import * as ss58 from '@chainflip/utils/ss58';
|
|
9
|
+
import { toUpperCase, isHex } from '@chainflip/utils/string';
|
|
10
|
+
import { priceX128ToPrice } from '@chainflip/utils/tickMath';
|
|
11
|
+
import BigNumber2 from 'bignumber.js';
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import { isValidSolanaAddress } from '@chainflip/solana';
|
|
10
14
|
import { isValidTronAddress } from '@chainflip/utils/tron';
|
|
11
15
|
import * as bitcoin from '@chainflip/bitcoin';
|
|
12
|
-
import { toUpperCase, isHex } from '@chainflip/utils/string';
|
|
13
16
|
import * as ethers from 'ethers';
|
|
14
17
|
import { hexEncodeNumber } from '@chainflip/utils/number';
|
|
15
|
-
import { HttpClient } from '@chainflip/rpc';
|
|
16
|
-
import { bytesToHex } from '@chainflip/utils/bytes';
|
|
17
|
-
import { priceX128ToPrice } from '@chainflip/utils/tickMath';
|
|
18
|
-
import BigNumber2 from 'bignumber.js';
|
|
19
18
|
import { CHAINFLIP_BLOCK_TIME_SECONDS } from '@chainflip/utils/consts';
|
|
20
19
|
import axios from 'axios';
|
|
21
20
|
|
|
@@ -38,16 +37,22 @@ var chainflipAddress = z.string().refine(
|
|
|
38
37
|
return false;
|
|
39
38
|
}
|
|
40
39
|
},
|
|
41
|
-
(
|
|
40
|
+
{ error: (issue) => `${issue.input} is not a valid Chainflip address` }
|
|
42
41
|
);
|
|
43
|
-
z.string().refine(isValidSolanaAddress,
|
|
44
|
-
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
})
|
|
49
|
-
var
|
|
50
|
-
var
|
|
42
|
+
z.string().refine(isValidSolanaAddress, {
|
|
43
|
+
error: (issue) => `${issue.input} is not a valid Solana address`
|
|
44
|
+
});
|
|
45
|
+
z.string().refine(isValidTronAddress, {
|
|
46
|
+
error: (issue) => `${issue.input} is not a valid Tron address`
|
|
47
|
+
});
|
|
48
|
+
var u16 = z.number().int().nonnegative().max(65535);
|
|
49
|
+
var u32 = z.number().int().nonnegative().max(4294967295);
|
|
50
|
+
var u128 = z.union([
|
|
51
|
+
z.number().int().nonnegative().safe(),
|
|
52
|
+
numericString,
|
|
53
|
+
hexString.refine((v) => v.length > 2, { message: "hex string must not be empty" })
|
|
54
|
+
]).transform((arg) => BigInt(arg)).refine((n) => n < 2n ** 128n, { message: "must be less than 2^128" });
|
|
55
|
+
var unsignedInteger = u128;
|
|
51
56
|
var chain = z.enum(chainflipChains);
|
|
52
57
|
var asset = z.enum(assetSymbols);
|
|
53
58
|
var uncheckedAssetAndChain = z.object({
|
|
@@ -55,58 +60,8 @@ var uncheckedAssetAndChain = z.object({
|
|
|
55
60
|
chain: z.string()
|
|
56
61
|
});
|
|
57
62
|
var assetAndChain = uncheckedAssetAndChain.refine(
|
|
58
|
-
(value) => isValidAssetAndChain(value)
|
|
63
|
+
(value) => chain.safeParse(value.chain).success && asset.safeParse(value.asset).success && isValidAssetAndChain(value)
|
|
59
64
|
);
|
|
60
|
-
|
|
61
|
-
// ../shared/src/api/encodeVaultSwapData.ts
|
|
62
|
-
var EncodedVaultSwapData = z.discriminatedUnion("chain", [
|
|
63
|
-
z.object({
|
|
64
|
-
chain: z.literal("Bitcoin"),
|
|
65
|
-
nulldataPayload: hexString,
|
|
66
|
-
depositAddress: z.string()
|
|
67
|
-
}),
|
|
68
|
-
z.object({
|
|
69
|
-
chain: z.enum(["Ethereum", "Arbitrum", "Bsc"]),
|
|
70
|
-
value: z.string(),
|
|
71
|
-
to: hexString,
|
|
72
|
-
calldata: hexString,
|
|
73
|
-
sourceTokenAddress: hexString.optional()
|
|
74
|
-
}),
|
|
75
|
-
z.object({
|
|
76
|
-
chain: z.literal("Tron"),
|
|
77
|
-
calldata: hexString,
|
|
78
|
-
value: z.string(),
|
|
79
|
-
to: tronAddress,
|
|
80
|
-
note: hexString,
|
|
81
|
-
sourceTokenAddress: tronAddress.optional()
|
|
82
|
-
}),
|
|
83
|
-
z.object({
|
|
84
|
-
chain: z.literal("Solana"),
|
|
85
|
-
programId: z.string(),
|
|
86
|
-
data: hexString,
|
|
87
|
-
accounts: z.array(
|
|
88
|
-
z.object({
|
|
89
|
-
pubkey: z.string(),
|
|
90
|
-
isSigner: z.boolean(),
|
|
91
|
-
isWritable: z.boolean()
|
|
92
|
-
})
|
|
93
|
-
)
|
|
94
|
-
})
|
|
95
|
-
]);
|
|
96
|
-
var NetworkInfo = z3.object({
|
|
97
|
-
assets: z3.array(
|
|
98
|
-
z3.object({
|
|
99
|
-
asset: z3.enum(chainflipAssets),
|
|
100
|
-
vaultSwapDepositsEnabled: z3.boolean(),
|
|
101
|
-
depositChannelDepositsEnabled: z3.boolean(),
|
|
102
|
-
depositChannelCreationEnabled: z3.boolean(),
|
|
103
|
-
egressEnabled: z3.boolean(),
|
|
104
|
-
boostDepositsEnabled: z3.boolean(),
|
|
105
|
-
livePriceProtectionEnabled: z3.boolean()
|
|
106
|
-
})
|
|
107
|
-
),
|
|
108
|
-
cfBrokerCommissionBps: z3.number()
|
|
109
|
-
});
|
|
110
65
|
var validatePolkadotAddress = (address) => {
|
|
111
66
|
if (isHex(address)) return address.length === 66;
|
|
112
67
|
try {
|
|
@@ -169,7 +124,7 @@ var validators = {
|
|
|
169
124
|
Bsc: validateEvmAddress
|
|
170
125
|
}
|
|
171
126
|
};
|
|
172
|
-
var validateAddress = (chain2, address, network) => validators[network][chain2](address);
|
|
127
|
+
var validateAddress = (chain2, address, network) => Object.hasOwn(validators, network) && Object.hasOwn(validators[network], chain2) && validators[network][chain2](address);
|
|
173
128
|
|
|
174
129
|
// ../shared/src/api/openAccountCreationDepositChannel.ts
|
|
175
130
|
var getAccountCreationDepositChannelSchema = (network) => z.object({
|
|
@@ -184,15 +139,15 @@ var getAccountCreationDepositChannelSchema = (network) => z.object({
|
|
|
184
139
|
}))
|
|
185
140
|
}),
|
|
186
141
|
transactionMetadata: z.object({
|
|
187
|
-
nonce:
|
|
188
|
-
expiryBlock:
|
|
142
|
+
nonce: u32,
|
|
143
|
+
expiryBlock: u32.positive()
|
|
189
144
|
}).transform(({ nonce, expiryBlock }) => ({
|
|
190
145
|
nonce,
|
|
191
146
|
expiry_block: expiryBlock
|
|
192
147
|
})),
|
|
193
148
|
asset: assetAndChain,
|
|
194
149
|
refundAddress: z.string(),
|
|
195
|
-
boostFeeBps:
|
|
150
|
+
boostFeeBps: u16.optional().default(0)
|
|
196
151
|
}).superRefine(({ asset: asset2, refundAddress }, ctx) => {
|
|
197
152
|
if (!validateAddress(asset2.chain, refundAddress, network)) {
|
|
198
153
|
ctx.addIssue({
|
|
@@ -201,29 +156,48 @@ var getAccountCreationDepositChannelSchema = (network) => z.object({
|
|
|
201
156
|
});
|
|
202
157
|
}
|
|
203
158
|
});
|
|
159
|
+
|
|
160
|
+
// ../shared/src/strings.ts
|
|
161
|
+
var toCamelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
162
|
+
|
|
163
|
+
// ../shared/src/objects.ts
|
|
164
|
+
var transformKeysToCamelCase = (obj) => {
|
|
165
|
+
if (Array.isArray(obj)) {
|
|
166
|
+
return obj.map(transformKeysToCamelCase);
|
|
167
|
+
}
|
|
168
|
+
if (obj !== null && typeof obj === "object") {
|
|
169
|
+
return Object.fromEntries(
|
|
170
|
+
Object.entries(obj).map(([key, value]) => [
|
|
171
|
+
toCamelCase(key),
|
|
172
|
+
transformKeysToCamelCase(value)
|
|
173
|
+
])
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
return obj;
|
|
177
|
+
};
|
|
204
178
|
z.object({
|
|
205
179
|
srcChain: chain,
|
|
206
180
|
srcAsset: asset,
|
|
207
181
|
destChain: chain,
|
|
208
182
|
destAsset: asset,
|
|
209
183
|
amount: numericString.transform((v) => BigInt(v)).refine((v) => v > 0n, { message: "swap input amount must be greater than 0" }).refine((v) => v < 2n ** 128n, { message: "swap input amount must be less than 2^128" }),
|
|
210
|
-
brokerCommissionBps: numericOrEmptyString.transform((v) => Number(v)).optional(),
|
|
184
|
+
brokerCommissionBps: numericOrEmptyString.transform((v) => Number(v)).pipe(u16).optional(),
|
|
211
185
|
ccmGasBudget: z.union([
|
|
212
186
|
numericString.transform((v) => BigInt(v)).refine((v) => v < 2n ** 128n, { message: "ccm gas budget must be less than 2^128" }),
|
|
213
187
|
z.literal("undefined").transform(() => void 0)
|
|
214
188
|
// sdk version 1.8.2 sends undefined string if not set
|
|
215
189
|
]).optional(),
|
|
216
190
|
ccmMessageLengthBytes: z.union([
|
|
217
|
-
numericString.transform((v) => Number(v)),
|
|
191
|
+
numericString.transform((v) => Number(v)).pipe(u32),
|
|
218
192
|
z.literal("undefined").transform(() => void 0)
|
|
219
193
|
// sdk version 1.8.2 sends undefined string if not set
|
|
220
194
|
]).optional(),
|
|
221
|
-
dcaV2Enabled: booleanString.
|
|
195
|
+
dcaV2Enabled: booleanString.prefault("false"),
|
|
222
196
|
isVaultSwap: booleanString.optional(),
|
|
223
197
|
isOnChain: booleanString.optional(),
|
|
224
198
|
brokerAccount: chainflipAddress.optional(),
|
|
225
199
|
brokerIdSs58: chainflipAddress.optional(),
|
|
226
|
-
expectedResponseTimeMs: numericString.transform((v) => Number(v)).optional()
|
|
200
|
+
expectedResponseTimeMs: numericString.transform((v) => Number(v)).pipe(u32).optional()
|
|
227
201
|
}).transform((args, ctx) => {
|
|
228
202
|
const { srcAsset, destAsset } = getInternalAssets(args, false);
|
|
229
203
|
let hadError = false;
|
|
@@ -304,92 +278,20 @@ var ccmParamsSchema = z.object({
|
|
|
304
278
|
}));
|
|
305
279
|
var affiliateBroker = z.object({
|
|
306
280
|
account: chainflipAddress,
|
|
307
|
-
commissionBps:
|
|
281
|
+
commissionBps: u16
|
|
308
282
|
}).transform(({ account, commissionBps: bps }) => ({ account, bps }));
|
|
309
283
|
var dcaParams = z.object({
|
|
310
|
-
numberOfChunks:
|
|
311
|
-
chunkIntervalBlocks:
|
|
284
|
+
numberOfChunks: u32.min(1, { message: "number of chunks must be greater than 0" }),
|
|
285
|
+
chunkIntervalBlocks: u32
|
|
312
286
|
});
|
|
313
|
-
|
|
314
|
-
retryDurationBlocks:
|
|
287
|
+
z.object({
|
|
288
|
+
retryDurationBlocks: u32,
|
|
315
289
|
refundAddress: z.string(),
|
|
316
290
|
minPriceX128: numericString,
|
|
317
291
|
maxOraclePriceSlippage: basisPoints.nullish().transform((v) => v ?? null),
|
|
318
292
|
refundCcmMetadata: ccmParamsSchema.nullish().optional().default(null)
|
|
319
293
|
});
|
|
320
294
|
|
|
321
|
-
// ../shared/src/api/openSwapDepositChannel.ts
|
|
322
|
-
var assets = z.object({
|
|
323
|
-
srcAsset: assetAndChain,
|
|
324
|
-
destAsset: assetAndChain
|
|
325
|
-
});
|
|
326
|
-
var amountString = numericString.max(30, { message: "must have at most 30 digits" });
|
|
327
|
-
var getOpenSwapDepositChannelSchema = (network) => z.intersection(
|
|
328
|
-
assets,
|
|
329
|
-
z.object({
|
|
330
|
-
destAddress: z.string(),
|
|
331
|
-
amount: amountString,
|
|
332
|
-
ccmParams: ccmParamsSchema.optional(),
|
|
333
|
-
maxBoostFeeBps: z.number().optional(),
|
|
334
|
-
srcAddress: z.string().optional(),
|
|
335
|
-
fillOrKillParams,
|
|
336
|
-
dcaParams: dcaParams.optional(),
|
|
337
|
-
quote: z.object({
|
|
338
|
-
intermediateAmount: amountString.optional(),
|
|
339
|
-
egressAmount: amountString,
|
|
340
|
-
estimatedPrice: z.string().regex(/^[0-9]+(\.[0-9]+)?(e[+-]?[0-9]+)?$/i, { message: "must be a decimal number" }).refine((v) => Number(v) < 1e35, { message: "must be less than 10^35" }),
|
|
341
|
-
recommendedSlippageTolerancePercent: z.number().optional(),
|
|
342
|
-
recommendedLivePriceSlippageTolerancePercent: z.number().optional()
|
|
343
|
-
}).optional(),
|
|
344
|
-
takeCommission: z.boolean().optional()
|
|
345
|
-
})
|
|
346
|
-
).superRefine(({ ...val }, ctx) => {
|
|
347
|
-
if (!validateAddress(val.destAsset.chain, val.destAddress, network)) {
|
|
348
|
-
ctx.addIssue({
|
|
349
|
-
message: `Address "${val.destAddress}" is not a valid "${val.destAsset.chain}" address for "${network}"`,
|
|
350
|
-
code: z.ZodIssueCode.custom
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
if (!validateAddress(val.srcAsset.chain, val.fillOrKillParams.refundAddress, network)) {
|
|
354
|
-
ctx.addIssue({
|
|
355
|
-
message: `Address "${val.fillOrKillParams.refundAddress}" is not a valid "${val.srcAsset.chain}" address for "${network}"`,
|
|
356
|
-
code: z.ZodIssueCode.custom
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
}).transform(({ amount, ...rest }) => ({
|
|
360
|
-
...rest,
|
|
361
|
-
expectedDepositAmount: amount
|
|
362
|
-
}));
|
|
363
|
-
var DepositChannelInfo = z.object({
|
|
364
|
-
id: z.string(),
|
|
365
|
-
depositAddress: z.string(),
|
|
366
|
-
brokerCommissionBps: z.number(),
|
|
367
|
-
maxBoostFeeBps: z.number(),
|
|
368
|
-
issuedBlock: z.number(),
|
|
369
|
-
srcChainExpiryBlock: z.string(),
|
|
370
|
-
estimatedExpiryTime: z.number().optional(),
|
|
371
|
-
channelOpeningFee: z.string()
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
// ../shared/src/strings.ts
|
|
375
|
-
var toCamelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
376
|
-
|
|
377
|
-
// ../shared/src/objects.ts
|
|
378
|
-
var transformKeysToCamelCase = (obj) => {
|
|
379
|
-
if (Array.isArray(obj)) {
|
|
380
|
-
return obj.map(transformKeysToCamelCase);
|
|
381
|
-
}
|
|
382
|
-
if (obj !== null && typeof obj === "object") {
|
|
383
|
-
return Object.fromEntries(
|
|
384
|
-
Object.entries(obj).map(([key, value]) => [
|
|
385
|
-
toCamelCase(key),
|
|
386
|
-
transformKeysToCamelCase(value)
|
|
387
|
-
])
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
return obj;
|
|
391
|
-
};
|
|
392
|
-
|
|
393
295
|
// ../shared/src/broker.ts
|
|
394
296
|
var transformedDcaParamsSchema = dcaParams.transform(
|
|
395
297
|
({ numberOfChunks, chunkIntervalBlocks }) => ({
|
|
@@ -405,7 +307,7 @@ var transformedCcmParamsSchema = ccmParamsSchema.transform(
|
|
|
405
307
|
})
|
|
406
308
|
);
|
|
407
309
|
var transformedFokSchema = z.object({
|
|
408
|
-
retryDurationBlocks:
|
|
310
|
+
retryDurationBlocks: u32,
|
|
409
311
|
refundAddress: z.string(),
|
|
410
312
|
minPriceX128: numericString,
|
|
411
313
|
maxOraclePriceSlippage: basisPoints.nullish().transform((v) => v ?? null),
|
|
@@ -429,9 +331,9 @@ var getDepositAddressRequestSchema = (network) => z.object({
|
|
|
429
331
|
srcAsset: assetAndChain,
|
|
430
332
|
destAsset: assetAndChain,
|
|
431
333
|
destAddress: z.string(),
|
|
432
|
-
commissionBps:
|
|
334
|
+
commissionBps: u16.optional().default(0),
|
|
433
335
|
ccmParams: transformedCcmParamsSchema.optional(),
|
|
434
|
-
maxBoostFeeBps:
|
|
336
|
+
maxBoostFeeBps: u16.optional(),
|
|
435
337
|
affiliates: z.array(affiliateBroker).optional(),
|
|
436
338
|
fillOrKillParams: transformedFokSchema,
|
|
437
339
|
dcaParams: transformedDcaParamsSchema.optional()
|
|
@@ -455,13 +357,14 @@ var getVaultSwapParameterEncodingRequestSchema = (network) => z.object({
|
|
|
455
357
|
destAsset: assetAndChain,
|
|
456
358
|
destAddress: z.string(),
|
|
457
359
|
amount: unsignedInteger,
|
|
458
|
-
commissionBps:
|
|
360
|
+
commissionBps: u16.optional().default(0),
|
|
459
361
|
ccmParams: transformedCcmParamsSchema.optional(),
|
|
460
|
-
maxBoostFeeBps:
|
|
362
|
+
maxBoostFeeBps: u16.optional(),
|
|
461
363
|
affiliates: z.array(affiliateBroker).optional(),
|
|
462
364
|
fillOrKillParams: transformedFokSchema,
|
|
463
365
|
dcaParams: transformedDcaParamsSchema.optional(),
|
|
464
366
|
extraParams: z.object({ seed: hexString.optional() }).optional(),
|
|
367
|
+
// TODO: remove after brokerAccount is deprecated
|
|
465
368
|
brokerAccount: chainflipAddress.optional()
|
|
466
369
|
}).superRefine((val, ctx) => {
|
|
467
370
|
if (val.srcAddress && !validateAddress(val.srcAsset.chain, val.srcAddress, network)) {
|
|
@@ -485,7 +388,7 @@ var getVaultSwapParameterEncodingRequestSchema = (network) => z.object({
|
|
|
485
388
|
if (val.extraParams?.seed && val.extraParams.seed.length !== 66) {
|
|
486
389
|
ctx.addIssue({ message: "Seed must be 32 bytes", code: z.ZodIssueCode.custom });
|
|
487
390
|
}
|
|
488
|
-
}).transform((data) => {
|
|
391
|
+
}).transform((data, ctx) => {
|
|
489
392
|
let extraParams;
|
|
490
393
|
if (data.srcAsset.chain === "Bitcoin") {
|
|
491
394
|
const minOutputAmount = BigNumber2(data.amount.toString()).multipliedBy(priceX128ToPrice(data.fillOrKillParams.min_price)).toFixed(0);
|
|
@@ -502,7 +405,13 @@ var getVaultSwapParameterEncodingRequestSchema = (network) => z.object({
|
|
|
502
405
|
refund_parameters: data.fillOrKillParams
|
|
503
406
|
};
|
|
504
407
|
} else if (data.srcAsset.chain === "Solana") {
|
|
505
|
-
|
|
408
|
+
if (!data.srcAddress) {
|
|
409
|
+
ctx.addIssue({
|
|
410
|
+
message: "srcAddress is required for Solana",
|
|
411
|
+
code: z.ZodIssueCode.custom
|
|
412
|
+
});
|
|
413
|
+
return z.NEVER;
|
|
414
|
+
}
|
|
506
415
|
extraParams = {
|
|
507
416
|
chain: "Solana",
|
|
508
417
|
from: data.srcAddress,
|
|
@@ -511,7 +420,11 @@ var getVaultSwapParameterEncodingRequestSchema = (network) => z.object({
|
|
|
511
420
|
refund_parameters: data.fillOrKillParams
|
|
512
421
|
};
|
|
513
422
|
} else {
|
|
514
|
-
|
|
423
|
+
ctx.addIssue({
|
|
424
|
+
message: `parameter encoding is not supported for ${data.srcAsset.chain}`,
|
|
425
|
+
code: z.ZodIssueCode.custom
|
|
426
|
+
});
|
|
427
|
+
return z.NEVER;
|
|
515
428
|
}
|
|
516
429
|
return { ...data, extraParams };
|
|
517
430
|
});
|
|
@@ -571,12 +484,13 @@ var getParameterEncodingRequestSchema = (network) => z.object({
|
|
|
571
484
|
destAsset: assetAndChain,
|
|
572
485
|
destAddress: z.string(),
|
|
573
486
|
amount: unsignedInteger,
|
|
574
|
-
commissionBps:
|
|
487
|
+
commissionBps: u16.optional().default(0),
|
|
575
488
|
ccmParams: transformedCcmParamsSchema.optional(),
|
|
576
|
-
maxBoostFeeBps:
|
|
489
|
+
maxBoostFeeBps: u16.optional(),
|
|
577
490
|
affiliates: z.array(affiliateBroker).optional(),
|
|
578
491
|
fillOrKillParams: transformedFokSchema,
|
|
579
492
|
dcaParams: transformedDcaParamsSchema.optional(),
|
|
493
|
+
// TODO: remove after brokerAccount is deprecated
|
|
580
494
|
brokerAccount: chainflipAddress.optional()
|
|
581
495
|
}).superRefine(({ ...val }, ctx) => {
|
|
582
496
|
if (val.srcAddress && !validateAddress(val.srcAsset.chain, val.srcAddress, network)) {
|
|
@@ -615,68 +529,34 @@ async function requestCfParametersEncoding(request, opts, network) {
|
|
|
615
529
|
);
|
|
616
530
|
return response;
|
|
617
531
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
body: getParameterEncodingRequestSchema(network),
|
|
635
|
-
responses: {
|
|
636
|
-
200: hexString
|
|
637
|
-
}
|
|
638
|
-
},
|
|
639
|
-
encodeVaultSwapData: {
|
|
640
|
-
method: "POST",
|
|
641
|
-
path: "/encodeVaultSwapData",
|
|
642
|
-
body: getVaultSwapParameterEncodingRequestSchema(network),
|
|
643
|
-
responses: {
|
|
644
|
-
200: EncodedVaultSwapData
|
|
645
|
-
}
|
|
646
|
-
},
|
|
647
|
-
openSwapDepositChannel: {
|
|
648
|
-
method: "POST",
|
|
649
|
-
path: "/openSwapDepositChannel",
|
|
650
|
-
body: getOpenSwapDepositChannelSchema(network),
|
|
651
|
-
responses: {
|
|
652
|
-
201: DepositChannelInfo
|
|
653
|
-
}
|
|
654
|
-
},
|
|
655
|
-
openAccountCreationDepositChannel: {
|
|
656
|
-
method: "POST",
|
|
657
|
-
path: "/openAccountCreationDepositChannel",
|
|
658
|
-
body: getAccountCreationDepositChannelSchema(network),
|
|
659
|
-
responses: {
|
|
660
|
-
201: DepositChannelInfo
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
},
|
|
664
|
-
{
|
|
665
|
-
pathPrefix: "/api"
|
|
666
|
-
}
|
|
667
|
-
);
|
|
532
|
+
async function requestAccountCreationDepositAddress(request, opts, network) {
|
|
533
|
+
const client = new HttpClient(opts.url);
|
|
534
|
+
const params = getAccountCreationDepositChannelSchema(network).parse(request);
|
|
535
|
+
const response = await client.sendRequest(
|
|
536
|
+
"broker_request_account_creation_deposit_address",
|
|
537
|
+
params.signatureData,
|
|
538
|
+
params.transactionMetadata,
|
|
539
|
+
params.asset,
|
|
540
|
+
params.boostFeeBps,
|
|
541
|
+
params.refundAddress
|
|
542
|
+
);
|
|
543
|
+
return {
|
|
544
|
+
...transformKeysToCamelCase(response),
|
|
545
|
+
address: formatBrokerDepositAddress(params.asset.chain, response.address)
|
|
546
|
+
};
|
|
547
|
+
}
|
|
668
548
|
var MultiCache = class {
|
|
669
549
|
constructor(fetchers) {
|
|
670
550
|
this.fetchers = fetchers;
|
|
671
551
|
}
|
|
672
552
|
values = {};
|
|
673
553
|
read(type) {
|
|
674
|
-
const { fetch, ttl } = this.fetchers[type];
|
|
554
|
+
const { fetch: fetch2, ttl } = this.fetchers[type];
|
|
675
555
|
let cached = this.values[type];
|
|
676
556
|
if (!cached || cached.timestamp + ttl < Date.now()) {
|
|
677
557
|
cached = {
|
|
678
558
|
timestamp: Date.now(),
|
|
679
|
-
value:
|
|
559
|
+
value: fetch2().catch((err) => {
|
|
680
560
|
this.values[type] = void 0;
|
|
681
561
|
throw err;
|
|
682
562
|
})
|
|
@@ -739,6 +619,17 @@ function parseFoKParams(params, quote) {
|
|
|
739
619
|
return "refundAddress" in params ? { ...parsed, refundAddress: params.refundAddress } : parsed;
|
|
740
620
|
}
|
|
741
621
|
var isTestnet = (network) => network !== "mainnet";
|
|
622
|
+
function calculateExpiryTime(args) {
|
|
623
|
+
const { chainInfo, expiryBlock } = args;
|
|
624
|
+
if (chainInfo == null || expiryBlock == null) {
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
const remainingBlocks = Number(BigInt(expiryBlock) - BigInt(chainInfo.height));
|
|
628
|
+
const { blockTimeSeconds } = anyChainConstants[chainInfo.chain];
|
|
629
|
+
return new Date(
|
|
630
|
+
new Date(chainInfo.blockTrackedAt).getTime() + remainingBlocks * blockTimeSeconds * 1e3
|
|
631
|
+
);
|
|
632
|
+
}
|
|
742
633
|
var MAX_SWAP_AMOUNT = 2n ** 128n - 1n;
|
|
743
634
|
var validateSwapAmount = (env, asset2, amount) => {
|
|
744
635
|
const minimumAmount = readAssetValue(env.ingressEgress.minimumDepositAmounts, asset2);
|
|
@@ -824,7 +715,7 @@ var getChainData = (chain2, network, env) => ({
|
|
|
824
715
|
|
|
825
716
|
// package.json
|
|
826
717
|
var package_default = {
|
|
827
|
-
version: "2.3.
|
|
718
|
+
version: "2.3.4-dev.1"};
|
|
828
719
|
|
|
829
720
|
// src/swap/consts.ts
|
|
830
721
|
var CF_SDK_VERSION_HEADERS = { "X-Chainflip-Sdk-Version": package_default.version };
|
|
@@ -872,8 +763,64 @@ var getStatusV2 = async (baseUrl, { id }, { signal }) => {
|
|
|
872
763
|
});
|
|
873
764
|
return data;
|
|
874
765
|
};
|
|
766
|
+
var lowercaseKeys = (headers) => Object.fromEntries(Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value]));
|
|
767
|
+
var backendRequest = async (baseUrl, method, path, body) => {
|
|
768
|
+
const headers = lowercaseKeys(CF_SDK_VERSION_HEADERS);
|
|
769
|
+
let serializedBody;
|
|
770
|
+
if (method !== "GET" && body != null) {
|
|
771
|
+
headers["content-type"] = "application/json";
|
|
772
|
+
serializedBody = JSON.stringify(body);
|
|
773
|
+
}
|
|
774
|
+
const res = await fetch(`${baseUrl.replace(/\/+$/, "")}${path}`, {
|
|
775
|
+
method,
|
|
776
|
+
headers,
|
|
777
|
+
body: serializedBody
|
|
778
|
+
});
|
|
779
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
780
|
+
if (contentType.includes("application/") && contentType.includes("json")) {
|
|
781
|
+
return { status: res.status, body: await res.json(), headers: res.headers };
|
|
782
|
+
}
|
|
783
|
+
if (contentType.includes("text/")) {
|
|
784
|
+
return { status: res.status, body: await res.text(), headers: res.headers };
|
|
785
|
+
}
|
|
786
|
+
return { status: res.status, body: await res.blob(), headers: res.headers };
|
|
787
|
+
};
|
|
788
|
+
var apiError = (action, res) => {
|
|
789
|
+
const { body } = res;
|
|
790
|
+
const message = body && typeof body === "object" && "message" in body && typeof body.message === "string" ? `: ${body.message}` : "";
|
|
791
|
+
return new Error(`Failed to ${action} (${res.status})${message}`, { cause: res });
|
|
792
|
+
};
|
|
793
|
+
var getNetworkInfo = async (baseUrl) => {
|
|
794
|
+
const res = await backendRequest(baseUrl, "GET", "/api/networkInfo");
|
|
795
|
+
assert(res.status === 200, "Failed to fetch network info");
|
|
796
|
+
return res.body;
|
|
797
|
+
};
|
|
798
|
+
var openSwapDepositChannel = async (baseUrl, body) => {
|
|
799
|
+
const res = await backendRequest(baseUrl, "POST", "/api/openSwapDepositChannel", body);
|
|
800
|
+
if (res.status !== 201) throw apiError("open swap deposit channel", res);
|
|
801
|
+
return res.body;
|
|
802
|
+
};
|
|
803
|
+
var encodeVaultSwapData = async (baseUrl, body) => {
|
|
804
|
+
const res = await backendRequest(baseUrl, "POST", "/api/encodeVaultSwapData", body);
|
|
805
|
+
if (res.status !== 200) throw apiError("encode vault swap data", res);
|
|
806
|
+
return res.body;
|
|
807
|
+
};
|
|
808
|
+
var encodeCfParameters = async (baseUrl, body) => {
|
|
809
|
+
const res = await backendRequest(baseUrl, "POST", "/api/encodeCfParameters", body);
|
|
810
|
+
if (res.status !== 200) throw apiError("encode cf parameters", res);
|
|
811
|
+
return res.body;
|
|
812
|
+
};
|
|
813
|
+
var openAccountCreationDepositChannel = async (baseUrl, body) => {
|
|
814
|
+
const res = await backendRequest(baseUrl, "POST", "/api/openAccountCreationDepositChannel", body);
|
|
815
|
+
if (res.status !== 201) throw apiError("open account creation deposit channel", res);
|
|
816
|
+
return res.body;
|
|
817
|
+
};
|
|
875
818
|
|
|
876
819
|
// src/swap/sdk.ts
|
|
820
|
+
var BrokerName = {
|
|
821
|
+
ChainflipSwap: "swap.chainflip.io"
|
|
822
|
+
};
|
|
823
|
+
var brokerNames = Object.values(BrokerName);
|
|
877
824
|
var assertQuoteValid = (quote) => {
|
|
878
825
|
switch (quote.type) {
|
|
879
826
|
case "REGULAR":
|
|
@@ -888,7 +835,6 @@ var assertQuoteValid = (quote) => {
|
|
|
888
835
|
var SwapSDK = class {
|
|
889
836
|
options;
|
|
890
837
|
rpcConfig;
|
|
891
|
-
apiClient;
|
|
892
838
|
cache;
|
|
893
839
|
dcaV2Enabled = false;
|
|
894
840
|
constructor(options = {}) {
|
|
@@ -899,12 +845,6 @@ var SwapSDK = class {
|
|
|
899
845
|
backendUrl: options.backendUrl ?? BACKEND_SERVICE_URLS[network]
|
|
900
846
|
};
|
|
901
847
|
this.rpcConfig = options.rpcUrl ? { rpcUrl: options.rpcUrl } : { network };
|
|
902
|
-
this.apiClient = initClient(createApiContract(this.options.network), {
|
|
903
|
-
// remove trailing slashes. this bug has been fixed in `@ts-rest/core`
|
|
904
|
-
// but it hasn't been released yet
|
|
905
|
-
baseUrl: this.options.backendUrl.replace(/\/+$/, ""),
|
|
906
|
-
baseHeaders: CF_SDK_VERSION_HEADERS
|
|
907
|
-
});
|
|
908
848
|
this.dcaV2Enabled = options.enabledFeatures?.dcaV2 ?? false;
|
|
909
849
|
this.cache = new MultiCache({
|
|
910
850
|
environment: {
|
|
@@ -912,11 +852,7 @@ var SwapSDK = class {
|
|
|
912
852
|
ttl: 6e4
|
|
913
853
|
},
|
|
914
854
|
networkInfo: {
|
|
915
|
-
fetch:
|
|
916
|
-
const res = await this.apiClient.networkInfo();
|
|
917
|
-
assert(res.status === 200, "Failed to fetch network info");
|
|
918
|
-
return res.body;
|
|
919
|
-
},
|
|
855
|
+
fetch: () => getNetworkInfo(this.options.backendUrl),
|
|
920
856
|
ttl: 6e4
|
|
921
857
|
}
|
|
922
858
|
});
|
|
@@ -937,8 +873,8 @@ var SwapSDK = class {
|
|
|
937
873
|
return this.cache.read("environment");
|
|
938
874
|
}
|
|
939
875
|
async getSupportedAssets(type) {
|
|
940
|
-
const
|
|
941
|
-
return
|
|
876
|
+
const assets = await this.cache.read("networkInfo");
|
|
877
|
+
return assets.assets.filter((a) => isChainflipAsset(a.asset)).filter((a) => {
|
|
942
878
|
switch (type) {
|
|
943
879
|
case "all":
|
|
944
880
|
return true;
|
|
@@ -969,20 +905,60 @@ var SwapSDK = class {
|
|
|
969
905
|
]);
|
|
970
906
|
return supportedAssets.map((asset2) => getAssetData(asset2, this.options.network, env)).filter((asset2) => !chain2 || asset2.chain === chain2);
|
|
971
907
|
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
908
|
+
resolveBroker(fees = {}) {
|
|
909
|
+
const { broker } = this.options;
|
|
910
|
+
assert(
|
|
911
|
+
!(broker && "name" in broker && "url" in broker),
|
|
912
|
+
"A broker can be set by url or name, not both"
|
|
913
|
+
);
|
|
914
|
+
if (broker && "name" in broker) {
|
|
915
|
+
assert(brokerNames.includes(broker.name), `Broker name "${broker.name}" is not supported`);
|
|
916
|
+
assert(
|
|
917
|
+
fees.brokerCommissionBps === void 0 && !fees.affiliates?.length,
|
|
918
|
+
`Broker commission and affiliates are not supported with the ${broker.name} broker`
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
return broker;
|
|
922
|
+
}
|
|
923
|
+
requireBroker(fees = {}) {
|
|
924
|
+
const broker = this.resolveBroker(fees);
|
|
925
|
+
assert(broker, "A broker url or name is required to open channels or encode swaps");
|
|
926
|
+
return broker;
|
|
927
|
+
}
|
|
928
|
+
// TODO: remove after brokerAccount is deprecated
|
|
929
|
+
resolveEncodingBroker(brokerAccount, fees) {
|
|
930
|
+
if (!brokerAccount) return this.requireBroker(fees);
|
|
931
|
+
assert(
|
|
932
|
+
!this.options.broker,
|
|
933
|
+
"Setting brokerAccount is not supported when the SDK is initialized with a broker"
|
|
934
|
+
);
|
|
935
|
+
return void 0;
|
|
975
936
|
}
|
|
976
|
-
async
|
|
977
|
-
|
|
978
|
-
|
|
937
|
+
async estimateExpiryTime(chain2, expiryBlock) {
|
|
938
|
+
try {
|
|
939
|
+
const { chainTracking } = await this.cache.read("networkInfo");
|
|
940
|
+
const chainInfo = chainTracking?.find((info) => info.chain === chain2);
|
|
941
|
+
return calculateExpiryTime({ chainInfo, expiryBlock })?.getTime();
|
|
942
|
+
} catch {
|
|
943
|
+
return void 0;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
async getCommissionBps({
|
|
947
|
+
brokerCommissionBps,
|
|
948
|
+
affiliateBrokers,
|
|
949
|
+
isOnChain
|
|
950
|
+
}) {
|
|
951
|
+
const broker = this.resolveBroker({ brokerCommissionBps, affiliates: affiliateBrokers });
|
|
952
|
+
if (!isOnChain && broker && "name" in broker && broker.name === BrokerName.ChainflipSwap) {
|
|
953
|
+
return this.getQuotedBrokerCommissionBps();
|
|
979
954
|
}
|
|
980
955
|
return brokerCommissionBps ?? 0;
|
|
981
956
|
}
|
|
957
|
+
async getQuotedBrokerCommissionBps() {
|
|
958
|
+
return (await this.cache.read("networkInfo")).cfBrokerCommissionBps;
|
|
959
|
+
}
|
|
982
960
|
async getQuoteV2(quoteRequest, options = {}) {
|
|
983
|
-
const submitterBrokerCommissionBps = await this.getCommissionBps(
|
|
984
|
-
quoteRequest.brokerCommissionBps
|
|
985
|
-
);
|
|
961
|
+
const submitterBrokerCommissionBps = await this.getCommissionBps(quoteRequest);
|
|
986
962
|
return getQuoteV2(
|
|
987
963
|
this.options.backendUrl,
|
|
988
964
|
{
|
|
@@ -1069,8 +1045,9 @@ var SwapSDK = class {
|
|
|
1069
1045
|
fillOrKillParams: inputFoKParams,
|
|
1070
1046
|
affiliateBrokers: affiliates,
|
|
1071
1047
|
ccmParams,
|
|
1072
|
-
brokerCommissionBps
|
|
1048
|
+
brokerCommissionBps
|
|
1073
1049
|
}) {
|
|
1050
|
+
const broker = this.requireBroker({ brokerCommissionBps, affiliates });
|
|
1074
1051
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
1075
1052
|
assertQuoteValid(quote);
|
|
1076
1053
|
assert(!quote.isVaultSwap, "Cannot open a deposit channel for a vault swap quote");
|
|
@@ -1080,7 +1057,6 @@ var SwapSDK = class {
|
|
|
1080
1057
|
} else {
|
|
1081
1058
|
assert(!quote.ccmParams, "Cannot open regular channel for quote with CCM params");
|
|
1082
1059
|
}
|
|
1083
|
-
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
1084
1060
|
const depositAddressRequest = {
|
|
1085
1061
|
srcAsset: quote.srcAsset,
|
|
1086
1062
|
destAsset: quote.destAsset,
|
|
@@ -1092,43 +1068,30 @@ var SwapSDK = class {
|
|
|
1092
1068
|
ccmParams,
|
|
1093
1069
|
amount: quote.depositAmount
|
|
1094
1070
|
};
|
|
1095
|
-
let
|
|
1096
|
-
if (
|
|
1071
|
+
let channel;
|
|
1072
|
+
if ("url" in broker) {
|
|
1097
1073
|
const result = await requestSwapDepositAddress(
|
|
1098
|
-
{
|
|
1099
|
-
|
|
1100
|
-
commissionBps: brokerCommissionBps,
|
|
1101
|
-
affiliates
|
|
1102
|
-
},
|
|
1103
|
-
{ url: this.options.broker.url },
|
|
1074
|
+
{ ...depositAddressRequest, commissionBps: brokerCommissionBps ?? 0, affiliates },
|
|
1075
|
+
broker,
|
|
1104
1076
|
this.options.network
|
|
1105
1077
|
);
|
|
1106
|
-
|
|
1078
|
+
channel = {
|
|
1107
1079
|
id: `${result.issuedBlock}-${quote.srcAsset.chain}-${result.channelId}`,
|
|
1108
1080
|
depositAddress: result.address,
|
|
1109
|
-
brokerCommissionBps,
|
|
1081
|
+
brokerCommissionBps: brokerCommissionBps ?? 0,
|
|
1082
|
+
maxBoostFeeBps: Number(depositAddressRequest.maxBoostFeeBps) || 0,
|
|
1110
1083
|
srcChainExpiryBlock: result.sourceChainExpiryBlock,
|
|
1111
|
-
|
|
1084
|
+
estimatedExpiryTime: await this.estimateExpiryTime(
|
|
1085
|
+
quote.srcAsset.chain,
|
|
1086
|
+
result.sourceChainExpiryBlock
|
|
1087
|
+
),
|
|
1112
1088
|
channelOpeningFee: result.channelOpeningFee
|
|
1113
1089
|
};
|
|
1114
1090
|
} else {
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
);
|
|
1119
|
-
assert(
|
|
1120
|
-
!affiliates?.length,
|
|
1121
|
-
"Affiliate brokers are supported only when initializing the SDK with a brokerUrl"
|
|
1122
|
-
);
|
|
1123
|
-
const res = await this.apiClient.openSwapDepositChannel({
|
|
1124
|
-
body: {
|
|
1125
|
-
...depositAddressRequest,
|
|
1126
|
-
quote,
|
|
1127
|
-
takeCommission: this.shouldTakeCommission()
|
|
1128
|
-
}
|
|
1091
|
+
channel = await openSwapDepositChannel(this.options.backendUrl, {
|
|
1092
|
+
...depositAddressRequest,
|
|
1093
|
+
quote
|
|
1129
1094
|
});
|
|
1130
|
-
assert(res.status === 201, "Failed to open swap deposit channel");
|
|
1131
|
-
response = res.body;
|
|
1132
1095
|
}
|
|
1133
1096
|
return {
|
|
1134
1097
|
...depositAddressRequest,
|
|
@@ -1136,14 +1099,14 @@ var SwapSDK = class {
|
|
|
1136
1099
|
destAsset: quote.destAsset.asset,
|
|
1137
1100
|
srcChain: quote.srcAsset.chain,
|
|
1138
1101
|
destChain: quote.destAsset.chain,
|
|
1139
|
-
depositChannelId:
|
|
1140
|
-
depositAddress:
|
|
1141
|
-
brokerCommissionBps:
|
|
1102
|
+
depositChannelId: channel.id,
|
|
1103
|
+
depositAddress: channel.depositAddress,
|
|
1104
|
+
brokerCommissionBps: channel.brokerCommissionBps,
|
|
1142
1105
|
affiliateBrokers: affiliates ?? [],
|
|
1143
|
-
maxBoostFeeBps:
|
|
1144
|
-
depositChannelExpiryBlock: BigInt(
|
|
1145
|
-
estimatedDepositChannelExpiryTime:
|
|
1146
|
-
channelOpeningFee: BigInt(
|
|
1106
|
+
maxBoostFeeBps: channel.maxBoostFeeBps,
|
|
1107
|
+
depositChannelExpiryBlock: BigInt(channel.srcChainExpiryBlock),
|
|
1108
|
+
estimatedDepositChannelExpiryTime: channel.estimatedExpiryTime,
|
|
1109
|
+
channelOpeningFee: BigInt(channel.channelOpeningFee),
|
|
1147
1110
|
fillOrKillParams: inputFoKParams
|
|
1148
1111
|
};
|
|
1149
1112
|
}
|
|
@@ -1155,9 +1118,10 @@ var SwapSDK = class {
|
|
|
1155
1118
|
affiliateBrokers: affiliates,
|
|
1156
1119
|
ccmParams,
|
|
1157
1120
|
brokerAccount,
|
|
1158
|
-
brokerCommissionBps
|
|
1121
|
+
brokerCommissionBps,
|
|
1159
1122
|
extraParams
|
|
1160
1123
|
}) {
|
|
1124
|
+
const broker = this.resolveEncodingBroker(brokerAccount, { brokerCommissionBps, affiliates });
|
|
1161
1125
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
1162
1126
|
assertQuoteValid(quote);
|
|
1163
1127
|
assert(quote.isVaultSwap, "Cannot encode vault swap data for non-vault swap quotes");
|
|
@@ -1166,7 +1130,6 @@ var SwapSDK = class {
|
|
|
1166
1130
|
} else {
|
|
1167
1131
|
assert(!quote.ccmParams, "Cannot encode regular swap for quote with CCM params");
|
|
1168
1132
|
}
|
|
1169
|
-
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
1170
1133
|
const vaultSwapRequest = {
|
|
1171
1134
|
srcAsset: quote.srcAsset,
|
|
1172
1135
|
destAsset: quote.destAsset,
|
|
@@ -1178,41 +1141,24 @@ var SwapSDK = class {
|
|
|
1178
1141
|
fillOrKillParams: parseFoKParams(inputFoKParams, quote),
|
|
1179
1142
|
dcaParams: quote.type === "DCA" ? quote.dcaParams : void 0,
|
|
1180
1143
|
extraParams,
|
|
1181
|
-
brokerAccount,
|
|
1182
1144
|
commissionBps: brokerCommissionBps,
|
|
1183
1145
|
affiliates
|
|
1184
1146
|
};
|
|
1185
|
-
if (
|
|
1186
|
-
|
|
1187
|
-
!vaultSwapRequest.brokerAccount,
|
|
1188
|
-
"Cannot overwrite broker account when initializing the SDK with a brokerUrl"
|
|
1189
|
-
);
|
|
1190
|
-
return requestSwapParameterEncoding(
|
|
1191
|
-
vaultSwapRequest,
|
|
1192
|
-
{ url: this.options.broker.url },
|
|
1193
|
-
this.options.network
|
|
1194
|
-
);
|
|
1147
|
+
if (broker && "url" in broker) {
|
|
1148
|
+
return requestSwapParameterEncoding(vaultSwapRequest, broker, this.options.network);
|
|
1195
1149
|
}
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
);
|
|
1200
|
-
assert(
|
|
1201
|
-
!vaultSwapRequest.affiliates?.length || vaultSwapRequest.brokerAccount,
|
|
1202
|
-
"Affiliate brokers are supported only when setting a broker account"
|
|
1150
|
+
const body = await encodeVaultSwapData(
|
|
1151
|
+
this.options.backendUrl,
|
|
1152
|
+
brokerAccount ? { ...vaultSwapRequest, brokerAccount, commissionBps: brokerCommissionBps ?? 0 } : { ...vaultSwapRequest, commissionBps: await this.getQuotedBrokerCommissionBps() }
|
|
1203
1153
|
);
|
|
1204
|
-
|
|
1205
|
-
if (res.status !== 200) {
|
|
1206
|
-
throw new Error("Failed to encode vault swap data", { cause: res });
|
|
1207
|
-
}
|
|
1208
|
-
switch (res.body.chain) {
|
|
1154
|
+
switch (body.chain) {
|
|
1209
1155
|
case "Arbitrum":
|
|
1210
1156
|
case "Ethereum":
|
|
1211
1157
|
case "Tron":
|
|
1212
1158
|
case "Bsc":
|
|
1213
|
-
return { ...
|
|
1159
|
+
return { ...body, value: BigInt(body.value) };
|
|
1214
1160
|
default:
|
|
1215
|
-
return
|
|
1161
|
+
return body;
|
|
1216
1162
|
}
|
|
1217
1163
|
}
|
|
1218
1164
|
async encodeCfParameters({
|
|
@@ -1225,6 +1171,7 @@ var SwapSDK = class {
|
|
|
1225
1171
|
brokerAccount,
|
|
1226
1172
|
brokerCommissionBps
|
|
1227
1173
|
}) {
|
|
1174
|
+
const broker = this.resolveEncodingBroker(brokerAccount, { brokerCommissionBps, affiliates });
|
|
1228
1175
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
1229
1176
|
assertQuoteValid(quote);
|
|
1230
1177
|
if (ccmParams) {
|
|
@@ -1242,54 +1189,52 @@ var SwapSDK = class {
|
|
|
1242
1189
|
maxBoostFeeBps: "maxBoostFeeBps" in quote ? quote.maxBoostFeeBps : void 0,
|
|
1243
1190
|
fillOrKillParams: parseFoKParams(fillOrKillParams2, quote),
|
|
1244
1191
|
dcaParams: quote.type === "DCA" ? quote.dcaParams : void 0,
|
|
1245
|
-
commissionBps:
|
|
1192
|
+
commissionBps: brokerCommissionBps,
|
|
1246
1193
|
affiliates
|
|
1247
1194
|
};
|
|
1248
|
-
if (
|
|
1249
|
-
|
|
1250
|
-
!brokerAccount,
|
|
1251
|
-
"Cannot overwrite broker account when initializing the SDK with a brokerUrl"
|
|
1252
|
-
);
|
|
1253
|
-
return requestCfParametersEncoding(
|
|
1254
|
-
requestParams,
|
|
1255
|
-
{ url: this.options.broker.url },
|
|
1256
|
-
this.options.network
|
|
1257
|
-
);
|
|
1195
|
+
if (broker && "url" in broker) {
|
|
1196
|
+
return requestCfParametersEncoding(requestParams, broker, this.options.network);
|
|
1258
1197
|
}
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
);
|
|
1263
|
-
assert(
|
|
1264
|
-
!requestParams.affiliates?.length || brokerAccount,
|
|
1265
|
-
"Affiliate brokers are supported only when setting a broker account"
|
|
1198
|
+
return encodeCfParameters(
|
|
1199
|
+
this.options.backendUrl,
|
|
1200
|
+
brokerAccount ? { ...requestParams, brokerAccount, commissionBps: brokerCommissionBps ?? 0 } : { ...requestParams, commissionBps: await this.getQuotedBrokerCommissionBps() }
|
|
1266
1201
|
);
|
|
1267
|
-
const res = await this.apiClient.encodeCfParameters({
|
|
1268
|
-
body: { ...requestParams, brokerAccount }
|
|
1269
|
-
});
|
|
1270
|
-
if (res.status !== 200) {
|
|
1271
|
-
throw new Error("Failed to encode cf parameters", { cause: res });
|
|
1272
|
-
}
|
|
1273
|
-
return res.body;
|
|
1274
1202
|
}
|
|
1275
1203
|
async checkBoostEnabled() {
|
|
1276
|
-
const { assets
|
|
1277
|
-
return
|
|
1204
|
+
const { assets } = await this.cache.read("networkInfo");
|
|
1205
|
+
return assets.find((a) => a.asset === "Btc")?.boostDepositsEnabled ?? true;
|
|
1278
1206
|
}
|
|
1279
1207
|
async requestAccountCreationDepositAddress(asset2, refundAddress, signatureData, transactionMetadata, boostFeeBps = 0) {
|
|
1280
|
-
const
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
}
|
|
1289
|
-
if (
|
|
1290
|
-
|
|
1208
|
+
const broker = this.requireBroker();
|
|
1209
|
+
const rpcAsset = internalAssetToRpcAsset[asset2];
|
|
1210
|
+
const request = {
|
|
1211
|
+
asset: rpcAsset,
|
|
1212
|
+
refundAddress,
|
|
1213
|
+
signatureData,
|
|
1214
|
+
transactionMetadata,
|
|
1215
|
+
boostFeeBps
|
|
1216
|
+
};
|
|
1217
|
+
if ("url" in broker) {
|
|
1218
|
+
const result = await requestAccountCreationDepositAddress(
|
|
1219
|
+
request,
|
|
1220
|
+
broker,
|
|
1221
|
+
this.options.network
|
|
1222
|
+
);
|
|
1223
|
+
return {
|
|
1224
|
+
id: `${result.issuedBlock}-${rpcAsset.chain}-${result.channelId}`,
|
|
1225
|
+
depositAddress: result.address,
|
|
1226
|
+
brokerCommissionBps: 0,
|
|
1227
|
+
maxBoostFeeBps: boostFeeBps,
|
|
1228
|
+
issuedBlock: result.issuedBlock,
|
|
1229
|
+
srcChainExpiryBlock: result.depositChainExpiryBlock.toString(),
|
|
1230
|
+
estimatedExpiryTime: await this.estimateExpiryTime(
|
|
1231
|
+
rpcAsset.chain,
|
|
1232
|
+
result.depositChainExpiryBlock
|
|
1233
|
+
),
|
|
1234
|
+
channelOpeningFee: result.channelOpeningFee.toString()
|
|
1235
|
+
};
|
|
1291
1236
|
}
|
|
1292
|
-
return
|
|
1237
|
+
return openAccountCreationDepositChannel(this.options.backendUrl, request);
|
|
1293
1238
|
}
|
|
1294
1239
|
};
|
|
1295
1240
|
var arrayToMap = (array) => Object.fromEntries(array.map((item) => [item, item]));
|
|
@@ -1298,4 +1243,4 @@ var InternalAssets = arrayToMap(chainflipAssets);
|
|
|
1298
1243
|
var Chains = arrayToMap(chainflipChains);
|
|
1299
1244
|
var Assets = arrayToMap(assetSymbols);
|
|
1300
1245
|
|
|
1301
|
-
export { Assets, InternalAssets as ChainflipIds, ChainflipNetworks, Chains, SwapSDK };
|
|
1246
|
+
export { Assets, BrokerName, InternalAssets as ChainflipIds, ChainflipNetworks, Chains, SwapSDK };
|