Skip to content

Commit

Permalink
Merge pull request hummingbot#306 from vic-en/feat/add_poolId
Browse files Browse the repository at this point in the history
feat/ add poolId with unit test.
  • Loading branch information
nikspz authored Apr 11, 2024
2 parents ee9dbcf + 6506c90 commit 0fe8449
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/amm/amm.requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface PriceRequest extends NetworkSelectionRequest {
amount: string;
side: Side;
allowedSlippage?: string;
poolId?: string;
}

export interface PriceResponse {
Expand All @@ -41,6 +42,7 @@ export interface PoolPriceRequest extends NetworkSelectionRequest {
fee?: string;
period?: number;
interval?: number;
poolId?: string;
}

export interface PoolPriceResponse {
Expand All @@ -67,6 +69,7 @@ export interface TradeRequest extends NetworkSelectionRequest {
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
allowedSlippage?: string;
poolId?: string;
}

export interface TradeResponse {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions src/amm/amm.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -203,6 +213,7 @@ export const validatePriceRequest: RequestValidator = mkRequestValidator([
validateAmount,
validateSide,
validateAllowedSlippage,
validatePoolId,
]);

export const validateTradeRequest: RequestValidator = mkRequestValidator([
Expand All @@ -218,6 +229,7 @@ export const validateTradeRequest: RequestValidator = mkRequestValidator([
validateMaxFeePerGas,
validateMaxPriorityFeePerGas,
validateAllowedSlippage,
validatePoolId,
]);

export const validatePerpPositionRequest: RequestValidator = mkRequestValidator(
Expand Down Expand Up @@ -302,6 +314,7 @@ export const validateAddLiquidityRequest: RequestValidator = mkRequestValidator(
validateNonce,
validateMaxFeePerGas,
validateMaxPriorityFeePerGas,
validatePoolId,
]
);

Expand Down Expand Up @@ -345,4 +358,5 @@ export const validatePoolPriceRequest: RequestValidator = mkRequestValidator([
validateFee,
validateInterval,
validatePeriod,
validatePoolId,
]);
27 changes: 18 additions & 9 deletions src/connectors/uniswap/uniswap.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export async function getTradeInfo(
quoteAsset: string,
baseAmount: Decimal,
tradeSide: string,
allowedSlippage?: string
allowedSlippage?: string,
poolId?: string,
): Promise<TradeInfo> {
const baseToken: Tokenish = getFullTokenFromSymbol(
ethereumish,
Expand All @@ -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
);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -270,7 +275,8 @@ export async function trade(
req.nonce,
maxFeePerGasBigNumber,
maxPriorityFeePerGasBigNumber,
req.allowedSlippage
req.allowedSlippage,
req.poolId,
);

if (tx.hash) {
Expand Down Expand Up @@ -335,7 +341,8 @@ export async function trade(
gasLimitTransaction,
req.nonce,
maxFeePerGasBigNumber,
maxPriorityFeePerGasBigNumber
maxPriorityFeePerGasBigNumber,
req.poolId,
);

logger.info(
Expand Down Expand Up @@ -407,7 +414,8 @@ export async function addLiquidity(
gasPrice,
req.nonce,
maxFeePerGasBigNumber,
maxPriorityFeePerGasBigNumber
maxPriorityFeePerGasBigNumber,
req.poolId,
);

logger.info(
Expand Down Expand Up @@ -571,7 +579,8 @@ export async function poolPrice(
token1,
req.fee!.toUpperCase(),
req.period!,
req.interval!
req.interval!,
req.poolId,
);

return {
Expand Down
15 changes: 10 additions & 5 deletions src/services/common-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ export interface Uniswapish {
baseToken: Tokenish,
quoteToken: Tokenish,
amount: BigNumber,
allowedSlippage?: string
allowedSlippage?: string,
poolId?: string,
): Promise<ExpectedTrade>;

/**
Expand All @@ -288,7 +289,8 @@ export interface Uniswapish {
quoteToken: Tokenish,
baseToken: Tokenish,
amount: BigNumber,
allowedSlippage?: string
allowedSlippage?: string,
poolId?: string,
): Promise<ExpectedTrade>;

/**
Expand Down Expand Up @@ -316,7 +318,8 @@ export interface Uniswapish {
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber,
allowedSlippage?: string
allowedSlippage?: string,
poolId?: string,
): Promise<Transaction>;
}

Expand Down Expand Up @@ -509,7 +512,8 @@ export interface UniswapLPish {
gasPrice: number,
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber
maxPriorityFeePerGas?: BigNumber,
poolId?: string,
): Promise<Transaction>;

/**
Expand Down Expand Up @@ -569,7 +573,8 @@ export interface UniswapLPish {
token1: UniswapCoreToken,
fee: string,
period: number,
interval: number
interval: number,
poolId?: string,
): Promise<string[]>;
}

Expand Down
35 changes: 35 additions & 0 deletions test/amm/amm.validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
validateLimitPrice,
invalidAllowedSlippageError,
validateAllowedSlippage,
validatePoolId,
invalidPoolIdError,
} from '../../src/amm/amm.validators';

import { missingParameter } from '../../src/services/validators';
Expand Down Expand Up @@ -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]);
});
});

0 comments on commit 0fe8449

Please sign in to comment.