@chainflip/sdk 1.9.0-beta.2 → 1.9.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 +51 -30
- package/dist/swap.d.cts +12 -7
- package/dist/swap.d.ts +12 -7
- package/dist/swap.js +51 -30
- package/package.json +4 -4
package/dist/swap.cjs
CHANGED
|
@@ -4,6 +4,8 @@ var chainflip = require('@chainflip/utils/chainflip');
|
|
|
4
4
|
var client = require('@trpc/client');
|
|
5
5
|
var superjson = require('superjson');
|
|
6
6
|
var rpc = require('@chainflip/rpc');
|
|
7
|
+
var assertion = require('@chainflip/utils/assertion');
|
|
8
|
+
var bytes = require('@chainflip/utils/bytes');
|
|
7
9
|
var ss58 = require('@chainflip/utils/ss58');
|
|
8
10
|
var string = require('@chainflip/utils/string');
|
|
9
11
|
var tickMath = require('@chainflip/utils/tickMath');
|
|
@@ -85,7 +87,7 @@ var chainflipAddress = zod.z.string().refine(
|
|
|
85
87
|
(address) => address.startsWith("cF") && ss58__namespace.decode(address),
|
|
86
88
|
(address) => ({ message: `${address} is not a valid Chainflip address` })
|
|
87
89
|
);
|
|
88
|
-
|
|
90
|
+
zod.z.string().refine(solana.isValidSolanaAddress, (address) => ({
|
|
89
91
|
message: `${address} is not a valid Solana address`
|
|
90
92
|
}));
|
|
91
93
|
var u128 = zod.z.union([zod.z.number(), numericString, hexString]).transform((arg) => BigInt(arg));
|
|
@@ -305,7 +307,7 @@ var getParameterEncodingRequestSchema = (network) => zod.z.object({
|
|
|
305
307
|
affiliates: zod.z.array(affiliateBroker).optional(),
|
|
306
308
|
fillOrKillParams: transformedFokSchema,
|
|
307
309
|
dcaParams: transformedDcaParamsSchema.optional(),
|
|
308
|
-
extraParams: zod.z.object({
|
|
310
|
+
extraParams: zod.z.object({ seed: hexString.optional() }).optional()
|
|
309
311
|
}).superRefine((val, ctx) => {
|
|
310
312
|
if (val.srcAddress && !validateAddress(val.srcAsset.chain, val.srcAddress, network)) {
|
|
311
313
|
ctx.addIssue({
|
|
@@ -313,20 +315,21 @@ var getParameterEncodingRequestSchema = (network) => zod.z.object({
|
|
|
313
315
|
code: zod.z.ZodIssueCode.custom
|
|
314
316
|
});
|
|
315
317
|
}
|
|
316
|
-
}).superRefine((val, ctx) => {
|
|
317
318
|
if (!validateAddress(val.destAsset.chain, val.destAddress, network)) {
|
|
318
319
|
ctx.addIssue({
|
|
319
320
|
message: `Address "${val.destAddress}" is not a valid "${val.destAsset.chain}" address for "${network}"`,
|
|
320
321
|
code: zod.z.ZodIssueCode.custom
|
|
321
322
|
});
|
|
322
323
|
}
|
|
323
|
-
}).superRefine((val, ctx) => {
|
|
324
324
|
if (!validateAddress(val.srcAsset.chain, val.fillOrKillParams.refund_address, network)) {
|
|
325
325
|
ctx.addIssue({
|
|
326
326
|
message: `Address "${val.fillOrKillParams.refund_address}" is not a valid "${val.srcAsset.chain}" address for "${network}"`,
|
|
327
327
|
code: zod.z.ZodIssueCode.custom
|
|
328
328
|
});
|
|
329
329
|
}
|
|
330
|
+
if (val.extraParams?.seed && val.extraParams.seed.length !== 66) {
|
|
331
|
+
ctx.addIssue({ message: "Seed must be 32 bytes", code: zod.z.ZodIssueCode.custom });
|
|
332
|
+
}
|
|
330
333
|
}).transform((data) => {
|
|
331
334
|
let extraParams;
|
|
332
335
|
if (data.srcAsset.chain === "Bitcoin") {
|
|
@@ -339,17 +342,16 @@ var getParameterEncodingRequestSchema = (network) => zod.z.object({
|
|
|
339
342
|
} else if (data.srcAsset.chain === "Ethereum" || data.srcAsset.chain === "Arbitrum") {
|
|
340
343
|
extraParams = {
|
|
341
344
|
chain: data.srcAsset.chain,
|
|
342
|
-
input_amount: `0x${
|
|
345
|
+
input_amount: `0x${data.amount.toString(16)}`,
|
|
343
346
|
refund_parameters: data.fillOrKillParams
|
|
344
347
|
};
|
|
345
348
|
} else if (data.srcAsset.chain === "Solana") {
|
|
346
349
|
assert(data.srcAddress, "srcAddress is required for Solana");
|
|
347
|
-
assert(data.extraParams?.solanaDataAccount, "solanaDataAccount is required for Solana");
|
|
348
350
|
extraParams = {
|
|
349
351
|
chain: "Solana",
|
|
350
352
|
from: data.srcAddress,
|
|
351
|
-
|
|
352
|
-
input_amount: `0x${
|
|
353
|
+
seed: data.extraParams?.seed ?? bytes.bytesToHex(crypto.getRandomValues(new Uint8Array(32))),
|
|
354
|
+
input_amount: `0x${data.amount.toString(16)}`,
|
|
353
355
|
refund_parameters: data.fillOrKillParams
|
|
354
356
|
};
|
|
355
357
|
} else {
|
|
@@ -378,8 +380,20 @@ async function requestSwapDepositAddress(request, opts, chainflipNetwork) {
|
|
|
378
380
|
params.fillOrKillParams,
|
|
379
381
|
params.dcaParams
|
|
380
382
|
);
|
|
381
|
-
|
|
382
|
-
|
|
383
|
+
switch (params.srcAsset.chain) {
|
|
384
|
+
case "Polkadot":
|
|
385
|
+
case "Assethub":
|
|
386
|
+
if (string.isHex(response.address)) {
|
|
387
|
+
response.address = ss58__namespace.encode({ data: response.address, ss58Format: DOT_PREFIX });
|
|
388
|
+
}
|
|
389
|
+
break;
|
|
390
|
+
case "Ethereum":
|
|
391
|
+
case "Arbitrum":
|
|
392
|
+
case "Bitcoin":
|
|
393
|
+
case "Solana":
|
|
394
|
+
break;
|
|
395
|
+
default:
|
|
396
|
+
return assertion.unreachable(params.srcAsset, "unexpected chain");
|
|
383
397
|
}
|
|
384
398
|
return transformKeysToCamelCase(response);
|
|
385
399
|
}
|
|
@@ -402,7 +416,7 @@ async function requestSwapParameterEncoding(request, opts, chainflipNetwork) {
|
|
|
402
416
|
}
|
|
403
417
|
|
|
404
418
|
// ../shared/src/dataStructures.ts
|
|
405
|
-
var
|
|
419
|
+
var MultiCache = class {
|
|
406
420
|
constructor(fetchers) {
|
|
407
421
|
this.fetchers = fetchers;
|
|
408
422
|
}
|
|
@@ -624,7 +638,7 @@ var getChainData = (chain2, network, env) => ({
|
|
|
624
638
|
|
|
625
639
|
// package.json
|
|
626
640
|
var package_default = {
|
|
627
|
-
version: "1.9.0
|
|
641
|
+
version: "1.9.0"};
|
|
628
642
|
|
|
629
643
|
// src/swap/consts.ts
|
|
630
644
|
var CF_SDK_VERSION_HEADERS = { "X-Chainflip-Sdk-Version": package_default.version };
|
|
@@ -706,7 +720,7 @@ var SwapSDK = class {
|
|
|
706
720
|
]
|
|
707
721
|
});
|
|
708
722
|
this.dcaEnabled = options.enabledFeatures?.dca ?? false;
|
|
709
|
-
this.cache = new
|
|
723
|
+
this.cache = new MultiCache({
|
|
710
724
|
environment: {
|
|
711
725
|
fetch: () => getEnvironment(this.rpcConfig),
|
|
712
726
|
ttl: 6e4
|
|
@@ -747,13 +761,20 @@ var SwapSDK = class {
|
|
|
747
761
|
]);
|
|
748
762
|
return supportedAssets.map((asset2) => getAssetData(asset2, this.options.network, env)).filter((asset2) => !chain2 || asset2.chain === chain2);
|
|
749
763
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
764
|
+
// eslint-disable-next-line class-methods-use-this
|
|
765
|
+
shouldTakeCommission() {
|
|
766
|
+
return false;
|
|
767
|
+
}
|
|
768
|
+
async getCommissionBps(brokerCommissionBps) {
|
|
769
|
+
if (this.shouldTakeCommission()) {
|
|
770
|
+
return (await this.cache.read("networkStatus")).cfBrokerCommissionBps;
|
|
756
771
|
}
|
|
772
|
+
return brokerCommissionBps ?? this.options.broker?.commissionBps ?? 0;
|
|
773
|
+
}
|
|
774
|
+
async getQuoteV2(quoteRequest, options = {}) {
|
|
775
|
+
const submitterBrokerCommissionBps = await this.getCommissionBps(
|
|
776
|
+
quoteRequest.brokerCommissionBps
|
|
777
|
+
);
|
|
757
778
|
return getQuoteV2(
|
|
758
779
|
this.options.backendUrl,
|
|
759
780
|
{
|
|
@@ -821,7 +842,7 @@ var SwapSDK = class {
|
|
|
821
842
|
fillOrKillParams: inputFoKParams,
|
|
822
843
|
affiliateBrokers: affiliates,
|
|
823
844
|
ccmParams,
|
|
824
|
-
brokerCommissionBps
|
|
845
|
+
brokerCommissionBps: brokerCommissionBpsParam
|
|
825
846
|
}) {
|
|
826
847
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
827
848
|
assertQuoteValid(quote);
|
|
@@ -831,6 +852,7 @@ var SwapSDK = class {
|
|
|
831
852
|
} else {
|
|
832
853
|
assert(!quote.ccmParams, "Cannot open regular channel for quote with CCM params");
|
|
833
854
|
}
|
|
855
|
+
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
834
856
|
const depositAddressRequest = {
|
|
835
857
|
srcAsset: quote.srcAsset.asset,
|
|
836
858
|
srcChain: quote.srcAsset.chain,
|
|
@@ -849,7 +871,7 @@ var SwapSDK = class {
|
|
|
849
871
|
const result = await requestSwapDepositAddress(
|
|
850
872
|
{
|
|
851
873
|
...depositAddressRequest,
|
|
852
|
-
commissionBps: brokerCommissionBps
|
|
874
|
+
commissionBps: brokerCommissionBps,
|
|
853
875
|
affiliates
|
|
854
876
|
},
|
|
855
877
|
{ url: this.options.broker.url },
|
|
@@ -858,14 +880,14 @@ var SwapSDK = class {
|
|
|
858
880
|
response = {
|
|
859
881
|
id: `${result.issuedBlock}-${quote.srcAsset.chain}-${result.channelId}`,
|
|
860
882
|
depositAddress: result.address,
|
|
861
|
-
brokerCommissionBps
|
|
883
|
+
brokerCommissionBps,
|
|
862
884
|
srcChainExpiryBlock: result.sourceChainExpiryBlock,
|
|
863
885
|
maxBoostFeeBps: depositAddressRequest.maxBoostFeeBps,
|
|
864
886
|
channelOpeningFee: result.channelOpeningFee
|
|
865
887
|
};
|
|
866
888
|
} else {
|
|
867
889
|
assert(
|
|
868
|
-
!brokerCommissionBps,
|
|
890
|
+
!brokerCommissionBps || this.shouldTakeCommission(),
|
|
869
891
|
"Broker commission is supported only when initializing the SDK with a brokerUrl"
|
|
870
892
|
);
|
|
871
893
|
assert(
|
|
@@ -874,7 +896,8 @@ var SwapSDK = class {
|
|
|
874
896
|
);
|
|
875
897
|
response = await this.trpc.openSwapDepositChannel.mutate({
|
|
876
898
|
...depositAddressRequest,
|
|
877
|
-
quote
|
|
899
|
+
quote,
|
|
900
|
+
takeCommission: this.shouldTakeCommission()
|
|
878
901
|
});
|
|
879
902
|
}
|
|
880
903
|
return {
|
|
@@ -898,7 +921,7 @@ var SwapSDK = class {
|
|
|
898
921
|
affiliateBrokers: affiliates,
|
|
899
922
|
ccmParams,
|
|
900
923
|
brokerAccount,
|
|
901
|
-
brokerCommissionBps,
|
|
924
|
+
brokerCommissionBps: brokerCommissionBpsParam,
|
|
902
925
|
extraParams
|
|
903
926
|
}) {
|
|
904
927
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
@@ -909,6 +932,7 @@ var SwapSDK = class {
|
|
|
909
932
|
} else {
|
|
910
933
|
assert(!quote.ccmParams, "Cannot encode regular swap for quote with CCM params");
|
|
911
934
|
}
|
|
935
|
+
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
912
936
|
const vaultSwapRequest = {
|
|
913
937
|
srcAsset: quote.srcAsset,
|
|
914
938
|
destAsset: quote.destAsset,
|
|
@@ -930,16 +954,13 @@ var SwapSDK = class {
|
|
|
930
954
|
"Cannot overwrite broker account when initializing the SDK with a brokerUrl"
|
|
931
955
|
);
|
|
932
956
|
return requestSwapParameterEncoding(
|
|
933
|
-
|
|
934
|
-
...vaultSwapRequest,
|
|
935
|
-
commissionBps: vaultSwapRequest.commissionBps ?? this.options.broker.commissionBps
|
|
936
|
-
},
|
|
957
|
+
vaultSwapRequest,
|
|
937
958
|
{ url: this.options.broker.url },
|
|
938
959
|
this.options.network
|
|
939
960
|
);
|
|
940
961
|
}
|
|
941
962
|
assert(
|
|
942
|
-
!vaultSwapRequest.commissionBps || vaultSwapRequest.brokerAccount,
|
|
963
|
+
!vaultSwapRequest.commissionBps || vaultSwapRequest.brokerAccount || this.shouldTakeCommission(),
|
|
943
964
|
"Broker commission is supported only when setting a broker account"
|
|
944
965
|
);
|
|
945
966
|
assert(
|
package/dist/swap.d.cts
CHANGED
|
@@ -51,6 +51,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
51
51
|
intermediateAmount?: string | undefined;
|
|
52
52
|
recommendedSlippageTolerancePercent?: number | undefined;
|
|
53
53
|
} | undefined;
|
|
54
|
+
takeCommission?: boolean | undefined;
|
|
54
55
|
};
|
|
55
56
|
_input_out: {
|
|
56
57
|
expectedDepositAmount: string;
|
|
@@ -81,6 +82,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
81
82
|
intermediateAmount?: string | undefined;
|
|
82
83
|
recommendedSlippageTolerancePercent?: number | undefined;
|
|
83
84
|
} | undefined;
|
|
85
|
+
takeCommission?: boolean | undefined;
|
|
84
86
|
};
|
|
85
87
|
_output_in: typeof _trpc_server.unsetMarker;
|
|
86
88
|
_output_out: typeof _trpc_server.unsetMarker;
|
|
@@ -137,7 +139,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
137
139
|
}[] | undefined;
|
|
138
140
|
srcAddress?: string | undefined;
|
|
139
141
|
extraParams?: {
|
|
140
|
-
|
|
142
|
+
seed?: string | undefined;
|
|
141
143
|
} | undefined;
|
|
142
144
|
} & {
|
|
143
145
|
brokerCommissionBps?: number | undefined;
|
|
@@ -151,7 +153,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
151
153
|
readonly input_amount?: undefined;
|
|
152
154
|
readonly refund_parameters?: undefined;
|
|
153
155
|
readonly from?: undefined;
|
|
154
|
-
readonly
|
|
156
|
+
readonly seed?: undefined;
|
|
155
157
|
} | {
|
|
156
158
|
readonly chain: "Ethereum" | "Arbitrum";
|
|
157
159
|
readonly input_amount: `0x${string}`;
|
|
@@ -163,11 +165,11 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
163
165
|
readonly min_output_amount?: undefined;
|
|
164
166
|
readonly retry_duration?: undefined;
|
|
165
167
|
readonly from?: undefined;
|
|
166
|
-
readonly
|
|
168
|
+
readonly seed?: undefined;
|
|
167
169
|
} | {
|
|
168
170
|
readonly chain: "Solana";
|
|
169
171
|
readonly from: string;
|
|
170
|
-
readonly
|
|
172
|
+
readonly seed: `0x${string}`;
|
|
171
173
|
readonly input_amount: `0x${string}`;
|
|
172
174
|
readonly refund_parameters: {
|
|
173
175
|
retry_duration: number;
|
|
@@ -566,6 +568,7 @@ interface Sending extends Receiving {
|
|
|
566
568
|
swap: SwapFields;
|
|
567
569
|
swapEgress: EgressFields | undefined;
|
|
568
570
|
refundEgress: EgressFields | undefined;
|
|
571
|
+
fallbackEgress: EgressFields | undefined;
|
|
569
572
|
}
|
|
570
573
|
type SwapStatusResponseV2 = ({
|
|
571
574
|
state: 'WAITING';
|
|
@@ -623,7 +626,7 @@ interface VaultSwapRequest {
|
|
|
623
626
|
brokerAccount?: `cF${string}`;
|
|
624
627
|
brokerCommissionBps?: number;
|
|
625
628
|
extraParams?: {
|
|
626
|
-
|
|
629
|
+
seed?: `0x${string}`;
|
|
627
630
|
};
|
|
628
631
|
}
|
|
629
632
|
type VaultSwapResponse = {
|
|
@@ -676,6 +679,8 @@ declare class SwapSDK {
|
|
|
676
679
|
private getSupportedAssets;
|
|
677
680
|
private getBoostPoolsDepth;
|
|
678
681
|
getAssets(chain?: ChainflipChain, type?: keyof NetworkStatus['assets']): Promise<AssetData[]>;
|
|
682
|
+
protected shouldTakeCommission(): boolean;
|
|
683
|
+
private getCommissionBps;
|
|
679
684
|
getQuoteV2(quoteRequest: QuoteRequest, options?: RequestOptions): Promise<QuoteResponseV2>;
|
|
680
685
|
getStatusV2(swapStatusRequest: SwapStatusRequest, options?: RequestOptions): Promise<SwapStatusResponseV2>;
|
|
681
686
|
private validateSwapAmount;
|
|
@@ -690,8 +695,8 @@ declare class SwapSDK {
|
|
|
690
695
|
} | ({
|
|
691
696
|
feeTierBps?: number;
|
|
692
697
|
} & UncheckedAssetAndChain)): Promise<BoostPoolDepth[]>;
|
|
693
|
-
requestDepositAddressV2({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerCommissionBps, }: DepositAddressRequestV2): Promise<DepositAddressResponseV2>;
|
|
694
|
-
encodeVaultSwapData({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerAccount, brokerCommissionBps, extraParams, }: VaultSwapRequest): Promise<VaultSwapResponse>;
|
|
698
|
+
requestDepositAddressV2({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerCommissionBps: brokerCommissionBpsParam, }: DepositAddressRequestV2): Promise<DepositAddressResponseV2>;
|
|
699
|
+
encodeVaultSwapData({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerAccount, brokerCommissionBps: brokerCommissionBpsParam, extraParams, }: VaultSwapRequest): Promise<VaultSwapResponse>;
|
|
695
700
|
checkBoostEnabled(): Promise<boolean>;
|
|
696
701
|
}
|
|
697
702
|
|
package/dist/swap.d.ts
CHANGED
|
@@ -51,6 +51,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
51
51
|
intermediateAmount?: string | undefined;
|
|
52
52
|
recommendedSlippageTolerancePercent?: number | undefined;
|
|
53
53
|
} | undefined;
|
|
54
|
+
takeCommission?: boolean | undefined;
|
|
54
55
|
};
|
|
55
56
|
_input_out: {
|
|
56
57
|
expectedDepositAmount: string;
|
|
@@ -81,6 +82,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
81
82
|
intermediateAmount?: string | undefined;
|
|
82
83
|
recommendedSlippageTolerancePercent?: number | undefined;
|
|
83
84
|
} | undefined;
|
|
85
|
+
takeCommission?: boolean | undefined;
|
|
84
86
|
};
|
|
85
87
|
_output_in: typeof _trpc_server.unsetMarker;
|
|
86
88
|
_output_out: typeof _trpc_server.unsetMarker;
|
|
@@ -137,7 +139,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
137
139
|
}[] | undefined;
|
|
138
140
|
srcAddress?: string | undefined;
|
|
139
141
|
extraParams?: {
|
|
140
|
-
|
|
142
|
+
seed?: string | undefined;
|
|
141
143
|
} | undefined;
|
|
142
144
|
} & {
|
|
143
145
|
brokerCommissionBps?: number | undefined;
|
|
@@ -151,7 +153,7 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
151
153
|
readonly input_amount?: undefined;
|
|
152
154
|
readonly refund_parameters?: undefined;
|
|
153
155
|
readonly from?: undefined;
|
|
154
|
-
readonly
|
|
156
|
+
readonly seed?: undefined;
|
|
155
157
|
} | {
|
|
156
158
|
readonly chain: "Ethereum" | "Arbitrum";
|
|
157
159
|
readonly input_amount: `0x${string}`;
|
|
@@ -163,11 +165,11 @@ declare const appRouter: _trpc_server.CreateRouterInner<_trpc_server.RootConfig<
|
|
|
163
165
|
readonly min_output_amount?: undefined;
|
|
164
166
|
readonly retry_duration?: undefined;
|
|
165
167
|
readonly from?: undefined;
|
|
166
|
-
readonly
|
|
168
|
+
readonly seed?: undefined;
|
|
167
169
|
} | {
|
|
168
170
|
readonly chain: "Solana";
|
|
169
171
|
readonly from: string;
|
|
170
|
-
readonly
|
|
172
|
+
readonly seed: `0x${string}`;
|
|
171
173
|
readonly input_amount: `0x${string}`;
|
|
172
174
|
readonly refund_parameters: {
|
|
173
175
|
retry_duration: number;
|
|
@@ -566,6 +568,7 @@ interface Sending extends Receiving {
|
|
|
566
568
|
swap: SwapFields;
|
|
567
569
|
swapEgress: EgressFields | undefined;
|
|
568
570
|
refundEgress: EgressFields | undefined;
|
|
571
|
+
fallbackEgress: EgressFields | undefined;
|
|
569
572
|
}
|
|
570
573
|
type SwapStatusResponseV2 = ({
|
|
571
574
|
state: 'WAITING';
|
|
@@ -623,7 +626,7 @@ interface VaultSwapRequest {
|
|
|
623
626
|
brokerAccount?: `cF${string}`;
|
|
624
627
|
brokerCommissionBps?: number;
|
|
625
628
|
extraParams?: {
|
|
626
|
-
|
|
629
|
+
seed?: `0x${string}`;
|
|
627
630
|
};
|
|
628
631
|
}
|
|
629
632
|
type VaultSwapResponse = {
|
|
@@ -676,6 +679,8 @@ declare class SwapSDK {
|
|
|
676
679
|
private getSupportedAssets;
|
|
677
680
|
private getBoostPoolsDepth;
|
|
678
681
|
getAssets(chain?: ChainflipChain, type?: keyof NetworkStatus['assets']): Promise<AssetData[]>;
|
|
682
|
+
protected shouldTakeCommission(): boolean;
|
|
683
|
+
private getCommissionBps;
|
|
679
684
|
getQuoteV2(quoteRequest: QuoteRequest, options?: RequestOptions): Promise<QuoteResponseV2>;
|
|
680
685
|
getStatusV2(swapStatusRequest: SwapStatusRequest, options?: RequestOptions): Promise<SwapStatusResponseV2>;
|
|
681
686
|
private validateSwapAmount;
|
|
@@ -690,8 +695,8 @@ declare class SwapSDK {
|
|
|
690
695
|
} | ({
|
|
691
696
|
feeTierBps?: number;
|
|
692
697
|
} & UncheckedAssetAndChain)): Promise<BoostPoolDepth[]>;
|
|
693
|
-
requestDepositAddressV2({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerCommissionBps, }: DepositAddressRequestV2): Promise<DepositAddressResponseV2>;
|
|
694
|
-
encodeVaultSwapData({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerAccount, brokerCommissionBps, extraParams, }: VaultSwapRequest): Promise<VaultSwapResponse>;
|
|
698
|
+
requestDepositAddressV2({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerCommissionBps: brokerCommissionBpsParam, }: DepositAddressRequestV2): Promise<DepositAddressResponseV2>;
|
|
699
|
+
encodeVaultSwapData({ quote, srcAddress, destAddress, fillOrKillParams: inputFoKParams, affiliateBrokers: affiliates, ccmParams, brokerAccount, brokerCommissionBps: brokerCommissionBpsParam, extraParams, }: VaultSwapRequest): Promise<VaultSwapResponse>;
|
|
695
700
|
checkBoostEnabled(): Promise<boolean>;
|
|
696
701
|
}
|
|
697
702
|
|
package/dist/swap.js
CHANGED
|
@@ -4,6 +4,8 @@ export { getInternalAsset as getChainflipId } from '@chainflip/utils/chainflip';
|
|
|
4
4
|
import { createTRPCProxyClient, httpLink } from '@trpc/client';
|
|
5
5
|
import superjson from 'superjson';
|
|
6
6
|
import { HttpClient } from '@chainflip/rpc';
|
|
7
|
+
import { unreachable } from '@chainflip/utils/assertion';
|
|
8
|
+
import { bytesToHex } from '@chainflip/utils/bytes';
|
|
7
9
|
import * as ss58 from '@chainflip/utils/ss58';
|
|
8
10
|
import { isHex, toUpperCase } from '@chainflip/utils/string';
|
|
9
11
|
import { priceX128ToPrice } from '@chainflip/utils/tickMath';
|
|
@@ -45,7 +47,7 @@ var chainflipAddress = z.string().refine(
|
|
|
45
47
|
(address) => address.startsWith("cF") && ss58.decode(address),
|
|
46
48
|
(address) => ({ message: `${address} is not a valid Chainflip address` })
|
|
47
49
|
);
|
|
48
|
-
|
|
50
|
+
z.string().refine(isValidSolanaAddress, (address) => ({
|
|
49
51
|
message: `${address} is not a valid Solana address`
|
|
50
52
|
}));
|
|
51
53
|
var u128 = z.union([z.number(), numericString, hexString]).transform((arg) => BigInt(arg));
|
|
@@ -265,7 +267,7 @@ var getParameterEncodingRequestSchema = (network) => z.object({
|
|
|
265
267
|
affiliates: z.array(affiliateBroker).optional(),
|
|
266
268
|
fillOrKillParams: transformedFokSchema,
|
|
267
269
|
dcaParams: transformedDcaParamsSchema.optional(),
|
|
268
|
-
extraParams: z.object({
|
|
270
|
+
extraParams: z.object({ seed: hexString.optional() }).optional()
|
|
269
271
|
}).superRefine((val, ctx) => {
|
|
270
272
|
if (val.srcAddress && !validateAddress(val.srcAsset.chain, val.srcAddress, network)) {
|
|
271
273
|
ctx.addIssue({
|
|
@@ -273,20 +275,21 @@ var getParameterEncodingRequestSchema = (network) => z.object({
|
|
|
273
275
|
code: z.ZodIssueCode.custom
|
|
274
276
|
});
|
|
275
277
|
}
|
|
276
|
-
}).superRefine((val, ctx) => {
|
|
277
278
|
if (!validateAddress(val.destAsset.chain, val.destAddress, network)) {
|
|
278
279
|
ctx.addIssue({
|
|
279
280
|
message: `Address "${val.destAddress}" is not a valid "${val.destAsset.chain}" address for "${network}"`,
|
|
280
281
|
code: z.ZodIssueCode.custom
|
|
281
282
|
});
|
|
282
283
|
}
|
|
283
|
-
}).superRefine((val, ctx) => {
|
|
284
284
|
if (!validateAddress(val.srcAsset.chain, val.fillOrKillParams.refund_address, network)) {
|
|
285
285
|
ctx.addIssue({
|
|
286
286
|
message: `Address "${val.fillOrKillParams.refund_address}" is not a valid "${val.srcAsset.chain}" address for "${network}"`,
|
|
287
287
|
code: z.ZodIssueCode.custom
|
|
288
288
|
});
|
|
289
289
|
}
|
|
290
|
+
if (val.extraParams?.seed && val.extraParams.seed.length !== 66) {
|
|
291
|
+
ctx.addIssue({ message: "Seed must be 32 bytes", code: z.ZodIssueCode.custom });
|
|
292
|
+
}
|
|
290
293
|
}).transform((data) => {
|
|
291
294
|
let extraParams;
|
|
292
295
|
if (data.srcAsset.chain === "Bitcoin") {
|
|
@@ -299,17 +302,16 @@ var getParameterEncodingRequestSchema = (network) => z.object({
|
|
|
299
302
|
} else if (data.srcAsset.chain === "Ethereum" || data.srcAsset.chain === "Arbitrum") {
|
|
300
303
|
extraParams = {
|
|
301
304
|
chain: data.srcAsset.chain,
|
|
302
|
-
input_amount: `0x${
|
|
305
|
+
input_amount: `0x${data.amount.toString(16)}`,
|
|
303
306
|
refund_parameters: data.fillOrKillParams
|
|
304
307
|
};
|
|
305
308
|
} else if (data.srcAsset.chain === "Solana") {
|
|
306
309
|
assert(data.srcAddress, "srcAddress is required for Solana");
|
|
307
|
-
assert(data.extraParams?.solanaDataAccount, "solanaDataAccount is required for Solana");
|
|
308
310
|
extraParams = {
|
|
309
311
|
chain: "Solana",
|
|
310
312
|
from: data.srcAddress,
|
|
311
|
-
|
|
312
|
-
input_amount: `0x${
|
|
313
|
+
seed: data.extraParams?.seed ?? bytesToHex(crypto.getRandomValues(new Uint8Array(32))),
|
|
314
|
+
input_amount: `0x${data.amount.toString(16)}`,
|
|
313
315
|
refund_parameters: data.fillOrKillParams
|
|
314
316
|
};
|
|
315
317
|
} else {
|
|
@@ -338,8 +340,20 @@ async function requestSwapDepositAddress(request, opts, chainflipNetwork) {
|
|
|
338
340
|
params.fillOrKillParams,
|
|
339
341
|
params.dcaParams
|
|
340
342
|
);
|
|
341
|
-
|
|
342
|
-
|
|
343
|
+
switch (params.srcAsset.chain) {
|
|
344
|
+
case "Polkadot":
|
|
345
|
+
case "Assethub":
|
|
346
|
+
if (isHex(response.address)) {
|
|
347
|
+
response.address = ss58.encode({ data: response.address, ss58Format: DOT_PREFIX });
|
|
348
|
+
}
|
|
349
|
+
break;
|
|
350
|
+
case "Ethereum":
|
|
351
|
+
case "Arbitrum":
|
|
352
|
+
case "Bitcoin":
|
|
353
|
+
case "Solana":
|
|
354
|
+
break;
|
|
355
|
+
default:
|
|
356
|
+
return unreachable(params.srcAsset, "unexpected chain");
|
|
343
357
|
}
|
|
344
358
|
return transformKeysToCamelCase(response);
|
|
345
359
|
}
|
|
@@ -362,7 +376,7 @@ async function requestSwapParameterEncoding(request, opts, chainflipNetwork) {
|
|
|
362
376
|
}
|
|
363
377
|
|
|
364
378
|
// ../shared/src/dataStructures.ts
|
|
365
|
-
var
|
|
379
|
+
var MultiCache = class {
|
|
366
380
|
constructor(fetchers) {
|
|
367
381
|
this.fetchers = fetchers;
|
|
368
382
|
}
|
|
@@ -492,7 +506,7 @@ var getChainData = (chain2, network, env) => ({
|
|
|
492
506
|
|
|
493
507
|
// package.json
|
|
494
508
|
var package_default = {
|
|
495
|
-
version: "1.9.0
|
|
509
|
+
version: "1.9.0"};
|
|
496
510
|
|
|
497
511
|
// src/swap/consts.ts
|
|
498
512
|
var CF_SDK_VERSION_HEADERS = { "X-Chainflip-Sdk-Version": package_default.version };
|
|
@@ -574,7 +588,7 @@ var SwapSDK = class {
|
|
|
574
588
|
]
|
|
575
589
|
});
|
|
576
590
|
this.dcaEnabled = options.enabledFeatures?.dca ?? false;
|
|
577
|
-
this.cache = new
|
|
591
|
+
this.cache = new MultiCache({
|
|
578
592
|
environment: {
|
|
579
593
|
fetch: () => getEnvironment(this.rpcConfig),
|
|
580
594
|
ttl: 6e4
|
|
@@ -615,13 +629,20 @@ var SwapSDK = class {
|
|
|
615
629
|
]);
|
|
616
630
|
return supportedAssets.map((asset2) => getAssetData(asset2, this.options.network, env)).filter((asset2) => !chain2 || asset2.chain === chain2);
|
|
617
631
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
632
|
+
// eslint-disable-next-line class-methods-use-this
|
|
633
|
+
shouldTakeCommission() {
|
|
634
|
+
return false;
|
|
635
|
+
}
|
|
636
|
+
async getCommissionBps(brokerCommissionBps) {
|
|
637
|
+
if (this.shouldTakeCommission()) {
|
|
638
|
+
return (await this.cache.read("networkStatus")).cfBrokerCommissionBps;
|
|
624
639
|
}
|
|
640
|
+
return brokerCommissionBps ?? this.options.broker?.commissionBps ?? 0;
|
|
641
|
+
}
|
|
642
|
+
async getQuoteV2(quoteRequest, options = {}) {
|
|
643
|
+
const submitterBrokerCommissionBps = await this.getCommissionBps(
|
|
644
|
+
quoteRequest.brokerCommissionBps
|
|
645
|
+
);
|
|
625
646
|
return getQuoteV2(
|
|
626
647
|
this.options.backendUrl,
|
|
627
648
|
{
|
|
@@ -689,7 +710,7 @@ var SwapSDK = class {
|
|
|
689
710
|
fillOrKillParams: inputFoKParams,
|
|
690
711
|
affiliateBrokers: affiliates,
|
|
691
712
|
ccmParams,
|
|
692
|
-
brokerCommissionBps
|
|
713
|
+
brokerCommissionBps: brokerCommissionBpsParam
|
|
693
714
|
}) {
|
|
694
715
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
695
716
|
assertQuoteValid(quote);
|
|
@@ -699,6 +720,7 @@ var SwapSDK = class {
|
|
|
699
720
|
} else {
|
|
700
721
|
assert(!quote.ccmParams, "Cannot open regular channel for quote with CCM params");
|
|
701
722
|
}
|
|
723
|
+
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
702
724
|
const depositAddressRequest = {
|
|
703
725
|
srcAsset: quote.srcAsset.asset,
|
|
704
726
|
srcChain: quote.srcAsset.chain,
|
|
@@ -717,7 +739,7 @@ var SwapSDK = class {
|
|
|
717
739
|
const result = await requestSwapDepositAddress(
|
|
718
740
|
{
|
|
719
741
|
...depositAddressRequest,
|
|
720
|
-
commissionBps: brokerCommissionBps
|
|
742
|
+
commissionBps: brokerCommissionBps,
|
|
721
743
|
affiliates
|
|
722
744
|
},
|
|
723
745
|
{ url: this.options.broker.url },
|
|
@@ -726,14 +748,14 @@ var SwapSDK = class {
|
|
|
726
748
|
response = {
|
|
727
749
|
id: `${result.issuedBlock}-${quote.srcAsset.chain}-${result.channelId}`,
|
|
728
750
|
depositAddress: result.address,
|
|
729
|
-
brokerCommissionBps
|
|
751
|
+
brokerCommissionBps,
|
|
730
752
|
srcChainExpiryBlock: result.sourceChainExpiryBlock,
|
|
731
753
|
maxBoostFeeBps: depositAddressRequest.maxBoostFeeBps,
|
|
732
754
|
channelOpeningFee: result.channelOpeningFee
|
|
733
755
|
};
|
|
734
756
|
} else {
|
|
735
757
|
assert(
|
|
736
|
-
!brokerCommissionBps,
|
|
758
|
+
!brokerCommissionBps || this.shouldTakeCommission(),
|
|
737
759
|
"Broker commission is supported only when initializing the SDK with a brokerUrl"
|
|
738
760
|
);
|
|
739
761
|
assert(
|
|
@@ -742,7 +764,8 @@ var SwapSDK = class {
|
|
|
742
764
|
);
|
|
743
765
|
response = await this.trpc.openSwapDepositChannel.mutate({
|
|
744
766
|
...depositAddressRequest,
|
|
745
|
-
quote
|
|
767
|
+
quote,
|
|
768
|
+
takeCommission: this.shouldTakeCommission()
|
|
746
769
|
});
|
|
747
770
|
}
|
|
748
771
|
return {
|
|
@@ -766,7 +789,7 @@ var SwapSDK = class {
|
|
|
766
789
|
affiliateBrokers: affiliates,
|
|
767
790
|
ccmParams,
|
|
768
791
|
brokerAccount,
|
|
769
|
-
brokerCommissionBps,
|
|
792
|
+
brokerCommissionBps: brokerCommissionBpsParam,
|
|
770
793
|
extraParams
|
|
771
794
|
}) {
|
|
772
795
|
await this.validateSwapAmount(quote.srcAsset, BigInt(quote.depositAmount));
|
|
@@ -777,6 +800,7 @@ var SwapSDK = class {
|
|
|
777
800
|
} else {
|
|
778
801
|
assert(!quote.ccmParams, "Cannot encode regular swap for quote with CCM params");
|
|
779
802
|
}
|
|
803
|
+
const brokerCommissionBps = await this.getCommissionBps(brokerCommissionBpsParam);
|
|
780
804
|
const vaultSwapRequest = {
|
|
781
805
|
srcAsset: quote.srcAsset,
|
|
782
806
|
destAsset: quote.destAsset,
|
|
@@ -798,16 +822,13 @@ var SwapSDK = class {
|
|
|
798
822
|
"Cannot overwrite broker account when initializing the SDK with a brokerUrl"
|
|
799
823
|
);
|
|
800
824
|
return requestSwapParameterEncoding(
|
|
801
|
-
|
|
802
|
-
...vaultSwapRequest,
|
|
803
|
-
commissionBps: vaultSwapRequest.commissionBps ?? this.options.broker.commissionBps
|
|
804
|
-
},
|
|
825
|
+
vaultSwapRequest,
|
|
805
826
|
{ url: this.options.broker.url },
|
|
806
827
|
this.options.network
|
|
807
828
|
);
|
|
808
829
|
}
|
|
809
830
|
assert(
|
|
810
|
-
!vaultSwapRequest.commissionBps || vaultSwapRequest.brokerAccount,
|
|
831
|
+
!vaultSwapRequest.commissionBps || vaultSwapRequest.brokerAccount || this.shouldTakeCommission(),
|
|
811
832
|
"Broker commission is supported only when setting a broker account"
|
|
812
833
|
);
|
|
813
834
|
assert(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainflip/sdk",
|
|
3
|
-
"version": "1.9.0
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./swap": {
|
|
@@ -43,11 +43,11 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@chainflip/bitcoin": "^1.2.5",
|
|
46
|
-
"@chainflip/rpc": "~1.9.
|
|
46
|
+
"@chainflip/rpc": "~1.9.7",
|
|
47
47
|
"@chainflip/solana": "^2.1.7",
|
|
48
48
|
"@chainflip/utils": "^0.8.6",
|
|
49
49
|
"@trpc/client": "^10.45.2",
|
|
50
|
-
"axios": "^1.
|
|
50
|
+
"axios": "^1.8.4",
|
|
51
51
|
"bignumber.js": "^9.3.0",
|
|
52
52
|
"ethers": "^6.13.3",
|
|
53
53
|
"superjson": "^1.13.3",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@trpc/server": "^10.45.2",
|
|
58
|
-
"tsup": "^8.
|
|
58
|
+
"tsup": "^8.4.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"test": "vitest",
|