From 2a0f58a7b6ecbc588871545ecfb6c6fb5b8703f9 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Fri, 6 Dec 2024 16:39:10 +0100 Subject: [PATCH 1/6] add enableSwaps --- packages/sdk/src/internal/validators.ts | 12 +++++---- packages/sdk/src/sprinter.ts | 36 ++++++++++++++----------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/sdk/src/internal/validators.ts b/packages/sdk/src/internal/validators.ts index 24de300..9e34d01 100644 --- a/packages/sdk/src/internal/validators.ts +++ b/packages/sdk/src/internal/validators.ts @@ -2,6 +2,7 @@ import { array, assign, bigint, + boolean, define, number, object, @@ -25,7 +26,7 @@ const numberLike = refine( (value) => { if (typeof value === "string") return !isNaN(Number(value)); return true; // If it's a number or bigint, it's already valid - }, + } ); const BridgeCoreSchema = object({ @@ -35,13 +36,14 @@ const BridgeCoreSchema = object({ amount: numberLike, threshold: optional(number()), sourceChains: optional(array(number())), + enableSwaps: optional(boolean()), }); const BridgeCoreWithRecipientSchema = assign( BridgeCoreSchema, object({ recipient: optional(hexString()), - }), + }) ); const ContractCallCoreSchema = object({ @@ -57,7 +59,7 @@ const TokenContractCallSchema = assign( object({ outputTokenAddress: optional(hexString()), approvalAddress: optional(hexString()), - }), + }) ); const ContractCallSchema = object({ @@ -70,10 +72,10 @@ export const MultiHopSchema = BridgeCoreSchema; export const SingleHopWithContractSchema = assign( BridgeCoreWithRecipientSchema, - ContractCallSchema, + ContractCallSchema ); export const MultiHopWithContractSchema = assign( BridgeCoreSchema, - ContractCallSchema, + ContractCallSchema ); diff --git a/packages/sdk/src/sprinter.ts b/packages/sdk/src/sprinter.ts index 5223f38..d61bd71 100644 --- a/packages/sdk/src/sprinter.ts +++ b/packages/sdk/src/sprinter.ts @@ -57,11 +57,11 @@ export class Sprinter { * ``` */ public async getAvailableTokens( - options: FetchOptions = {}, + options: FetchOptions = {} ): Promise { if (!this.#tokens) this.#tokens = await this.deferredRequest("tokens", () => - getFungibleTokens(this.makeFetchOptions(options)), + getFungibleTokens(this.makeFetchOptions(options)) ); return this.#tokens; } @@ -85,11 +85,11 @@ export class Sprinter { * ``` */ public async getAvailableChains( - options: FetchOptions = {}, + options: FetchOptions = {} ): Promise { if (!this.#chains) this.#chains = await this.deferredRequest("chains", () => - getSupportedChains(this.makeFetchOptions(options)), + getSupportedChains(this.makeFetchOptions(options)) ); return this.#chains; } @@ -151,7 +151,7 @@ export class Sprinter { public async getUserBalances( account: Address, tokens?: FungibleToken[], - options: FetchOptions = {}, + options: FetchOptions = {} ): Promise { const tokenList = tokens || (await this.getAvailableTokens(options)); @@ -161,8 +161,8 @@ export class Sprinter { getUserBalances( account, tokenList, - this.makeFetchOptions(options || {}), - ), + this.makeFetchOptions(options || {}) + ) ); return formatBalances([balances, nativeTokens]); } @@ -253,7 +253,7 @@ export class Sprinter { */ public async poolAssetOnDestination( settings: Infer, - options?: FetchOptions, + options?: FetchOptions ): Promise { assert(settings, MultiHopSchema); @@ -264,7 +264,7 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, - this.makeFetchOptions(options), + this.makeFetchOptions(options) ); } @@ -327,7 +327,7 @@ export class Sprinter { */ public async poolAssetOnDestinationWithHook( settings: Infer, - options?: FetchOptions, + options?: FetchOptions ): Promise { assert(settings, MultiHopWithContractSchema); @@ -338,7 +338,7 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as ContractSolutionOptions, - this.makeFetchOptions(options), + this.makeFetchOptions(options) ); } @@ -355,6 +355,7 @@ export class Sprinter { * - `recipient` (optional): The address of the recipient of any leftover tokens. * - `threshold` (optional): The minimum amount threshold required for the transfer. * - `sourceChains` (optional): An array of whitelisted source chain IDs for the transfer. + * - `enableSwaps` {boolean} (optional): Whether to enable token swaps on the source chain. * * @param {FetchOptions} [options] - Optional configuration for the fetch request, such as custom headers or query parameters. * @@ -372,6 +373,7 @@ export class Sprinter { * token: "USDC", * amount: "100000000", * recipient: "0xRecipientAddress", // Optional recipient of leftover tokens + * enableSwaps: true, // Enabling swaps on the source chain * }; * * sprinter.transfer(settings).then(solution => { @@ -383,7 +385,7 @@ export class Sprinter { */ public async transfer( settings: Infer, - options?: FetchOptions, + options?: FetchOptions ): Promise { assert(settings, SingleHopSchema); @@ -394,7 +396,7 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, - this.makeFetchOptions(options), + this.makeFetchOptions(options) ); } @@ -422,6 +424,7 @@ export class Sprinter { * - `recipient` {string} (optional): The address of the recipient of any leftover tokens. * - `sourceChains` {Array} (optional): An array of source chain IDs to be considered for the transfer. * - `threshold` {number} (optional): The minimum amount threshold required for the transfer. + * - `enableSwaps` {boolean} (optional): Whether to enable token swaps on the source chain. * * @param {FetchOptions} [options] - Optional configuration for the fetch request, such as custom headers or query parameters. * @@ -444,6 +447,7 @@ export class Sprinter { * gasLimit: 21000, * }, * recipient: "0xRecipientAddress", // for sending leftover tokens + * enableSwaps: true, // Enabling swaps on the source chain * }; * * sprinter.transferWithHook(settings).then(solution => { @@ -455,7 +459,7 @@ export class Sprinter { */ public async transferWithHook( settings: Infer, - options?: FetchOptions, + options?: FetchOptions ): Promise { assert(settings, SingleHopWithContractSchema); @@ -466,13 +470,13 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, - this.makeFetchOptions(options), + this.makeFetchOptions(options) ); } private deferredRequest( name: string, - request: () => Promise, + request: () => Promise ): Promise { if (!(name in this.#requests)) { this.#requests[name] = request(); From 8a33bdbdf5b9ead0c114b1889f2ee56114cd19c3 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Fri, 6 Dec 2024 17:00:23 +0100 Subject: [PATCH 2/6] add prettierrc --- packages/react/.prettierrc | 6 +++++ packages/sdk/.prettierrc | 6 +++++ packages/sdk/src/internal/validators.ts | 10 ++++---- packages/sdk/src/sprinter.ts | 32 ++++++++++++------------- 4 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 packages/react/.prettierrc create mode 100644 packages/sdk/.prettierrc diff --git a/packages/react/.prettierrc b/packages/react/.prettierrc new file mode 100644 index 0000000..60998ab --- /dev/null +++ b/packages/react/.prettierrc @@ -0,0 +1,6 @@ +{ + "useTabs": false, + "singleQuote": false, + "trailingComma": "all", + "printWidth": 80 +} diff --git a/packages/sdk/.prettierrc b/packages/sdk/.prettierrc new file mode 100644 index 0000000..99c88b4 --- /dev/null +++ b/packages/sdk/.prettierrc @@ -0,0 +1,6 @@ +{ + "useTabs": false, + "singleQuote": false, + "trailingComma": "all", + "printWidth": 80 +} diff --git a/packages/sdk/src/internal/validators.ts b/packages/sdk/src/internal/validators.ts index 9e34d01..14fec4e 100644 --- a/packages/sdk/src/internal/validators.ts +++ b/packages/sdk/src/internal/validators.ts @@ -26,7 +26,7 @@ const numberLike = refine( (value) => { if (typeof value === "string") return !isNaN(Number(value)); return true; // If it's a number or bigint, it's already valid - } + }, ); const BridgeCoreSchema = object({ @@ -43,7 +43,7 @@ const BridgeCoreWithRecipientSchema = assign( BridgeCoreSchema, object({ recipient: optional(hexString()), - }) + }), ); const ContractCallCoreSchema = object({ @@ -59,7 +59,7 @@ const TokenContractCallSchema = assign( object({ outputTokenAddress: optional(hexString()), approvalAddress: optional(hexString()), - }) + }), ); const ContractCallSchema = object({ @@ -72,10 +72,10 @@ export const MultiHopSchema = BridgeCoreSchema; export const SingleHopWithContractSchema = assign( BridgeCoreWithRecipientSchema, - ContractCallSchema + ContractCallSchema, ); export const MultiHopWithContractSchema = assign( BridgeCoreSchema, - ContractCallSchema + ContractCallSchema, ); diff --git a/packages/sdk/src/sprinter.ts b/packages/sdk/src/sprinter.ts index d61bd71..b1dc04e 100644 --- a/packages/sdk/src/sprinter.ts +++ b/packages/sdk/src/sprinter.ts @@ -57,11 +57,11 @@ export class Sprinter { * ``` */ public async getAvailableTokens( - options: FetchOptions = {} + options: FetchOptions = {}, ): Promise { if (!this.#tokens) this.#tokens = await this.deferredRequest("tokens", () => - getFungibleTokens(this.makeFetchOptions(options)) + getFungibleTokens(this.makeFetchOptions(options)), ); return this.#tokens; } @@ -85,11 +85,11 @@ export class Sprinter { * ``` */ public async getAvailableChains( - options: FetchOptions = {} + options: FetchOptions = {}, ): Promise { if (!this.#chains) this.#chains = await this.deferredRequest("chains", () => - getSupportedChains(this.makeFetchOptions(options)) + getSupportedChains(this.makeFetchOptions(options)), ); return this.#chains; } @@ -151,7 +151,7 @@ export class Sprinter { public async getUserBalances( account: Address, tokens?: FungibleToken[], - options: FetchOptions = {} + options: FetchOptions = {}, ): Promise { const tokenList = tokens || (await this.getAvailableTokens(options)); @@ -161,8 +161,8 @@ export class Sprinter { getUserBalances( account, tokenList, - this.makeFetchOptions(options || {}) - ) + this.makeFetchOptions(options || {}), + ), ); return formatBalances([balances, nativeTokens]); } @@ -253,7 +253,7 @@ export class Sprinter { */ public async poolAssetOnDestination( settings: Infer, - options?: FetchOptions + options?: FetchOptions, ): Promise { assert(settings, MultiHopSchema); @@ -264,7 +264,7 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, - this.makeFetchOptions(options) + this.makeFetchOptions(options), ); } @@ -327,7 +327,7 @@ export class Sprinter { */ public async poolAssetOnDestinationWithHook( settings: Infer, - options?: FetchOptions + options?: FetchOptions, ): Promise { assert(settings, MultiHopWithContractSchema); @@ -338,7 +338,7 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as ContractSolutionOptions, - this.makeFetchOptions(options) + this.makeFetchOptions(options), ); } @@ -385,7 +385,7 @@ export class Sprinter { */ public async transfer( settings: Infer, - options?: FetchOptions + options?: FetchOptions, ): Promise { assert(settings, SingleHopSchema); @@ -396,7 +396,7 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, - this.makeFetchOptions(options) + this.makeFetchOptions(options), ); } @@ -459,7 +459,7 @@ export class Sprinter { */ public async transferWithHook( settings: Infer, - options?: FetchOptions + options?: FetchOptions, ): Promise { assert(settings, SingleHopWithContractSchema); @@ -470,13 +470,13 @@ export class Sprinter { amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, - this.makeFetchOptions(options) + this.makeFetchOptions(options), ); } private deferredRequest( name: string, - request: () => Promise + request: () => Promise, ): Promise { if (!(name in this.#requests)) { this.#requests[name] = request(); From 035ed1d871cf2cee064bde37bebf8487a3619197 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Mon, 9 Dec 2024 15:24:55 +0100 Subject: [PATCH 3/6] default to false and add docs --- .../04-methods-reference/transfer/transferWithHook.md | 2 ++ .../04-methods-reference/transfer/transfer_method.md | 1 + packages/sdk/src/api.ts | 4 ++++ packages/sdk/src/sprinter.ts | 10 ++++++---- packages/sdk/src/types.ts | 1 + 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/docs/03-sdk/04-methods-reference/transfer/transferWithHook.md b/docs/docs/03-sdk/04-methods-reference/transfer/transferWithHook.md index 24e0e02..e4321ea 100644 --- a/docs/docs/03-sdk/04-methods-reference/transfer/transferWithHook.md +++ b/docs/docs/03-sdk/04-methods-reference/transfer/transferWithHook.md @@ -109,6 +109,8 @@ sprinter.transferWithHook(settings, { baseUrl: 'https://custom.api.url' }).then( - `recipient?`: *(Optional)* The address of the recipient of any leftover tokens. - `sourceChains?`: *(Optional)* An array of source chain IDs to be considered for the transfer. If omitted, Sprinter will use all available chains for the solution. - `threshold?`: *(Optional)* The minimum amount of tokens required to trigger the transfer solution. If not met, the transfer solution will not proceed. + - `enableSwaps`: *(Optional)* Defaults to `false`. Whether to enable token swaps on the source chain. + - `fetchOptions?`: *(Optional)* An object containing `baseUrl` to override the default API endpoint for this request. import HowToCallData from "../_how-to-calldata.md" diff --git a/docs/docs/03-sdk/04-methods-reference/transfer/transfer_method.md b/docs/docs/03-sdk/04-methods-reference/transfer/transfer_method.md index 3bc1224..4d84fee 100644 --- a/docs/docs/03-sdk/04-methods-reference/transfer/transfer_method.md +++ b/docs/docs/03-sdk/04-methods-reference/transfer/transfer_method.md @@ -37,6 +37,7 @@ sprinter.transfer(settings).then(solution => { - `recipient?`: *(Optional)* The address of the recipient of any leftover tokens. - `sourceChains?`: *(Optional)* An array of source chain IDs to be considered for the transfer. If omitted, Sprinter will use all available chains for the solution. To limit the solution to a specific chain, provide an array containing only that chain's ID. - `threshold?`: *(Optional)* The minimum amount of tokens required to trigger the transfer solution. If not met, the transfer solution will not proceed. + - `enableSwaps`: *(Optional)* Defaults to `false`. Whether to enable token swaps on the source chain. - `fetchOptions?`: *(Optional)* An object containing `baseUrl` to override the default API endpoint for this request. diff --git a/packages/sdk/src/api.ts b/packages/sdk/src/api.ts index fb0b94a..4731e1c 100644 --- a/packages/sdk/src/api.ts +++ b/packages/sdk/src/api.ts @@ -148,6 +148,7 @@ export async function getContractSolution( contractCall, threshold, whitelistedSourceChains, + enableSwaps, }: ContractSolutionOptions, { baseUrl, signal }: FetchOptions = {}, ): Promise { @@ -165,6 +166,7 @@ export async function getContractSolution( type: "fungible", threshold, whitelistedSourceChains, + enableSwaps }), }).then( (response) => @@ -185,6 +187,7 @@ export async function getContractCallSolution( recipient, threshold, whitelistedSourceChains, + enableSwaps, }: SingleHopContractSolutionOptions, { baseUrl, signal }: FetchOptions = {}, ): Promise { @@ -203,6 +206,7 @@ export async function getContractCallSolution( recipient, threshold, whitelistedSourceChains, + enableSwaps, }), }).then( (response) => diff --git a/packages/sdk/src/sprinter.ts b/packages/sdk/src/sprinter.ts index b1dc04e..fedb7e4 100644 --- a/packages/sdk/src/sprinter.ts +++ b/packages/sdk/src/sprinter.ts @@ -355,7 +355,7 @@ export class Sprinter { * - `recipient` (optional): The address of the recipient of any leftover tokens. * - `threshold` (optional): The minimum amount threshold required for the transfer. * - `sourceChains` (optional): An array of whitelisted source chain IDs for the transfer. - * - `enableSwaps` {boolean} (optional): Whether to enable token swaps on the source chain. + * - `enableSwaps` {boolean} (optional): Defaults to `false`. Whether to enable token swaps on the source chain. * * @param {FetchOptions} [options] - Optional configuration for the fetch request, such as custom headers or query parameters. * @@ -389,10 +389,11 @@ export class Sprinter { ): Promise { assert(settings, SingleHopSchema); - const { sourceChains, amount, ...data } = settings; + const { sourceChains, amount, enableSwaps = false, ...data } = settings; return await getContractCallSolution( { ...data, + enableSwaps, amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, @@ -424,7 +425,7 @@ export class Sprinter { * - `recipient` {string} (optional): The address of the recipient of any leftover tokens. * - `sourceChains` {Array} (optional): An array of source chain IDs to be considered for the transfer. * - `threshold` {number} (optional): The minimum amount threshold required for the transfer. - * - `enableSwaps` {boolean} (optional): Whether to enable token swaps on the source chain. + * - `enableSwaps` {boolean} (optional): Defaults to `false`. Whether to enable token swaps on the source chain. * * @param {FetchOptions} [options] - Optional configuration for the fetch request, such as custom headers or query parameters. * @@ -463,10 +464,11 @@ export class Sprinter { ): Promise { assert(settings, SingleHopWithContractSchema); - const { sourceChains, amount, ...data } = settings; + const { sourceChains, amount, enableSwaps = false, ...data } = settings; return await getContractCallSolution( { ...data, + enableSwaps, amount: BigInt(amount), whitelistedSourceChains: sourceChains, } as SolutionOptions, diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index f408d17..50e68e6 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -45,6 +45,7 @@ export interface SolutionOptions { amount: NumberLike; threshold?: number; whitelistedSourceChains?: ChainID[]; + enableSwaps?: boolean; } export interface ContractCallSolutionOptions { From b57e2c350a6e8d7b5e4d22fab4f8ab9c95e93104 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Mon, 9 Dec 2024 15:55:23 +0100 Subject: [PATCH 4/6] fix lint --- packages/sdk/src/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/api.ts b/packages/sdk/src/api.ts index 4731e1c..517d8f7 100644 --- a/packages/sdk/src/api.ts +++ b/packages/sdk/src/api.ts @@ -166,7 +166,7 @@ export async function getContractSolution( type: "fungible", threshold, whitelistedSourceChains, - enableSwaps + enableSwaps, }), }).then( (response) => From ac1e80e7199329890d2ca0d57e3a7fca6731f727 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Tue, 10 Dec 2024 13:46:41 +0100 Subject: [PATCH 5/6] only for /solution/call --- packages/sdk/src/api.ts | 2 -- packages/sdk/src/types.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/sdk/src/api.ts b/packages/sdk/src/api.ts index 517d8f7..ad4ab41 100644 --- a/packages/sdk/src/api.ts +++ b/packages/sdk/src/api.ts @@ -148,7 +148,6 @@ export async function getContractSolution( contractCall, threshold, whitelistedSourceChains, - enableSwaps, }: ContractSolutionOptions, { baseUrl, signal }: FetchOptions = {}, ): Promise { @@ -166,7 +165,6 @@ export async function getContractSolution( type: "fungible", threshold, whitelistedSourceChains, - enableSwaps, }), }).then( (response) => diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 50e68e6..a44e149 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -45,7 +45,6 @@ export interface SolutionOptions { amount: NumberLike; threshold?: number; whitelistedSourceChains?: ChainID[]; - enableSwaps?: boolean; } export interface ContractCallSolutionOptions { @@ -60,6 +59,7 @@ export interface ContractCallSolutionOptions { export interface SingleHopContractSolutionOptions extends SolutionOptions { recipient?: Address; contractCall?: ContractCallSolutionOptions; + enableSwaps?: boolean; } export interface ContractSolutionOptions extends SolutionOptions { From 308afaba35b3e6fb73358a1a45b2b9d70ec545ff Mon Sep 17 00:00:00 2001 From: Thibaut Sardan Date: Tue, 10 Dec 2024 19:16:34 +0100 Subject: [PATCH 6/6] move to BridgeCoreWithRecipientSchema --- packages/sdk/src/internal/validators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/internal/validators.ts b/packages/sdk/src/internal/validators.ts index 14fec4e..cf11ec2 100644 --- a/packages/sdk/src/internal/validators.ts +++ b/packages/sdk/src/internal/validators.ts @@ -36,12 +36,12 @@ const BridgeCoreSchema = object({ amount: numberLike, threshold: optional(number()), sourceChains: optional(array(number())), - enableSwaps: optional(boolean()), }); const BridgeCoreWithRecipientSchema = assign( BridgeCoreSchema, object({ + enableSwaps: optional(boolean()), recipient: optional(hexString()), }), );