Skip to content

Commit

Permalink
Merge pull request #102 from Koku-Stacks/order_book
Browse files Browse the repository at this point in the history
Order book [WIP]
  • Loading branch information
ilyabukalov authored Jun 23, 2022
2 parents e814085 + 51fc782 commit eb8d09e
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contracts/futures-market.clar
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ u20
)

;; FIXME adjust to final update cooldown value
(define-constant POSITION_UPDATE_COOLDOWN u86400) ;; seconds in a day
(define-constant POSITION_UPDATE_COOLDOWN u60) ;; seconds

;; FIXME adjust to proper final value
(define-constant POSITION_MAX_DURATION u10) ;; number of days a position can be held
Expand Down
5 changes: 4 additions & 1 deletion contracts/max-heap.clar
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,7 @@
(define-public (initialize)
(begin
(populate-heap)
(ok true)))
(ok true)))

(define-public (get-orders)
(ok (map get-position HEAP_INDICES)))
5 changes: 4 additions & 1 deletion contracts/min-heap.clar
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,7 @@
(define-public (initialize)
(begin
(populate-heap)
(ok true)))
(ok true)))

(define-public (get-orders)
(ok (map get-position HEAP_INDICES)))
3 changes: 3 additions & 0 deletions integration_tests/test_pool/futures_position.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from "fs";
import * as path from "path";
import { CONTRACT_FOLDER, TRAITS_FOLDER, STACKS_API_URL } from "../config";
import { StacksChain } from "dy-finance.lib";
const POSITION_UPDATE_COOLDOWN = 60000; // ms

describe("futures position", () => {
const chain = new StacksChain(STACKS_API_URL, {
Expand Down Expand Up @@ -155,6 +156,8 @@ describe("futures position", () => {
expect(insertPosition).to.be.ok;
}

await new Promise(r => setTimeout(r, POSITION_UPDATE_COOLDOWN));

const batchPositionMaintenance = await chain.callContract(
deployer.address,
futuresMarketContractName,
Expand Down
144 changes: 144 additions & 0 deletions integration_tests/test_pool/order_book.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { uintCV } from "@stacks/transactions";
import { expect } from "chai";
import * as fs from "fs";
import * as path from "path";
import { CONTRACT_FOLDER, STACKS_API_URL } from "../config";
import { StacksChain } from "dy-finance.lib";

describe("order book", () => {
const chain = new StacksChain(STACKS_API_URL, {
defaultFee: 100000,
});

let contractAddress: string;
const maxHeapContractName = "max-heap";
const minHeapContractName = "min-heap";

before(async () => {
await chain.loadAccounts();

const deployer = chain.accounts.get("deployer")!;

const maxHeapContractCode = fs.readFileSync(
path.join(CONTRACT_FOLDER, `${maxHeapContractName}.clar`),
{ encoding: "utf8" }
);

await chain.deployContract(maxHeapContractName, maxHeapContractCode, deployer.secretKey);

const minHeapContractCode = fs.readFileSync(
path.join(CONTRACT_FOLDER, `${minHeapContractName}.clar`),
{ encoding: "utf8" }
);
const contractId = await chain.deployContract(minHeapContractName, minHeapContractCode, deployer.secretKey);

contractAddress = contractId.split(".")[0];
});

it("max heap testing", async () => {
const deployer = chain.accounts.get("deployer")!;
const INDEX_CHUNK_SIZE = 20;
const positions_to_open = INDEX_CHUNK_SIZE;

// initialize contract
await chain.callContract(
contractAddress,
maxHeapContractName,
"initialize",
[],
deployer.secretKey
);

for (let i = 1; i <= positions_to_open; i++) {
const position_size = 1;
const insertPosition = await chain.callContract(
contractAddress,
maxHeapContractName,
"max-heap-insert",
[
uintCV(i),
uintCV(position_size + i),
],
deployer.secretKey
);

expect(insertPosition).to.be.ok;
}

const all_orders_tx = await chain.callContract(
contractAddress,
maxHeapContractName,
"get-orders",
[],
deployer.secretKey
);

const all_orders = await chain.getTransactionResponse(all_orders_tx.txid);

all_orders.value.value.forEach(element => {
const price = +element.value.price.value;
const volume = +element.value.value.value;

if(price !== 0 && volume !== 0){
expect(price + 1).to.be.equal(volume);
}
else {
expect(price).to.be.equal(volume);
}
});
}).timeout(10000000);


it("min heap testing", async () => {
const deployer = chain.accounts.get("deployer")!;
const INDEX_CHUNK_SIZE = 20;
const positions_to_open = INDEX_CHUNK_SIZE;

// initialize contract
await chain.callContract(
contractAddress,
minHeapContractName,
"initialize",
[],
deployer.secretKey
);

for (let i = 1; i <= positions_to_open; i++) {
const position_size = 1;
const insertPosition = await chain.callContract(
contractAddress,
minHeapContractName,
"min-heap-insert",
[
uintCV(i),
uintCV(position_size + i),
],
deployer.secretKey
);

expect(insertPosition).to.be.ok;
}

const all_orders_tx = await chain.callContract(
contractAddress,
minHeapContractName,
"get-orders",
[],
deployer.secretKey
);

const all_orders = await chain.getTransactionResponse(all_orders_tx.txid);

all_orders.value.value.forEach(element => {
const price = +element.value.price.value;
const volume = +element.value.value.value;

if(price !== 0 && volume !== 0){
expect(price + 1).to.be.equal(volume);
}
else {
expect(price).to.be.equal(volume);
}
});
}).timeout(10000000);
});
11 changes: 11 additions & 0 deletions tests/max-heap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ function verify_priority_position(
const position: {[key: string]: string} = read_only_call.result.expectTuple() as any;

position['price'].expectUint(price);

const get_orders_call = chain.callReadOnlyFn(
'max-heap',
'get-orders',
[],
deployer.address
);

const order_list = get_orders_call.result.expectOk().expectList();
const order: {[key: string]: string} = order_list[position_index - 1].expectTuple() as any;
order['price'].expectUint(price);
}

function extract_max(chain: Chain, accounts: Map<string, Account>) {
Expand Down
11 changes: 11 additions & 0 deletions tests/min-heap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ function verify_priority_position(
const position: {[key: string]: string} = read_only_call.result.expectTuple() as any;

position['price'].expectUint(price);

const get_orders_call = chain.callReadOnlyFn(
'min-heap',
'get-orders',
[],
deployer.address
);

const order_list = get_orders_call.result.expectOk().expectList();
const order: {[key: string]: string} = order_list[position_index - 1].expectTuple() as any;
order['price'].expectUint(price);
}

function extract_min(chain: Chain, accounts: Map<string, Account>) {
Expand Down

0 comments on commit eb8d09e

Please sign in to comment.