Skip to content

Commit

Permalink
Added db things
Browse files Browse the repository at this point in the history
  • Loading branch information
mehranhydary committed Sep 13, 2024
1 parent 9457b1b commit b972834
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 132 deletions.
262 changes: 137 additions & 125 deletions operator/core/graphql/extensions/orders/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gql from 'graphql-tag'
import { MutationResolvers, QueryResolvers } from '@/types/resolvers'
import { GraphQLError } from 'graphql'
import { ZeroAddress } from 'ethers'

// TODO: Pool Key might give some details about token address and range and what
// not so ensure that you are not capturing more than you need to
Expand All @@ -9,10 +10,8 @@ import { GraphQLError } from 'graphql'
export const typeDefs = gql`
input CreateOrderInput {
trader: String!
# Can be current tick
tickToSellAt: Int
tickToSellAt: String
tokenInput: String!
# TODO: Fix this to be a Decimal or BigInt
inputAmount: String
outputAmount: String
tokenA: String!
Expand All @@ -21,6 +20,9 @@ export const typeDefs = gql`
tickSpacing: String!
hooks: String # Figure out if we need to store hooks in an array or...
permit2Signature: String!
orderSignature: String!
permit2Nonce: String!
permit2Deadline: ISO8601Date!
startTime: ISO8601Date
# Set this to a default of 1 week from now
deadline: ISO8601Date
Expand All @@ -29,7 +31,7 @@ export const typeDefs = gql`
type Order {
id: String!
trader: String!
tickToSellAt: Int!
tickToSellAt: String!
zeroForOne: Boolean!
tokenInput: String!
inputAmount: String
Expand All @@ -39,6 +41,9 @@ export const typeDefs = gql`
permit2Signature: String!
startTime: ISO8601Date!
deadline: ISO8601Date!
orderSignature: String!
permit2Nonce: String!
permit2Deadline: ISO8601Date!
}
type PoolKey {
Expand All @@ -60,128 +65,135 @@ 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: _hooks,
fee,
tickSpacing,
permit2Signature,
startTime,
deadline,
orderSignature,
permit2Nonce,
permit2Deadline,
},
},
{ 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
if (!permit2Deadline)
throw new GraphQLError('permit2Deadline is required')
const hooks = _hooks || ZeroAddress
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
: '0',
zeroForOne,
startTime: _startTime,
tokenInput,
poolKeyId: poolKey.id,
},
},
create: {
trader,
tickToSellAt: tickToSellAt ? tickToSellAt : '0',
inputAmount: inputAmount
? inputAmount.toString()
: null,
outputAmount: outputAmount
? outputAmount.toString()
: null,
tokenInput,
poolKeyId: poolKey.id,
permit2Signature,
zeroForOne,
startTime: _startTime,
deadline: _deadline,
orderSignature,
permit2Nonce,
permit2Deadline,
},
update: {},
include: {
poolKey: true,
},
})
return order
} catch (error) {
console.error('Error creating order:', error)
throw new GraphQLError('Failed to create order')
}
},
},
},
}
10 changes: 8 additions & 2 deletions operator/core/types/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ export interface CreateOrderInput {
fee: Scalars['String']['input'];
hooks?: InputMaybe<Scalars['String']['input']>;
inputAmount?: InputMaybe<Scalars['String']['input']>;
orderSignature: Scalars['String']['input'];
outputAmount?: InputMaybe<Scalars['String']['input']>;
permit2Deadline: Scalars['ISO8601Date']['input'];
permit2Nonce: Scalars['String']['input'];
permit2Signature: Scalars['String']['input'];
startTime?: InputMaybe<Scalars['ISO8601Date']['input']>;
tickSpacing: Scalars['String']['input'];
tickToSellAt?: InputMaybe<Scalars['Int']['input']>;
tickToSellAt?: InputMaybe<Scalars['String']['input']>;
tokenA: Scalars['String']['input'];
tokenB: Scalars['String']['input'];
tokenInput: Scalars['String']['input'];
Expand All @@ -44,11 +47,14 @@ export interface Order {
deadline: Scalars['ISO8601Date']['output'];
id: Scalars['String']['output'];
inputAmount?: Maybe<Scalars['String']['output']>;
orderSignature: Scalars['String']['output'];
outputAmount?: Maybe<Scalars['String']['output']>;
permit2Deadline: Scalars['ISO8601Date']['output'];
permit2Nonce: Scalars['String']['output'];
permit2Signature: Scalars['String']['output'];
poolKey: PoolKey;
startTime: Scalars['ISO8601Date']['output'];
tickToSellAt: Scalars['Int']['output'];
tickToSellAt: Scalars['String']['output'];
tokenInput: Scalars['String']['output'];
trader: Scalars['String']['output'];
zeroForOne: Scalars['Boolean']['output'];
Expand Down
Loading

0 comments on commit b972834

Please sign in to comment.