Skip to content

Commit

Permalink
feat, test: return fixed parameters for non-Unimind addresses (#519)
Browse files Browse the repository at this point in the history
* feat: return fixed parameters for non-unimind addresses

* test: update tests
  • Loading branch information
alanhwu authored Feb 13, 2025
1 parent 43b0fc2 commit 73682be
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lib/handlers/get-unimind/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Unit } from 'aws-embedded-metrics'
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'
import { metrics } from '../../util/metrics'
import { UnimindQueryParams, unimindQueryParamsSchema } from './schema'
import { DEFAULT_UNIMIND_PARAMETERS } from '../../util/constants'
import { DEFAULT_UNIMIND_PARAMETERS, PUBLIC_UNIMIND_PARAMETERS, UNIMIND_DEV_SWAPPER_ADDRESS } from '../../util/constants'

type UnimindResponse = {
pi: number
Expand All @@ -20,9 +20,8 @@ export class GetUnimindHandler extends APIGLambdaHandler<ContainerInjected, Requ
): Promise<Response<UnimindResponse> | ErrorResponse> {
const { containerInjected, requestQueryParams } = params
const { quoteMetadataRepository, unimindParametersRepository } = containerInjected

try {
const { logOnly, ...quoteMetadataFields } = requestQueryParams
const { logOnly, swapper, ...quoteMetadataFields } = requestQueryParams
const quoteMetadata = {
...quoteMetadataFields,
route: requestQueryParams.route ? JSON.parse(requestQueryParams.route) : undefined
Expand All @@ -38,6 +37,12 @@ export class GetUnimindHandler extends APIGLambdaHandler<ContainerInjected, Requ
}
}
}
if (!swapper || swapper != UNIMIND_DEV_SWAPPER_ADDRESS) {
return {
statusCode: 200,
body: PUBLIC_UNIMIND_PARAMETERS
}
}

let [, unimindParameters] = await Promise.all([
quoteMetadataRepository.put(quoteMetadata),
Expand Down
6 changes: 4 additions & 2 deletions lib/handlers/get-unimind/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ export const unimindQueryParamsSchema = Joi.object({
logOnly: Joi.boolean().optional()
.truthy('true')
.falsy('false')
.sensitive()
.sensitive(),
// All other values are rejected for a 400 error
swapper: Joi.string().optional()
})

export type UnimindQueryParams = Omit<QuoteMetadata, 'route'> & {
route: string, // route is now a JSON string to be used as a GET query param
logOnly?: boolean
logOnly?: boolean,
swapper?: string
}
5 changes: 5 additions & 0 deletions lib/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ export const DYNAMO_BATCH_WRITE_MAX = 25
export const DEFAULT_UNIMIND_PARAMETERS = {
pi: 5,
tau: 5
}
export const UNIMIND_DEV_SWAPPER_ADDRESS = '0x2b813964306D8F12bdaB5504073a52e5802f049D'
export const PUBLIC_UNIMIND_PARAMETERS = {
pi: 8,
tau: 5
}
24 changes: 20 additions & 4 deletions test/unit/handlers/get-unimind/get-unimind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GetUnimindHandler } from '../../../../lib/handlers/get-unimind/handler'
import { QuoteMetadataRepository } from '../../../../lib/repositories/quote-metadata-repository'
import { UnimindParametersRepository } from '../../../../lib/repositories/unimind-parameters-repository'
import { ErrorCode } from '../../../../lib/handlers/base'
import { UNIMIND_DEV_SWAPPER_ADDRESS } from '../../../../lib/util/constants'

const SAMPLE_ROUTE = {
quote: "1234",
Expand Down Expand Up @@ -53,6 +54,10 @@ describe('Testing get unimind handler', () => {
priceImpact: 0.01,
route: STRINGIFIED_ROUTE,
}
const quoteQueryParams = {
...quoteMetadata,
swapper: UNIMIND_DEV_SWAPPER_ADDRESS
}

mockUnimindParametersRepo.getByPair.mockResolvedValue({
pair: 'ETH-USDC',
Expand All @@ -62,7 +67,7 @@ describe('Testing get unimind handler', () => {

const response = await getUnimindHandler.handler(
{
queryStringParameters: quoteMetadata,
queryStringParameters: quoteQueryParams,
requestContext: {
requestId: 'test-request-id'
}
Expand Down Expand Up @@ -91,12 +96,16 @@ describe('Testing get unimind handler', () => {
pair: 'ALAN-LEN',
route: STRINGIFIED_ROUTE,
}
const quoteQueryParams = {
...quoteMetadata,
swapper: UNIMIND_DEV_SWAPPER_ADDRESS
}

mockUnimindParametersRepo.getByPair.mockResolvedValue(undefined)

const response = await getUnimindHandler.handler(
{
queryStringParameters: quoteMetadata,
queryStringParameters: quoteQueryParams,
requestContext: {
requestId: 'test-request-id'
}
Expand All @@ -117,7 +126,7 @@ describe('Testing get unimind handler', () => {
})
})

it('Returns empty parameters when expectParams is false', async () => {
it('Returns empty parameters when logOnly is true', async () => {
const quoteMetadata = {
quoteId: 'this-should-work',
referencePrice: '100',
Expand Down Expand Up @@ -247,12 +256,16 @@ describe('Testing get unimind handler', () => {
priceImpact: 0.01,
route: STRINGIFIED_ROUTE,
}
const quoteQueryParams = {
...quoteMetadata,
swapper: UNIMIND_DEV_SWAPPER_ADDRESS
}

mockQuoteMetadataRepo.put.mockRejectedValue(new Error('DB Error'))

const response = await getUnimindHandler.handler(
{
queryStringParameters: quoteMetadata,
queryStringParameters: quoteQueryParams,
requestContext: {
requestId: 'test-request-id-repo-error'
}
Expand Down Expand Up @@ -394,6 +407,7 @@ describe('Testing get unimind handler', () => {
priceImpact: 0.01,
pair: 'ETH-USDC',
route: STRINGIFIED_ROUTE,
swapper: UNIMIND_DEV_SWAPPER_ADDRESS
}

const response = await getUnimindHandler.handler(
Expand All @@ -417,6 +431,7 @@ describe('Testing get unimind handler', () => {
referencePrice: '4221.21',
priceImpact: 0.01,
pair: 'ETH-USDC',
swapper: UNIMIND_DEV_SWAPPER_ADDRESS,
blockNumber: 1234,
// missing route
}
Expand All @@ -440,6 +455,7 @@ describe('Testing get unimind handler', () => {
referencePrice: '4221.21',
priceImpact: 0.01,
pair: 'ETH-USDC',
swapper: UNIMIND_DEV_SWAPPER_ADDRESS,
// missing blockNumber
route: STRINGIFIED_ROUTE,
}
Expand Down

0 comments on commit 73682be

Please sign in to comment.