Skip to content

Commit 10a2a84

Browse files
doc: added bundle object
1 parent 1c48daf commit 10a2a84

File tree

1 file changed

+173
-47
lines changed

1 file changed

+173
-47
lines changed

packages/marketplace/docs/Exchange.md

Lines changed: 173 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,33 @@ of the bundle is the sum of all individual asset prices.
5252
```
5353
Alice wants to sell a bundle containing:
5454
- 1 LAND (ERC721) with token id 1000
55-
- Price: 1200 MATIC
55+
- Price: 1200 POL
5656
- 10 ASSET (ERC1155) with token id 2000
57-
- Price: 800 MATIC
58-
Total bundle price: 2000 MATIC
57+
- Price: 800 POL
58+
Total bundle price: 2000 POL
5959
```
6060

6161
##### Example 2: Bundle with Quads
6262

6363
```
6464
Alice wants to sell a bundle containing:
6565
- 2 Quads (3x3 size each)
66-
- First Quad: 500 MATIC
67-
- Second Quad: 500 MATIC
66+
- First Quad: 500 POL
67+
- Second Quad: 500 POL
6868
- 1 LAND (ERC721) with token id 1000
69-
- Price: 4000 MATIC
70-
Total bundle price: 5000 MATIC
69+
- Price: 4000 POL
70+
Total bundle price: 5000 POL
7171
```
7272

7373
##### Example 3: Bundle with Multiple ERC1155
7474

7575
```
7676
Alice wants to sell a bundle containing:
7777
- 10 ASSET_A (ERC1155) with token id 1000
78-
- Price: 10000 MATIC
78+
- Price: 10000 POL
7979
- 5 ASSET_B (ERC1155) with token id 2000
80-
- Price: 5000 MATIC
81-
Total bundle price: 15000 MATIC
80+
- Price: 5000 POL
81+
Total bundle price: 15000 POL
8282
```
8383

8484
#### Invalid Bundle Examples
@@ -101,10 +101,137 @@ Total bundle price: 15000 MATIC
101101

102102
```
103103
- 2 LAND (ERC721) with token id 1000
104-
- Price: 1200 MATIC each
104+
- Price: 1200 POL each
105105
// ERC721 tokens are unique and cannot have a value > 1 in a bundle
106106
```
107107

108+
#### Code Examples
109+
110+
##### Example 1: Simple Bundle with ERC721 and ERC1155
111+
112+
```typescript
113+
// Bundle containing 1 ERC721 and 10 ERC1155
114+
const bundleData = {
115+
bundledERC721: [
116+
{
117+
erc721Address: '0x...', // ERC721 contract address
118+
ids: [1], // Token ID
119+
},
120+
],
121+
bundledERC1155: [
122+
{
123+
erc1155Address: '0x...', // ERC1155 contract address
124+
ids: [1], // Token ID
125+
supplies: [10], // Quantity
126+
},
127+
],
128+
quads: {
129+
sizes: [], // No quads in this bundle
130+
xs: [],
131+
ys: [],
132+
data: '0x',
133+
},
134+
priceDistribution: {
135+
erc721Prices: [[1200000000000]], // 1200 POL
136+
erc1155Prices: [[800000000000]], // 800 POL
137+
quadPrices: [],
138+
},
139+
};
140+
```
141+
142+
##### Example 2: Bundle with Quads
143+
144+
```typescript
145+
// Bundle containing 2 Quads and 1 ERC721
146+
const bundleData = {
147+
bundledERC721: [
148+
{
149+
erc721Address: '0x...', // ERC721 contract address
150+
ids: [1], // Token ID
151+
},
152+
],
153+
bundledERC1155: [], // No ERC1155 in this bundle
154+
quads: {
155+
sizes: [3, 3], // Two 3x3 quads
156+
xs: [0, 3], // X coordinates
157+
ys: [0, 3], // Y coordinates
158+
data: '0x',
159+
},
160+
priceDistribution: {
161+
erc721Prices: [[4000000000000]], // 4000 POL
162+
erc1155Prices: [],
163+
quadPrices: [500000000000, 500000000000], // 500 POL each in wei
164+
},
165+
};
166+
```
167+
168+
##### Example 3: Bundle with Multiple ERC1155
169+
170+
```typescript
171+
// Bundle containing multiple ERC1155 tokens
172+
const bundleData = {
173+
bundledERC721: [], // No ERC721 in this bundle
174+
bundledERC1155: [
175+
{
176+
erc1155Address: '0x...', // First ERC1155 contract address
177+
ids: [1],
178+
supplies: [10],
179+
},
180+
{
181+
erc1155Address: '0x...', // Second ERC1155 contract address
182+
ids: [2],
183+
supplies: [5],
184+
},
185+
],
186+
quads: {
187+
sizes: [],
188+
xs: [],
189+
ys: [],
190+
data: '0x',
191+
},
192+
priceDistribution: {
193+
erc721Prices: [],
194+
erc1155Prices: [[10000000000000], [5000000000000]], // 10000 POL and 5000 POL
195+
quadPrices: [],
196+
},
197+
};
198+
```
199+
200+
##### Example 4: Creating and Signing a Bundle Order
201+
202+
```typescript
203+
// Create the bundle asset
204+
const bundleAsset = await AssetBundle(bundleData, 1); // Value must be 1 if bundle contains ERC721
205+
206+
// Create the bundle order
207+
const bundleOrder = await OrderDefault(
208+
maker, // Signer
209+
bundleAsset, // The bundle to sell
210+
ZeroAddress, // No specific taker
211+
erc20Asset, // The ERC20 token to receive
212+
1, // Salt
213+
0, // Start time
214+
0 // End time
215+
);
216+
217+
// Sign the bundle order
218+
const bundleSignature = await signOrder(bundleOrder, maker, orderValidator);
219+
```
220+
221+
##### Example 5: Matching Bundle Orders
222+
223+
```typescript
224+
// Match a bundle order with an ERC20 order
225+
await exchangeContract.matchOrders([
226+
{
227+
orderLeft: bundleOrder, // The bundle order
228+
signatureLeft: bundleSignature,
229+
orderRight: erc20Order, // The ERC20 order
230+
signatureRight: erc20Signature,
231+
},
232+
]);
233+
```
234+
108235
### Maker & Taker
109236

110237
The maker and taker represent the 2 parties of the exchange. Each order can
@@ -127,15 +254,15 @@ satisfy each party. For instance, let's take again our seller use case:
127254
`Order A`
128255

129256
```
130-
Alice wants to sell to anybody 1 LAND (ERC721) with token id 1000 against 100 MATIC (ERC20).
257+
Alice wants to sell to anybody 1 LAND (ERC721) with token id 1000 against 100 POL (ERC20).
131258
```
132259

133260
An order satisfying `Order A` could simply be:
134261

135262
`Order B`
136263

137264
```
138-
Bob wants to buy from anybody 1 LAND (ERC721) with token id 1000 against 100 MATIC (ERC20).
265+
Bob wants to buy from anybody 1 LAND (ERC721) with token id 1000 against 100 POL (ERC20).
139266
```
140267

141268
### Validation
@@ -171,15 +298,14 @@ right) and deciding how much to transfer to each user accordingly. To represent
171298
the prices each order has two fields:
172299

173300
1. The make side: the maximum the user is ready to give. For example less than
174-
`2000 SAND`.
301+
`2000 POL`.
175302
2. The take side: the minimum the user wants to get. For example more than
176303
`4000 USDT`.
177304

178305
The minimum price the user accepts is the division between the take and the make
179-
side, the lower bound for the price is `2 = 4000/2000 [USDT/SAND]`. An
180-
equivalent way of expressing it is the inverse, if you divide make side by the
181-
take side then you get an upper bound for the price:
182-
`2000/4000 = 0.5 [SAND/USDT]`.
306+
side, the lower bound for the price is `2 = 4000/2000 [USDT/POL]`. An equivalent
307+
way of expressing it is the inverse, if you divide make side by the take side
308+
then you get an upper bound for the price: `2000/4000 = 0.5 [POL/USDT]`.
183309

184310
When comparing two orders the amount that the left side wants to make is
185311
compared against the amount that the right side wants to take and vise versa. If
@@ -192,21 +318,21 @@ For example:
192318
`Order A`
193319

194320
```
195-
Alice wants to sell to anybody 2000 SAND (make) against 4000 USDT (take).
196-
The lower bound is 4000/2000 = 2 [USDT/SAND].
321+
Alice wants to sell to anybody 2000 POL (make) against 4000 USDT (take).
322+
The lower bound is 4000/2000 = 2 [USDT/POL].
197323
```
198324

199325
An order satisfying `Order A` could be:
200326

201327
`Order B`
202328

203329
```
204-
Bob wants to buy from anybody 1000 SAND (take) against 3000 USDT (make).
205-
The upper bound is 3000/1000 = 3 [USDT/SAND]
330+
Bob wants to buy from anybody 1000 POL (take) against 3000 USDT (make).
331+
The upper bound is 3000/1000 = 3 [USDT/POL]
206332
```
207333

208-
The equilibrium price must be anything between `2 [USDT/SAND]` and
209-
`3 [USDT/SAND]`.
334+
The equilibrium price must be anything between `2 [USDT/POL]` and
335+
`3 [USDT/POL]`.
210336

211337
After deciding the equilibrium price, the quantity to transfer of each asset is
212338
calculated. The marketplace supports partial filling, that is: respecting the
@@ -218,16 +344,16 @@ For example: Let's consider `Order C` and `Order D`:
218344
`Order C`
219345

220346
```
221-
Alice wants to sell to anybody 10 ASSET (ERC1155) against 100 MATIC (ERC20) for each token.
347+
Alice wants to sell to anybody 10 ASSET (ERC1155) against 100 POL (ERC20) for each token.
222348
```
223349

224350
`Order D`
225351

226352
```
227-
Bob wants to buy from anybody 1 ASSET (ERC1155) against 100 MATIC (ERC20) for each token.
353+
Bob wants to buy from anybody 1 ASSET (ERC1155) against 100 POL (ERC20) for each token.
228354
```
229355

230-
The equilibrium price is `100 [MATIC/ASSET]`, but, the selling order can't be
356+
The equilibrium price is `100 [POL/ASSET]`, but, the selling order can't be
231357
fully matched because the buyer can only buy 1 ASSET of 10. In that case, the
232358
`Order C` is partially filled. `Order C` can be re-matched with another order in
233359
a different transaction until being fully filled.
@@ -238,29 +364,29 @@ the other. Let's consider `Order E` and `Order F`:
238364
`Order E`
239365

240366
```
241-
Alice wants to sell 8 ASSETs at 1000 MATIC for each.
242-
Alice wants to get 8000 MATIC in total. The lower bound for the price is 1000 [MATIC/ASSET]
367+
Alice wants to sell 8 ASSETs at 1000 POL for each.
368+
Alice wants to get 8000 POL in total. The lower bound for the price is 1000 [POL/ASSET]
243369
```
244370

245371
`Order F`
246372

247373
```
248-
Bob wants to buy 6 ASSETs at 2000 MATIC each.
249-
Bob is ready to spend 12000 MATIC for 6 ASSETs. The upper bound for the price is 2000 [MATIC/ASSET]
374+
Bob wants to buy 6 ASSETs at 2000 POL each.
375+
Bob is ready to spend 12000 POL for 6 ASSETs. The upper bound for the price is 2000 [POL/ASSET]
250376
```
251377

252-
The equilibrium price must be between 1000 and 2000 MATIC for each ASSET.
378+
The equilibrium price must be between 1000 and 2000 POL for each ASSET.
253379
In the extreme cases:
254380

255-
- Equilibrium price 1000 MATIC/ASSET: Bob buys 8 ASSETS at 1000 MATIC each,
256-
Alice gets 8000 MATIC and sells everything, Bob buys partially. Both sides are
257-
happy and Bob has an extra benefit because he is paying 1000 MATIC instead of
258-
2000 MATIC each ASSET. Bob gets more assets than expected.
259-
- Equilibrium price 2000 MATIC/ASSET: Bob buys 6 ASSETS at 2000 MATIC spending
260-
12000 MATIC and fully fill his order. Alice gets 12000 MATIC for 6 ASSETS
261-
filling partially his order. Both sides are happy, but, in this case Alice
262-
gets the benefit because she sells at 2000 MATIC each ASSET. Alice gets more
263-
MATIC than expected.
381+
- Equilibrium price 1000 POL/ASSET: Bob buys 8 ASSETS at 1000 POL each, Alice
382+
gets 8000 POL and sells everything, Bob buys partially. Both sides are happy
383+
and Bob has an extra benefit because he is paying 1000 POL instead of 2000 POL
384+
each ASSET. Bob gets more assets than expected.
385+
- Equilibrium price 2000 POL/ASSET: Bob buys 6 ASSETS at 2000 POL spending 12000
386+
POL and fully fill his order. Alice gets 12000 POL for 6 ASSETS filling
387+
partially his order. Both sides are happy, but, in this case Alice gets the
388+
benefit because she sells at 2000 POL each ASSET. Alice gets more POL than
389+
expected.
264390

265391
### Custom recipients
266392

@@ -339,18 +465,18 @@ fee is set 5%.
339465
Bob is the creator of the ASSET (ERC1155) with token id 1.
340466

341467
```
342-
Bob sells 1 ASSET (ERC1155) with token id 1 to Alice for 100 SAND (ERC20)
343-
Bob receives 98 SAND from Alice
344-
Fee Receiver receives 2 SAND from Alice
468+
Bob sells 1 ASSET (ERC1155) with token id 1 to Alice for 100 POL (ERC20)
469+
Bob receives 98 POL from Alice
470+
Fee Receiver receives 2 POL from Alice
345471
Alice receives 1 ASSET (ERC1155) with token id 1
346472
```
347473

348474
Carol is the creator of the ASSET (ERC1155) with token id 2.
349475

350476
```
351-
Bob sells 1 ASSET (ERC1155) with token id 2 to Alice for 100 SAND (ERC20)
352-
Bob receives 95 SAND from Alice
353-
Fee Receiver receives 5 SAND from Alice
477+
Bob sells 1 ASSET (ERC1155) with token id 2 to Alice for 100 POL (ERC20)
478+
Bob receives 95 POL from Alice
479+
Fee Receiver receives 5 POL from Alice
354480
Alice receives 1 ASSET (ERC1155) with token id 1
355481
```
356482

0 commit comments

Comments
 (0)