Skip to content

Commit

Permalink
create: database for the exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
berzanorg committed Dec 20, 2023
1 parent de12a0c commit bb085f6
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
23 changes: 23 additions & 0 deletions backend/src/database/Order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Field, Poseidon, PublicKey } from 'o1js'

export class Order {
maker: string
amount: number
price: number

constructor(maker: string, amount: number, price: number) {
this.maker = maker
this.amount = amount
this.price = price
}

_GetHash(): Field {
const maker = PublicKey.fromBase58(this.maker)
const amount = new Field(this.amount)
const price = new Field(this.price)

const hash = Poseidon.hash([...maker.toFields(), ...amount.toFields(), ...price.toFields()])

return hash
}
}
59 changes: 59 additions & 0 deletions backend/src/database/Pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Field, MerkleTree, Poseidon, PublicKey } from 'o1js'
import { Order } from './Order'
import { ORDERS_HEIGHT } from 'xane'

interface AddOrder {
side: 'BUY' | 'SELL'
amount: number
price: number
maker: string
}

export class Pair {
baseCurrency: string
quoteCurrency: string
buyOrders: Array<Order>
sellOrders: Array<Order>
buyOrdersTree: MerkleTree
sellOrdersTree: MerkleTree

constructor(baseCurrency: string, quoteCurrency: string) {
this.baseCurrency = baseCurrency
this.quoteCurrency = quoteCurrency
this.buyOrders = []
this.sellOrders = []
this.buyOrdersTree = new MerkleTree(ORDERS_HEIGHT)
this.sellOrdersTree = new MerkleTree(ORDERS_HEIGHT)
}

_GetHash(): Field {
const baseCurrency = PublicKey.fromBase58(this.baseCurrency)
const quoteCurrency = PublicKey.fromBase58(this.quoteCurrency)
const buyOrdersRoot = this.buyOrdersTree.getRoot()
const sellOrdersRoot = this.sellOrdersTree.getRoot()

const hash = Poseidon.hash([
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
buyOrdersRoot,
sellOrdersRoot,
])

return hash
}

_AddOrder(params: AddOrder) {
const order = new Order(params.maker, params.amount, params.price)

switch (params.side) {
case 'BUY':
this.buyOrders.push(order)
this.buyOrdersTree.setLeaf(BigInt(this.buyOrders.length), order._GetHash())
break
case 'SELL':
this.sellOrders.push(order)
this.sellOrdersTree.setLeaf(BigInt(this.sellOrders.length), order._GetHash())
break
}
}
}
41 changes: 41 additions & 0 deletions backend/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MerkleTree, PublicKey } from 'o1js'
import { ORDERS_HEIGHT, PAIRS_HEIGHT } from 'xane'
import { Pair } from './Pair'
import { Errors } from 'xane/build/src/Exchange'

export class Database {
#data: Array<Pair>
#tree: MerkleTree

constructor() {
this.#data = []
this.#tree = new MerkleTree(PAIRS_HEIGHT)
}

findPair(baseCurrency: string, quoteCurrency: string): Pair | undefined {
const index = this.#data.findIndex(
(pair) => pair.baseCurrency === baseCurrency && pair.quoteCurrency === quoteCurrency
)
return this.#data.at(index)
}

addPair(baseCurrency: string, quoteCurrency: string) {
const existingIndex = this.#data.findIndex(
(pair) => pair.baseCurrency === baseCurrency && pair.quoteCurrency === quoteCurrency
)

const existingIndexReversed = this.#data.findIndex(
(pair) => pair.baseCurrency === quoteCurrency && pair.quoteCurrency === baseCurrency
)

if (existingIndex) throw Errors.PairAlreadyExists
if (existingIndexReversed) throw Errors.PairAlreadyExists

const pairIndex = BigInt(this.#data.length)

const pair = new Pair(baseCurrency, quoteCurrency)

this.#data.push(pair)
this.#tree.setLeaf(pairIndex, pair._GetHash())
}
}
3 changes: 3 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { App } from '@tinyhttp/app'
import { Encoding } from 'o1js'
import { Token } from 'xane'
import { Database } from './database'

const database = new Database()

const app = new App()

Expand Down

0 comments on commit bb085f6

Please sign in to comment.