Skip to content

Commit

Permalink
expose the gas details in the trade context and the routes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Aug 4, 2021
1 parent edce2e7 commit 853c304
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Please note this is not owned or maintained by uniswap and is a open source pack
<br/>
🚀 Exposes all the route paths it tried so you can see every detail in how it worked out the best price
<br/>
🚀 Factor in the cost of the transaction into the quotes with 1 config change
<br/>
🚀 Easy subscriptions to get alerted when the price moves or the trade expires
<br/>
🚀 The transaction is generated for you, just fill it with the gas details and send it on its way
Expand Down Expand Up @@ -399,6 +401,10 @@ export interface TradeContext {
expectedConvertQuote: string;
expectedConvertQuoteOrTokenAmountInMaxWithSlippage: string;
transaction: Transaction;
// if you have enabled to factor in the transaction cost in the quotes
// then this is the gas price in gwei we used to estimate the transactions
// it only be defined by the ones it decided to pick depending on the hops abouts
gasPriceEstimatedBy: string | undefined;
tradeExpires: number;
routePathArrayTokenMap: Token[];
routeText: string;
Expand Down Expand Up @@ -449,6 +455,9 @@ export interface TradeContext {
data: string;
value: string;
};
// if you have enabled to factor in the transaction cost in the quotes
// then this is the gas price in gwei we used to estimate the transactions
gasPriceEstimatedBy: string | undefined;
// this is a stream which emits if the quote has changed, this will emit
// not matter what you should listen to this for the source of truth
// for a reactive dApp. If you dont listen to this the user could end up
Expand Down
9 changes: 5 additions & 4 deletions src/__TEST-SCRIPT__/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const routeTest = async () => {
deadlineMinutes: 20,
disableMultihops: false,
uniswapVersions: [UniswapVersion.v2, UniswapVersion.v3],
// gasSettings: {
// getGasPrice: async () => 100000000000,
// },
gasSettings: {
getGasPrice: async () => '90',
},
}),
});

Expand All @@ -43,6 +43,7 @@ const routeTest = async () => {
const trade = await uniswapPairFactory.trade('0.0001', TradeDirection.input);

console.log(new Date().getTime() - startTime);
console.log(trade);

// console.log(JSON.stringify(trade, null, 4));
// console.log(trade);
Expand All @@ -59,7 +60,7 @@ const routeTest = async () => {
// (await ethers.provider.estimateGas(trade.transaction)).toHexString()
// );

process.stdin.resume();
// process.stdin.resume();

// console.log(JSON.stringify(trade));

Expand Down
1 change: 1 addition & 0 deletions src/factories/pair/models/trade-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface TradeContext {
toToken: Token;
toBalance: string;
transaction: Transaction;
gasPriceEstimatedBy: string | undefined;
quoteChanged$: UniswapStream<TradeContext>;
destroy: () => void;
}
3 changes: 3 additions & 0 deletions src/factories/pair/uniswap-pair.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export class UniswapPairFactory {
balance: bestRouteQuotes.fromBalance,
},
transaction: bestRouteQuote.transaction,
gasPriceEstimatedBy: bestRouteQuote.gasPriceEstimatedBy,
allTriedRoutesQuotes: bestRouteQuotes.triedRoutesQuote,
quoteChanged$: this._quoteChanged$,
destroy: () => this.destroy(),
Expand Down Expand Up @@ -423,6 +424,7 @@ export class UniswapPairFactory {
balance: bestRouteQuotes.fromBalance,
},
transaction: bestRouteQuote.transaction,
gasPriceEstimatedBy: bestRouteQuote.gasPriceEstimatedBy,
allTriedRoutesQuotes: bestRouteQuotes.triedRoutesQuote,
quoteChanged$: this._quoteChanged$,
destroy: () => this.destroy(),
Expand Down Expand Up @@ -485,6 +487,7 @@ export class UniswapPairFactory {
balance: bestRouteQuotes.fromBalance,
},
transaction: bestRouteQuote.transaction,
gasPriceEstimatedBy: bestRouteQuote.gasPriceEstimatedBy,
allTriedRoutesQuotes: bestRouteQuotes.triedRoutesQuote,
quoteChanged$: this._quoteChanged$,
destroy: () => this.destroy(),
Expand Down
1 change: 1 addition & 0 deletions src/factories/router/models/route-quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface RouteQuote {
uniswapVersion: UniswapVersion;
liquidityProviderFee: number;
quoteDirection: TradeDirection;
gasPriceEstimatedBy?: string | undefined;
}
8 changes: 5 additions & 3 deletions src/factories/router/uniswap-router.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ export class UniswapRouterFactory {
uniswapVersion: route.uniswapVersion,
liquidityProviderFee: route.liquidityProviderFee,
quoteDirection: route.quoteDirection,
gasPriceEstimatedBy: route.gasPriceEstimatedBy,
};
}),
hasEnoughBalance: allowanceAndBalances.enoughBalance,
Expand Down Expand Up @@ -1018,9 +1019,8 @@ export class UniswapRouterFactory {
enoughAllowanceV3
);

const gasPrice = new BigNumber(
await this._settings.gasSettings.getGasPrice()
).times(1e9);
const gasPriceGwei = await this._settings.gasSettings.getGasPrice();
const gasPrice = new BigNumber(gasPriceGwei).times(1e9);

let bestRoute:
| {
Expand All @@ -1044,6 +1044,8 @@ export class UniswapRouterFactory {
).times(gasPrice)
).times(ethUsdValue);

route.gasPriceEstimatedBy = gasPriceGwei;

const expectedConvertQuoteMinusTxFees =
expectedConvertQuoteFiatPrice.minus(txFee);

Expand Down

0 comments on commit 853c304

Please sign in to comment.