Skip to content

Commit

Permalink
add: new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
berzanorg committed Dec 24, 2023
1 parent 7def29b commit 75bfeb1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 17 deletions.
79 changes: 71 additions & 8 deletions contracts/src/common/database/Database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MerkleTree } from 'o1js'
import { Pair } from './Pair.js'
import { DatabaseError } from './DatabaseError.js'
import { PAIRS_HEIGHT } from '../../Exchange.js'
import { OrderWitness, PAIRS_HEIGHT, PairWitness } from '../../Exchange.js'

interface FindPairIndex {
baseCurrency: string
Expand All @@ -17,8 +17,8 @@ interface AddOrder {
baseCurrency: string
quoteCurrency: string
side: 'BUY' | 'SELL'
amount: number
price: number
amount: bigint
price: bigint
maker: string
}

Expand All @@ -29,6 +29,31 @@ interface RemoveOrder {
orderIndex: number
}

interface GetPairWitness {
baseCurrency: string
quoteCurrency: string
}

interface GetOrderWitness {
baseCurrency: string
quoteCurrency: string
side: 'BUY' | 'SELL'
orderIndex: number
}

interface GetOrdersRoot {
baseCurrency: string
quoteCurrency: string
side: 'BUY' | 'SELL'
}

interface GetOrder {
baseCurrency: string
quoteCurrency: string
side: 'BUY' | 'SELL'
orderIndex: number
}

export class Database {
#data: Array<Pair>
#tree: MerkleTree
Expand Down Expand Up @@ -66,11 +91,11 @@ export class Database {

const pair = this.#data[pairIndex]

if (!pair) throw DatabaseError.PairDoesntExist

pair._AddOrder(params)
const orderIndex = pair._AddOrder(params)

this.#tree.setLeaf(BigInt(pairIndex), pair._GetHash())

return orderIndex
}

removeOrder(params: RemoveOrder) {
Expand All @@ -80,10 +105,48 @@ export class Database {

const pair = this.#data[pairIndex]

if (!pair) throw DatabaseError.PairDoesntExist

pair._RemoveOrder(params)

this.#tree.setLeaf(BigInt(pairIndex), pair._GetHash())
}

getOrder(params: GetOrder) {
const pairIndex = this.findPairIndex(params)

if (pairIndex === undefined) throw DatabaseError.PairDoesntExist

const pair = this.#data[pairIndex]

return pair._GetOrder(params)
}

getPairWitness(params: GetPairWitness) {
const pairIndex = this.findPairIndex(params)

if (pairIndex === undefined) throw DatabaseError.PairDoesntExist

const witness = this.#tree.getWitness(BigInt(pairIndex))

return new PairWitness(witness)
}

getOrderWitness(params: GetOrderWitness) {
const pairIndex = this.findPairIndex(params)

if (pairIndex === undefined) throw DatabaseError.PairDoesntExist

const pair = this.#data[pairIndex]

return pair._GetOrderWitness(params)
}

getOrdersRoot(params: GetOrdersRoot) {
const pairIndex = this.findPairIndex(params)

if (pairIndex === undefined) throw DatabaseError.PairDoesntExist

const pair = this.#data[pairIndex]

return pair._GetOrdersRoot(params)
}
}
6 changes: 3 additions & 3 deletions contracts/src/common/database/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Field, Poseidon, PublicKey } from 'o1js'

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

constructor(maker: string, amount: number, price: number) {
constructor(maker: string, amount: bigint, price: bigint) {
this.maker = maker
this.amount = amount
this.price = price
Expand Down
62 changes: 56 additions & 6 deletions contracts/src/common/database/Pair.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Field, MerkleTree, Poseidon, PublicKey } from 'o1js'
import { Order } from './Order.js'
import { ORDERS_HEIGHT } from '../../Exchange.js'
import { ORDERS_HEIGHT, OrderWitness } from '../../Exchange.js'
import { DatabaseError } from './DatabaseError.js'

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

Expand All @@ -18,6 +19,20 @@ interface RemoveOrder {
orderIndex: number
}

interface GetOrderWitness {
side: 'BUY' | 'SELL'
orderIndex: number
}

interface GetOrdersRoot {
side: 'BUY' | 'SELL'
}

interface GetOrder {
side: 'BUY' | 'SELL'
orderIndex: number
}

export class Pair {
baseCurrency: string
quoteCurrency: string
Expand Down Expand Up @@ -63,20 +78,20 @@ export class Pair {
return orders.length
}

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

switch (params.side) {
case 'BUY':
const emptyBuyOrderIndex = this._FindEmptyOrderSlot({ side: 'BUY' })
this.buyOrders[emptyBuyOrderIndex] = order
this.buyOrdersTree.setLeaf(BigInt(emptyBuyOrderIndex), order._GetHash())
break
return emptyBuyOrderIndex
case 'SELL':
const emptySellOrderIndex = this._FindEmptyOrderSlot({ side: 'BUY' })
this.sellOrders[emptySellOrderIndex] = order
this.sellOrdersTree.setLeaf(BigInt(emptySellOrderIndex), order._GetHash())
break
return emptySellOrderIndex
}
}

Expand All @@ -92,4 +107,39 @@ export class Pair {
break
}
}

_GetOrder(params: GetOrder) {
switch (params.side) {
case 'BUY':
const buyOrder = this.buyOrders.at(params.orderIndex)
if (buyOrder === undefined) throw DatabaseError.OrderDoesntExist
return buyOrder
case 'SELL':
const sellOrder = this.sellOrders.at(params.orderIndex)
if (sellOrder === undefined) throw DatabaseError.OrderDoesntExist
return sellOrder
}
}

_GetOrderWitness(params: GetOrderWitness) {
switch (params.side) {
case 'BUY':
if (this.buyOrders.at(params.orderIndex) === undefined) throw DatabaseError.OrderDoesntExist
const buyOrderWitness = this.buyOrdersTree.getWitness(BigInt(params.orderIndex))
return new OrderWitness(buyOrderWitness)
case 'SELL':
if (this.sellOrders.at(params.orderIndex) === undefined) throw DatabaseError.OrderDoesntExist
const sellOrderWitness = this.sellOrdersTree.getWitness(BigInt(params.orderIndex))
return new OrderWitness(sellOrderWitness)
}
}

_GetOrdersRoot(params: GetOrdersRoot) {
switch (params.side) {
case 'BUY':
return this.buyOrdersTree.getRoot()
case 'SELL':
return this.sellOrdersTree.getRoot()
}
}
}

0 comments on commit 75bfeb1

Please sign in to comment.