diff --git a/CHANGELOG.md b/CHANGELOG.md index 93d442f..29ae6bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ +## [1.14.1](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.14.1) (2023-10-18) + + +### Features + +* get from amount ([#218](https://github.com/0xsquid/api-sdk/issues/218)) ([d727de5](https://github.com/0xsquid/api-sdk/commit/d727de5d2c07b1ef12fb091bfe4bea49ba2cb987)) + +## [1.14.0](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.14.0) (2023-09-26) + ## [1.12.1](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.12.1) (2023-09-06) ## [1.12.0](https://github.com/0xsquid/api-sdk/compare/v1.10.0...v1.12.0) (2023-08-29) diff --git a/package.json b/package.json index c143e37..7cb7e32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xsquid/sdk", - "version": "1.13.0", + "version": "1.14.1", "description": "🛠 An SDK for building applications on top of 0xsquid", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index c1f0628..7139671 100644 --- a/src/index.ts +++ b/src/index.ts @@ -847,6 +847,45 @@ export class Squid { evmBalances, cosmosBalances }; + + public async getFromAmount({ + fromToken, + toAmount, + toToken, + slippagePercentage = 1.5 + }: { + fromToken: TokenData; + toToken: TokenData; + toAmount: string; + slippagePercentage?: number; + }): Promise { + try { + // parallelize requests + const [fromTokenPrice, toTokenPrice] = await Promise.all([ + this.getTokenPrice({ + chainId: fromToken.chainId, + tokenAddress: fromToken.address + }), + this.getTokenPrice({ + chainId: toToken.chainId, + tokenAddress: toToken.address + }) + ]); + + // example fromAmount: 10 + const fromAmount = + (toTokenPrice * Number(toAmount ?? 0)) / fromTokenPrice; + + // fromAmount (10) * slippagePercentage (1.5) / 100 = 0.15 + const slippage = fromAmount * (slippagePercentage / 100); + + // fromAmount (10) + slippage (0.15) = 10.15 + const fromAmountPlusSlippage = fromAmount + slippage; + + return fromAmountPlusSlippage.toString(); + } catch (error) { + return null; + } } }