Skip to content

Commit

Permalink
Merge branch 'feat/get-all-balances' of https://github.com/0xsquid/sq…
Browse files Browse the repository at this point in the history
…uid-sdk into feat/get-all-balances
  • Loading branch information
genaroibc committed Oct 25, 2023
2 parents 4ad643a + 4ff3b47 commit a9c8b73
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
39 changes: 39 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
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;
}
}
}

Expand Down

0 comments on commit a9c8b73

Please sign in to comment.