Skip to content

Commit

Permalink
added comments to swap quote function
Browse files Browse the repository at this point in the history
  • Loading branch information
kev1n-peters committed May 17, 2024
1 parent 525aa9e commit 182168c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions wormhole-connect/src/routes/porticoBridge/porticoBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,34 @@ export abstract class PorticoBridge extends BaseRoute {
return true;
}

// This function calculates the amounts for a token swap operation
async computeSwapAmounts(
sendAmount: number,
token: string,
destToken: string,
sendingChain: ChainName | undefined,
recipientChain: ChainName | undefined,
): Promise<PorticoSwapAmounts> {
// Validate the parameters
if (!sendAmount || !destToken || !sendingChain || !recipientChain) {
throw new Error('Invalid params');
}
// Get the start and finish tokens
const startToken = getWrappedToken(config.tokens[token]);
const finishToken = getWrappedToken(config.tokens[destToken]);
// Validate the tokens
if (!startToken.tokenId) {
throw new Error('Unable to get start token');
}
const finishToken = getWrappedToken(config.tokens[destToken]);
if (!finishToken.tokenId) {
throw new Error('Unable to get finish token');
}
// Get the canonical addresses of the start and finish tokens
const [startCanonicalToken, finishCanonicalToken] = await Promise.all([
getCanonicalTokenAddress(startToken),
getCanonicalTokenAddress(finishToken),
]);
// Parse the send amount to the correct decimal representation
const startTokenDecimals = getTokenDecimals(
toChainId(sendingChain),
startToken.tokenId,
Expand All @@ -184,53 +190,65 @@ export abstract class PorticoBridge extends BaseRoute {
sendAmount.toString(),
startTokenDecimals,
);
// Get the quote for swapping the send amount
const startQuote = await this.getQuote(
sendingChain,
startToken.tokenId.address,
startCanonicalToken,
parsedSendAmount,
FEE_TIER,
);
// Calculate the slippage for the start quote
const startSlippage = startQuote.amountOut
.mul(SLIPPAGE_BPS)
.div(BPS_PER_HUNDRED_PERCENT);
// Validate the slippage
if (startSlippage.gte(startQuote.amountOut)) {
throw new Error('Start slippage too high');
}
// Calculate the minimum amount for the start quote
const minAmountStart = startQuote.amountOut.sub(startSlippage);
// Get the quote for the recipient chain
const finishQuote = await this.getQuote(
recipientChain,
finishCanonicalToken,
finishToken.tokenId.address,
minAmountStart,
FEE_TIER,
);
// Calculate the slippage for the finish quote
const finishSlippage = finishQuote.amountOut
.mul(SLIPPAGE_BPS)
.div(BPS_PER_HUNDRED_PERCENT);
// Validate the slippage
if (finishSlippage.gte(finishQuote.amountOut)) {
throw new Error('Finish slippage too high');
}
// Calculate the minimum amount for the finish quote
const minAmountFinish = finishQuote.amountOut.sub(finishSlippage);
// Get the quote for the recipient chain without slippage
const amountFinishQuote = await this.getQuote(
recipientChain,
finishCanonicalToken,
finishToken.tokenId.address,
startQuote.amountOut, // no slippage
FEE_TIER,
);
// the expected receive amount is the amount out from the swap
// minus 5bps slippage
// Calculate the slippage for the finish quote
const amountFinishSlippage = amountFinishQuote.amountOut
.mul(5)
.div(BPS_PER_HUNDRED_PERCENT);
// Validate the slippage
if (amountFinishSlippage.gte(amountFinishQuote.amountOut)) {
throw new Error('Amount finish slippage too high');
}
// Calculate the final amount for the finish quote
const amountFinish = amountFinishQuote.amountOut.sub(amountFinishSlippage);
// Validate the final amount
if (amountFinish.lte(minAmountFinish)) {
throw new Error('Amount finish too low');
}
// Return the calculated amounts
return {
minAmountStart: minAmountStart.toString(),
minAmountFinish: minAmountFinish.toString(),
Expand Down

0 comments on commit 182168c

Please sign in to comment.