Skip to content

Commit

Permalink
Adding proper fns
Browse files Browse the repository at this point in the history
  • Loading branch information
mehranhydary committed Sep 13, 2024
1 parent 5f0b6fb commit 0068cae
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 157 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Instructions to manually run Milady Pool are listed below. Please note that this
1. To get started, we will prepare the contracts
2. Run `cd contracts && cp .env.example .env`
3. Run `forge build`
4. Start anvil by opening another terminal and running `anvil`
4. Start anvil by opening another terminal and running `anvil --host 0.0.0.0`
5. In another terminal, deploy Eigenlayer contracts

Change into `contracts/lib/eigenlayer-contracts` and run the following commands (the other one is old (in eigenlayer-middleware/lib/eigenlayer-contracts))
Expand Down
5 changes: 4 additions & 1 deletion contracts/src/base/Hook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ abstract contract Hook is BaseHook, WyvernInspired {
walletAddress,
permit2Signature
);
// TODO:
// TODO: Instead of passing back toBeforeSwapDelta(0, 0), pass back the token in and out amounts
// for the new swap function

// Which means...

// BalanceDelta delta = poolManager.swap(
// key,
Expand Down
12 changes: 12 additions & 0 deletions operator/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getDb } from './lib/db/getDb'
import { useServer } from 'graphql-ws/lib/use/ws'
import { WebSocketServer } from 'ws'
import { getOriginResponse } from './config/cors'
import { monitorNewTicks } from './lib/web3'

const schema = getSchema()
// The ApolloServer constructor requires two parameters: your schema
Expand Down Expand Up @@ -96,3 +97,14 @@ server.start().then(() => {
httpServer.listen(serverConfig.port, () => {
console.log(`Milady Pool AVS API running on port: ${serverConfig.port}\n`)
})

monitorNewTicks()
.then(() => {
console.log(
'Monitoring new ticks on MiladyPool deployed at http://127.0.0.1:8545'
)
})
.catch((error: any) => {
console.error('Error monitoring new ticks', error)
process.exit(1)
})
13 changes: 5 additions & 8 deletions operator/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@ import type { CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
overwrite: true,
generates: {
'src/types/queries.ts': {
'core/types/queries.ts': {
schema: 'http://localhost:8081/graphql',
plugins: [
'typescript',
'typescript-operations',
'fragment-matcher',
],
},
'src/types/resolvers.ts': {
'core/types/resolvers.ts': {
plugins: [
'@graphql-codegen/typescript',
'@graphql-codegen/typescript-resolvers',
],
schema: ['./core/graphql/extensions/*/**'],
config: {
contextType: './core/graphql/schema#GraphqlContext',
rootValueType: './core/graphql/schema#RootValue',
mappers: {
Decimal: './core/graphql/schema#Decimal',
},
contextType: '../graphql/schema#GraphqlContext',
rootValueType: '../graphql/schema#RootValue',
useIndexSignature: true,
printFieldsOnNewLines: true,
scalars: {
ISO8601Date: 'string | Date',
BigInt: {
input: 'bigint',
output: 'bigint | string | Decimal',
output: 'bigint | string',
},
EthereumAddress: 'string | Hex',
TxHash: 'string | Hex',
Expand Down
252 changes: 123 additions & 129 deletions operator/core/graphql/extensions/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const typeDefs = gql`
tokenA: String!
tokenB: String!
fee: String!
tickSpacing: Int!
tickSpacing: String!
hooks: String # Figure out if we need to store hooks in an array or...
permit2Signature: String!
startTime: ISO8601Date
Expand Down Expand Up @@ -46,7 +46,7 @@ export const typeDefs = gql`
token0: String!
token1: String!
fee: String!
tickSpacing: Int!
tickSpacing: String!
hooks: String! # Figure out if we need to store hooks in an array or...
}
Expand All @@ -60,134 +60,128 @@ export const typeDefs = gql`
`

interface OrdersResolvers {
Query: Pick<QueryResolvers, 'orders'>
Mutation: Pick<MutationResolvers, 'createOrder'>
// Query: Pick<QueryResolvers, 'orders'>
// Mutation: Pick<MutationResolvers, 'createOrder'>
}

export const resolvers: OrdersResolvers = {
Query: {
orders: {
resolve: async (_parent, _args, { getDb }) => {
const db = getDb()
const orders = await db.order.findMany({
include: { poolKey: true },
})
return orders
},
},
// TODO: Add get order by person, get order by id, get order by pool key, get order by status
},
Mutation: {
createOrder: {
resolve: async (
_parent,
{
input: {
trader,
tickToSellAt,
tokenInput,
inputAmount,
outputAmount,
tokenA,
tokenB,
hooks,
fee,
tickSpacing,
permit2Signature,
startTime,
deadline,
},
},
{ getDb }
) => {
const db = getDb()
// TODO: Create a function here to valiate inputs
// then create the pool key (or find it)
// and then create the order

// TODO: Once this is done, we should also figure out how to
// validate hooks and pool keys on chain

try {
const [token0, token1] =
tokenA < tokenB ? [tokenA, tokenB] : [tokenB, tokenA]

const zeroForOne = tokenInput === tokenA

const poolKey = await db.poolKey.findOrCreate({
where: {
token0,
token1,
fee,
tickSpacing: tickSpacing.toString(),
hooks,
token0_token1_fee_tickSpacing_hooks: {
token0,
token1,
fee,
tickSpacing: tickSpacing.toString(),
hooks,
},
},
create: {
token0,
token1,
fee,
tickSpacing: tickSpacing.toString(),
hooks,
},
update: {},
})

const _startTime = startTime || new Date()
const _deadline =
deadline || new Date(Date.now() + 604800000)

const order = await db.order.findOrCreate({
where: {
trader,
poolKeyId: poolKey.id,
trader_tickToSellAt_zeroForOne_tokenInput_startTime_poolKeyId:
{
trader,
tickToSellAt: tickToSellAt
? tickToSellAt.toString()
: '0',
zeroForOne,
startTime: _startTime,
tokenInput,
poolKeyId: poolKey.id,
},
},
create: {
trader,
tickToSellAt: tickToSellAt
? tickToSellAt.toString()
: '0',
inputAmount: inputAmount
? inputAmount.toString()
: null,
outputAmount: outputAmount
? outputAmount.toString()
: null,
tokenInput,
poolKeyId: poolKey.id,
permit2Signature,
zeroForOne,
startTime: _startTime,
deadline: _deadline,
},
update: {},
include: {
poolKey: true,
},
})
return order
} catch (error) {
console.error('Error creating order:', error)
throw new GraphQLError('Failed to create order')
}
},
},
},
// Query: {
// orders: {
// resolve: async (_parent, _args, { getDb }) => {
// const db = getDb()
// const orders = await db.order.findMany({
// include: { poolKey: true },
// })
// return orders
// },
// },
// // TODO: Add get order by person, get order by id, get order by pool key, get order by status
// },
// Mutation: {
// createOrder: {
// resolve: async (
// _parent,
// {
// input: {
// trader,
// tickToSellAt,
// tokenInput,
// inputAmount,
// outputAmount,
// tokenA,
// tokenB,
// hooks,
// fee,
// tickSpacing,
// permit2Signature,
// startTime,
// deadline,
// },
// },
// { getDb }
// ) => {
// const db = getDb()
// // TODO: Create a function here to valiate inputs
// // then create the pool key (or find it)
// // and then create the order
// // TODO: Once this is done, we should also figure out how to
// // validate hooks and pool keys on chain
// try {
// const [token0, token1] =
// tokenA < tokenB ? [tokenA, tokenB] : [tokenB, tokenA]
// const zeroForOne = tokenInput === tokenA
// const poolKey = await db.poolKey.findOrCreate({
// where: {
// token0,
// token1,
// fee,
// tickSpacing: tickSpacing.toString(),
// hooks,
// token0_token1_fee_tickSpacing_hooks: {
// token0,
// token1,
// fee,
// tickSpacing: tickSpacing.toString(),
// hooks,
// },
// },
// create: {
// token0,
// token1,
// fee,
// tickSpacing: tickSpacing.toString(),
// hooks,
// },
// update: {},
// })
// const _startTime = startTime || new Date()
// const _deadline =
// deadline || new Date(Date.now() + 604800000)
// const order = await db.order.findOrCreate({
// where: {
// trader,
// poolKeyId: poolKey.id,
// trader_tickToSellAt_zeroForOne_tokenInput_startTime_poolKeyId:
// {
// trader,
// tickToSellAt: tickToSellAt
// ? tickToSellAt.toString()
// : '0',
// zeroForOne,
// startTime: _startTime,
// tokenInput,
// poolKeyId: poolKey.id,
// },
// },
// create: {
// trader,
// tickToSellAt: tickToSellAt
// ? tickToSellAt.toString()
// : '0',
// inputAmount: inputAmount
// ? inputAmount.toString()
// : null,
// outputAmount: outputAmount
// ? outputAmount.toString()
// : null,
// tokenInput,
// poolKeyId: poolKey.id,
// permit2Signature,
// zeroForOne,
// startTime: _startTime,
// deadline: _deadline,
// },
// update: {},
// include: {
// poolKey: true,
// },
// })
// return order
// } catch (error) {
// console.error('Error creating order:', error)
// throw new GraphQLError('Failed to create order')
// }
// },
// },
// },
}
2 changes: 1 addition & 1 deletion operator/core/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pkg from 'lodash'
import { dateScalar } from './scalars'
import { DB } from '../../lib/db/model'
const { merge } = pkg
import type { Mutation, Query, Resolvers } from '@/types/resolvers'
import type { Mutation, Query, Resolvers } from '../types/resolvers'

const resolvers: Resolvers = merge(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface CreateOrderInput {
outputAmount?: InputMaybe<Scalars['String']['input']>;
permit2Signature: Scalars['String']['input'];
startTime?: InputMaybe<Scalars['ISO8601Date']['input']>;
tickSpacing: Scalars['Int']['input'];
tickSpacing: Scalars['String']['input'];
tickToSellAt?: InputMaybe<Scalars['Int']['input']>;
tokenA: Scalars['String']['input'];
tokenB: Scalars['String']['input'];
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface PoolKey {
fee: Scalars['String']['output'];
hooks: Scalars['String']['output'];
id: Scalars['String']['output'];
tickSpacing: Scalars['Int']['output'];
tickSpacing: Scalars['String']['output'];
token0: Scalars['String']['output'];
token1: Scalars['String']['output'];
}
Expand Down
Loading

0 comments on commit 0068cae

Please sign in to comment.