@chainflip/sdk 1.11.1 → 2.0.0-beta.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/swap.cjs +304 -379
- package/dist/swap.d.cts +185 -75
- package/dist/swap.d.ts +185 -75
- package/dist/swap.js +217 -293
- package/package.json +2 -2
package/dist/swap.js
CHANGED
|
@@ -1,39 +1,21 @@
|
|
|
1
1
|
import { assert, getEnvironment, envSafeAssetBlacklist, getEvmChainId, getAllBoostPoolsDepth, parseFoKParams, isNotNullish, isTestnet, ADDRESSES } from './chunk-7L3ZOIWM.js';
|
|
2
2
|
import { unreachable } from '@chainflip/utils/assertion';
|
|
3
|
-
import { chainflipChains, assetSymbols, isValidAssetAndChain,
|
|
3
|
+
import { chainflipChains, assetSymbols, isValidAssetAndChain, chainflipAssets, getInternalAssets, assetConstants, getInternalAsset, internalAssetToRpcAsset, chainflipNetworks, readAssetValue } from '@chainflip/utils/chainflip';
|
|
4
4
|
export { getInternalAsset as getChainflipId } from '@chainflip/utils/chainflip';
|
|
5
5
|
import { initContract, initClient } from '@ts-rest/core';
|
|
6
|
-
import
|
|
7
|
-
import { HttpClient } from '@chainflip/rpc';
|
|
8
|
-
import { bytesToHex } from '@chainflip/utils/bytes';
|
|
9
|
-
import * as ss58 from '@chainflip/utils/ss58';
|
|
10
|
-
import { isHex, toUpperCase } from '@chainflip/utils/string';
|
|
11
|
-
import { priceX128ToPrice } from '@chainflip/utils/tickMath';
|
|
12
|
-
import BigNumber$1, { BigNumber } from 'bignumber.js';
|
|
6
|
+
import z3, { z } from 'zod';
|
|
13
7
|
import { isValidSolanaAddress } from '@chainflip/solana';
|
|
8
|
+
import * as ss58 from '@chainflip/utils/ss58';
|
|
14
9
|
import { hexEncodeNumber } from '@chainflip/utils/number';
|
|
15
10
|
import * as bitcoin from '@chainflip/bitcoin';
|
|
11
|
+
import { isHex, toUpperCase } from '@chainflip/utils/string';
|
|
16
12
|
import * as ethers from 'ethers';
|
|
13
|
+
import { HttpClient } from '@chainflip/rpc';
|
|
14
|
+
import { bytesToHex } from '@chainflip/utils/bytes';
|
|
15
|
+
import { priceX128ToPrice } from '@chainflip/utils/tickMath';
|
|
16
|
+
import BigNumber from 'bignumber.js';
|
|
17
17
|
import axios from 'axios';
|
|
18
18
|
|
|
19
|
-
// ../shared/src/strings.ts
|
|
20
|
-
var toCamelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
21
|
-
|
|
22
|
-
// ../shared/src/objects.ts
|
|
23
|
-
var transformKeysToCamelCase = (obj) => {
|
|
24
|
-
if (Array.isArray(obj)) {
|
|
25
|
-
return obj.map(transformKeysToCamelCase);
|
|
26
|
-
}
|
|
27
|
-
if (obj !== null && typeof obj === "object") {
|
|
28
|
-
return Object.fromEntries(
|
|
29
|
-
Object.entries(obj).map(([key, value]) => [
|
|
30
|
-
toCamelCase(key),
|
|
31
|
-
transformKeysToCamelCase(value)
|
|
32
|
-
])
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
return obj;
|
|
36
|
-
};
|
|
37
19
|
var booleanString = z.string().regex(/^(true|false)$/i).transform((v) => v.toLowerCase() === "true");
|
|
38
20
|
var numericString = z.string().regex(/^[0-9]+$/);
|
|
39
21
|
var numericOrEmptyString = z.string().regex(/^[0-9]*$/);
|
|
@@ -61,6 +43,47 @@ var uncheckedAssetAndChain = z.object({
|
|
|
61
43
|
var assetAndChain = uncheckedAssetAndChain.refine(
|
|
62
44
|
(value) => isValidAssetAndChain(value)
|
|
63
45
|
);
|
|
46
|
+
|
|
47
|
+
// ../shared/src/api/encodeVaultSwapData.ts
|
|
48
|
+
var EncodedVaultSwapData = z.discriminatedUnion("chain", [
|
|
49
|
+
z.object({
|
|
50
|
+
chain: z.literal("Bitcoin"),
|
|
51
|
+
nulldataPayload: hexString,
|
|
52
|
+
depositAddress: z.string()
|
|
53
|
+
}),
|
|
54
|
+
z.object({
|
|
55
|
+
chain: z.enum(["Ethereum", "Arbitrum"]),
|
|
56
|
+
value: z.string(),
|
|
57
|
+
to: hexString,
|
|
58
|
+
calldata: hexString,
|
|
59
|
+
sourceTokenAddress: hexString.optional()
|
|
60
|
+
}),
|
|
61
|
+
z.object({
|
|
62
|
+
chain: z.literal("Solana"),
|
|
63
|
+
programId: z.string(),
|
|
64
|
+
data: hexString,
|
|
65
|
+
accounts: z.array(
|
|
66
|
+
z.object({
|
|
67
|
+
pubkey: z.string(),
|
|
68
|
+
isSigner: z.boolean(),
|
|
69
|
+
isWritable: z.boolean()
|
|
70
|
+
})
|
|
71
|
+
)
|
|
72
|
+
})
|
|
73
|
+
]);
|
|
74
|
+
var NetworkInfo = z3.object({
|
|
75
|
+
assets: z3.array(
|
|
76
|
+
z3.object({
|
|
77
|
+
asset: z3.enum(chainflipAssets),
|
|
78
|
+
vaultSwapDepositsEnabled: z3.boolean(),
|
|
79
|
+
depositChannelDepositsEnabled: z3.boolean(),
|
|
80
|
+
depositChannelCreationEnabled: z3.boolean(),
|
|
81
|
+
egressEnabled: z3.boolean(),
|
|
82
|
+
boostDepositsEnabled: z3.boolean()
|
|
83
|
+
})
|
|
84
|
+
),
|
|
85
|
+
cfBrokerCommissionBps: z3.number()
|
|
86
|
+
});
|
|
64
87
|
z.object({
|
|
65
88
|
srcChain: chain,
|
|
66
89
|
srcAsset: asset,
|
|
@@ -152,12 +175,12 @@ var ccmParamsSchema = z.object({
|
|
|
152
175
|
message: hexStringWithMaxByteSize(15e3),
|
|
153
176
|
// https://github.com/chainflip-io/chainflip-backend/blob/415aa9e20ec4046c68892cd34798e5d831c5b83f/state-chain/chains/src/lib.rs#L710
|
|
154
177
|
ccmAdditionalData: hexStringWithMaxByteSize(3e3).optional(),
|
|
155
|
-
/** @deprecated DEPRECATED(1.
|
|
156
|
-
cfParameters:
|
|
157
|
-
}).transform(({ gasBudget, message, ccmAdditionalData
|
|
178
|
+
/** @deprecated DEPRECATED(1.12) pass additionalData instead */
|
|
179
|
+
cfParameters: z.never({ message: "cfParameters is deprecated, use ccmAdditionalData instead" }).optional()
|
|
180
|
+
}).transform(({ gasBudget, message, ccmAdditionalData }) => ({
|
|
158
181
|
gasBudget,
|
|
159
182
|
message,
|
|
160
|
-
ccmAdditionalData: ccmAdditionalData ??
|
|
183
|
+
ccmAdditionalData: ccmAdditionalData ?? "0x"
|
|
161
184
|
}));
|
|
162
185
|
var affiliateBroker = z.object({
|
|
163
186
|
account: chainflipAddress,
|
|
@@ -172,13 +195,7 @@ var fillOrKillParams = z.object({
|
|
|
172
195
|
refundAddress: z.string(),
|
|
173
196
|
minPriceX128: numericString,
|
|
174
197
|
maxOraclePriceSlippage: basisPoints.nullish().transform((v) => v ?? null),
|
|
175
|
-
refundCcmMetadata: ccmParamsSchema.nullish().optional().default(null)
|
|
176
|
-
(v) => v && {
|
|
177
|
-
...v,
|
|
178
|
-
cfParameters: v.ccmAdditionalData
|
|
179
|
-
// deprecated
|
|
180
|
-
}
|
|
181
|
-
)
|
|
198
|
+
refundCcmMetadata: ccmParamsSchema.nullish().optional().default(null)
|
|
182
199
|
});
|
|
183
200
|
var validatePolkadotAddress = (address) => {
|
|
184
201
|
if (isHex(address)) return address.length === 66;
|
|
@@ -238,6 +255,89 @@ var validators = {
|
|
|
238
255
|
};
|
|
239
256
|
var validateAddress = (chain2, address, network) => validators[network][chain2](address);
|
|
240
257
|
|
|
258
|
+
// ../shared/src/api/openSwapDepositChannel.ts
|
|
259
|
+
var assets = z.union([
|
|
260
|
+
z.object({
|
|
261
|
+
srcAsset: assetAndChain,
|
|
262
|
+
destAsset: assetAndChain
|
|
263
|
+
}),
|
|
264
|
+
/** @deprecated DEPRECATED(1.12): remove this variant */
|
|
265
|
+
z.object({
|
|
266
|
+
srcAsset: asset,
|
|
267
|
+
destAsset: asset,
|
|
268
|
+
srcChain: chain,
|
|
269
|
+
destChain: chain
|
|
270
|
+
}).transform(({ srcAsset, destAsset, srcChain, destChain }) => ({
|
|
271
|
+
srcAsset: assetAndChain.parse({ asset: srcAsset, chain: srcChain }),
|
|
272
|
+
destAsset: assetAndChain.parse({ asset: destAsset, chain: destChain })
|
|
273
|
+
}))
|
|
274
|
+
]);
|
|
275
|
+
var getOpenSwapDepositChannelSchema = (network) => z.intersection(
|
|
276
|
+
assets,
|
|
277
|
+
z.object({
|
|
278
|
+
destAddress: z.string(),
|
|
279
|
+
amount: numericString,
|
|
280
|
+
ccmParams: ccmParamsSchema.optional(),
|
|
281
|
+
maxBoostFeeBps: z.number().optional(),
|
|
282
|
+
srcAddress: z.string().optional(),
|
|
283
|
+
fillOrKillParams,
|
|
284
|
+
dcaParams: dcaParams.optional(),
|
|
285
|
+
quote: z.object({
|
|
286
|
+
intermediateAmount: z.string().optional(),
|
|
287
|
+
egressAmount: z.string(),
|
|
288
|
+
estimatedPrice: z.string(),
|
|
289
|
+
recommendedSlippageTolerancePercent: z.number().optional(),
|
|
290
|
+
recommendedLivePriceSlippageTolerancePercent: z.number().optional()
|
|
291
|
+
}).optional(),
|
|
292
|
+
takeCommission: z.boolean().optional()
|
|
293
|
+
})
|
|
294
|
+
).superRefine(({ ...val }, ctx) => {
|
|
295
|
+
if (!validateAddress(val.destAsset.chain, val.destAddress, network)) {
|
|
296
|
+
ctx.addIssue({
|
|
297
|
+
message: `Address "${val.destAddress}" is not a valid "${val.destAsset.chain}" address for "${network}"`,
|
|
298
|
+
code: z.ZodIssueCode.custom
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
if (!validateAddress(val.srcAsset.chain, val.fillOrKillParams.refundAddress, network)) {
|
|
302
|
+
ctx.addIssue({
|
|
303
|
+
message: `Address "${val.fillOrKillParams.refundAddress}" is not a valid "${val.srcAsset.chain}" address for "${network}"`,
|
|
304
|
+
code: z.ZodIssueCode.custom
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}).transform(({ amount, ...rest }) => ({
|
|
308
|
+
...rest,
|
|
309
|
+
expectedDepositAmount: amount
|
|
310
|
+
}));
|
|
311
|
+
var DepositChannelInfo = z.object({
|
|
312
|
+
id: z.string(),
|
|
313
|
+
depositAddress: z.string(),
|
|
314
|
+
brokerCommissionBps: z.number(),
|
|
315
|
+
maxBoostFeeBps: z.number(),
|
|
316
|
+
issuedBlock: z.number(),
|
|
317
|
+
srcChainExpiryBlock: z.string(),
|
|
318
|
+
estimatedExpiryTime: z.number().optional(),
|
|
319
|
+
channelOpeningFee: z.string()
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
// ../shared/src/strings.ts
|
|
323
|
+
var toCamelCase = (str) => str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
324
|
+
|
|
325
|
+
// ../shared/src/objects.ts
|
|
326
|
+
var transformKeysToCamelCase = (obj) => {
|
|
327
|
+
if (Array.isArray(obj)) {
|
|
328
|
+
return obj.map(transformKeysToCamelCase);
|
|
329
|
+
}
|
|
330
|
+
if (obj !== null && typeof obj === "object") {
|
|
331
|
+
return Object.fromEntries(
|
|
332
|
+
Object.entries(obj).map(([key, value]) => [
|
|
333
|
+
toCamelCase(key),
|
|
334
|
+
transformKeysToCamelCase(value)
|
|
335
|
+
])
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
return obj;
|
|
339
|
+
};
|
|
340
|
+
|
|
241
341
|
// ../shared/src/broker.ts
|
|
242
342
|
var transformedDcaParamsSchema = dcaParams.transform(
|
|
243
343
|
({ numberOfChunks, chunkIntervalBlocks }) => ({
|
|
@@ -249,9 +349,7 @@ var transformedCcmParamsSchema = ccmParamsSchema.transform(
|
|
|
249
349
|
({ message, gasBudget, ccmAdditionalData }) => ({
|
|
250
350
|
message,
|
|
251
351
|
gas_budget: gasBudget,
|
|
252
|
-
ccm_additional_data: ccmAdditionalData
|
|
253
|
-
/** @deprecated DEPRECATED(1.10) we still need to pass cf_parameters until 1.8 is deployed to all networks */
|
|
254
|
-
cf_parameters: ccmAdditionalData
|
|
352
|
+
ccm_additional_data: ccmAdditionalData
|
|
255
353
|
})
|
|
256
354
|
);
|
|
257
355
|
var transformedFokSchema = z.object({
|
|
@@ -338,7 +436,7 @@ var getVaultSwapParameterEncodingRequestSchema = (network) => z.object({
|
|
|
338
436
|
}).transform((data) => {
|
|
339
437
|
let extraParams;
|
|
340
438
|
if (data.srcAsset.chain === "Bitcoin") {
|
|
341
|
-
const minOutputAmount = BigNumber
|
|
439
|
+
const minOutputAmount = BigNumber(data.amount.toString()).multipliedBy(priceX128ToPrice(data.fillOrKillParams.min_price)).toFixed(0);
|
|
342
440
|
extraParams = {
|
|
343
441
|
chain: "Bitcoin",
|
|
344
442
|
min_output_amount: `0x${BigInt(minOutputAmount).toString(16)}`,
|
|
@@ -367,12 +465,6 @@ var getVaultSwapParameterEncodingRequestSchema = (network) => z.object({
|
|
|
367
465
|
});
|
|
368
466
|
async function requestSwapDepositAddress(request, opts, chainflipNetwork) {
|
|
369
467
|
const client = new HttpClient(opts.url);
|
|
370
|
-
if (request.srcChain && typeof request.srcAsset === "string") {
|
|
371
|
-
request = { ...request, srcAsset: { asset: request.srcAsset, chain: request.srcChain } };
|
|
372
|
-
}
|
|
373
|
-
if (request.destChain && typeof request.destAsset === "string") {
|
|
374
|
-
request = { ...request, destAsset: { asset: request.destAsset, chain: request.destChain } };
|
|
375
|
-
}
|
|
376
468
|
const params = getDepositAddressRequestSchema(chainflipNetwork).parse(request);
|
|
377
469
|
const response = await client.sendRequest(
|
|
378
470
|
"broker_request_swap_deposit_address",
|
|
@@ -419,7 +511,7 @@ async function requestSwapParameterEncoding(request, opts, chainflipNetwork) {
|
|
|
419
511
|
);
|
|
420
512
|
return transformKeysToCamelCase(response);
|
|
421
513
|
}
|
|
422
|
-
var
|
|
514
|
+
var getParameterEncodingRequestSchema = (network) => z.object({
|
|
423
515
|
srcAsset: assetAndChain,
|
|
424
516
|
srcAddress: z.string().optional(),
|
|
425
517
|
destAsset: assetAndChain,
|
|
@@ -431,8 +523,8 @@ var cfParameterEncodingRequestSchema = z.object({
|
|
|
431
523
|
affiliates: z.array(affiliateBroker).optional(),
|
|
432
524
|
fillOrKillParams: transformedFokSchema,
|
|
433
525
|
dcaParams: transformedDcaParamsSchema.optional(),
|
|
434
|
-
|
|
435
|
-
}).superRefine(({
|
|
526
|
+
brokerAccount: chainflipAddress.optional()
|
|
527
|
+
}).superRefine(({ ...val }, ctx) => {
|
|
436
528
|
if (val.srcAddress && !validateAddress(val.srcAsset.chain, val.srcAddress, network)) {
|
|
437
529
|
ctx.addIssue({
|
|
438
530
|
message: `Address "${val.srcAddress}" is not a valid "${val.srcAsset.chain}" address for "${network}"`,
|
|
@@ -451,10 +543,10 @@ var cfParameterEncodingRequestSchema = z.object({
|
|
|
451
543
|
code: z.ZodIssueCode.custom
|
|
452
544
|
});
|
|
453
545
|
}
|
|
454
|
-
}).transform(({
|
|
455
|
-
async function requestCfParametersEncoding(request, opts) {
|
|
546
|
+
}).transform(({ ...rest }) => rest);
|
|
547
|
+
async function requestCfParametersEncoding(request, opts, network) {
|
|
456
548
|
const client = new HttpClient(opts.url);
|
|
457
|
-
const params =
|
|
549
|
+
const params = getParameterEncodingRequestSchema(network).parse(request);
|
|
458
550
|
const response = await client.sendRequest(
|
|
459
551
|
"broker_encode_cf_parameters",
|
|
460
552
|
params.srcAsset,
|
|
@@ -469,213 +561,40 @@ async function requestCfParametersEncoding(request, opts) {
|
|
|
469
561
|
);
|
|
470
562
|
return response;
|
|
471
563
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
)
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
})
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
}) => ({
|
|
496
|
-
retry_duration: retryDurationBlocks,
|
|
497
|
-
refund_address: refundAddress,
|
|
498
|
-
min_price: `0x${BigInt(minPriceX128).toString(16)}`,
|
|
499
|
-
max_oracle_price_slippage: maxOraclePriceSlippage,
|
|
500
|
-
refund_ccm_metadata: refundCcmMetadata && {
|
|
501
|
-
gas_budget: refundCcmMetadata.gasBudget,
|
|
502
|
-
message: refundCcmMetadata.message,
|
|
503
|
-
ccm_additional_data: refundCcmMetadata.ccmAdditionalData,
|
|
504
|
-
/** @deprecated DEPRECATED(1.10) */
|
|
505
|
-
cf_parameters: refundCcmMetadata.cfParameters
|
|
506
|
-
}
|
|
507
|
-
})
|
|
508
|
-
);
|
|
509
|
-
var TransformedDcaParams = dcaParams.transform(
|
|
510
|
-
({ numberOfChunks, chunkIntervalBlocks }) => ({
|
|
511
|
-
number_of_chunks: numberOfChunks,
|
|
512
|
-
chunk_interval: chunkIntervalBlocks
|
|
513
|
-
})
|
|
514
|
-
);
|
|
515
|
-
var EncodeVaultSwapBody = z.object({
|
|
516
|
-
srcAsset: assetAndChain,
|
|
517
|
-
srcAddress: z.string().optional(),
|
|
518
|
-
destAsset: assetAndChain,
|
|
519
|
-
destAddress: z.string(),
|
|
520
|
-
amount: unsignedInteger,
|
|
521
|
-
commissionBps: z.number().optional().default(0),
|
|
522
|
-
ccmParams: TransformedCcmParams.optional(),
|
|
523
|
-
maxBoostFeeBps: z.number().optional(),
|
|
524
|
-
affiliates: z.array(affiliateBroker).optional(),
|
|
525
|
-
fillOrKillParams: FillOrKillParams,
|
|
526
|
-
dcaParams: TransformedDcaParams.optional(),
|
|
527
|
-
extraParams: z.object({ seed: hexString.optional() }).optional(),
|
|
528
|
-
network: z.enum(chainflipNetworks),
|
|
529
|
-
brokerAccount: chainflipAddress.optional()
|
|
530
|
-
}).superRefine(({ network, ...val }, ctx) => {
|
|
531
|
-
if (val.srcAddress && !validateAddress(val.srcAsset.chain, val.srcAddress, network)) {
|
|
532
|
-
ctx.addIssue({
|
|
533
|
-
message: `Address "${val.srcAddress}" is not a valid "${val.srcAsset.chain}" address for "${network}"`,
|
|
534
|
-
code: z.ZodIssueCode.custom
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
if (!validateAddress(val.destAsset.chain, val.destAddress, network)) {
|
|
538
|
-
ctx.addIssue({
|
|
539
|
-
message: `Address "${val.destAddress}" is not a valid "${val.destAsset.chain}" address for "${network}"`,
|
|
540
|
-
code: z.ZodIssueCode.custom
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
if (!validateAddress(val.srcAsset.chain, val.fillOrKillParams.refund_address, network)) {
|
|
564
|
+
var getAccountCreationDepositChannelSchema = (network) => z.object({
|
|
565
|
+
signatureData: z.object({
|
|
566
|
+
Ethereum: z.object({
|
|
567
|
+
signature: hexString,
|
|
568
|
+
signer: hexString,
|
|
569
|
+
sigType: z.literal("Eip712")
|
|
570
|
+
}).transform(({ sigType, ...rest }) => ({
|
|
571
|
+
sig_type: sigType,
|
|
572
|
+
...rest
|
|
573
|
+
}))
|
|
574
|
+
}),
|
|
575
|
+
transactionMetadata: z.object({
|
|
576
|
+
nonce: z.number().gte(0),
|
|
577
|
+
expiryBlock: z.number().positive()
|
|
578
|
+
}).transform(({ nonce, expiryBlock }) => ({
|
|
579
|
+
nonce,
|
|
580
|
+
expiry_block: expiryBlock
|
|
581
|
+
})),
|
|
582
|
+
asset: assetAndChain,
|
|
583
|
+
refundAddress: z.string(),
|
|
584
|
+
boostFeeBps: z.number().optional().default(0)
|
|
585
|
+
}).superRefine(({ asset: asset2, refundAddress }, ctx) => {
|
|
586
|
+
if (!validateAddress(asset2.chain, refundAddress, network)) {
|
|
544
587
|
ctx.addIssue({
|
|
545
|
-
message: `Address "${
|
|
588
|
+
message: `Address "${refundAddress}" is not a valid "${asset2.chain}" address for "${network}"`,
|
|
546
589
|
code: z.ZodIssueCode.custom
|
|
547
590
|
});
|
|
548
591
|
}
|
|
549
|
-
if (val.extraParams?.seed && val.extraParams.seed.length !== 66) {
|
|
550
|
-
ctx.addIssue({ message: "Seed must be 32 bytes", code: z.ZodIssueCode.custom });
|
|
551
|
-
}
|
|
552
|
-
}).transform(({ network, ...data }) => {
|
|
553
|
-
let extraParams;
|
|
554
|
-
if (data.srcAsset.chain === "Bitcoin") {
|
|
555
|
-
const minOutputAmount = BigNumber(data.amount.toString()).multipliedBy(priceX128ToPrice(data.fillOrKillParams.min_price)).toFixed(0);
|
|
556
|
-
extraParams = {
|
|
557
|
-
chain: "Bitcoin",
|
|
558
|
-
min_output_amount: `0x${BigInt(minOutputAmount).toString(16)}`,
|
|
559
|
-
retry_duration: data.fillOrKillParams.retry_duration,
|
|
560
|
-
max_oracle_price_slippage: data.fillOrKillParams.max_oracle_price_slippage
|
|
561
|
-
};
|
|
562
|
-
} else if (data.srcAsset.chain === "Ethereum" || data.srcAsset.chain === "Arbitrum") {
|
|
563
|
-
extraParams = {
|
|
564
|
-
chain: data.srcAsset.chain,
|
|
565
|
-
input_amount: `0x${data.amount.toString(16)}`,
|
|
566
|
-
refund_parameters: data.fillOrKillParams
|
|
567
|
-
};
|
|
568
|
-
} else if (data.srcAsset.chain === "Solana") {
|
|
569
|
-
assert(data.srcAddress, "srcAddress is required for Solana");
|
|
570
|
-
extraParams = {
|
|
571
|
-
chain: "Solana",
|
|
572
|
-
from: data.srcAddress,
|
|
573
|
-
seed: data.extraParams?.seed ?? bytesToHex(crypto.getRandomValues(new Uint8Array(32))),
|
|
574
|
-
input_amount: `0x${data.amount.toString(16)}`,
|
|
575
|
-
refund_parameters: data.fillOrKillParams
|
|
576
|
-
};
|
|
577
|
-
} else {
|
|
578
|
-
throw new Error(`parameter encoding is not supported for ${data.srcAsset.chain}`);
|
|
579
|
-
}
|
|
580
|
-
return { ...data, extraParams };
|
|
581
|
-
});
|
|
582
|
-
var EncodedVaultSwapData = z.discriminatedUnion("chain", [
|
|
583
|
-
z.object({
|
|
584
|
-
chain: z.literal("Bitcoin"),
|
|
585
|
-
nulldataPayload: hexString,
|
|
586
|
-
depositAddress: z.string()
|
|
587
|
-
}),
|
|
588
|
-
z.object({
|
|
589
|
-
chain: z.enum(["Ethereum", "Arbitrum"]),
|
|
590
|
-
value: z.string(),
|
|
591
|
-
to: hexString,
|
|
592
|
-
calldata: hexString,
|
|
593
|
-
sourceTokenAddress: hexString.optional()
|
|
594
|
-
}),
|
|
595
|
-
z.object({
|
|
596
|
-
chain: z.literal("Solana"),
|
|
597
|
-
programId: z.string(),
|
|
598
|
-
data: hexString,
|
|
599
|
-
accounts: z.array(
|
|
600
|
-
z.object({
|
|
601
|
-
pubkey: z.string(),
|
|
602
|
-
isSigner: z.boolean(),
|
|
603
|
-
isWritable: z.boolean()
|
|
604
|
-
})
|
|
605
|
-
)
|
|
606
|
-
})
|
|
607
|
-
]);
|
|
608
|
-
var NetworkInfo = z6.object({
|
|
609
|
-
assets: z6.array(
|
|
610
|
-
z6.object({
|
|
611
|
-
asset: z6.enum(chainflipAssets),
|
|
612
|
-
vaultSwapDepositsEnabled: z6.boolean(),
|
|
613
|
-
depositChannelDepositsEnabled: z6.boolean(),
|
|
614
|
-
depositChannelCreationEnabled: z6.boolean(),
|
|
615
|
-
egressEnabled: z6.boolean(),
|
|
616
|
-
boostDepositsEnabled: z6.boolean()
|
|
617
|
-
})
|
|
618
|
-
),
|
|
619
|
-
cfBrokerCommissionBps: z6.number()
|
|
620
|
-
});
|
|
621
|
-
var ChainflipAsset = z.enum(chainflipAssets);
|
|
622
|
-
var NetworkStatus = z.object({
|
|
623
|
-
assets: z.object({
|
|
624
|
-
deposit: z.array(ChainflipAsset),
|
|
625
|
-
destination: z.array(ChainflipAsset),
|
|
626
|
-
all: z.array(ChainflipAsset)
|
|
627
|
-
}),
|
|
628
|
-
boostDepositsEnabled: z.boolean(),
|
|
629
|
-
cfBrokerCommissionBps: z.number()
|
|
630
|
-
});
|
|
631
|
-
var OpenSwapDepositChannelBody = z.object({
|
|
632
|
-
srcAsset: asset,
|
|
633
|
-
destAsset: asset,
|
|
634
|
-
srcChain: chain,
|
|
635
|
-
destChain: chain,
|
|
636
|
-
destAddress: z.string(),
|
|
637
|
-
amount: numericString,
|
|
638
|
-
ccmParams: ccmParamsSchema.optional(),
|
|
639
|
-
maxBoostFeeBps: z.number().optional(),
|
|
640
|
-
srcAddress: z.string().optional(),
|
|
641
|
-
fillOrKillParams,
|
|
642
|
-
dcaParams: dcaParams.optional(),
|
|
643
|
-
quote: z.object({
|
|
644
|
-
intermediateAmount: z.string().optional(),
|
|
645
|
-
egressAmount: z.string(),
|
|
646
|
-
estimatedPrice: z.string(),
|
|
647
|
-
recommendedSlippageTolerancePercent: z.number().optional(),
|
|
648
|
-
recommendedLivePriceSlippageTolerancePercent: z.number().optional()
|
|
649
|
-
}).optional(),
|
|
650
|
-
takeCommission: z.boolean().optional()
|
|
651
|
-
}).transform(({ amount, ...rest }) => ({
|
|
652
|
-
...rest,
|
|
653
|
-
expectedDepositAmount: amount
|
|
654
|
-
}));
|
|
655
|
-
var DepositChannelInfo = z.object({
|
|
656
|
-
id: z.string(),
|
|
657
|
-
depositAddress: z.string(),
|
|
658
|
-
brokerCommissionBps: z.number(),
|
|
659
|
-
maxBoostFeeBps: z.number(),
|
|
660
|
-
issuedBlock: z.number(),
|
|
661
|
-
srcChainExpiryBlock: z.string(),
|
|
662
|
-
estimatedExpiryTime: z.number().optional(),
|
|
663
|
-
channelOpeningFee: z.string()
|
|
664
592
|
});
|
|
665
593
|
|
|
666
594
|
// ../shared/src/api/contract.ts
|
|
667
595
|
var c = initContract();
|
|
668
|
-
var
|
|
596
|
+
var createApiContract = (network) => c.router(
|
|
669
597
|
{
|
|
670
|
-
/** @deprecated DEPRECATED(1.10): using `networkInfo` now */
|
|
671
|
-
networkStatus: {
|
|
672
|
-
method: "GET",
|
|
673
|
-
path: "/networkStatus",
|
|
674
|
-
responses: {
|
|
675
|
-
200: NetworkStatus
|
|
676
|
-
},
|
|
677
|
-
summary: "Get information about the Chainflip network"
|
|
678
|
-
},
|
|
679
598
|
networkInfo: {
|
|
680
599
|
method: "GET",
|
|
681
600
|
path: "/networkInfo",
|
|
@@ -687,7 +606,7 @@ var apiContract = c.router(
|
|
|
687
606
|
encodeCfParameters: {
|
|
688
607
|
method: "POST",
|
|
689
608
|
path: "/encodeCfParameters",
|
|
690
|
-
body:
|
|
609
|
+
body: getParameterEncodingRequestSchema(network),
|
|
691
610
|
responses: {
|
|
692
611
|
200: hexString
|
|
693
612
|
}
|
|
@@ -695,7 +614,7 @@ var apiContract = c.router(
|
|
|
695
614
|
encodeVaultSwapData: {
|
|
696
615
|
method: "POST",
|
|
697
616
|
path: "/encodeVaultSwapData",
|
|
698
|
-
body:
|
|
617
|
+
body: getVaultSwapParameterEncodingRequestSchema(network),
|
|
699
618
|
responses: {
|
|
700
619
|
200: EncodedVaultSwapData
|
|
701
620
|
}
|
|
@@ -703,7 +622,15 @@ var apiContract = c.router(
|
|
|
703
622
|
openSwapDepositChannel: {
|
|
704
623
|
method: "POST",
|
|
705
624
|
path: "/openSwapDepositChannel",
|
|
706
|
-
body:
|
|
625
|
+
body: getOpenSwapDepositChannelSchema(network),
|
|
626
|
+
responses: {
|
|
627
|
+
201: DepositChannelInfo
|
|
628
|
+
}
|
|
629
|
+
},
|
|
630
|
+
openAccountCreationDepositChannel: {
|
|
631
|
+
method: "POST",
|
|
632
|
+
path: "/openAccountCreationDepositChannel",
|
|
633
|
+
body: getAccountCreationDepositChannelSchema(network),
|
|
707
634
|
responses: {
|
|
708
635
|
201: DepositChannelInfo
|
|
709
636
|
}
|
|
@@ -713,8 +640,6 @@ var apiContract = c.router(
|
|
|
713
640
|
pathPrefix: "/api"
|
|
714
641
|
}
|
|
715
642
|
);
|
|
716
|
-
|
|
717
|
-
// ../shared/src/dataStructures.ts
|
|
718
643
|
var MultiCache = class {
|
|
719
644
|
constructor(fetchers) {
|
|
720
645
|
this.fetchers = fetchers;
|
|
@@ -815,7 +740,7 @@ var getChainData = (chain2, network, env) => ({
|
|
|
815
740
|
|
|
816
741
|
// package.json
|
|
817
742
|
var package_default = {
|
|
818
|
-
version: "
|
|
743
|
+
version: "2.0.0-beta.0"};
|
|
819
744
|
|
|
820
745
|
// src/swap/consts.ts
|
|
821
746
|
var CF_SDK_VERSION_HEADERS = { "X-Chainflip-Sdk-Version": package_default.version };
|
|
@@ -882,6 +807,7 @@ var SwapSDK = class {
|
|
|
882
807
|
dcaEnabled = false;
|
|
883
808
|
blackListedAssets;
|
|
884
809
|
constructor(options = {}) {
|
|
810
|
+
assert(options.broker?.commissionBps === void 0, "broker.commissionBps is deprecated");
|
|
885
811
|
const network = options.network ?? "perseverance";
|
|
886
812
|
this.options = {
|
|
887
813
|
...options,
|
|
@@ -889,7 +815,7 @@ var SwapSDK = class {
|
|
|
889
815
|
backendUrl: options.backendUrl ?? BACKEND_SERVICE_URLS[network]
|
|
890
816
|
};
|
|
891
817
|
this.rpcConfig = options.rpcUrl ? { rpcUrl: options.rpcUrl } : { network };
|
|
892
|
-
this.apiClient = initClient(
|
|
818
|
+
this.apiClient = initClient(createApiContract(this.options.network), {
|
|
893
819
|
// remove trailing slashes. this bug has been fixed in `@ts-rest/core`
|
|
894
820
|
// but it hasn't been released yet
|
|
895
821
|
baseUrl: this.options.backendUrl.replace(/\/+$/, ""),
|
|
@@ -928,8 +854,8 @@ var SwapSDK = class {
|
|
|
928
854
|
return this.cache.read("environment");
|
|
929
855
|
}
|
|
930
856
|
async getSupportedAssets(type) {
|
|
931
|
-
const
|
|
932
|
-
return
|
|
857
|
+
const assets2 = await this.cache.read("networkInfo");
|
|
858
|
+
return assets2.assets.filter((a) => !this.blackListedAssets.includes(a.asset)).filter((a) => {
|
|
933
859
|
switch (type) {
|
|
934
860
|
case "all":
|
|
935
861
|
return true;
|
|
@@ -965,7 +891,7 @@ var SwapSDK = class {
|
|
|
965
891
|
if (this.shouldTakeCommission()) {
|
|
966
892
|
return (await this.cache.read("networkInfo")).cfBrokerCommissionBps;
|
|
967
893
|
}
|
|
968
|
-
return brokerCommissionBps ??
|
|
894
|
+
return brokerCommissionBps ?? 0;
|
|
969
895
|
}
|
|
970
896
|
async getQuoteV2(quoteRequest, options = {}) {
|
|
971
897
|
const submitterBrokerCommissionBps = await this.getCommissionBps(
|
|
@@ -1051,10 +977,8 @@ var SwapSDK = class {
|
|
|
1051
977
|
}
|
|
1052
978
|
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
1053
979
|
const depositAddressRequest = {
|
|
1054
|
-
srcAsset: quote.srcAsset
|
|
1055
|
-
|
|
1056
|
-
destAsset: quote.destAsset.asset,
|
|
1057
|
-
destChain: quote.destAsset.chain,
|
|
980
|
+
srcAsset: quote.srcAsset,
|
|
981
|
+
destAsset: quote.destAsset,
|
|
1058
982
|
srcAddress,
|
|
1059
983
|
destAddress,
|
|
1060
984
|
dcaParams: quote.type === "DCA" ? quote.dcaParams : void 0,
|
|
@@ -1103,6 +1027,10 @@ var SwapSDK = class {
|
|
|
1103
1027
|
}
|
|
1104
1028
|
return {
|
|
1105
1029
|
...depositAddressRequest,
|
|
1030
|
+
srcAsset: quote.srcAsset.asset,
|
|
1031
|
+
destAsset: quote.destAsset.asset,
|
|
1032
|
+
srcChain: quote.srcAsset.chain,
|
|
1033
|
+
destChain: quote.destAsset.chain,
|
|
1106
1034
|
depositChannelId: response.id,
|
|
1107
1035
|
depositAddress: response.depositAddress,
|
|
1108
1036
|
brokerCommissionBps: response.brokerCommissionBps,
|
|
@@ -1168,9 +1096,7 @@ var SwapSDK = class {
|
|
|
1168
1096
|
!vaultSwapRequest.affiliates?.length || vaultSwapRequest.brokerAccount,
|
|
1169
1097
|
"Affiliate brokers are supported only when setting a broker account"
|
|
1170
1098
|
);
|
|
1171
|
-
const res = await this.apiClient.encodeVaultSwapData({
|
|
1172
|
-
body: { ...vaultSwapRequest, network: this.options.network }
|
|
1173
|
-
});
|
|
1099
|
+
const res = await this.apiClient.encodeVaultSwapData({ body: vaultSwapRequest });
|
|
1174
1100
|
if (res.status !== 200) {
|
|
1175
1101
|
throw new Error("Failed to encode vault swap data", { cause: res });
|
|
1176
1102
|
}
|
|
@@ -1210,15 +1136,18 @@ var SwapSDK = class {
|
|
|
1210
1136
|
fillOrKillParams: parseFoKParams(fillOrKillParams2, quote),
|
|
1211
1137
|
dcaParams: quote.type === "DCA" ? quote.dcaParams : void 0,
|
|
1212
1138
|
commissionBps: await this.getCommissionBps(brokerCommissionBps),
|
|
1213
|
-
affiliates
|
|
1214
|
-
network: this.options.network
|
|
1139
|
+
affiliates
|
|
1215
1140
|
};
|
|
1216
1141
|
if (this.options.broker) {
|
|
1217
1142
|
assert(
|
|
1218
1143
|
!brokerAccount,
|
|
1219
1144
|
"Cannot overwrite broker account when initializing the SDK with a brokerUrl"
|
|
1220
1145
|
);
|
|
1221
|
-
return requestCfParametersEncoding(
|
|
1146
|
+
return requestCfParametersEncoding(
|
|
1147
|
+
requestParams,
|
|
1148
|
+
{ url: this.options.broker.url },
|
|
1149
|
+
this.options.network
|
|
1150
|
+
);
|
|
1222
1151
|
}
|
|
1223
1152
|
assert(
|
|
1224
1153
|
!requestParams.commissionBps || brokerAccount || this.shouldTakeCommission(),
|
|
@@ -1236,29 +1165,24 @@ var SwapSDK = class {
|
|
|
1236
1165
|
}
|
|
1237
1166
|
return res.body;
|
|
1238
1167
|
}
|
|
1239
|
-
/** @deprecated DEPRECATED(1.11) removing this method as it appears to not be used */
|
|
1240
|
-
// eslint-disable-next-line class-methods-use-this
|
|
1241
|
-
getOnChainSwapExtrinsicArgs({
|
|
1242
|
-
quote,
|
|
1243
|
-
fillOrKillParams: fillOrKillParams2
|
|
1244
|
-
}) {
|
|
1245
|
-
assert(quote.isOnChain, "Cannot get extrinsic args for non-on-chain quotes");
|
|
1246
|
-
const { retryDurationBlocks, minPriceX128 } = parseFoKParams(fillOrKillParams2, quote);
|
|
1247
|
-
return [
|
|
1248
|
-
quote.depositAmount,
|
|
1249
|
-
getInternalAsset(quote.srcAsset),
|
|
1250
|
-
getInternalAsset(quote.destAsset),
|
|
1251
|
-
retryDurationBlocks,
|
|
1252
|
-
minPriceX128,
|
|
1253
|
-
quote.type === "DCA" ? {
|
|
1254
|
-
number_of_chunks: quote.dcaParams.numberOfChunks,
|
|
1255
|
-
chunk_interval: quote.dcaParams.chunkIntervalBlocks
|
|
1256
|
-
} : null
|
|
1257
|
-
];
|
|
1258
|
-
}
|
|
1259
1168
|
async checkBoostEnabled() {
|
|
1260
|
-
const { assets } = await this.cache.read("networkInfo");
|
|
1261
|
-
return
|
|
1169
|
+
const { assets: assets2 } = await this.cache.read("networkInfo");
|
|
1170
|
+
return assets2.find((a) => a.asset === "Btc")?.boostDepositsEnabled ?? true;
|
|
1171
|
+
}
|
|
1172
|
+
async requestAccountCreationDepositAddress(asset2, refundAddress, signatureData, transactionMetadata, boostFeeBps = 0) {
|
|
1173
|
+
const res = await this.apiClient.openAccountCreationDepositChannel({
|
|
1174
|
+
body: {
|
|
1175
|
+
asset: internalAssetToRpcAsset[asset2],
|
|
1176
|
+
refundAddress,
|
|
1177
|
+
signatureData,
|
|
1178
|
+
transactionMetadata,
|
|
1179
|
+
boostFeeBps
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
if (res.status !== 201) {
|
|
1183
|
+
throw new Error("Failed to request account creation deposit channel", { cause: res });
|
|
1184
|
+
}
|
|
1185
|
+
return res.body;
|
|
1262
1186
|
}
|
|
1263
1187
|
};
|
|
1264
1188
|
var arrayToMap = (array) => Object.fromEntries(array.map((item) => [item, item]));
|