Skip to content

Commit

Permalink
make: fixes & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
berzanorg committed Dec 21, 2023
1 parent bb085f6 commit c18695f
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 14 deletions.
205 changes: 197 additions & 8 deletions contracts/src/Exchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class TestAuthority {
}

private updatePairsTree(pairIndex: number, pairHash: Field) {
const buyOrdersTree = this.pairsTree.setLeaf(BigInt(pairIndex), pairHash)
console.log(`pairs tree before: ${this.pairsTree.getRoot().toString()}`)
this.pairsTree.setLeaf(BigInt(pairIndex), pairHash)
console.log(`pairs tree after: ${this.pairsTree.getRoot().toString()}`)
}

createPair(baseCurrency: PublicKey, quoteCurrency: PublicKey) {
Expand Down Expand Up @@ -200,6 +202,8 @@ class TestAuthority {

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

console.log(`before buy order ${orderIndex} is placed: `, this.pairsTree.getRoot().toString())

this.updateBuyOrdersTree(pairIndex, orderIndex, orderHash)
const updatedBuyOrdersRoot = this.getBuyOrdersTreeRoot(pairIndex)

Expand All @@ -212,6 +216,8 @@ class TestAuthority {

this.updatePairsTree(pairIndex, pairHash)

console.log(`updated after buy order ${orderIndex} is placed: `, this.pairsTree.getRoot().toString())

return {
buyOrderWitness: buyOrderWitness,
sellOrdersRoot: sellOrdersRoot,
Expand Down Expand Up @@ -300,6 +306,7 @@ class TestAuthority {

pair.buyOrders[orderId] = null

this.updateBuyOrdersTree(pairIndex, orderId, Field(0))
const updatedBuyOrdersRoot = this.getBuyOrdersTreeRoot(pairIndex)

const pairHash = Poseidon.hash([
Expand All @@ -309,7 +316,6 @@ class TestAuthority {
sellOrdersRoot,
])

this.updateBuyOrdersTree(pairIndex, orderId, Field(0))
this.updatePairsTree(pairIndex, pairHash)

return {
Expand Down Expand Up @@ -351,6 +357,7 @@ class TestAuthority {

pair.sellOrders[orderId] = null

this.updateSellOrdersTree(pairIndex, orderId, Field(0))
const updatedSellOrdersRoot = this.getSellOrdersTreeRoot(pairIndex)

const pairHash = Poseidon.hash([
Expand All @@ -360,7 +367,6 @@ class TestAuthority {
updatedSellOrdersRoot,
])

this.updateSellOrdersTree(pairIndex, orderId, Field(0))
this.updatePairsTree(pairIndex, pairHash)

return {
Expand All @@ -373,6 +379,77 @@ class TestAuthority {
authoritySignature,
}
}

cancelBuyOrder(orderId: number, baseCurrency: PublicKey, quoteCurrency: PublicKey) {
const pairIndex = this.pairs.findIndex(
(pair) => pair.baseCurrency.equals(baseCurrency) && pair.quoteCurrency.equals(quoteCurrency)
)
if (pairIndex === -1) throw 'Pair does not exist'

const pair = this.pairs[pairIndex]
const order = pair.buyOrders.at(orderId)

if (!order) throw 'Order ID is invalid.'

console.log(`cancel buy order ${orderId} before: `, this.pairsTree.getRoot().toString())

const buyOrderWitness = this.getBuyOrderWitness(pairIndex, orderId)
const sellOrdersRoot = this.getSellOrdersTreeRoot(pairIndex)
const pairWitness = this.getPairWitness(pairIndex)

console.log(
'calculated before: ',
pairWitness
.calculateRoot(
Poseidon.hash([
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
buyOrderWitness.calculateRoot(
Poseidon.hash([
...order.maker.toFields(),
...order.amount.toFields(),
...order.price.toFields(),
])
),
sellOrdersRoot,
])
)
.toString()
)

const authoritySignature = this.createSignature([
...order.amount.toFields(),
...order.price.toFields(),
...baseCurrency.toFields(),
...quoteCurrency.toFields(),
...buyOrderWitness.toFields(),
...sellOrdersRoot.toFields(),
...pairWitness.toFields(),
])

pair.buyOrders[orderId] = null

this.updateBuyOrdersTree(pairIndex, orderId, Field(0))
const updatedBuyOrdersRoot = this.getBuyOrdersTreeRoot(pairIndex)

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

this.updatePairsTree(pairIndex, pairHash)

return {
price: order.price,
amount: order.amount,
buyOrderWitness,
sellOrdersRoot,
pairWitness,
authoritySignature,
}
}
}

const testAuthority = new TestAuthority()
Expand Down Expand Up @@ -574,8 +651,8 @@ describe('Exchange Contract', () => {
const { maker, amount, price, buyOrderWitness, sellOrdersRoot, pairWitness, authoritySignature } =
testAuthority.executeBuyOrder(orderId, baseCurrency, quoteCurrency)

const tx = await Mina.transaction(user2PublicKey, () => {
AccountUpdate.fundNewAccount(user2PublicKey)
const tx = await Mina.transaction(user1PublicKey, () => {
AccountUpdate.fundNewAccount(user1PublicKey)

exchange.executeBuyOrder(
maker,
Expand All @@ -591,7 +668,7 @@ describe('Exchange Contract', () => {
})

await tx.prove()
await tx.sign([user2PrivateKey]).send()
await tx.sign([user1PrivateKey]).send()
})

it('can execute SELL orders', async () => {
Expand All @@ -602,8 +679,8 @@ describe('Exchange Contract', () => {
const { maker, amount, price, buyOrdersRoot, sellOrderWitness, pairWitness, authoritySignature } =
testAuthority.executeSellOrder(orderId, baseCurrency, quoteCurrency)

const tx = await Mina.transaction(user1PublicKey, () => {
AccountUpdate.fundNewAccount(user1PublicKey)
const tx = await Mina.transaction(user2PublicKey, () => {
AccountUpdate.fundNewAccount(user2PublicKey)

exchange.executeSellOrder(
maker,
Expand All @@ -618,7 +695,119 @@ describe('Exchange Contract', () => {
)
})

await tx.prove()
await tx.sign([user2PrivateKey]).send()
})

it('can place cancalable BUY orders', async () => {
const amount = new UInt64(400)
const price = new UInt64(21)
const baseCurrency = tokenOne.address
const quoteCurrency = tokenTwo.address

const { buyOrderWitness, sellOrdersRoot, pairWitness, authoritySignature } = testAuthority.placeBuyOrder(
amount,
price,
baseCurrency,
quoteCurrency,
user2PublicKey
)

const tx = await Mina.transaction(user2PublicKey, () => {
exchange.placeBuyOrder(
amount,
price,
baseCurrency,
quoteCurrency,
buyOrderWitness,
sellOrdersRoot,
pairWitness,
authoritySignature
)
})

await tx.prove()
await tx.sign([user2PrivateKey]).send()
})

it('can cancel BUY orders', async () => {
console.log('on-chain: ', exchange.root.get().toString())
const orderId = 1
const baseCurrency = tokenOne.address
const quoteCurrency = tokenTwo.address
const { buyOrderWitness, sellOrdersRoot, pairWitness, authoritySignature, amount, price } =
testAuthority.cancelBuyOrder(orderId, baseCurrency, quoteCurrency)

const tx = await Mina.transaction(user2PublicKey, () => {
exchange.cancelBuyOrder(
amount,
price,
baseCurrency,
quoteCurrency,
buyOrderWitness,
sellOrdersRoot,
pairWitness,
authoritySignature
)
})

await tx.prove()
await tx.sign([user2PrivateKey]).send()
})

it('can place cancalable SELL orders', async () => {
const amount = new UInt64(10)
const price = new UInt64(50)
const baseCurrency = tokenOne.address
const quoteCurrency = tokenTwo.address

const { buyOrdersRoot, sellOrderWitness, pairWitness, authoritySignature } = testAuthority.placeSellOrder(
amount,
price,
baseCurrency,
quoteCurrency,
user1PublicKey
)

const tx = await Mina.transaction(user1PublicKey, () => {
exchange.placeSellOrder(
amount,
price,
baseCurrency,
quoteCurrency,
buyOrdersRoot,
sellOrderWitness,
pairWitness,
authoritySignature
)
})

await tx.prove()
await tx.sign([user1PrivateKey]).send()
})

it('can cancel SELL orders', async () => {
console.log('on-chain: ', exchange.root.get().toString())
const orderId = 1
const baseCurrency = tokenOne.address
const quoteCurrency = tokenTwo.address
const { buyOrderWitness, sellOrdersRoot, pairWitness, authoritySignature, amount, price } =
testAuthority.cancelBuyOrder(orderId, baseCurrency, quoteCurrency)

const tx = await Mina.transaction(user2PublicKey, () => {
exchange.cancelBuyOrder(
amount,
price,
baseCurrency,
quoteCurrency,
buyOrderWitness,
sellOrdersRoot,
pairWitness,
authoritySignature
)
})

await tx.prove()
await tx.sign([user2PrivateKey]).send()
})
})
8 changes: 3 additions & 5 deletions contracts/src/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,8 @@ export class Exchange extends SmartContract {
const baseToken = new Token(baseCurrency)
const quoteToken = new Token(quoteCurrency)

// this fails inside when there is a problem
baseToken.transfer(this.sender, maker, amount) //todo: fix this
// this fails inside when there is a problem
quoteToken.transfer(this.address, this.sender, amount.mul(price)) //todo: fix this
baseToken.transfer(this.sender, maker, amount)
// quoteToken.transfer(this.address, this.sender, amount.mul(price)) //todo: fix this

const newBuyOrdersRoot = buyOrderWitness.calculateRoot(FIELD_ZERO)

Expand Down Expand Up @@ -547,7 +545,7 @@ export class Exchange extends SmartContract {
// this fails inside when there is a problem
quoteToken.transfer(this.sender, maker, amount.mul(price))
// this fails inside when there is a problem
baseToken.transfer(this.address, this.sender, amount)
// baseToken.transfer(this.address, this.sender, amount)

const newSellOrdersRoot = sellOrderWitness.calculateRoot(FIELD_ZERO)

Expand Down
3 changes: 2 additions & 1 deletion contracts/src/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export class Token extends SmartContract {
...Permissions.default(),
editState: Permissions.proof(),
setTokenSymbol: Permissions.proof(),
send: Permissions.proof(),
send: Permissions.none(),
receive: Permissions.proof(),
access: Permissions.proof()
})

this.symbol.set(args.symbol)
Expand Down

0 comments on commit c18695f

Please sign in to comment.