Skip to content

Commit

Permalink
feat: make function execution stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
Verisana committed Sep 26, 2023
1 parent 76a4dae commit ca41f94
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 72 deletions.
6 changes: 3 additions & 3 deletions src/dex-helper/dummy-dex-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export class DummyDexHelper implements IDexHelper {
network: number,
dexKeys: string,
methodSelector: string,
payload: any[],
) => Promise<any>;
payload: unknown[],
) => Promise<unknown>;

constructor(network: number, rpcUrl?: string) {
this.config = new ConfigHelper(false, generateConfig(network), 'is');
Expand Down Expand Up @@ -292,7 +292,7 @@ export class DummyDexHelper implements IDexHelper {
network: number,
dexKey: string,
methodSelector: string,
payload: any[],
payload: unknown[],
) => {
return null;
};
Expand Down
6 changes: 3 additions & 3 deletions src/dex-helper/idex-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface IDexHelper {
network: number,
dexKey: string,
methodSelector: string,
// For POC it is ok to have any
payload: any[],
) => Promise<any>;
payload: unknown[],
// For POC it is ok to have this unknown
) => Promise<unknown>;
getTokenUSDPrice: (token: Token, amount: bigint) => Promise<number>;
}
44 changes: 22 additions & 22 deletions src/dex/algebra/algebra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,20 +527,20 @@ export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
const balanceDestToken =
_destAddress === pool.token0 ? state.balance0 : state.balance1;

const outputFunc = this.getOutputs;
const unitResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'getOutputs',
[state, [unitAmount], zeroForOne, side, balanceDestToken],
)) as ReturnType<typeof outputFunc>;

const pricesResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'getOutputs',
[state, _amounts, zeroForOne, side, balanceDestToken],
)) as ReturnType<typeof outputFunc>;
const unitResult = await this._getOutputs(
state,
[unitAmount],
zeroForOne,
side,
balanceDestToken,
);
const pricesResult = await this._getOutputs(
state,
_amounts,
zeroForOne,
side,
balanceDestToken,
);

if (!unitResult || !pricesResult) {
this.logger.debug('Prices or unit is not calculated');
Expand Down Expand Up @@ -800,20 +800,20 @@ export class Algebra extends SimpleExchange implements IDex<AlgebraData> {
return newConfig;
}

getOutputs(
private async _getOutputs(
state: DeepReadonly<IAlgebraPoolState>,
amounts: bigint[],
zeroForOne: boolean,
side: SwapSide,
destTokenBalance: bigint,
): OutputResult | null {
): Promise<OutputResult | null> {
try {
const outputsResult = AlgebraMath.queryOutputs(
state,
amounts,
zeroForOne,
side,
);
const outputsResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'queryOutputs',
[state, amounts, zeroForOne, side],
)) as ReturnType<typeof AlgebraMath.queryOutputs>;

if (side === SwapSide.SELL) {
if (outputsResult.outputs[0] > destTokenBalance) {
Expand Down
48 changes: 25 additions & 23 deletions src/dex/pancakeswap-v3/pancakeswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class PancakeswapV3
// To receive revert reasons
this.dexHelper.web3Provider.eth.handleRevert = false;

// Normalise once all config addresses and use across all scenarios
// Normalize once all config addresses and use across all scenarios
this.config = this._toLowerForAllConfigAddresses();

this.notExistingPoolSetKey =
Expand Down Expand Up @@ -253,7 +253,7 @@ export class PancakeswapV3
e,
);
} else {
// on unkown error mark as failed and increase retryCount for retry init strategy
// on unknown error mark as failed and increase retryCount for retry init strategy
// note: state would be null by default which allows to fallback
this.logger.warn(
`${this.dexKey}: Can not generate pool state for srcAddress=${srcAddress}, destAddress=${destAddress}, fee=${fee} pool fallback to rpc and retry every ${this.config.initRetryFrequency} times, initRetryAttemptCount=${pool.initRetryAttemptCount}`,
Expand Down Expand Up @@ -608,19 +608,21 @@ export class PancakeswapV3
const balanceDestToken =
_destAddress === pool.token0 ? state.balance0 : state.balance1;

const unitResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'getOutputs',
[state, [unitAmount], zeroForOne, side, balanceDestToken],
)) as ReturnType<typeof this.getOutputs>;

const pricesResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'getOutputs',
[state, _amounts, zeroForOne, side, balanceDestToken],
)) as ReturnType<typeof this.getOutputs>;
const unitResult = await this._getOutputs(
state,
[unitAmount],
zeroForOne,
side,
balanceDestToken,
);
const pricesResult = await this._getOutputs(
state,
_amounts,
zeroForOne,
side,
balanceDestToken,
);

if (!pricesResult) {
this.logger.debug('Prices or unit is not calculated');
return null;
Expand Down Expand Up @@ -909,20 +911,20 @@ export class PancakeswapV3
return newConfig;
}

getOutputs(
private async _getOutputs(
state: DeepReadonly<PoolState>,
amounts: bigint[],
zeroForOne: boolean,
side: SwapSide,
destTokenBalance: bigint,
): OutputResult | null {
): Promise<OutputResult | null> {
try {
const outputsResult = pancakeswapV3Math.queryOutputs(
state,
amounts,
zeroForOne,
side,
);
const outputsResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'queryOutputs',
[state, amounts, zeroForOne, side],
)) as ReturnType<typeof pancakeswapV3Math.queryOutputs>;

if (side === SwapSide.SELL) {
if (outputsResult.outputs[0] > destTokenBalance) {
Expand Down
43 changes: 22 additions & 21 deletions src/dex/uniswap-v3/uniswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,19 +641,20 @@ export class UniswapV3
const balanceDestToken =
_destAddress === pool.token0 ? state.balance0 : state.balance1;

const unitResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'getOutputs',
[state, [unitAmount], zeroForOne, side, balanceDestToken],
)) as ReturnType<typeof this.getOutputs>;

const pricesResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'getOutputs',
[state, _amounts, zeroForOne, side, balanceDestToken],
)) as ReturnType<typeof this.getOutputs>;
const unitResult = await this._getOutputs(
state,
[unitAmount],
zeroForOne,
side,
balanceDestToken,
);
const pricesResult = await this._getOutputs(
state,
_amounts,
zeroForOne,
side,
balanceDestToken,
);

if (!unitResult || !pricesResult) {
this.logger.debug('Prices or unit is not calculated');
Expand Down Expand Up @@ -1070,20 +1071,20 @@ export class UniswapV3
return newConfig;
}

getOutputs(
private async _getOutputs(
state: DeepReadonly<PoolState>,
amounts: bigint[],
zeroForOne: boolean,
side: SwapSide,
destTokenBalance: bigint,
): OutputResult | null {
): Promise<OutputResult | null> {
try {
const outputsResult = uniswapV3Math.queryOutputs(
state,
amounts,
zeroForOne,
side,
);
const outputsResult = (await this.dexHelper.executeOnWorkerPool(
this.network,
this.dexKey,
'queryOutputs',
[state, amounts, zeroForOne, side],
)) as ReturnType<typeof uniswapV3Math.queryOutputs>;

if (side === SwapSide.SELL) {
if (outputsResult.outputs[0] > destTokenBalance) {
Expand Down

0 comments on commit ca41f94

Please sign in to comment.