Skip to content

Commit

Permalink
feat(SDK): add getAmountToSendFromChain & getAmountToBeReceivedFromChain
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Jan 3, 2024
1 parent 581a1bd commit a7305cc
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 8 deletions.
64 changes: 64 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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
Expand All @@ -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<string> {
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
Expand Down
92 changes: 84 additions & 8 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,46 @@ export class AllbridgeCoreSdkService {
destinationChainToken: TokenWithChainDetails,
messenger?: Messenger
): Promise<string> {
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<string> {
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);
Expand All @@ -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(
Expand All @@ -309,6 +349,46 @@ export class AllbridgeCoreSdkService {
destinationChainToken: TokenWithChainDetails,
messenger?: Messenger
): Promise<string> {
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<string> {
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);
Expand All @@ -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();
}
Expand Down

0 comments on commit a7305cc

Please sign in to comment.