Skip to content

Commit

Permalink
Add integration tests (#149)
Browse files Browse the repository at this point in the history
* Before adding hardhat, mocha, and headaches

* Add skipLibCheck = true in tsconfig

* Get order status running on fork rpc, and basic integ test setup

* remove hardhat related imports and update test

* Remove hardhat config

* Fix code style issues with Prettier

* bump gouda-sdk

* save state

* Fix code style issues with Prettier

* bump jest timeout

* Working integ tests

* Fix code style issues with Prettier

* Add other tests

* Fix code style issues with Prettier

* create random wallet and override storage

* Fix code style issues with Prettier

* Remove TENDERLY from supported chains

* Fix code style issues with Prettier

* Add one more test

* unskip nonce test

* Update readme

* Remove comments

* Fix code style issues with Prettier

* fix unit test

* Fix code style issues with Prettier

* Add advanced order query system and fix expiry

* Fix code style issues with Prettier

* riley comments

* Fix code style issues with Prettier

* minor tweaks

* Add tenderly fork creation + deletion

* Remove fork creation

* Add logging

* Add logic to use rpc tenderlyx

* Do not dump entire abi of quoter into logs

* Pass stage into post order handler

* Do not log entire orderChainVerifier abis

* Add typing for stage conditional logic

* save state

* Pass all integ tests

* remove comments + fix:prettier

* fix readme, and other small things

* remove conditional logic for RPC_TENDERLY, and add back sentinal value

* Change chainId to TENDERLY in integ tests

* Bump gouda-sdk to 0.9.6

* Save state, refactored for 0.9.6 but will rollback

* bump gouda-sdk to 1.0.0-alpha.1

* save state

* bump gouda-sdk to latest version

* remove goerli support

* Remove depreacted delete func

* pass integ tests?

* fix failing unit tests from gouda sdk renaming

* fix readme

---------

Co-authored-by: Lint Action <lint-action@samuelmeuli.com>
Co-authored-by: ConjunctiveNormalForm <zhiyuan.zach.yang@gmail.com>
  • Loading branch information
3 people authored Jun 2, 2023
1 parent eb727dd commit 0a171f4
Show file tree
Hide file tree
Showing 25 changed files with 608 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

cdk.out/
cache/

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ To test your changes you must redeploy your service. The dev cycle is thus:
1. Make code changes. Make sure all env variables are present in the .env file:

```
WEB3_RPC_TENDERLY=<>
FIREHOSE_ARN_LOCAL=<>
RPC_12341234=<>
# Only need these if testing against custom contract deployments
REACTOR_TENDERLY=<>
QUOTER_TENDERLY=<>
PERMIT_TENDERLY=<>
FIREHOSE_ARN_LOCAL=<>
```

1. `yarn build && cdk deploy GoudaServiceStack`
Expand Down
1 change: 1 addition & 0 deletions bin/stacks/lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class LambdaStack extends cdk.NestedStack {
},
environment: {
...props.envVars,
stage: props.stage as STAGE,
VERSION: '2',
NODE_OPTIONS: '--enable-source-maps',
STATE_MACHINE_ARN: sfnStack.statusTrackingStateMachine.attrArn,
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/check-order-status/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DutchLimitOrder, OrderValidation } from '@uniswap/gouda-sdk'
import { DutchOrder, OrderValidation } from '@uniswap/gouda-sdk'
import { default as Logger } from 'bunyan'
import { ethers } from 'ethers'
import Joi from 'joi'
Expand Down Expand Up @@ -35,7 +35,7 @@ export class CheckOrderStatusHandler extends SfnLambdaHandler<ContainerInjected,
'cannot find order by hash when updating order status'
)

const parsedOrder = DutchLimitOrder.parse(order.encodedOrder, chainId)
const parsedOrder = DutchOrder.parse(order.encodedOrder, chainId)
log.info({ order: parsedOrder, signature: order.signature }, 'parsed order')
const validation = await orderQuoter.validate({ order: parsedOrder, signature: order.signature })
const curBlockNumber = await provider.getBlockNumber()
Expand Down
9 changes: 4 additions & 5 deletions lib/handlers/check-order-status/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ export class CheckOrderStatusInjector extends SfnInjector<ContainerInjected, Req
serializers: bunyan.stdSerializers,
})

// for local environment, override mainnnet (chainId = 1) to Tenderly
// otherwise, inheret contract addrs from SDK
const chainId = event.chainId
const provider = new ethers.providers.JsonRpcProvider(process.env[`RPC_${chainId}`])
const rpcURL = process.env[`RPC_${chainId}`]
const provider = new ethers.providers.JsonRpcProvider(rpcURL)
const quoter = new OrderValidator(provider, parseInt(chainId as string))
// TODO: use different reactor address for different order type
const watcher = new EventWatcher(provider, REACTOR_ADDRESS_MAPPING[chainId as number][OrderType.DutchLimit])
const watcher = new EventWatcher(provider, REACTOR_ADDRESS_MAPPING[chainId as number][OrderType.Dutch])

log.info({ quoter: quoter, watcher: watcher, quoterAddr: quoter.orderQuoterAddress }, 'getRequestInjected')
log.info({ quoterAddr: quoter.orderQuoterAddress }, 'getRequestInjected')
return {
log,
chainId: event.chainId as number,
Expand Down
23 changes: 12 additions & 11 deletions lib/handlers/post-order/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn'
import { DutchLimitOrder, OrderType, OrderValidation } from '@uniswap/gouda-sdk'
import { DutchOrder, OrderType, OrderValidation } from '@uniswap/gouda-sdk'
import Logger from 'bunyan'
import Joi from 'joi'
import { OrderEntity, ORDER_STATUS } from '../../entities'
Expand Down Expand Up @@ -36,11 +36,18 @@ export class PostOrderHandler extends APIGLambdaHandler<
} = params

log.info('Handling POST order request', params)
log.info({ onchainValidatorByChainId }, 'onchain validators')
let decodedOrder: DutchLimitOrder
log.info(
{
onchainValidatorByChainId: Object.keys(onchainValidatorByChainId).map(
(chainId) => onchainValidatorByChainId[Number(chainId)].orderQuoterAddress
),
},
'onchain validators'
)
let decodedOrder: DutchOrder

try {
decodedOrder = DutchLimitOrder.parse(encodedOrder, chainId) as DutchLimitOrder
decodedOrder = DutchOrder.parse(encodedOrder, chainId) as DutchOrder
} catch (e: unknown) {
log.error(e, 'Failed to parse order')
return {
Expand Down Expand Up @@ -74,13 +81,7 @@ export class PostOrderHandler extends APIGLambdaHandler<
}
}

const order: OrderEntity = formatOrderEntity(
decodedOrder,
signature,
OrderType.DutchLimit,
ORDER_STATUS.OPEN,
quoteId
)
const order: OrderEntity = formatOrderEntity(decodedOrder, signature, OrderType.Dutch, ORDER_STATUS.OPEN, quoteId)
const id = order.orderHash

try {
Expand Down
1 change: 0 additions & 1 deletion lib/handlers/post-order/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class PostOrderInjector extends ApiInjector<ContainerInjected, ApiRInj, P
public async buildContainerInjected(): Promise<ContainerInjected> {
const onchainValidatorByChainId: { [chainId: number]: OnchainValidator } = {}
SUPPORTED_CHAINS.forEach((chainId) => {
// TODO: remove when we bring back tenderly
if (typeof chainId === 'number') {
const rpc = process.env[`RPC_${chainId}`]
if (rpc) {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum ChainId {
OPTIMISM = 10,
ARBITRUM_ONE = 42161,
POLYGON = 137,
TENDERLY = 'TENDERLY',
TENDERLY = 12341234,
}

export const SUPPORTED_CHAINS = [ChainId.MAINNET, ChainId.TENDERLY, ChainId.POLYGON]
export const SUPPORTED_CHAINS = [ChainId.MAINNET, ChainId.POLYGON, ChainId.TENDERLY]
3 changes: 2 additions & 1 deletion lib/util/field-validator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import { OrderType } from '@uniswap/gouda-sdk'
import { OrderType } from '@uniswap/gouda-sdk'
import { BigNumber, ethers } from 'ethers'
import Joi, { CustomHelpers, NumberSchema, StringSchema } from 'joi'
Expand Down Expand Up @@ -38,7 +39,7 @@ export default class FieldValidator {
)
private static readonly SORT_KEY_JOI = Joi.string().valid(SORT_FIELDS.CREATED_AT)
private static readonly SORT_JOI = Joi.string().regex(SORT_REGEX)
private static readonly ORDER_TYPE_JOI = Joi.string().valid(OrderType.DutchLimit)
private static readonly ORDER_TYPE_JOI = Joi.string().valid(OrderType.Dutch)

private static readonly ETH_ADDRESS_JOI = Joi.string().custom((value: string, helpers: CustomHelpers<any>) => {
if (!ethers.utils.isAddress(value)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/order-validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DutchLimitOrder, DutchOutput } from '@uniswap/gouda-sdk'
import { DutchOrder, DutchOutput } from '@uniswap/gouda-sdk'
import { BigNumber } from 'ethers'
import FieldValidator from './field-validator'

Expand All @@ -12,7 +12,7 @@ const THIRTY_MINUTES_IN_SECONDS = 60 * 30
export class OrderValidator {
constructor(private readonly getCurrentTime: () => number) {}

validate(order: DutchLimitOrder): OrderValidationResponse {
validate(order: DutchOrder): OrderValidationResponse {
const chainIdValidation = this.validateChainId(order.chainId)
if (!chainIdValidation.valid) {
return chainIdValidation
Expand Down
4 changes: 2 additions & 2 deletions lib/util/order.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DutchLimitOrder, OrderType } from '@uniswap/gouda-sdk'
import { DutchOrder, OrderType } from '@uniswap/gouda-sdk'
import { DynamoDBRecord } from 'aws-lambda'
import { OrderEntity, ORDER_STATUS } from '../entities'

Expand Down Expand Up @@ -38,7 +38,7 @@ export const eventRecordToOrder = (record: DynamoDBRecord): ParsedOrder => {
}

export const formatOrderEntity = (
decodedOrder: DutchLimitOrder,
decodedOrder: DutchOrder,
signature: string,
orderType: OrderType,
orderStatus: ORDER_STATUS,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@aws-sdk/client-s3": "^3.341.0",
"@aws-sdk/client-sfn": "^3.341.0",
"@types/sinon": "^10.0.13",
"@uniswap/gouda-sdk": "0.9.3",
"@uniswap/gouda-sdk": "^1.0.0-alpha.2",
"aws-cdk-lib": "2.43.1",
"aws-sdk": "^2.1238.0",
"axios": "^1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
},
"OrderType": {
"type": "string",
"enum": ["DutchLimit"]
"enum": ["Dutch"]
},
"OrderEntity": {
"type": "object",
Expand Down
Loading

0 comments on commit 0a171f4

Please sign in to comment.