From 6506c90fe7634f1e8ebb59d766e436a04da0adfd Mon Sep 17 00:00:00 2001 From: vic-en Date: Sat, 6 Apr 2024 15:42:43 -0500 Subject: [PATCH] add poolId with unit test. --- src/amm/amm.requests.ts | 4 +++ src/amm/amm.validators.ts | 14 ++++++++ src/connectors/uniswap/uniswap.controllers.ts | 27 +++++++++----- src/services/common-interfaces.ts | 15 +++++--- test/amm/amm.validators.test.ts | 35 +++++++++++++++++++ 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/amm/amm.requests.ts b/src/amm/amm.requests.ts index 055561e7eb..a02f0470c9 100644 --- a/src/amm/amm.requests.ts +++ b/src/amm/amm.requests.ts @@ -15,6 +15,7 @@ export interface PriceRequest extends NetworkSelectionRequest { amount: string; side: Side; allowedSlippage?: string; + poolId?: string; } export interface PriceResponse { @@ -41,6 +42,7 @@ export interface PoolPriceRequest extends NetworkSelectionRequest { fee?: string; period?: number; interval?: number; + poolId?: string; } export interface PoolPriceResponse { @@ -67,6 +69,7 @@ export interface TradeRequest extends NetworkSelectionRequest { maxFeePerGas?: string; maxPriorityFeePerGas?: string; allowedSlippage?: string; + poolId?: string; } export interface TradeResponse { @@ -106,6 +109,7 @@ export interface AddLiquidityRequest extends NetworkSelectionRequest { // now al maxFeePerGas?: string; maxPriorityFeePerGas?: string; allowedSlippage?: string; // COSMOS: used to calc TokenMinAmount + poolId?: string; } export interface AddLiquidityResponse { diff --git a/src/amm/amm.validators.ts b/src/amm/amm.validators.ts index 111bf0d943..e867adc75b 100644 --- a/src/amm/amm.validators.ts +++ b/src/amm/amm.validators.ts @@ -62,6 +62,9 @@ export const invalidDecreasePercentError: string = export const invalidAllowedSlippageError: string = 'The allowedSlippage param may be null or a string of a fraction.'; +export const invalidPoolIdError: string = + 'PoolId(if supplied) must be a string.'; + export const validateConnector: Validator = mkValidator( 'connector', invalidConnectorError, @@ -194,6 +197,13 @@ export const validateAllowedSlippage: Validator = mkValidator( true ); +export const validatePoolId: Validator = mkValidator( + 'poolId', + invalidPoolIdError, + (val) => typeof val === 'string' && val.length !== 0, + true +); + export const validatePriceRequest: RequestValidator = mkRequestValidator([ validateConnector, validateChain, @@ -203,6 +213,7 @@ export const validatePriceRequest: RequestValidator = mkRequestValidator([ validateAmount, validateSide, validateAllowedSlippage, + validatePoolId, ]); export const validateTradeRequest: RequestValidator = mkRequestValidator([ @@ -218,6 +229,7 @@ export const validateTradeRequest: RequestValidator = mkRequestValidator([ validateMaxFeePerGas, validateMaxPriorityFeePerGas, validateAllowedSlippage, + validatePoolId, ]); export const validatePerpPositionRequest: RequestValidator = mkRequestValidator( @@ -302,6 +314,7 @@ export const validateAddLiquidityRequest: RequestValidator = mkRequestValidator( validateNonce, validateMaxFeePerGas, validateMaxPriorityFeePerGas, + validatePoolId, ] ); @@ -345,4 +358,5 @@ export const validatePoolPriceRequest: RequestValidator = mkRequestValidator([ validateFee, validateInterval, validatePeriod, + validatePoolId, ]); diff --git a/src/connectors/uniswap/uniswap.controllers.ts b/src/connectors/uniswap/uniswap.controllers.ts index 5aca4a8c80..fc9c50202d 100644 --- a/src/connectors/uniswap/uniswap.controllers.ts +++ b/src/connectors/uniswap/uniswap.controllers.ts @@ -93,7 +93,8 @@ export async function getTradeInfo( quoteAsset: string, baseAmount: Decimal, tradeSide: string, - allowedSlippage?: string + allowedSlippage?: string, + poolId?: string, ): Promise { const baseToken: Tokenish = getFullTokenFromSymbol( ethereumish, @@ -115,14 +116,16 @@ export async function getTradeInfo( quoteToken, baseToken, requestAmount, - allowedSlippage + allowedSlippage, + poolId ); } else { expectedTrade = await uniswapish.estimateSellTrade( baseToken, quoteToken, requestAmount, - allowedSlippage + allowedSlippage, + poolId ); } @@ -149,7 +152,8 @@ export async function price( req.quote, new Decimal(req.amount), req.side, - req.allowedSlippage + req.allowedSlippage, + req.poolId, ); } catch (e) { if (e instanceof Error) { @@ -217,7 +221,8 @@ export async function trade( req.base, req.quote, new Decimal(req.amount), - req.side + req.side, + req.poolId, ); } catch (e) { if (e instanceof Error) { @@ -270,7 +275,8 @@ export async function trade( req.nonce, maxFeePerGasBigNumber, maxPriorityFeePerGasBigNumber, - req.allowedSlippage + req.allowedSlippage, + req.poolId, ); if (tx.hash) { @@ -335,7 +341,8 @@ export async function trade( gasLimitTransaction, req.nonce, maxFeePerGasBigNumber, - maxPriorityFeePerGasBigNumber + maxPriorityFeePerGasBigNumber, + req.poolId, ); logger.info( @@ -407,7 +414,8 @@ export async function addLiquidity( gasPrice, req.nonce, maxFeePerGasBigNumber, - maxPriorityFeePerGasBigNumber + maxPriorityFeePerGasBigNumber, + req.poolId, ); logger.info( @@ -571,7 +579,8 @@ export async function poolPrice( token1, req.fee!.toUpperCase(), req.period!, - req.interval! + req.interval!, + req.poolId, ); return { diff --git a/src/services/common-interfaces.ts b/src/services/common-interfaces.ts index 0e26a115d2..464b06e29d 100644 --- a/src/services/common-interfaces.ts +++ b/src/services/common-interfaces.ts @@ -271,7 +271,8 @@ export interface Uniswapish { baseToken: Tokenish, quoteToken: Tokenish, amount: BigNumber, - allowedSlippage?: string + allowedSlippage?: string, + poolId?: string, ): Promise; /** @@ -288,7 +289,8 @@ export interface Uniswapish { quoteToken: Tokenish, baseToken: Tokenish, amount: BigNumber, - allowedSlippage?: string + allowedSlippage?: string, + poolId?: string, ): Promise; /** @@ -316,7 +318,8 @@ export interface Uniswapish { nonce?: number, maxFeePerGas?: BigNumber, maxPriorityFeePerGas?: BigNumber, - allowedSlippage?: string + allowedSlippage?: string, + poolId?: string, ): Promise; } @@ -509,7 +512,8 @@ export interface UniswapLPish { gasPrice: number, nonce?: number, maxFeePerGas?: BigNumber, - maxPriorityFeePerGas?: BigNumber + maxPriorityFeePerGas?: BigNumber, + poolId?: string, ): Promise; /** @@ -569,7 +573,8 @@ export interface UniswapLPish { token1: UniswapCoreToken, fee: string, period: number, - interval: number + interval: number, + poolId?: string, ): Promise; } diff --git a/test/amm/amm.validators.test.ts b/test/amm/amm.validators.test.ts index 82ccfeb61b..90c969d49f 100644 --- a/test/amm/amm.validators.test.ts +++ b/test/amm/amm.validators.test.ts @@ -9,6 +9,8 @@ import { validateLimitPrice, invalidAllowedSlippageError, validateAllowedSlippage, + validatePoolId, + invalidPoolIdError, } from '../../src/amm/amm.validators'; import { missingParameter } from '../../src/services/validators'; @@ -190,3 +192,36 @@ describe('validateAllowedSlippage', () => { ).toEqual([invalidAllowedSlippageError]); }); }); + +describe('validatePoolId', () => { + it('valid when req.poolId is a string', () => { + expect( + validatePoolId({ + poolId: '0x123...', + }) + ).toEqual([]); + + expect( + validatePoolId({ + poolId: '0123', + }) + ).toEqual([]); + }); + + it('pass when req.poolId does not exist', () => { + expect( + validatePoolId({ + hello: 'world', + }) + ).toEqual([]); + }); + + it('return error when req.poolId is a number', () => { + expect( + validatePoolId({ + poolId: 100, + }) + ).toEqual([invalidPoolIdError]); + }); +}); +