Skip to content

Commit

Permalink
fix the watcher trade changes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Jul 26, 2021
1 parent 9d1ee39 commit b7db07b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-uniswap-sdk",
"version": "3.1.0",
"version": "3.2.0",
"description": "Simple easy to understand SDK for uniswap which looks over best v2 and v3 to find you the best swap quote",
"main": "dist/index.js",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions src/factories/pair/models/current-trade-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Token } from '../../token/models/token';
import { TradeDirection } from './trade-direction';
import { Transaction } from './transaction';

export interface CurrentTradeContext {
baseConvertRequest: string;
expectedConvertQuote: string;
quoteDirection: TradeDirection;
fromToken: Token;
toToken: Token;
liquidityProviderFee: string;
transaction: Transaction;
routeText: string;
tradeExpires: number;
}
116 changes: 77 additions & 39 deletions src/factories/pair/uniswap-pair.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
removeEthFromContractAddress,
turnTokenIntoEthForResponse,
} from '../../common/tokens/eth';
import { deepClone } from '../../common/utils/deep-clone';
import { hexlify } from '../../common/utils/hexlify';
import { parseEther } from '../../common/utils/parse-ether';
import { toEthersBigNumber } from '../../common/utils/to-ethers-big-number';
Expand All @@ -30,6 +31,7 @@ import { AllowanceAndBalanceOf } from '../token/models/allowance-balance-of';
import { Token } from '../token/models/token';
import { TokenFactory } from '../token/token.factory';
import { TokensFactory } from '../token/tokens.factory';
import { CurrentTradeContext } from './models/current-trade-context';
import { TradeContext } from './models/trade-context';
import { TradeDirection } from './models/trade-direction';
import { Transaction } from './models/transaction';
Expand Down Expand Up @@ -65,8 +67,8 @@ export class UniswapPairFactory {
this._uniswapPairFactoryContext.ethersProvider
);

private _currentTradeContext: TradeContext | undefined;
private _quoteChangeInternal: NodeJS.Timeout | undefined;
private _watchingBlocks = false;
private _currentTradeContext: CurrentTradeContext | undefined;
private _quoteChanged$: Subject<TradeContext> = new Subject<TradeContext>();

constructor(private _uniswapPairFactoryContext: UniswapPairFactoryContext) {}
Expand Down Expand Up @@ -159,6 +161,7 @@ export class UniswapPairFactory {
for (let i = 0; i < this._quoteChanged$.observers.length; i++) {
this._quoteChanged$.observers[i].complete();
}
this.unwatchTradePrice();
}

/**
Expand All @@ -172,25 +175,13 @@ export class UniswapPairFactory {
direction: TradeDirection = TradeDirection.input
): Promise<TradeContext> {
this.destroy();
if (this._quoteChangeInternal) {
clearInterval(this._quoteChangeInternal);
}

this._currentTradeContext = await this.executeTradePath(
new BigNumber(amount),
direction
);
const trade = await this.executeTradePath(new BigNumber(amount), direction);
this._currentTradeContext = this.buildCurrentTradeContext(trade);

this.watchTradePrice();

return this._currentTradeContext;
}

/**
* Route getter
*/
private get _routes(): UniswapRouterFactory {
return this._uniswapRouterFactory;
return trade;
}

/**
Expand Down Expand Up @@ -433,6 +424,31 @@ export class UniswapPairFactory {
};
}

/**
* Route getter
*/
private get _routes(): UniswapRouterFactory {
return this._uniswapRouterFactory;
}

/**
* Build the current trade context
* @param trade The trade context
*/
private buildCurrentTradeContext(trade: TradeContext): CurrentTradeContext {
return deepClone({
baseConvertRequest: trade.baseConvertRequest,
expectedConvertQuote: trade.expectedConvertQuote,
quoteDirection: trade.quoteDirection,
fromToken: trade.fromToken,
toToken: trade.toToken,
liquidityProviderFee: trade.liquidityProviderFee,
transaction: trade.transaction,
routeText: trade.routeText,
tradeExpires: trade.tradeExpires,
});
}

/**
* finds the best price and path for Erc20ToEth
* @param baseConvertRequest The base convert request can be both input or output direction
Expand Down Expand Up @@ -1164,37 +1180,59 @@ export class UniswapPairFactory {
* Watch trade price move automatically emitting the stream if it changes
*/
private watchTradePrice(): void {
this._quoteChangeInternal = setInterval(async () => {
if (!this._watchingBlocks) {
this._uniswapPairFactoryContext.ethersProvider.provider.on(
'block',
async (block: number) => {
console.log('block', block);
await this.handleNewBlock(block);
}
);
this._watchingBlocks = true;
}
}

/**
* unwatch any block streams
*/
private unwatchTradePrice(): void {
this._uniswapPairFactoryContext.ethersProvider.provider.removeAllListeners(
'block'
);
this._watchingBlocks = false;
}

/**
* Handle new block for the trade price moving automatically emitting the stream if it changes
*/
private async handleNewBlock(block: number): Promise<void> {
if (this._quoteChanged$.observers.length > 0 && this._currentTradeContext) {
const trade = await this.executeTradePath(
new BigNumber(this._currentTradeContext.baseConvertRequest),
this._currentTradeContext.quoteDirection
);

if (
this._quoteChanged$.observers.length > 0 &&
this._currentTradeContext
trade.fromToken.contractAddress ===
this._currentTradeContext.fromToken.contractAddress &&
trade.toToken.contractAddress ===
this._currentTradeContext.toToken.contractAddress &&
trade.transaction.from ===
this._uniswapPairFactoryContext.ethereumAddress
) {
const trade = await this.executeTradePath(
new BigNumber(this._currentTradeContext.baseConvertRequest),
this._currentTradeContext.quoteDirection
);
if (
trade.expectedConvertQuote !==
this._currentTradeContext.expectedConvertQuote ||
trade.routeText !== this._currentTradeContext.routeText
) {
this._currentTradeContext = trade;
this._quoteChanged$.next(trade);
return;
}

// it has expired send another one to them
if (
trade.routeText !== this._currentTradeContext.routeText ||
trade.liquidityProviderFee !==
this._currentTradeContext.liquidityProviderFee ||
this._currentTradeContext.tradeExpires >
this.generateTradeDeadlineUnixTime()
this.generateTradeDeadlineUnixTime()
) {
this._currentTradeContext = trade;
this._currentTradeContext = this.buildCurrentTradeContext(trade);
this._quoteChanged$.next(trade);
return;
}
}
// maybe make config???
// query new prices every 25 seconds
}, 25000);
}
}
}

0 comments on commit b7db07b

Please sign in to comment.