Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Simulate Bundle and find top token holders endpoints #87

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@
# JWT_CERT_PASSPHRASE=secret

# Authorized domains
# AUTHORIZED_ORIGINS=cow.fi
# AUTHORIZED_ORIGINS=cow.fi

# GOLD RUSH
# GOLD_RUSH_API_KEY=

# TENDERLY
# TENDERLY_API_KEY=
# TENDERLY_ORG_NAME=
# TENDERLY_PROJECT_NAME=
36 changes: 36 additions & 0 deletions apps/api/src/app/inversify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
Erc20Repository,
Erc20RepositoryCache,
Erc20RepositoryViem,
TenderlyRepository,
TokenHolderRepository,
TokenHolderRepositoryCache,
TokenHolderRepositoryGoldRush,
UsdRepository,
UsdRepositoryCache,
UsdRepositoryCoingecko,
Expand All @@ -14,6 +18,8 @@ import {
cowApiClients,
erc20RepositorySymbol,
redisClient,
tenderlyRepositorySymbol,
tokenHolderRepositorySymbol,
usdRepositorySymbol,
viemClients,
} from '@cowprotocol/repositories';
Expand All @@ -27,9 +33,14 @@ import { Container } from 'inversify';
import {
SlippageService,
SlippageServiceMain,
TenderlyService,
TokenHolderService,
TokenHolderServiceMain,
UsdService,
UsdServiceMain,
slippageServiceSymbol,
tenderlyServiceSymbol,
tokenHolderServiceSymbol,
usdServiceSymbol,
} from '@cowprotocol/services';
import ms from 'ms';
Expand Down Expand Up @@ -86,6 +97,17 @@ function getUsdRepository(
]);
}

function getTokenHolderRepositoryGoldRush(
cacheRepository: CacheRepository
): TokenHolderRepository {
return new TokenHolderRepositoryCache(
new TokenHolderRepositoryGoldRush(),
cacheRepository,
'tokenHolderGoldRush',
CACHE_TOKEN_INFO_SECONDS
);
}

function getApiContainer(): Container {
const apiContainer = new Container();
// Repositories
Expand All @@ -96,6 +118,10 @@ function getApiContainer(): Container {
.bind<Erc20Repository>(erc20RepositorySymbol)
.toConstantValue(erc20Repository);

apiContainer
.bind<TenderlyRepository>(tenderlyRepositorySymbol)
.toConstantValue(new TenderlyRepository());

apiContainer
.bind<CacheRepository>(cacheRepositorySymbol)
.toConstantValue(cacheRepository);
Expand All @@ -104,13 +130,23 @@ function getApiContainer(): Container {
.bind<UsdRepository>(usdRepositorySymbol)
.toConstantValue(getUsdRepository(cacheRepository, erc20Repository));

apiContainer
.bind<TokenHolderRepository>(tokenHolderRepositorySymbol)
.toConstantValue(getTokenHolderRepositoryGoldRush(cacheRepository));

// Services
apiContainer
.bind<SlippageService>(slippageServiceSymbol)
.to(SlippageServiceMain);

apiContainer
.bind<TokenHolderService>(tokenHolderServiceSymbol)
.to(TokenHolderServiceMain);

apiContainer.bind<UsdService>(usdServiceSymbol).to(UsdServiceMain);

apiContainer.bind<TenderlyService>(tenderlyServiceSymbol).to(TenderlyService);

return apiContainer;
}

Expand Down
12 changes: 12 additions & 0 deletions apps/api/src/app/plugins/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ const schema = {
COINGECKO_API_KEY: {
type: 'string',
},
GOLD_RUSH_API_KEY: {
type: 'string',
},
TENDERLY_API_KEY: {
type: 'string',
},
TENDERLY_PROJECT: {
type: 'string',
},
TENDERLY_ACCOUNT: {
type: 'string',
},
},
};

Expand Down
135 changes: 135 additions & 0 deletions apps/api/src/app/routes/__chainId/tenderly/simulateBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { TenderlyService, tenderlyServiceSymbol } 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: TenderlyService = apiContainer.get(
tenderlyServiceSymbol
);

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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
TokenHolderService,
tokenHolderServiceSymbol,
} from '@cowprotocol/services';
import { AddressSchema, ChainIdSchema } from '../../../../schemas';
import { FastifyPluginAsync } from 'fastify';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { apiContainer } from '../../../../inversify.config';

const paramsSchema = {
type: 'object',
required: ['chainId', 'tokenAddress'],
additionalProperties: false,
properties: {
chainId: ChainIdSchema,
tokenAddress: AddressSchema,
},
} as const satisfies JSONSchema;

const successSchema = {
type: 'array',
items: {
type: 'object',
required: ['address', 'balance'],
additionalProperties: false,
properties: {
address: {
title: 'Address',
description: 'Address of the token holder.',
type: 'string',
},
balance: {
title: 'Balance',
description: 'Balance of the token holder.',
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>;

const tokenHolderService: TokenHolderService = apiContainer.get(
tokenHolderServiceSymbol
);

const root: FastifyPluginAsync = async (fastify): Promise<void> => {
// example: http://localhost:3010/1/tokens/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/topHolders
fastify.get<{
Params: RouteSchema;
Reply: SuccessSchema | ErrorSchema;
}>(
'/topHolders',
{
schema: {
params: paramsSchema,
response: {
'2XX': successSchema,
'404': errorSchema,
},
},
},
async function (request, reply) {
const { chainId, tokenAddress } = request.params;

const tokenHolders = await tokenHolderService.getTopTokenHolders(
chainId,
tokenAddress
);
fastify.log.info(
`Get token holders for ${tokenAddress} on chain ${chainId}: ${tokenHolders?.length} holder found`
);
if (tokenHolders === null) {
reply.code(404).send({ message: 'Token holders not found' });
return;
}

reply.send(tokenHolders);
}
);
};

export default root;
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import { UsdService, usdServiceSymbol } from '@cowprotocol/services';
import { ChainIdSchema, ETHEREUM_ADDRESS_PATTERN } from '../../../../schemas';
import { AddressSchema, ChainIdSchema } from '../../../../schemas';
import { FastifyPluginAsync } from 'fastify';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { apiContainer } from '../../../../inversify.config';

// TODO: Add this in a follow up PR
// import { ALL_SUPPORTED_CHAIN_IDS } from '@cowprotocol/cow-sdk';

interface Result {
price: number;
}

const paramsSchema = {
type: 'object',
required: ['chainId', 'tokenAddress'],
additionalProperties: false,
properties: {
chainId: ChainIdSchema,
tokenAddress: {
title: 'Token address',
description: 'Token address.',
type: 'string',
pattern: ETHEREUM_ADDRESS_PATTERN,
},
tokenAddress: AddressSchema,
},
} as const satisfies JSONSchema;

Expand Down Expand Up @@ -66,7 +54,7 @@ const root: FastifyPluginAsync = async (fastify): Promise<void> => {
Params: RouteSchema;
Reply: SuccessSchema | ErrorSchema;
}>(
'/usdPrice',
'/simulateBundle',
{
schema: {
params: paramsSchema,
Expand Down
Loading
Loading