-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add simulation and token holder endpoints (#88)
* add token holder repository * feat: add token holder endpoint * add tenderly bundle simulation endpoint * refactor: remove unused code * feat: add ethplorer token holder repository * add null value cache on token holder * chore: add fallback strategy on token holder * refactor: add generic fallback repository * refactor: add generic cache repository factory * revert: usdPrice changes * refactor: rename tenderly endpoint to simulation * rename tenderly repository and service to simulation * refactor cache factory to receive converfns * fix import typo * revert refactoring to the fallback and cache factories
- Loading branch information
1 parent
4eaa935
commit 4a913cb
Showing
30 changed files
with
1,880 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
apps/api/src/app/routes/__chainId/simulation/simulateBundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { | ||
SimulationService, | ||
simulationServiceSymbol, | ||
} from '@cowprotocol/services'; | ||
import { FastifyPluginAsync } from 'fastify'; | ||
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; | ||
import { AddressSchema, ChainIdSchema } from '../../../schemas'; | ||
import { apiContainer } from '../../../inversify.config'; | ||
|
||
const paramsSchema = { | ||
type: 'object', | ||
required: ['chainId'], | ||
additionalProperties: false, | ||
properties: { | ||
chainId: ChainIdSchema, | ||
}, | ||
} as const satisfies JSONSchema; | ||
|
||
const successSchema = { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: ['status', 'id', 'link'], | ||
additionalProperties: false, | ||
properties: { | ||
status: { | ||
title: 'Status', | ||
description: 'If the transaction was successful.', | ||
type: 'boolean', | ||
}, | ||
id: { | ||
title: 'ID', | ||
description: 'Tenderly ID of the transaction.', | ||
type: 'string', | ||
}, | ||
link: { | ||
title: 'Link', | ||
description: 'Link to the transaction on Tenderly.', | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
} as const satisfies JSONSchema; | ||
|
||
const bodySchema = { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
required: ['from', 'to', 'input'], | ||
additionalProperties: false, | ||
properties: { | ||
from: AddressSchema, | ||
to: AddressSchema, | ||
value: { | ||
title: 'Value', | ||
description: 'Amount of native coin to send.', | ||
type: 'string', | ||
}, | ||
input: { | ||
title: 'Input', | ||
description: 'Transaction data.', | ||
type: 'string', | ||
}, | ||
gas: { | ||
title: 'Gas', | ||
description: 'Transaction gas limit.', | ||
type: 'number', | ||
}, | ||
gas_price: { | ||
title: 'Gas price', | ||
description: 'Gas price.', | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
} as const satisfies JSONSchema; | ||
|
||
const errorSchema = { | ||
type: 'object', | ||
required: ['message'], | ||
additionalProperties: false, | ||
properties: { | ||
message: { | ||
title: 'Message', | ||
description: 'Message describing the error.', | ||
type: 'string', | ||
}, | ||
}, | ||
} as const satisfies JSONSchema; | ||
|
||
type RouteSchema = FromSchema<typeof paramsSchema>; | ||
type SuccessSchema = FromSchema<typeof successSchema>; | ||
type ErrorSchema = FromSchema<typeof errorSchema>; | ||
type BodySchema = FromSchema<typeof bodySchema>; | ||
|
||
const tenderlyService: SimulationService = apiContainer.get( | ||
simulationServiceSymbol | ||
); | ||
|
||
const root: FastifyPluginAsync = async (fastify): Promise<void> => { | ||
fastify.post<{ | ||
Params: RouteSchema; | ||
Reply: SuccessSchema | ErrorSchema; | ||
Body: BodySchema; | ||
}>( | ||
'/simulateBundle', | ||
{ | ||
schema: { | ||
params: paramsSchema, | ||
response: { | ||
'2XX': successSchema, | ||
'404': errorSchema, | ||
}, | ||
}, | ||
}, | ||
async function (request, reply) { | ||
const { chainId } = request.params; | ||
|
||
const simulationResult = | ||
await tenderlyService.postTenderlyBundleSimulation( | ||
chainId, | ||
request.body | ||
); | ||
|
||
if (simulationResult === null) { | ||
reply.code(404).send({ message: 'Token holders not found' }); | ||
return; | ||
} | ||
fastify.log.info( | ||
`Post Tenderly bundle of ${request.body.length} simulation on chain ${chainId}` | ||
); | ||
|
||
reply.send(simulationResult); | ||
} | ||
); | ||
}; | ||
|
||
export default root; |
Oops, something went wrong.