From a7305ccf13d3ea33f996eeaf8fd403e7cb21ef3e Mon Sep 17 00:00:00 2001 From: Kozer4 Date: Wed, 3 Jan 2024 15:17:10 +0200 Subject: [PATCH] feat(SDK): add getAmountToSendFromChain & getAmountToBeReceivedFromChain --- src/index.ts | 64 ++++++++++++++++++++++++++++++ src/services/index.ts | 92 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 148 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 79a2ffcb..a998ffa3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -271,6 +271,38 @@ export class AllbridgeCoreSdk { return this.service.getAmountToBeReceived(amountToSendFloat, sourceChainToken, destinationChainToken, messenger); } + /** + * Calculates the amount of tokens to be received as a result of transfer based on actual blockchain pool state. + * @param amountToSendFloat the amount of tokens that will be sent + * @param sourceChainToken selected token on the source chain + * @param destinationChainToken selected token on the destination chain + * @param messenger Optional. selected messenger + * @param sourceProvider Optional. source chain Provider + * @param destinationProvider Optional. destination chain Provider + */ + async getAmountToBeReceivedFromChain( + amountToSendFloat: BigSource, + sourceChainToken: TokenWithChainDetails, + destinationChainToken: TokenWithChainDetails, + /** + * The Messengers for different routes. + * Optional. + * The {@link Messenger.ALLBRIDGE}, {@link Messenger.WORMHOLE} by default. + */ + messenger?: Messenger, + sourceProvider?: Provider, + destinationProvider?: Provider + ): Promise { + return this.service.getAmountToBeReceivedFromChain( + amountToSendFloat, + sourceChainToken, + destinationChainToken, + messenger, + sourceProvider, + destinationProvider + ); + } + /** * Calculates the amount of tokens to send based on requested tokens amount be received as a result of transfer. * @param amountToBeReceivedFloat the amount of tokens that should be received @@ -292,6 +324,38 @@ export class AllbridgeCoreSdk { return this.service.getAmountToSend(amountToBeReceivedFloat, sourceChainToken, destinationChainToken, messenger); } + /** + * Calculates the amount of tokens to send based on requested tokens amount be received as a result of transfer based on actual blockchain pool state. + * @param amountToBeReceivedFloat the amount of tokens that should be received + * @param sourceChainToken selected token on the source chain + * @param destinationChainToken selected token on the destination chain + * @param messenger Optional. selected messenger + * @param sourceProvider Optional. source chain Provider + * @param destinationProvider Optional. destination chain Provider + */ + async getAmountToSendFromChain( + amountToBeReceivedFloat: BigSource, + sourceChainToken: TokenWithChainDetails, + destinationChainToken: TokenWithChainDetails, + /** + * The Messengers for different routes. + * Optional. + * The {@link Messenger.ALLBRIDGE}, {@link Messenger.WORMHOLE} by default. + */ + messenger?: Messenger, + sourceProvider?: Provider, + destinationProvider?: Provider + ): Promise { + return this.service.getAmountToSendFromChain( + amountToBeReceivedFloat, + sourceChainToken, + destinationChainToken, + messenger, + sourceProvider, + destinationProvider + ); + } + /** * Fetches possible ways to pay the transfer gas fee. * @param sourceChainToken selected token on the source chain diff --git a/src/services/index.ts b/src/services/index.ts index 01ab6a99..c9c12df4 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -282,6 +282,46 @@ export class AllbridgeCoreSdkService { destinationChainToken: TokenWithChainDetails, messenger?: Messenger ): Promise { + const sourcePool: PoolInfo = await getPoolInfoByToken(this.api, sourceChainToken); + const destPool: PoolInfo = await getPoolInfoByToken(this.api, destinationChainToken); + return this.getAmountToBeReceivedFromPools( + amountToSendFloat, + sourceChainToken, + destinationChainToken, + sourcePool, + destPool, + messenger + ); + } + + async getAmountToBeReceivedFromChain( + amountToSendFloat: number | string | Big, + sourceChainToken: TokenWithChainDetails, + destinationChainToken: TokenWithChainDetails, + messenger?: Messenger, + sourceProvider?: Provider, + destinationProvider?: Provider + ): Promise { + const sourcePool: PoolInfo = await this.pool.getPoolInfoFromChain(sourceChainToken, sourceProvider); + const destPool: PoolInfo = await this.pool.getPoolInfoFromChain(destinationChainToken, destinationProvider); + return this.getAmountToBeReceivedFromPools( + amountToSendFloat, + sourceChainToken, + destinationChainToken, + sourcePool, + destPool, + messenger + ); + } + + private getAmountToBeReceivedFromPools( + amountToSendFloat: number | string | Big, + sourceChainToken: TokenWithChainDetails, + destinationChainToken: TokenWithChainDetails, + sourcePool: PoolInfo, + destinationPool: PoolInfo, + messenger?: Messenger + ): string { validateAmountGtZero(amountToSendFloat); validateAmountDecimals("amountToSendFloat", amountToSendFloat, sourceChainToken.decimals); const amountToSend = convertFloatAmountToInt(amountToSendFloat, sourceChainToken.decimals); @@ -299,8 +339,8 @@ export class AllbridgeCoreSdkService { return convertIntAmountToFloat(resultInDestPrecision, destinationChainToken.decimals).toFixed(); } - const vUsd = swapToVUsd(amountToSend, sourceChainToken, await getPoolInfoByToken(this.api, sourceChainToken)); - return (await this.getAmountFromVUsd(vUsd, destinationChainToken)).float; + const vUsd = swapToVUsd(amountToSend, sourceChainToken, sourcePool); + return this.getAmountFromVUsdFormatted(vUsd, destinationChainToken, destinationPool).float; } async getAmountToSend( @@ -309,6 +349,46 @@ export class AllbridgeCoreSdkService { destinationChainToken: TokenWithChainDetails, messenger?: Messenger ): Promise { + const sourcePool: PoolInfo = await getPoolInfoByToken(this.api, sourceChainToken); + const destPool: PoolInfo = await getPoolInfoByToken(this.api, destinationChainToken); + return this.getAmountToBeReceivedFromPools( + amountToBeReceivedFloat, + sourceChainToken, + destinationChainToken, + sourcePool, + destPool, + messenger + ); + } + + async getAmountToSendFromChain( + amountToBeReceivedFloat: number | string | Big, + sourceChainToken: TokenWithChainDetails, + destinationChainToken: TokenWithChainDetails, + messenger?: Messenger, + sourceProvider?: Provider, + destinationProvider?: Provider + ): Promise { + const sourcePool: PoolInfo = await this.pool.getPoolInfoFromChain(sourceChainToken, sourceProvider); + const destPool: PoolInfo = await this.pool.getPoolInfoFromChain(destinationChainToken, destinationProvider); + return this.getAmountToSendFromPools( + amountToBeReceivedFloat, + sourceChainToken, + destinationChainToken, + sourcePool, + destPool, + messenger + ); + } + + private getAmountToSendFromPools( + amountToBeReceivedFloat: number | string | Big, + sourceChainToken: TokenWithChainDetails, + destinationChainToken: TokenWithChainDetails, + sourcePool: PoolInfo, + destinationPool: PoolInfo, + messenger?: Messenger + ): string { validateAmountGtZero(amountToBeReceivedFloat); validateAmountDecimals("amountToBeReceivedFloat", amountToBeReceivedFloat, destinationChainToken.decimals); const amountToBeReceived = convertFloatAmountToInt(amountToBeReceivedFloat, destinationChainToken.decimals); @@ -326,12 +406,8 @@ export class AllbridgeCoreSdkService { return convertIntAmountToFloat(resultInSourcePrecision, sourceChainToken.decimals).toFixed(); } - const vUsd = swapFromVUsdReverse( - amountToBeReceived, - destinationChainToken, - await getPoolInfoByToken(this.api, destinationChainToken) - ); - const resultInt = swapToVUsdReverse(vUsd, sourceChainToken, await getPoolInfoByToken(this.api, sourceChainToken)); + const vUsd = swapFromVUsdReverse(amountToBeReceived, destinationChainToken, sourcePool); + const resultInt = swapToVUsdReverse(vUsd, sourceChainToken, destinationPool); if (Big(resultInt).lte(0)) { throw new InsufficientPoolLiquidityError(); }