Skip to content

Commit

Permalink
Add load tests and refactor orderbook
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Campos committed Jun 17, 2024
1 parent a43683d commit 1c5d071
Show file tree
Hide file tree
Showing 12 changed files with 1,113 additions and 28 deletions.
2 changes: 1 addition & 1 deletion contracts/orderbook/hts/ExchangeHTS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract ExchangeHTS is OrderBookHTS, ReentrancyGuard {
_insertBuyOrder(msg.sender, price, remainVolume);
}

return 0;
return currentOrderId;
}

/**
Expand Down
14 changes: 10 additions & 4 deletions contracts/orderbook/hts/OrderBookHTS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ abstract contract OrderBookHTS {

event Trade(int64 tradedVolume, int64 price, address buyer, address seller);
event NewOrder(bool isBuy, uint256 orderId, address trader, int64 price, int64 volume);
event Deposit(address indexed trader, address token, int64 amount);
event Deposit(address trader, address token, int64 amount);
event Withdraw(address trader, address token, int64 amount);
event OrderCanceled(bool isBuy, uint256 indexed orderId, address indexed trader);
event OrderCanceled(bool isBuy, uint256 orderId, address trader);

struct Order {
uint256 id;
Expand Down Expand Up @@ -93,8 +93,10 @@ abstract contract OrderBookHTS {
int64 sellVolume
) internal returns (int64) {
uint256 currentBuyId = firstBuyOrderId;
uint256 maxIterations = 10; // Define a maximum number of iterations per call
uint256 iterations = 0;

while (currentBuyId != 0 && sellVolume > 0) {
while (currentBuyId != 0 && sellVolume > 0 && iterations < maxIterations) {
Order storage buyOrder = buyOrders[currentBuyId];

if (sellPrice <= buyOrder.price) {
Expand All @@ -118,6 +120,7 @@ abstract contract OrderBookHTS {
} else {
break;
}
iterations++;
}

firstBuyOrderId = currentBuyId; // Update the first buy order ID
Expand All @@ -130,8 +133,10 @@ abstract contract OrderBookHTS {
int64 buyVolume
) internal returns (int64) {
uint256 currentSellId = firstSellOrderId;
uint256 maxIterations = 10; // Define a maximum number of iterations per call
uint256 iterations = 0;

while (currentSellId != 0 && buyVolume > 0) {
while (currentSellId != 0 && buyVolume > 0 && iterations < maxIterations) {
Order storage sellOrder = sellOrders[currentSellId];

if (buyPrice >= sellOrder.price) {
Expand All @@ -155,6 +160,7 @@ abstract contract OrderBookHTS {
} else {
break;
}
iterations++;
}

firstSellOrderId = currentSellId; // Update the first sell order ID
Expand Down
123 changes: 123 additions & 0 deletions data/abis/ExchangeFactory.json

Large diffs are not rendered by default.

123 changes: 123 additions & 0 deletions data/abis/ExchangeFactoryHTS.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions data/abis/ExchangeHTS.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions data/abis/OrderBookHTS.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"anonymous": false,
"inputs": [
{
"indexed": true,
"indexed": false,
"internalType": "address",
"name": "trader",
"type": "address"
Expand Down Expand Up @@ -75,13 +75,13 @@
"type": "bool"
},
{
"indexed": true,
"indexed": false,
"internalType": "uint256",
"name": "orderId",
"type": "uint256"
},
{
"indexed": true,
"indexed": false,
"internalType": "address",
"name": "trader",
"type": "address"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.2.0",
"@types/elliptic": "^6.4.18",
"@types/mocha": "^9.1.0",
"@types/node": "^18.0.0",
"chai": "^4.2.0",
"dotenv": "^16.4.5",
"elliptic": "^6.5.5",
"eslint": "^8.57.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "^2.29.1",
Expand Down
Loading

0 comments on commit 1c5d071

Please sign in to comment.