-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28ac1cc
commit 7ad2c53
Showing
1 changed file
with
67 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,70 @@ | ||
import { pgTable, text } from 'drizzle-orm/pg-core'; | ||
import { bigint, index, pgTable, primaryKey, smallint, smallserial, varchar } from 'drizzle-orm/pg-core'; | ||
|
||
// TODO: table(s) representing order book for each market. | ||
|
||
// https://docs.rs/solana-program/latest/src/solana_program/pubkey.rs.html#24 | ||
const MAX_PUBKEY_B58_STR_LEN = 44; | ||
const pubkey = (columnName: string) => varchar(columnName, {length: MAX_PUBKEY_B58_STR_LEN}); | ||
|
||
const MAX_TRANSACTION_B58_STR_LEN = 88; | ||
const transaction = (columnName: string) => varchar(columnName, {length: MAX_TRANSACTION_B58_STR_LEN}); | ||
|
||
const tokenAmount = (columnName: string) => bigint(columnName, {mode: 'bigint'}); | ||
|
||
const timestamp = (columnName: string) => bigint(columnName, {mode: 'bigint'}); | ||
|
||
export const proposals = pgTable('proposals', { | ||
id: text('id').primaryKey(), | ||
proposalId: pubkey('proposal_id').primaryKey(), | ||
autocratVersion: smallint('autocrat_version').notNull() | ||
}); | ||
|
||
export enum MarketType { | ||
OPEN_BOOK = 'OPEN_BOOK' | ||
} | ||
|
||
type NonEmptyList<E> = [E, ...E[]]; | ||
|
||
export const markets = pgTable('markets', { | ||
marketId: pubkey('market_id').primaryKey(), | ||
// may be null as market might not be tied to any one proposal (ex: the META spot market) | ||
proposalId: pubkey('proposal_id').references(() => proposals.proposalId), | ||
marketType: varchar('market_type', {enum: Object.values(MarketType) as NonEmptyList<MarketType>}).notNull(), | ||
baseCurrencyMint: pubkey('base_currency_mint').references(() => tokens.tokenMint).notNull(), | ||
quoteCurrencyMint: pubkey('quote_currency_mint').references(() => tokens.tokenMint).notNull(), | ||
}); | ||
|
||
export const tokens = pgTable('tokens', { | ||
tokenMint: pubkey('token_mint').primaryKey(), | ||
name: varchar('name', {length: 30}).notNull(), | ||
symbol: varchar('symbol', {length: 10}).notNull(), | ||
decimals: smallserial('decimals').notNull() | ||
}); | ||
|
||
export const trades = pgTable('trades', { | ||
transactionId: transaction('transaction_id').primaryKey(), | ||
slot: bigint('slot', {mode: 'bigint'}).notNull(), | ||
block: bigint('block', {mode: 'bigint'}).notNull(), | ||
time: timestamp('time').notNull(), | ||
marketId: pubkey('market_id').references(() => markets.marketId).notNull(), | ||
takerId: pubkey('taker_id').notNull(), | ||
baseAmount: tokenAmount('base_amount').notNull(), | ||
quotePrice: tokenAmount('quote_price').notNull(), | ||
}, table => ({ | ||
slotIdx: index('slot_index').on(table.marketId, table.slot), | ||
blockIdx: index('block_index').on(table.marketId, table.block), | ||
timeIdx: index('time_index').on(table.marketId, table.time) | ||
})); | ||
|
||
export const candles = pgTable('candles', { | ||
marketId: pubkey('market_id').references(() => markets.marketId).notNull(), | ||
// increments by minute | ||
timestamp: timestamp('timestamp').notNull(), | ||
high: tokenAmount('high'), | ||
low: tokenAmount('low'), | ||
close: tokenAmount('close'), | ||
volume: tokenAmount('volume').notNull(), | ||
// time-weighted average | ||
average: tokenAmount('average'), | ||
}, table => ({ | ||
pk: primaryKey(table.marketId, table.timestamp) | ||
})); |