-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
4 changed files
with
126 additions
and
0 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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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()) | ||
} | ||
} |
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