Skip to content

Commit

Permalink
Estimate gas on swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
uncoolzero committed Jul 1, 2023
1 parent 0b7143d commit a4e11b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions projects/sdk-wells/src/lib/Well.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export class Well {
}

/**
* Estimage gas for `swapTo()`
* Estimate gas for `swapTo()`
* @param fromToken The token to swap from
* @param toToken The token to swap to
* @param maxAmountIn The maximum amount of `fromToken` to spend
Expand All @@ -465,7 +465,7 @@ export class Well {
* @param deadline The transaction deadline in seconds (defaults to MAX_UINT256)
* @return Estimated gas needed
*/
async swapToEstimageGas(
async swapToGasEstimate(
fromToken: Token,
toToken: Token,
maxAmountIn: TokenValue,
Expand Down
8 changes: 7 additions & 1 deletion projects/sdk-wells/src/lib/swap/Quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type QuotePrepareResult = {

export type QuoteResult = {
amount: TokenValue;
estimate: TokenValue;
gas: TokenValue;
doSwap: () => Promise<ContractTransaction>;
doApproval?: () => Promise<ContractTransaction>;
};
Expand Down Expand Up @@ -92,18 +94,22 @@ export class Quote {

let prevQuote: TokenValue = amount;
let prevQuoteWSlippage: TokenValue = amount;
let prevQuoteGasEstimate: TokenValue = TokenValue.ZERO;
for (const step of steps) {
// console.log("Quote Step:", step.fromToken.symbol, " -> ", step.toToken.symbol);
const { quote, quoteWithSlippage } = await step.quote(isMultiReverse ? prevQuoteWSlippage : prevQuote, direction, slippage);
const { quote, quoteWithSlippage, quoteGasEstimate } = await step.quote(isMultiReverse ? prevQuoteWSlippage : prevQuote, direction, slippage);
prevQuote = quote;
prevQuoteWSlippage = quoteWithSlippage;
prevQuoteGasEstimate = prevQuoteGasEstimate.add(quoteGasEstimate)
}

this.fullQuote = prevQuote;
const { doApproval, doSwap } = await this.prepare(recipient); // TODO: Add deadline

return {
amount: prevQuote,
estimate: prevQuoteWSlippage,
gas: prevQuoteGasEstimate,
doApproval,
doSwap
};
Expand Down
14 changes: 13 additions & 1 deletion projects/sdk-wells/src/lib/swap/SwapStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class SwapStep {
quoteResult: TokenValue | undefined;
// The resulting quote after slippage applied
quoteResultWithSlippage: TokenValue | undefined;
// The resulting quote's gas estimate
quoteGasEstimate: TokenValue | undefined;
slippage: number;

constructor(well: Well, fromToken: Token, toToken: Token) {
Expand All @@ -53,14 +55,24 @@ export class SwapStep {
if (this.direction == Direction.FORWARD) {
this.quoteResult = await this.well.swapFromQuote(this.fromToken, this.toToken, amount);
this.quoteResultWithSlippage = this.quoteResult.subSlippage(slippage);
try {
this.quoteGasEstimate = await this.well.swapFromGasEstimate(this.fromToken, this.toToken, amount, this.quoteResultWithSlippage);
} catch (e) {
this.quoteGasEstimate = TokenValue.ZERO;
}
} else {
this.quoteResult = await this.well.swapToQuote(this.fromToken, this.toToken, amount);
this.quoteResultWithSlippage = this.quoteResult.addSlippage(slippage);
try {
this.quoteGasEstimate = await this.well.swapToGasEstimate(this.fromToken, this.toToken, amount, this.quoteResultWithSlippage);
} catch (e) {
this.quoteGasEstimate = TokenValue.ZERO;
}
}

this.hasQuoted = true;

return { quote: this.quoteResult, quoteWithSlippage: this.quoteResultWithSlippage };
return { quote: this.quoteResult, quoteWithSlippage: this.quoteResultWithSlippage, quoteGasEstimate: this.quoteGasEstimate };
}

swapSingle(amount: TokenValue, amountWithSlippage: TokenValue, recipient: string, deadline: number): SwapFromOp | SwapToOp {
Expand Down

0 comments on commit a4e11b4

Please sign in to comment.