Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bokkypoobah committed Oct 11, 2024
1 parent f09efc2 commit ec03c75
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
18 changes: 13 additions & 5 deletions contracts/Chadex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import "./BokkyPooBahsRedBlackTreeLibrary.sol";


type Account is address; // 2^160
type AccountIndex is uint40; // 2^40 = 1,099,511,627,776
type AccountIndex is uint32; // 2^32 = 4,294,967,296
type Decimals is uint8; // 2^8
type Nonce is uint24; // 2^24 = 16,777,216
type OfferIndex is uint32; // 2^32 = 4,294,967,296
type OrderKey is bytes32; // 2^256
type PairKey is bytes32; // 2^256
type Token is address; // 2^160
Expand Down Expand Up @@ -94,11 +96,15 @@ contract ChadexBase {
Unixtime expiry; // 2^40
Tokens tokens; // 2^128
}
struct NewOrder {
AccountIndex maker; // 2^40
struct Offer {
AccountIndex maker; // 2^32
Unixtime expiry; // 2^40
Tokens tokens; // 2^128
}
struct Offers {
OfferIndex head; // 2^32
Offer[] offers;
}
struct TradeInput {
Action action; // 2^8
BuySell buySell; // 2^8
Expand Down Expand Up @@ -145,9 +151,11 @@ contract ChadexBase {
mapping(PairKey => mapping(BuySell => mapping(Price => OrderQueue))) orderQueues;
mapping(OrderKey => Order) orders;

mapping(Account => AccountIndex) accountToIndex; // Note that this will be the index + 1
mapping(Account => AccountIndex) accountToIndex; // Note that we will use index + 1 for array below
Account[] indexToAccount;

mapping(PairKey => mapping(BuySell => mapping(Price => Offers))) offers;

event PairAdded(PairKey indexed pairKey, Account maker, Token indexed base, Token indexed quote, Decimals[2] decimalss, Unixtime timestamp);
event OrderAdded(PairKey indexed pairKey, OrderKey indexed orderKey, Account indexed maker, BuySell buySell, Price price, Unixtime expiry, Tokens baseTokens, Tokens quoteTokens, Unixtime timestamp);
event OrderRemoved(PairKey indexed pairKey, OrderKey indexed orderKey, Account indexed maker, BuySell buySell, Price price, Tokens baseTokens, Unixtime timestamp);
Expand Down Expand Up @@ -315,7 +323,7 @@ contract Chadex is ChadexBase, ReentrancyGuard {
AccountIndex msgSender = accountToIndex[Account.wrap(msg.sender)];
if (AccountIndex.unwrap(msgSender) == 0) {
indexToAccount.push(Account.wrap(msg.sender));
msgSender = AccountIndex.wrap(uint40(indexToAccount.length));
msgSender = AccountIndex.wrap(uint32(indexToAccount.length));
accountToIndex[Account.wrap(msg.sender)] = msgSender;
}

Expand Down
69 changes: 35 additions & 34 deletions testIt.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Compiled 1 Solidity file successfully (evm target: cancun).


Chadex
Expand Down Expand Up @@ -72,7 +73,7 @@
],
"price": "690100000",
"targetPrice": "690100000",
"expiry": 1728612815,
"expiry": 1728620784,
"tokens": "1000000000000000000",
"skipCheck": false
},
Expand All @@ -85,7 +86,7 @@
],
"price": "690200000",
"targetPrice": "690200000",
"expiry": 1728612815,
"expiry": 1728620784,
"tokens": "2000000000000000000",
"skipCheck": false
},
Expand All @@ -98,33 +99,33 @@
],
"price": "690300000",
"targetPrice": "690300000",
"expiry": 1728612815,
"expiry": 1728620784,
"tokens": "3000000000000000000",
"skipCheck": false
}
]
> user0->chadex.execute(actionsA) - gasUsed: 665657 ~ ETH 0.00665657 ~ USD 16
+ Chadex:0xCf7E PairAdded(pairKey: 0x2c831da670a13afa44, maker: user0:0x7099, base: Token0:0x5FbD, quote: WETH:0x9fE4, decimalss: [18,18], timestamp: 1728609243)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xba50bff8ca4fde4978, maker: user0:0x7099, buySell: 0, price: 0.6901, expiry: 1728612815, baseTokens: 1.0, quoteTokens: 0.6901, timestamp: 1728609243)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x16ee2eb820c1dc86c5, maker: user0:0x7099, buySell: 0, price: 0.6902, expiry: 1728612815, baseTokens: 2.0, quoteTokens: 1.3804, timestamp: 1728609243)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x216071ecd309e02f0a, maker: user0:0x7099, buySell: 0, price: 0.6903, expiry: 1728612815, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728609243)
+ Chadex:0xCf7E PairAdded(pairKey: 0x2c831da670a13afa44, maker: user0:0x7099, base: Token0:0x5FbD, quote: WETH:0x9fE4, decimalss: [18,18], timestamp: 1728617211)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xba50bff8ca4fde4978, maker: user0:0x7099, buySell: 0, price: 0.6901, expiry: 1728620784, baseTokens: 1.0, quoteTokens: 0.6901, timestamp: 1728617211)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x16ee2eb820c1dc86c5, maker: user0:0x7099, buySell: 0, price: 0.6902, expiry: 1728620784, baseTokens: 2.0, quoteTokens: 1.3804, timestamp: 1728617211)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x216071ecd309e02f0a, maker: user0:0x7099, buySell: 0, price: 0.6903, expiry: 1728620784, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728617211)
> user1->chadex.execute(actionsA) - gasUsed: 376296 ~ ETH 0.00376296 ~ USD 9
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x57e1bbde3a5a6e6ba5, maker: user1:0x3C44, buySell: 0, price: 0.6901, expiry: 1728612815, baseTokens: 1.0, quoteTokens: 0.6901, timestamp: 1728609244)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x4fcf79cf98e01a03f6, maker: user1:0x3C44, buySell: 0, price: 0.6902, expiry: 1728612815, baseTokens: 2.0, quoteTokens: 1.3804, timestamp: 1728609244)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xc4fd7db027bc75d73f, maker: user1:0x3C44, buySell: 0, price: 0.6903, expiry: 1728612815, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728609244)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x57e1bbde3a5a6e6ba5, maker: user1:0x3C44, buySell: 0, price: 0.6901, expiry: 1728620784, baseTokens: 1.0, quoteTokens: 0.6901, timestamp: 1728617212)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x4fcf79cf98e01a03f6, maker: user1:0x3C44, buySell: 0, price: 0.6902, expiry: 1728620784, baseTokens: 2.0, quoteTokens: 1.3804, timestamp: 1728617212)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xc4fd7db027bc75d73f, maker: user1:0x3C44, buySell: 0, price: 0.6903, expiry: 1728620784, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728617212)
> user2->chadex.execute(actionsA) - gasUsed: 376296 ~ ETH 0.00376296 ~ USD 9
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x452f99950d915502ce, maker: user2:0x90F7, buySell: 0, price: 0.6901, expiry: 1728612815, baseTokens: 1.0, quoteTokens: 0.6901, timestamp: 1728609245)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xdc9dfdc4362c69200e, maker: user2:0x90F7, buySell: 0, price: 0.6902, expiry: 1728612815, baseTokens: 2.0, quoteTokens: 1.3804, timestamp: 1728609245)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xe7bb22970ed8b3926f, maker: user2:0x90F7, buySell: 0, price: 0.6903, expiry: 1728612815, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728609245)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0x452f99950d915502ce, maker: user2:0x90F7, buySell: 0, price: 0.6901, expiry: 1728620784, baseTokens: 1.0, quoteTokens: 0.6901, timestamp: 1728617213)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xdc9dfdc4362c69200e, maker: user2:0x90F7, buySell: 0, price: 0.6902, expiry: 1728620784, baseTokens: 2.0, quoteTokens: 1.3804, timestamp: 1728617213)
+ Chadex:0xCf7E OrderAdded(pairKey: 0x2c831da670a13afa44, orderKey: 0xe7bb22970ed8b3926f, maker: user2:0x90F7, buySell: 0, price: 0.6903, expiry: 1728620784, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728617213)

--- After Adding Orders ---
Account ETH TOK0[18] TOK1[18] WETH[18] Blah
-------------------- ------------------------ ------------------------ ------------------------ ------------------------ ---------------------------------------------
deployer:0xf39F 9999.988181440316473361 0.0 0.0 0.0
user0:0x7099 9899.999114192188709382 100.0 100.0 100.0
user1:0x3C44 9899.999416552681207036 100.0 100.0 100.0
user2:0x90F7 9899.999421363384773366 100.0 100.0 100.0
user3:0x15d3 9899.999806994510806249 100.0 100.0 100.0
deployer:0xf39F 9999.988182781243124841 0.0 0.0 0.0
user0:0x7099 9899.999114192469822948 100.0 100.0 100.0
user1:0x3C44 9899.999416552863371731 100.0 100.0 100.0
user2:0x90F7 9899.999421363530047768 100.0 100.0 100.0
user3:0x15d3 9899.999806994585042133 100.0 100.0 100.0
Chadex:0xCf7E 0.0 0.0 0.0 0.0

Chadex: Chadex:0xCf7E
Expand Down Expand Up @@ -159,7 +160,7 @@



pairKey: 0x2c831da670a13afa44000f723f930fbd31948192e5612f758ae2cfaeee460dc2, tokenz=["0x5FbDB2315678afecb367f032d93F642f64180aa3","0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"], decimalss=[18,18], bestBuyOrder=["690300000","0x216071ecd309e02f0a7c41a681f63c840beb698b2aedb288198fa4cb3667138e","0xc4fd7db027bc75d73f894dcf8f248f903df706acd79ab89dd74e57ca3c14a360","0x70997970C51812dc3A010C7d01b50e0d17dc79C8","1728612815","3000000000000000000","3896856439229320585","2690000000000000000"], bestSellOrder=["0","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000","0","0","0","0"]
pairKey: 0x2c831da670a13afa44000f723f930fbd31948192e5612f758ae2cfaeee460dc2, tokenz=["0x5FbDB2315678afecb367f032d93F642f64180aa3","0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"], decimalss=[18,18], bestBuyOrder=["690300000","0x216071ecd309e02f0a7c41a681f63c840beb698b2aedb288198fa4cb3667138e","0xc4fd7db027bc75d73f894dcf8f248f903df706acd79ab89dd74e57ca3c14a360","0x70997970C51812dc3A010C7d01b50e0d17dc79C8","1728620784","3000000000000000000","3896856439229320585","2690000000000000000"], bestSellOrder=["0","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000","0","0","0","0"]

Executing: [
{
Expand All @@ -171,37 +172,37 @@
],
"price": "690100000",
"targetPrice": "690100000",
"expiry": 1728612815,
"expiry": 1728620784,
"tokens": "10000000000000000000",
"skipCheck": false
}
]
> user3->chadex.execute(actions) - gasUsed: 314227 ~ ETH 0.00314227 ~ USD 7
+ Token0:0x5FbD Transfer(from: user3:0x15d3, to: user0:0x7099, tokens: 3.0)
+ WETH:0x9fE4 Transfer(from: user0:0x7099, to: user3:0x15d3, tokens: 2.0709)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0x216071ecd309e02f0a, taker: user3:0x15d3, maker: user0:0x7099, buySell: 1, price: 0.6903, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728609246)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0x216071ecd309e02f0a, taker: user3:0x15d3, maker: user0:0x7099, buySell: 1, price: 0.6903, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728617214)
+ Token0:0x5FbD Transfer(from: user3:0x15d3, to: user1:0x3C44, tokens: 3.0)
+ WETH:0x9fE4 Transfer(from: user1:0x3C44, to: user3:0x15d3, tokens: 2.0709)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0xc4fd7db027bc75d73f, taker: user3:0x15d3, maker: user1:0x3C44, buySell: 1, price: 0.6903, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728609246)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0xc4fd7db027bc75d73f, taker: user3:0x15d3, maker: user1:0x3C44, buySell: 1, price: 0.6903, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728617214)
+ Token0:0x5FbD Transfer(from: user3:0x15d3, to: user2:0x90F7, tokens: 3.0)
+ WETH:0x9fE4 Transfer(from: user2:0x90F7, to: user3:0x15d3, tokens: 2.0709)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0xe7bb22970ed8b3926f, taker: user3:0x15d3, maker: user2:0x90F7, buySell: 1, price: 0.6903, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728609246)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0xe7bb22970ed8b3926f, taker: user3:0x15d3, maker: user2:0x90F7, buySell: 1, price: 0.6903, baseTokens: 3.0, quoteTokens: 2.0709, timestamp: 1728617214)
+ Token0:0x5FbD Transfer(from: user3:0x15d3, to: user0:0x7099, tokens: 0.896986380759200231)
+ WETH:0x9fE4 Transfer(from: user0:0x7099, to: user3:0x15d3, tokens: 0.6191)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0x16ee2eb820c1dc86c5, taker: user3:0x15d3, maker: user0:0x7099, buySell: 1, price: 0.6902, baseTokens: 0.896986380759200231, quoteTokens: 0.6191, timestamp: 1728609246)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0x16ee2eb820c1dc86c5, taker: user3:0x15d3, maker: user0:0x7099, buySell: 1, price: 0.6902, baseTokens: 0.896986380759200231, quoteTokens: 0.6191, timestamp: 1728617214)
+ Token0:0x5FbD Transfer(from: user3:0x15d3, to: user1:0x3C44, tokens: 0.103013619240799769)
+ WETH:0x9fE4 Transfer(from: user1:0x3C44, to: user3:0x15d3, tokens: 0.0711)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0x4fcf79cf98e01a03f6, taker: user3:0x15d3, maker: user1:0x3C44, buySell: 1, price: 0.6902, baseTokens: 0.103013619240799769, quoteTokens: 0.0711, timestamp: 1728609246)
+ Chadex:0xCf7E TradeSummary(pairKey: 0x2c831da670a13afa44, taker: user3:0x15d3, buySell: 1, price: 0.69029, baseTokens: 10.0, quoteTokens: 6.9029, tokensOnOrder: 0.0, timestamp: 1728609246)
+ Chadex:0xCf7E Trade(pairKey: 0x2c831da670a13afa44, orderKey: 0x4fcf79cf98e01a03f6, taker: user3:0x15d3, maker: user1:0x3C44, buySell: 1, price: 0.6902, baseTokens: 0.103013619240799769, quoteTokens: 0.0711, timestamp: 1728617214)
+ Chadex:0xCf7E TradeSummary(pairKey: 0x2c831da670a13afa44, taker: user3:0x15d3, buySell: 1, price: 0.69029, baseTokens: 10.0, quoteTokens: 6.9029, tokensOnOrder: 0.0, timestamp: 1728617214)

--- After Executing Against Orders ---
Account ETH TOK0[18] TOK1[18] WETH[18] Blah
-------------------- ------------------------ ------------------------ ------------------------ ------------------------ ---------------------------------------------
deployer:0xf39F 9999.988181440316473361 0.0 0.0 0.0
user0:0x7099 9899.999114192188709382 103.896986380759200231 100.0 97.31
user1:0x3C44 9899.999416552681207036 103.103013619240799769 100.0 97.858
user2:0x90F7 9899.999421363384773366 103.0 100.0 97.9291
user3:0x15d3 9899.999487981236564949 90.0 100.0 106.9029
deployer:0xf39F 9999.988182781243124841 0.0 0.0 0.0
user0:0x7099 9899.999114192469822948 103.896986380759200231 100.0 97.31
user1:0x3C44 9899.999416552863371731 103.103013619240799769 100.0 97.858
user2:0x90F7 9899.999421363530047768 103.0 100.0 97.9291
user3:0x15d3 9899.999487981347251165 90.0 100.0 106.9029
Chadex:0xCf7E 0.0 0.0 0.0 0.0

Chadex: Chadex:0xCf7E
Expand All @@ -227,14 +228,14 @@



pairKey: 0x2c831da670a13afa44000f723f930fbd31948192e5612f758ae2cfaeee460dc2, tokenz=["0x5FbDB2315678afecb367f032d93F642f64180aa3","0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"], decimalss=[18,18], bestBuyOrder=["690200000","0x4fcf79cf98e01a03f641e90081ae6c9f2f25715e7200410f5e6faf60d2968aec","0xdc9dfdc4362c69200ed3447b7901034ef868941b0169e3e83d49f52053e39c49","0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC","1728612815","1896986380759200231","793972761518400463","548000000000000000"], bestSellOrder=["0","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000","0","0","0","0"]
pairKey: 0x2c831da670a13afa44000f723f930fbd31948192e5612f758ae2cfaeee460dc2, tokenz=["0x5FbDB2315678afecb367f032d93F642f64180aa3","0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"], decimalss=[18,18], bestBuyOrder=["690200000","0x4fcf79cf98e01a03f641e90081ae6c9f2f25715e7200410f5e6faf60d2968aec","0xdc9dfdc4362c69200ed3447b7901034ef868941b0169e3e83d49f52053e39c49","0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC","1728620784","1896986380759200231","793972761518400463","548000000000000000"], bestSellOrder=["0","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000000000000000000000000000000000000000","0","0","0","0"]

tokenBalanceAndAllowance: [
"10000000000000000000,103896986380759200231"
]

✔ 00. Test 00 (73ms)
✔ 00. Test 00 (102ms)


1 passing (680ms)
1 passing (867ms)

0 comments on commit ec03c75

Please sign in to comment.