Skip to content

Commit 9c01c8f

Browse files
committed
feat: add simplest order matching
1 parent 88d348a commit 9c01c8f

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

packages/contracts/noir/lob_router/src/lib.nr

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@ mod LobRouter {
33
context: &mut common::Context,
44
seller_secret_key: Field,
55
seller_note: erc20::Erc20NoteConsumptionInputs,
6-
seller_amount: common::TokenAmount,
6+
seller_order: crate::Order,
77
seller_randomness: Field,
88
buyer_secret_key: Field,
99
buyer_note: erc20::Erc20NoteConsumptionInputs,
10-
buyer_amount: common::TokenAmount,
10+
buyer_order: crate::Order,
1111
buyer_randomness: Field,
1212
) {
13-
// TODO(security): orders must be signed by parties and the prices should match
13+
// TODO(security): orders must be signed by parties
14+
15+
assert(
16+
seller_order.sell_amount == buyer_order.buy_amount,
17+
"seller order amount does not match buyer order amount",
18+
);
19+
assert(
20+
seller_order.buy_amount == buyer_order.sell_amount,
21+
"buyer order amount does not match seller order amount",
22+
);
23+
let seller_amount = seller_order.sell_amount;
24+
let buyer_amount = seller_order.buy_amount;
25+
assert(seller_amount.token == seller_note.note.amount.token, "invalid seller note token");
26+
assert(buyer_amount.token == buyer_note.note.amount.token, "invalid buyer note token");
27+
1428
erc20::Token::transfer(
1529
context,
1630
seller_secret_key,
@@ -32,3 +46,8 @@ mod LobRouter {
3246
);
3347
}
3448
}
49+
50+
pub struct Order {
51+
pub sell_amount: common::TokenAmount,
52+
pub buy_amount: common::TokenAmount,
53+
}

packages/contracts/noir/lob_router_swap/src/main.nr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ fn main(
33
// seller
44
seller_secret_key: Field,
55
seller_note: erc20::Erc20NoteConsumptionInputs,
6-
seller_amount: common::TokenAmount,
6+
seller_order: lob_router::Order,
77
seller_randomness: Field,
88
// buyer
99
buyer_secret_key: Field,
1010
buyer_note: erc20::Erc20NoteConsumptionInputs,
11-
buyer_amount: common::TokenAmount,
11+
buyer_order: lob_router::Order,
1212
buyer_randomness: Field,
1313
) -> pub common::Result<4, 2> {
1414
let mut context = common::Context::from(tree_roots);
@@ -17,11 +17,11 @@ fn main(
1717
&mut context,
1818
seller_secret_key,
1919
seller_note,
20-
seller_amount,
20+
seller_order,
2121
seller_randomness,
2222
buyer_secret_key,
2323
buyer_note,
24-
buyer_amount,
24+
buyer_order,
2525
buyer_randomness,
2626
);
2727

packages/contracts/sdk/LobService.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,31 @@ export class LobService {
5656
randomness: buyerRandomness,
5757
});
5858

59+
const seller_order = {
60+
sell_amount: await params.sellerAmount.toNoir(),
61+
buy_amount: await params.buyerAmount.toNoir(),
62+
};
63+
const buyer_order = {
64+
sell_amount: await params.buyerAmount.toNoir(),
65+
buy_amount: await params.sellerAmount.toNoir(),
66+
};
67+
5968
const input = {
6069
tree_roots: await this.trees.getTreeRoots(),
6170
seller_secret_key: params.sellerSecretKey,
6271
seller_note: await this.poolErc20.toNoteConsumptionInputs(
6372
params.sellerSecretKey,
6473
params.sellerNote,
6574
),
66-
seller_amount: await params.sellerAmount.toNoir(),
75+
seller_order,
6776
seller_randomness: sellerRandomness,
6877

6978
buyer_secret_key: params.buyerSecretKey,
7079
buyer_note: await this.poolErc20.toNoteConsumptionInputs(
7180
params.buyerSecretKey,
7281
params.buyerNote,
7382
),
74-
buyer_amount: await params.buyerAmount.toNoir(),
83+
buyer_order,
7584
buyer_randomness: buyerRandomness,
7685
};
7786
const { proof } = await prove("swap", swapCircuit, input);

0 commit comments

Comments
 (0)