Skip to content

Commit

Permalink
impl: backend server
Browse files Browse the repository at this point in the history
  • Loading branch information
berzanorg committed Dec 27, 2023
1 parent 8285bd4 commit 039df25
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 17 deletions.
242 changes: 231 additions & 11 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,253 @@
import { App } from '@tinyhttp/app'
import { PrivateKey } from 'o1js'
import { Database, Exchange } from 'xane'
import { Field, PrivateKey, PublicKey, Signature, UInt64 } from 'o1js'
import { AUTHORITY_PRIVATE_KEY, Database, Exchange } from 'xane'

const SAMPLE_PUBLIC_KEY = PrivateKey.random().toPublicKey()

const tokens: {
id: string
pubkey: string
}[] = []

const db = new Database()

const app = new App()

const contract = new Exchange(SAMPLE_PUBLIC_KEY)

app.post('/create-pair', (_req, res) => {
// todo
app.post('/create-token', (req, res) => {
try {
const id = Field(req.query.id).toString()
const pubkey = Field(req.query.pubkey).toString()

if (!tokens.find((a) => a.id === id)) {
tokens.push({
id,
pubkey,
})
}
res.sendStatus(200)
} catch (error) {
console.error(`/create-token`)
console.error(JSON.stringify(req))
console.error(error)
res.sendStatus(400)
}
})

app.post('/create-pair', (req, res) => {
try {
const baseCurrency = PublicKey.fromBase58(req.query.baseCurrency)
const quoteCurrency = PublicKey.fromBase58(req.query.quoteCurrency)
db.addPair({
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const pairWitness = db.getPairWitness({
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const authoritySignature = Signature.create(AUTHORITY_PRIVATE_KEY, [
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...pairWitness.toFields(),
])

res.send({
pairWitness: pairWitness.toJSON(),
authoritySignature: authoritySignature.toJSON(),
})
} catch (error) {
console.error(`/create-pair`)
console.error(JSON.stringify(req))
console.error(error)
res.sendStatus(400)
}
})

app.post('/place-order', (_req, res) => {
// todo
app.post('/place-order', (req, res) => {
try {
const baseCurrency = PublicKey.fromBase58(req.query.baseCurrency)
const quoteCurrency = PublicKey.fromBase58(req.query.quoteCurrency)
const maker = PublicKey.fromBase58(req.query.maker)
const price = UInt64.from(req.query.price)
const amount = UInt64.from(req.query.amount)
const side = req.query.side === 'BUY' ? 'BUY' : 'SELL'


const pairWitness = db.getPairWitness({
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const orderIndex = db.addOrder({
side,
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
maker: maker.toBase58(),
amount: amount.toBigInt(),
price: price.toBigInt(),
})

const orderWitness = db.getOrderWitness({
side,
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
orderIndex,
})

const ordersRoot = db.getOrdersRoot({
side: side === 'BUY' ? 'SELL' : 'BUY',
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const authoritySignature = Signature.create(AUTHORITY_PRIVATE_KEY, [
...amount.toFields(),
...price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...ordersRoot.toFields(),
...orderWitness.toFields(),
...pairWitness.toFields(),
])

res.send({
ordersRoot: ordersRoot.toJSON(),
orderWitness: orderWitness.toJSON(),
pairWitness: pairWitness.toJSON(),
authoritySignature: authoritySignature.toJSON(),
})
} catch (error) {
console.error(`/place-order`)
console.error(JSON.stringify(req))
console.error(error)
res.sendStatus(400)
}
})

app.post('/cancel-order', (_req, res) => {
// todo
app.post('/cancel-order', (req, res) => {
try {
const baseCurrency = PublicKey.fromBase58(req.query.baseCurrency)
const quoteCurrency = PublicKey.fromBase58(req.query.quoteCurrency)
const price = UInt64.from(req.query.price)
const amount = UInt64.from(req.query.amount)
const orderIndex = parseInt(req.query.index)
const side = req.query.side === 'BUY' ? 'BUY' : 'SELL'

const pairWitness = db.getPairWitness({
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const orderWitness = db.getOrderWitness({
side,
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
orderIndex,
})

db.removeOrder({
side,
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
orderIndex,
})

const ordersRoot = db.getOrdersRoot({
side: side === 'BUY' ? 'SELL' : 'BUY',
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const authoritySignature = Signature.create(AUTHORITY_PRIVATE_KEY, [
...amount.toFields(),
...price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...ordersRoot.toFields(),
...orderWitness.toFields(),
...pairWitness.toFields(),
])

res.send({
ordersRoot: ordersRoot.toJSON(),
orderWitness: orderWitness.toJSON(),
pairWitness: pairWitness.toJSON(),
authoritySignature: authoritySignature.toJSON(),
})
} catch (error) {
console.error(`/cancel-order`)
console.error(JSON.stringify(req))
console.error(error)
res.sendStatus(400)
}
})

app.post('/execute-order', (_req, res) => {
// todo
app.post('/execute-order', (req, res) => {
try {
const baseCurrency = PublicKey.fromBase58(req.query.baseCurrency)
const quoteCurrency = PublicKey.fromBase58(req.query.quoteCurrency)
const maker = PublicKey.fromBase58(req.query.maker)
const price = UInt64.from(req.query.price)
const amount = UInt64.from(req.query.amount)
const orderIndex = parseInt(req.query.index)
const side = req.query.side === 'BUY' ? 'BUY' : 'SELL'

const pairWitness = db.getPairWitness({
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const ordersRoot = db.getOrdersRoot({
side: side === 'BUY' ? 'SELL' : 'BUY',
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
})

const orderWitness = db.getOrderWitness({
side,
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
orderIndex,
})

db.removeOrder({
side,
baseCurrency: baseCurrency.toBase58(),
quoteCurrency: quoteCurrency.toBase58(),
orderIndex,
})

const authoritySignature = Signature.create(AUTHORITY_PRIVATE_KEY, [
...maker.toFields(),
...amount.toFields(),
...price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...ordersRoot.toFields(),
...orderWitness.toFields(),
...pairWitness.toFields(),
])

res.send({
ordersRoot: ordersRoot.toJSON(),
orderWitness: orderWitness.toJSON(),
pairWitness: pairWitness.toJSON(),
authoritySignature: authoritySignature.toJSON()
})
} catch (error) {
console.error(`/execute-order`)
console.error(JSON.stringify(req))
console.error(error)
res.sendStatus(400)
}
})

app.get('/order-book', (_req, res) => {
// todo
res.send(db.data())
})

app.listen(3000, () => console.log('Started on http://localhost:3000'))
4 changes: 2 additions & 2 deletions contracts/src/Exchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ describe('token vault test', () => {
...BUY_ORDER_PRICE.toFields(),
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...buyOrderWitness.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])

Expand Down Expand Up @@ -297,8 +297,8 @@ describe('token vault test', () => {
...BUY_ORDER_PRICE.toFields(),
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...buyOrderWitness.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])

Expand Down
6 changes: 3 additions & 3 deletions contracts/src/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ export class Exchange extends SmartContract {
...price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...buyOrderWitness.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])
.assertTrue(Errors.InvalidSignature)
Expand Down Expand Up @@ -339,8 +339,8 @@ export class Exchange extends SmartContract {
...price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...buyOrderWitness.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])
.assertTrue(Errors.InvalidSignature)
Expand Down Expand Up @@ -467,8 +467,8 @@ export class Exchange extends SmartContract {
...price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...buyOrderWitness.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])
.assertTrue(Errors.InvalidSignature)
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/common/database/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ export class Database {

return pair._GetOrdersRoot(params)
}

data() {
return this.#data
}
}
3 changes: 2 additions & 1 deletion contracts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Vault } from './Vault.js'
export { Token } from './Token.js'
export { Exchange } from './Exchange.js'
export { Exchange, AUTHORITY_PRIVATE_KEY } from './Exchange.js'
export { Database } from './common/database/index.js'

0 comments on commit 039df25

Please sign in to comment.