@sodax/sdk 0.0.1-rc.19 → 0.0.1-rc.20

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/README.md CHANGED
@@ -254,8 +254,8 @@ const bscSpokeProvider: EvmSpokeProvider = new EvmSpokeProvider(
254
254
  The `estimateGas` function allows you to estimate the gas cost for raw transactions before executing them. This is particularly useful for all Sodax operations (swaps, money market operations, approvals) to provide users with accurate gas estimates.
255
255
 
256
256
  The function is available on all service classes:
257
- - `SolverService.estimateGas()` - for solver/intent operations
258
- - `MoneyMarketService.estimateGas()` - for money market operations
257
+ - `SolverService.estimateGas()` - for solver/intent operations (reachable through `sodax.solver`)
258
+ - `MoneyMarketService.estimateGas()` - for money market operations (reachable through `sodax.moneyMarket`)
259
259
  - `SpokeService.estimateGas()` - for general spoke chain operations
260
260
 
261
261
  ```typescript
package/dist/index.cjs CHANGED
@@ -12452,7 +12452,7 @@ var SolverService = class {
12452
12452
  * const fee: bigint = await solverService.getFee(1000000000000000n);
12453
12453
  * console.log('Fee:', fee);
12454
12454
  */
12455
- async getFee(inputAmount) {
12455
+ getFee(inputAmount) {
12456
12456
  if (!this.config.partnerFee) {
12457
12457
  return 0n;
12458
12458
  }
@@ -12562,29 +12562,37 @@ var SolverService = class {
12562
12562
  }
12563
12563
  }
12564
12564
  /**
12565
- * Swap is a syntatic sugar for createAndSubmitIntent that creates an intent and submits it to the Solver API and Relayer API
12566
- * @param {CreateIntentParams} payload - The intent to create
12567
- * @param {ISpokeProvider} spokeProvider - The spoke provider
12568
- * @param {number} timeout - The timeout in milliseconds for the transaction. Default is 60 seconds.
12569
- * @returns {Promise<Result<[SolverExecutionResponse, Intent, PacketData], IntentError<IntentErrorCode>>>} The solver execution response, intent, and packet data
12565
+ * Syntactic sugar for createAndSubmitIntent: creates an intent and submits it to the Solver API and Relayer API.
12570
12566
  *
12571
- * @example
12572
- * const payload = {
12573
- * "inputToken": "0x2170Ed0880ac9A755fd29B2688956BD959F933F8", // BSC ETH token address
12574
- * "outputToken": "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f", // ARB WBTC token address
12575
- * "inputAmount": 1000000000000000n, // The amount of input tokens
12576
- * "minOutputAmount": 900000000000000n, // min amount you are expecting to receive
12577
- * "deadline": 0n, // Optional timestamp after which intent expires (0 = no deadline)
12578
- * "allowPartialFill": false, // Whether the intent can be partially filled
12579
- * "srcChain": "0x38.bsc", // Chain ID where input tokens originate
12580
- * "dstChain": "0xa4b1.arbitrum", // Chain ID where output tokens should be delivered
12581
- * "srcAddress": "0x..", // Source address (original address on spoke chain)
12582
- * "dstAddress": "0x...", // Destination address (original address on spoke chain)
12583
- * "solver": "0x..", // Optional specific solver address (address(0) = any solver)
12584
- * "data": "0x..", // Additional arbitrary data
12585
- * } satisfies CreateIntentParams;
12567
+ * @param {Prettify<SwapParams<S> & OptionalTimeout>} params - Object containing:
12568
+ * - intentParams: The parameters for creating the intent.
12569
+ * - spokeProvider: The spoke provider instance.
12570
+ * - fee: (Optional) Partner fee configuration.
12571
+ * - timeout: (Optional) Timeout in milliseconds for the transaction (default: 60 seconds).
12572
+ * @returns {Promise<Result<[SolverExecutionResponse, Intent, Hex], IntentError<IntentErrorCode>>>}
12573
+ * A promise resolving to a Result containing a tuple of SolverExecutionResponse, Intent, and packet data (Hex),
12574
+ * or an IntentError if the operation fails.
12586
12575
  *
12587
- * const swapResult = await solverService.swap(payload, spokeProvider);
12576
+ * @example
12577
+ * const swapResult = await solverService.swap({
12578
+ * intentParams: {
12579
+ * inputToken: "0x2170Ed0880ac9A755fd29B2688956BD959F933F8",
12580
+ * outputToken: "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
12581
+ * inputAmount: 1000000000000000n,
12582
+ * minOutputAmount: 900000000000000n,
12583
+ * deadline: 0n,
12584
+ * allowPartialFill: false,
12585
+ * srcChain: "0x38.bsc",
12586
+ * dstChain: "0xa4b1.arbitrum",
12587
+ * srcAddress: "0x..",
12588
+ * dstAddress: "0x...",
12589
+ * solver: "0x..",
12590
+ * data: "0x..",
12591
+ * },
12592
+ * spokeProvider,
12593
+ * fee, // optional
12594
+ * timeout, // optional
12595
+ * });
12588
12596
  *
12589
12597
  * if (swapResult.ok) {
12590
12598
  * const [solverExecutionResponse, intent, packetData] = swapResult.value;
@@ -12595,33 +12603,51 @@ var SolverService = class {
12595
12603
  * // handle error
12596
12604
  * }
12597
12605
  */
12598
- async swap(payload, spokeProvider, timeout = DEFAULT_RELAY_TX_TIMEOUT) {
12599
- return this.createAndSubmitIntent(payload, spokeProvider, this.config.partnerFee, timeout);
12606
+ async swap({
12607
+ intentParams: params,
12608
+ spokeProvider,
12609
+ fee = this.config.partnerFee,
12610
+ timeout = DEFAULT_RELAY_TX_TIMEOUT
12611
+ }) {
12612
+ return this.createAndSubmitIntent({
12613
+ intentParams: params,
12614
+ spokeProvider,
12615
+ fee,
12616
+ timeout
12617
+ });
12600
12618
  }
12601
12619
  /**
12602
12620
  * Creates an intent and submits it to the Solver API and Relayer API
12603
- * @param {CreateIntentParams} payload - The intent to create
12604
- * @param {ISpokeProvider} spokeProvider - The spoke provider
12605
- * @param {number} timeout - The timeout in milliseconds for the transaction. Default is 60 seconds.
12606
- * @returns {Promise<Result<[SolverExecutionResponse, Intent, Hex], IntentError<IntentErrorCode>>>} The solver execution response, intent, and packet data
12621
+ * @param {Prettify<SwapParams<S> & OptionalTimeout>} params - Object containing:
12622
+ * - intentParams: The parameters for creating the intent.
12623
+ * - spokeProvider: The spoke provider instance.
12624
+ * - fee: (Optional) Partner fee configuration.
12625
+ * - timeout: (Optional) Timeout in milliseconds for the transaction (default: 60 seconds).
12626
+ * @returns {Promise<Result<[SolverExecutionResponse, Intent, Hex], IntentError<IntentErrorCode>>>}
12627
+ * A promise resolving to a Result containing a tuple of SolverExecutionResponse, Intent, and packet data (Hex),
12628
+ * or an IntentError if the operation fails.
12607
12629
  *
12608
12630
  * @example
12609
- * const payload = {
12610
- * "inputToken": "0x2170Ed0880ac9A755fd29B2688956BD959F933F8", // BSC ETH token address
12611
- * "outputToken": "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f", // ARB WBTC token address
12612
- * "inputAmount": 1000000000000000n, // The amount of input tokens
12613
- * "minOutputAmount": 900000000000000n, // min amount you are expecting to receive
12614
- * "deadline": 0n, // Optional timestamp after which intent expires (0 = no deadline)
12615
- * "allowPartialFill": false, // Whether the intent can be partially filled
12616
- * "srcChain": "0x38.bsc", // Chain ID where input tokens originate
12617
- * "dstChain": "0xa4b1.arbitrum", // Chain ID where output tokens should be delivered
12618
- * "srcAddress": "0x..", // Source address (original address on spoke chain)
12619
- * "dstAddress": "0x...", // Destination address (original address on spoke chain)
12620
- * "solver": "0x..", // Optional specific solver address (address(0) = any solver)
12621
- * "data": "0x..", // Additional arbitrary data
12622
- * } satisfies CreateIntentParams;
12631
+ * const createAndSubmitIntentResult = await solverService.createAndSubmitIntent({
12632
+ * intentParams: {
12633
+ * inputToken: "0x2170Ed0880ac9A755fd29B2688956BD959F933F8",
12634
+ * outputToken: "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
12635
+ * inputAmount: 1000000000000000n,
12636
+ * minOutputAmount: 900000000000000n,
12637
+ * deadline: 0n,
12638
+ * allowPartialFill: false,
12639
+ * srcChain: "0x38.bsc",
12640
+ * dstChain: "0xa4b1.arbitrum",
12641
+ * srcAddress: "0x..",
12642
+ * dstAddress: "0x...",
12643
+ * solver: "0x..",
12644
+ * data: "0x..",
12645
+ * },
12646
+ * spokeProvider,
12647
+ * fee, // optional
12648
+ * timeout, // optional
12649
+ * });
12623
12650
  *
12624
- * const createAndSubmitIntentResult = await solverService.createAndSubmitIntent(payload, spokeProvider);
12625
12651
  *
12626
12652
  * if (createAndSubmitIntentResult.ok) {
12627
12653
  * const [solverExecutionResponse, intent, packetData] = createAndSubmitIntentResult.value;
@@ -12632,17 +12658,27 @@ var SolverService = class {
12632
12658
  * // handle error
12633
12659
  * }
12634
12660
  */
12635
- async createAndSubmitIntent(payload, spokeProvider, fee = this.config.partnerFee, timeout = DEFAULT_RELAY_TX_TIMEOUT) {
12661
+ async createAndSubmitIntent({
12662
+ intentParams: params,
12663
+ spokeProvider,
12664
+ fee = this.config.partnerFee,
12665
+ timeout = DEFAULT_RELAY_TX_TIMEOUT
12666
+ }) {
12636
12667
  try {
12637
- const createIntentResult = await this.createIntent(payload, spokeProvider, fee, false);
12668
+ const createIntentResult = await this.createIntent({
12669
+ intentParams: params,
12670
+ spokeProvider,
12671
+ fee,
12672
+ raw: false
12673
+ });
12638
12674
  if (!createIntentResult.ok) {
12639
12675
  return createIntentResult;
12640
12676
  }
12641
12677
  const [spokeTxHash, intent, data] = createIntentResult.value;
12642
12678
  let intentTxHash = null;
12643
12679
  if (spokeProvider.chainConfig.chain.id !== types.SONIC_MAINNET_CHAIN_ID) {
12644
- const intentRelayChainId = getIntentRelayChainId(payload.srcChain).toString();
12645
- const submitPayload = payload.srcChain === types.SOLANA_MAINNET_CHAIN_ID && data ? {
12680
+ const intentRelayChainId = getIntentRelayChainId(params.srcChain).toString();
12681
+ const submitPayload = params.srcChain === types.SOLANA_MAINNET_CHAIN_ID && data ? {
12646
12682
  action: "submit",
12647
12683
  params: {
12648
12684
  chain_id: intentRelayChainId,
@@ -12701,7 +12737,7 @@ var SolverService = class {
12701
12737
  error: {
12702
12738
  code: "UNKNOWN",
12703
12739
  data: {
12704
- payload,
12740
+ payload: params,
12705
12741
  error
12706
12742
  }
12707
12743
  }
@@ -12709,44 +12745,53 @@ var SolverService = class {
12709
12745
  }
12710
12746
  }
12711
12747
  /**
12712
- * Check whether assetManager contract is allowed to move the given payload amount
12713
- * @param {CreateIntentParams} params - The intent to create
12714
- * @param {SpokeProvider} spokeProvider - The spoke provider
12715
- * @return {Promise<Result<boolean>>} - valid = true, invalid = false
12748
+ * Check whether the Asset Manager contract is allowed to spend the specified amount of tokens
12749
+ * @param {Prettify<SwapParams<S>} params - Object containing:
12750
+ * - intentParams: The parameters for creating the intent.
12751
+ * - spokeProvider: The spoke provider instance.
12752
+ * - fee: (Optional) Partner fee configuration.
12753
+ * @returns {Promise<Result<boolean>>} - Returns true if allowance is sufficient, false if approval is needed
12716
12754
  *
12717
12755
  * @example
12718
- * const payload = {
12719
- * "inputToken": "0x2170Ed0880ac9A755fd29B2688956BD959F933F8", // BSC ETH token address
12720
- * "outputToken": "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f", // ARB WBTC token address
12721
- * "inputAmount": 1000000000000000n, // The amount of input tokens
12722
- * "minOutputAmount": 900000000000000n, // min amount you are expecting to receive
12723
- * "deadline": 0n, // Optional timestamp after which intent expires (0 = no deadline)
12724
- * "allowPartialFill": false, // Whether the intent can be partially filled
12725
- * "srcChain": "0x38.bsc", // Chain ID where input tokens originate
12726
- * "dstChain": "0xa4b1.arbitrum", // Chain ID where output tokens should be delivered
12727
- * "srcAddress": "0x..", // Source address (original address on spoke chain)
12728
- * "dstAddress": "0x...", // Destination address (original address on spoke chain)
12729
- * "solver": "0x..", // Optional specific solver address (address(0) = any solver)
12730
- * "data": "0x..", // Additional arbitrary data
12756
+ * const createIntentParams = {
12757
+ * inputToken: '0x2170Ed0880ac9A755fd29B2688956BD959F933F8', // BSC ETH token address
12758
+ * outputToken: '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f', // ARB WBTC token address
12759
+ * inputAmount: 1000000000000000n, // The amount of input tokens
12760
+ * minOutputAmount: 900000000000000n, // min amount you are expecting to receive
12761
+ * deadline: 0n, // Optional timestamp after which intent expires (0 = no deadline)
12762
+ * allowPartialFill: false, // Whether the intent can be partially filled
12763
+ * srcChain: BSC_MAINNET_CHAIN_ID, // Chain ID where input tokens originate
12764
+ * dstChain: ARBITRUM_MAINNET_CHAIN_ID, // Chain ID where output tokens should be delivered
12765
+ * srcAddress: '0x..', // Source address (original address on spoke chain)
12766
+ * dstAddress: '0x...', // Destination address (original address on spoke chain)
12767
+ * solver: '0x0000000000000000000000000000000000000000', // Optional specific solver address
12768
+ * data: '0x', // Additional arbitrary data
12731
12769
  * } satisfies CreateIntentParams;
12732
12770
  *
12733
- * const isAllowanceValid = await solverService.isAllowanceValid(payload, spokeProvider);
12771
+ * const isAllowanceValid = await sodax.solver.isAllowanceValid({
12772
+ * intentParams: createIntentParams,
12773
+ * spokeProvider: bscSpokeProvider,
12774
+ * });
12734
12775
  *
12735
- * if (!allowanceValid.ok) {
12776
+ * if (!isAllowanceValid.ok) {
12736
12777
  * // Handle error
12737
- * }
12738
- *
12739
- * if (!allowanceValid.value) {
12740
- * // Need to approve
12778
+ * console.error('Failed to check allowance:', isAllowanceValid.error);
12779
+ * } else if (!isAllowanceValid.value) {
12780
+ * // Need to approve tokens
12781
+ * console.log('Approval required');
12741
12782
  * }
12742
12783
  */
12743
- async isAllowanceValid(params, spokeProvider) {
12784
+ async isAllowanceValid({
12785
+ intentParams: params,
12786
+ spokeProvider,
12787
+ fee = this.config.partnerFee
12788
+ }) {
12744
12789
  try {
12745
12790
  if (spokeProvider instanceof EvmSpokeProvider || spokeProvider instanceof SonicSpokeProvider) {
12746
12791
  const walletAddress = await spokeProvider.walletProvider.getWalletAddress();
12747
- return Erc20Service.isAllowanceValid(
12792
+ return await Erc20Service.isAllowanceValid(
12748
12793
  params.inputToken,
12749
- params.inputAmount,
12794
+ params.inputAmount + calculateFeeAmount(params.inputAmount, fee),
12750
12795
  walletAddress,
12751
12796
  spokeProvider instanceof EvmSpokeProvider ? spokeProvider.chainConfig.addresses.assetManager : spokeProvider.chainConfig.addresses.walletRouter,
12752
12797
  spokeProvider
@@ -12764,35 +12809,55 @@ var SolverService = class {
12764
12809
  }
12765
12810
  }
12766
12811
  /**
12767
- * Approve amount spending (currently required for EVM only)
12768
- * @param token - ERC20 token address
12769
- * @param amount - Amount to approve
12770
- * @param spender - Spender address
12771
- * @param spokeProvider - Spoke provider
12772
- * @param raw - Whether to return the raw transaction hash instead of the transaction receipt
12773
- * @returns {Promise<Result<TxReturnType<S, R>>>} - Returns the raw transaction payload or transaction hash
12812
+ * Approve the Asset Manager contract to spend tokens on behalf of the user (required for EVM chains)
12813
+ * @param {Prettify<SwapParams<S> & OptionalRaw<R>>} params - Object containing:
12814
+ * - intentParams: The parameters for creating the intent.
12815
+ * - spokeProvider: The spoke provider instance.
12816
+ * - fee: (Optional) Partner fee configuration.
12817
+ * - raw: (Optional) Whether to return the raw transaction data instead of executing it
12818
+ * @returns {Promise<Result<TxReturnType<S, R>>>} - Returns transaction hash or raw transaction data
12774
12819
  *
12775
12820
  * @example
12776
- * const approveResult = await approve(
12777
- * '0x...', // ERC20 token address
12778
- * 1000n, // Amount to approve (in token decimals)
12779
- * '0x...', // Spender address (usually the asset manager contract: spokeProvider.chainConfig.addresses.assetManager)
12780
- * spokeProvider,
12781
- * true // if true, returns raw transaction hash instead of raw transaction
12782
- * );
12821
+ * const createIntentParams = {
12822
+ * inputToken: '0x2170Ed0880ac9A755fd29B2688956BD959F933F8', // BSC ETH token address
12823
+ * outputToken: '0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f', // ARB WBTC token address
12824
+ * inputAmount: 1000000000000000n, // The amount of input tokens
12825
+ * minOutputAmount: 900000000000000n, // min amount you are expecting to receive
12826
+ * deadline: 0n, // Optional timestamp after which intent expires (0 = no deadline)
12827
+ * allowPartialFill: false, // Whether the intent can be partially filled
12828
+ * srcChain: BSC_MAINNET_CHAIN_ID, // Chain ID where input tokens originate
12829
+ * dstChain: ARBITRUM_MAINNET_CHAIN_ID, // Chain ID where output tokens should be delivered
12830
+ * srcAddress: '0x..', // Source address (original address on spoke chain)
12831
+ * dstAddress: '0x...', // Destination address (original address on spoke chain)
12832
+ * solver: '0x0000000000000000000000000000000000000000', // Optional specific solver address
12833
+ * data: '0x', // Additional arbitrary data
12834
+ * } satisfies CreateIntentParams;
12835
+ *
12836
+ * const approveResult = await sodax.solver.approve({
12837
+ * intentParams: createIntentParams,
12838
+ * spokeProvider: bscSpokeProvider,
12839
+ * });
12783
12840
  *
12784
12841
  * if (!approveResult.ok) {
12785
12842
  * // Handle error
12843
+ * console.error('Failed to approve tokens:', approveResult.error);
12844
+ * } else {
12845
+ * // Transaction hash or raw transaction data
12846
+ * const txHash = approveResult.value;
12847
+ * console.log('Approval transaction:', txHash);
12786
12848
  * }
12787
- *
12788
- * const txReceipt = approveResult.value;
12789
12849
  */
12790
- async approve(token, amount, spokeProvider, raw) {
12850
+ async approve({
12851
+ intentParams: params,
12852
+ spokeProvider,
12853
+ fee = this.config.partnerFee,
12854
+ raw
12855
+ }) {
12791
12856
  try {
12792
12857
  if (spokeProvider instanceof EvmSpokeProvider || spokeProvider instanceof SonicSpokeProvider) {
12793
12858
  const result = await Erc20Service.approve(
12794
- token,
12795
- amount,
12859
+ params.inputToken,
12860
+ params.inputAmount + calculateFeeAmount(params.inputAmount, fee),
12796
12861
  spokeProvider.chainConfig.addresses.assetManager,
12797
12862
  spokeProvider,
12798
12863
  raw
@@ -12816,11 +12881,12 @@ var SolverService = class {
12816
12881
  /**
12817
12882
  * Creates an intent by handling token approval and intent creation
12818
12883
  * NOTE: This method does not submit the intent to the Solver API
12819
- * @param {Omit<CreateIntentParams, 'srcAddress'>} params - The intent to create
12820
- * @param {SpokeProvider} spokeProvider - The spoke provider
12821
- * @param {boolean} raw - Whether to return the raw transaction
12822
- * @param {PartnerFee} fee - The fee to apply to the intent
12823
- * @returns {Promise<Result<[TxReturnType<S, R>, Intent & FeeAmount], IntentError<'CREATION_FAILED'>>>} The encoded contract call
12884
+ * @param {Prettify<SwapParams<S> & OptionalRaw<R>>} params - Object containing:
12885
+ * - intentParams: The parameters for creating the intent.
12886
+ * - spokeProvider: The spoke provider instance.
12887
+ * - fee: (Optional) Partner fee configuration.
12888
+ * - raw: (Optional) Whether to return the raw transaction data instead of executing it
12889
+ * @returns {Promise<Result<[TxReturnType<S, R>, Intent & FeeAmount], IntentError<'CREATION_FAILED'>>>} The encoded contract call or raw transaction data
12824
12890
  *
12825
12891
  * @example
12826
12892
  * const payload = {
@@ -12838,14 +12904,27 @@ var SolverService = class {
12838
12904
  * "data": "0x..", // Additional arbitrary data
12839
12905
  * } satisfies CreateIntentParams;
12840
12906
  *
12841
- * const createIntentResult = await solverService.createIntent(payload, spokeProvider);
12907
+ * const createIntentResult = await solverService.createIntent({
12908
+ * intentParams: payload,
12909
+ * spokeProvider,
12910
+ * fee, // optional
12911
+ * raw, // optional
12912
+ * });
12842
12913
  *
12843
12914
  * if (createIntentResult.ok) {
12844
12915
  * const [txResult, intent] = createIntentResult.value;
12845
12916
  * console.log('Intent:', intent);
12846
- *
12917
+ * console.log('Packet data:', packetData);
12918
+ * } else {
12919
+ * // handle error
12920
+ * }
12847
12921
  */
12848
- async createIntent(params, spokeProvider, fee, raw) {
12922
+ async createIntent({
12923
+ intentParams: params,
12924
+ spokeProvider,
12925
+ fee = this.config.partnerFee,
12926
+ raw
12927
+ }) {
12849
12928
  invariant2__default.default(
12850
12929
  isValidOriginalAssetAddress(params.srcChain, params.inputToken),
12851
12930
  `Unsupported spoke chain token (params.srcChain): ${params.srcChain}, params.inputToken): ${params.inputToken}`