This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathv00_adapter.js
565 lines (504 loc) · 17.3 KB
/
v00_adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
const OFFER_STATUS = [
'error',
'created',
'accepted',
'disputed',
'finalized',
'sellerReviewed',
'withdrawn',
'ruling'
]
const SUPPORTED_DEPOSIT_CURRENCIES = ['OGN']
const emptyAddress = '0x0000000000000000000000000000000000000000'
class V00_MarkeplaceAdapter {
constructor({ contractService, blockEpoch }) {
this.web3 = contractService.web3
this.contractService = contractService
this.contractName = 'V00_Marketplace'
this.tokenContractName = 'OriginToken'
this.blockEpoch = blockEpoch || 0
}
async getContract() {
if (!this.contract) {
this.contract = await this.contractService.deployed(
this.contractService.contracts[this.contractName]
)
}
}
async call(methodName, args, opts) {
return await this.contractService.call(
this.contractName,
methodName,
args,
opts
)
}
async getListingsCount() {
const total = await this.call('totalListings')
return Number(total)
}
async createListing(
ipfsBytes,
{ deposit = '0', arbitrator, commission = {} },
confirmationCallback
) {
const from = await this.contractService.currentAccount()
const { amount, currency } = commission
if (currency && !SUPPORTED_DEPOSIT_CURRENCIES.includes(currency)) {
throw `${currency} is not a supported deposit currency`
}
if (amount > 0) {
deposit = await this.contractService.moneyToUnits(commission)
const {
market_address,
selector,
call_params
} = await this._getTokenAndCallWithSenderParams(
'createListingWithSender',
ipfsBytes,
deposit,
arbitrator || from
)
// In order to estimate gas correctly, we need to add the call to a create listing since that's called by the token
const extra_estimated_gas = await this.contract.methods['createListing'](
ipfsBytes,
0,
arbitrator || from
).estimateGas({ from })
const { transactionReceipt, timestamp } = await this.contractService.call(
this.tokenContractName,
'approveAndCallWithSender',
[market_address, deposit, selector, call_params],
{ from, confirmationCallback, additionalGas: extra_estimated_gas }
)
const events = await this.contract.getPastEvents('ListingCreated', {
fromBlock: transactionReceipt.blockNumber,
toBlock: transactionReceipt.blockNumber
})
for (const e of events) {
if (e.transactionHash == transactionReceipt.transactionHash) {
const listingIndex = e.returnValues.listingID
return Object.assign({ timestamp, listingIndex }, transactionReceipt)
}
}
} else {
const { transactionReceipt, timestamp } = await this.call(
'createListing',
[ipfsBytes, deposit, arbitrator || from],
{ from, confirmationCallback }
)
const listingIndex =
transactionReceipt.events['ListingCreated'].returnValues.listingID
return Object.assign({ timestamp, listingIndex }, transactionReceipt)
}
}
async withdrawListing(listingId, ipfsBytes, confirmationCallback) {
const from = await this.contractService.currentAccount()
const { transactionReceipt, timestamp } = await this.call(
'withdrawListing',
[listingId, from, ipfsBytes],
{ from, confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async makeOffer(listingId, ipfsBytes, data, confirmationCallback) {
const {
affiliate,
arbitrator,
commission,
finalizes,
totalPrice = {},
unitsPurchased
} = data
// For V1, we only support quantity of 1.
if (unitsPurchased && unitsPurchased != 1)
throw new Error(
`Attempted to purchase ${unitsPurchased} - only 1 allowed.`
)
const price = await this.contractService.moneyToUnits(totalPrice)
const commissionUnits = await this.contractService.moneyToUnits(commission)
const currencies = await this.contractService.currencies()
const args = [
listingId,
ipfsBytes,
finalizes || 30 * 24 * 60 * 60, // 30 days from offer acceptance
affiliate || emptyAddress,
commissionUnits,
price,
currencies[totalPrice.currency].address,
arbitrator || emptyAddress
]
const opts = { confirmationCallback }
if (totalPrice.currency === 'ETH') {
opts.value = price
}
const { transactionReceipt, timestamp } = await this.call(
'makeOffer',
args,
opts
)
const offerIndex =
transactionReceipt.events['OfferCreated'].returnValues.offerID
return Object.assign({ timestamp, offerIndex }, transactionReceipt)
}
async withdrawOffer(
listingIndex,
offerIndex,
ipfsBytes,
confirmationCallback
) {
const { transactionReceipt, timestamp } = await this.call(
'withdrawOffer',
[listingIndex, offerIndex, ipfsBytes],
{ confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async acceptOffer(listingIndex, offerIndex, ipfsBytes, confirmationCallback) {
const { transactionReceipt, timestamp } = await this.call(
'acceptOffer',
[listingIndex, offerIndex, ipfsBytes],
{ confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async finalizeOffer(
listingIndex,
offerIndex,
ipfsBytes,
confirmationCallback
) {
const { transactionReceipt, timestamp } = await this.call(
'finalize',
[listingIndex, offerIndex, ipfsBytes],
{ confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async initiateDispute(
listingIndex,
offerIndex,
ipfsBytes,
confirmationCallback
) {
const { transactionReceipt, timestamp } = await this.call(
'dispute',
[listingIndex, offerIndex, ipfsBytes],
{ confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async resolveDispute(
listingIndex,
offerIndex,
ipfsBytes,
ruling,
refund,
confirmationCallback
) {
const { transactionReceipt, timestamp } = await this.call(
'executeRuling',
[listingIndex, offerIndex, ipfsBytes, ruling, refund],
{ confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async getListing(listingId) {
await this.getContract()
// Get the raw listing data from the contract.
// Note: once a listing is withdrawn, it is deleted from the blockchain to save
// on gas. In this cases rawListing is returned as an object with all its fields set to zero.
const rawListing = await this.call('listings', [listingId])
// Find all events related to this listing
const listingTopic = this.padTopic(listingId)
const events = await this.contract.getPastEvents('allEvents', {
topics: [null, null, listingTopic],
fromBlock: this.blockEpoch
})
let status = 'active'
// Loop through the events looking and update the IPFS hash and offers appropriately.
let ipfsHash
const offers = {}
events.forEach(event => {
if (event.event === 'ListingCreated') {
ipfsHash = event.returnValues.ipfsHash
} else if (event.event === 'ListingUpdated') {
ipfsHash = event.returnValues.ipfsHash
} else if (event.event === 'ListingWithdrawn') {
status = 'inactive'
} else if (event.event === 'OfferCreated') {
offers[event.returnValues.offerID] = { status: 'created', event }
} else if (event.event === 'OfferAccepted') {
offers[event.returnValues.offerID] = { status: 'accepted', event }
} else if (event.event === 'OfferDisputed') {
offers[event.returnValues.offerID] = { status: 'disputed', event }
} else if (event.event === 'OfferRuling') {
offers[event.returnValues.offerID] = { status: 'ruling', event }
} else if (event.event === 'OfferFinalized') {
offers[event.returnValues.offerID] = { status: 'finalized', event }
} else if (event.event === 'OfferData') {
offers[event.returnValues.offerID] = { status: 'sellerReviewed', event }
}
})
if (!ipfsHash) {
throw(`ipfsHash not found in events for listing ${listingId}`)
}
// Return the raw listing along with events and IPFS hash
return Object.assign({}, rawListing, { ipfsHash, events, offers, status })
}
// Logs a listing and its associated events and offers to the browser's JS
// console. This is meant for live debugging of listings and should not be
// used from other functions.
async logListing(listingId) {
if (!window || !window.originTest) {
throw new Error('This can only be called from a browser JS console')
}
await this.getContract()
// Dump Listing struct from marketplace contract
const rawListing = await this.call('listings', [listingId])
const filteredListing = {
seller: rawListing.seller,
deposit: rawListing.deposit,
depositManager: rawListing.depositManager
}
console.log('Listing:')
console.table(filteredListing)
// Find all events related to this listing
const listingTopic = this.padTopic(listingId)
const events = await this.contract.getPastEvents('allEvents', {
topics: [null, null, listingTopic],
fromBlock: this.blockEpoch
})
console.log('Events:')
// Objects returned from web3 often have redundant entries with numeric
// keys, so we filter those out.
const filterObject = (obj) => {
const filteredObj = {}
for (const [key, value] of Object.entries(obj)) {
if (isNaN(parseInt(key[0], 10))) {
filteredObj[key] = value
}
}
return filteredObj
}
// Log each event and its corresponding IPFS object, if available.
let eventIndex = 0
for (const event of events) {
const filteredEvent = Object.assign(
{ eventIndex, name: event.event },
filterObject(event.returnValues)
)
// Only keep keys that don't start with a digit. The filtered keys are
// redundant.
for (const [key, value] of Object.entries(event.returnValues)) {
if (isNaN(parseInt(key[0], 10))) {
filteredEvent[key] = value
}
}
// Load the associated IPFS object.
const ipfsService = window.originTest.marketplace.ipfsService
try {
const ipfsHash = this.contractService.getIpfsHashFromBytes32(
event.returnValues.ipfsHash
)
if (ipfsHash && ipfsService) {
const ipfsObj = await ipfsService.loadObjFromFile(ipfsHash)
filteredEvent.ipfsData = ipfsObj
}
} catch(e) {
filteredEvent.ipfsData = 'Could not load from IPFS'
}
// Load the associated offer, if applicable.
if (filteredEvent.offerID !== undefined) {
filteredEvent.offer = filterObject(
await this.call('offers', [listingId, filteredEvent.offerID])
)
}
console.log(filteredEvent)
eventIndex++
}
}
async getListings(opts) {
await this.getContract()
if (opts.purchasesFor) {
const events = await this.contract.getPastEvents('OfferCreated', {
filter: { party: opts.purchasesFor },
fromBlock: this.blockEpoch
})
const listingIds = []
events.forEach(e => {
const listingId = Number(e.returnValues.listingID)
if (listingIds.indexOf(listingId) < 0) {
listingIds.push(listingId)
}
})
return listingIds
} else if (opts.listingsFor) {
const events = await this.contract.getPastEvents('ListingCreated', {
filter: { party: opts.listingsFor },
fromBlock: this.blockEpoch
})
return events.map(e => Number(e.returnValues.listingID))
} else {
const total = await this.call('totalListings')
return [...Array(Number(total)).keys()]
}
}
async getOffers(listingIndex, opts) {
await this.getContract()
let filter = {}
if (listingIndex) {
filter = Object.assign(filter, { listingID: listingIndex })
}
if (opts.for) {
filter = Object.assign(filter, { party: opts.for })
}
const events = await this.contract.getPastEvents('OfferCreated', {
filter,
fromBlock: this.blockEpoch
})
return events.map(e => Number(e.returnValues.offerID))
}
async getOffer(listingIndex, offerIndex) {
await this.getContract()
// Get the raw listing data from the contract
// Note: once an offer is finalized|ruled|withdrawn, it is deleted from the blockchain to save
// on gas. In those cases rawOffer is returned as an object with all its fields set to zero.
const rawOffer = await this.call('offers', [listingIndex, offerIndex])
// Find all events related to this offer
const listingTopic = this.padTopic(listingIndex)
const offerTopic = this.padTopic(offerIndex)
const events = await this.contract.getPastEvents('allEvents', {
topics: [null, null, listingTopic, offerTopic],
fromBlock: this.blockEpoch
})
// Scan through the events to retrieve information of interest.
let buyer, ipfsHash, createdAt
for (const e of events) {
const timestamp = await this.contractService.getTimestamp(e)
e.timestamp = timestamp
switch (e.event) {
case 'OfferCreated':
buyer = e.returnValues.party
ipfsHash = e.returnValues.ipfsHash
createdAt = timestamp
break
// In all cases below, the offer was deleted from the blochain
// rawOffer fields are set to zero => populate rawOffer.status based on event history.
case 'OfferFinalized':
rawOffer.status = 4
break
// TODO: Assumes OfferData event is a seller review
case 'OfferData':
rawOffer.status = 5
break
case 'OfferWithdrawn':
rawOffer.status = 6
break
case 'OfferRuling':
rawOffer.status = 7
break
}
e.timestamp = timestamp
}
// Translate status number to string
rawOffer.status = OFFER_STATUS[rawOffer.status]
// Return the raw listing along with events and IPFS hash
return Object.assign({}, rawOffer, { buyer, ipfsHash, events, createdAt })
}
async addData(ipfsBytes, listingIndex, offerIndex, confirmationCallback) {
const { transactionReceipt, timestamp } = await this.call(
'addData',
[listingIndex, offerIndex, ipfsBytes],
{ confirmationCallback }
)
return Object.assign({ timestamp }, transactionReceipt)
}
async getNotifications(party) {
await this.getContract()
const notifications = []
const partyListingIds = []
const partyOfferIds = []
const events = await this.contract.getPastEvents('allEvents', {
topics: [null, this.padTopic(party)],
fromBlock: this.blockEpoch
})
for (const event of events) {
if (event.event === 'ListingCreated') {
partyListingIds.push(event.returnValues.listingID)
}
if (event.event === 'OfferCreated') {
partyOfferIds.push([
event.returnValues.listingID,
event.returnValues.offerID
])
}
}
// Find pending offers and pending reviews
for (const listingId of partyListingIds) {
const listing = await this.getListing(listingId)
for (const offerId in listing.offers) {
const offer = listing.offers[offerId]
if (offer.status === 'created') {
notifications.push({
event: offer.event,
type: 'seller_listing_purchased',
resources: { listingId, offerId }
})
}
if (offer.status === 'finalized') {
notifications.push({
event: offer.event,
type: 'seller_review_received',
resources: { listingId, offerId }
})
}
}
}
// Find pending offers and pending reviews
for (const [listingId, offerId] of partyOfferIds) {
const listing = await this.getListing(listingId)
const offer = listing.offers[offerId]
if (offer.status === 'accepted') {
notifications.push({
event: offer.event,
type: 'buyer_listing_shipped',
resources: { listingId, offerId }
})
}
}
return notifications
}
async getTokenAddress() {
await this.getContract()
return await this.contract.methods.tokenAddr().call()
}
padTopic(id) {
return this.web3.utils.padLeft(this.web3.utils.numberToHex(id), 64)
}
async _getTokenAndCallWithSenderParams(call_name, ...args) {
await this.getContract()
for (const call of this.contract.options.jsonInterface) {
if (
call.name === call_name &&
call.type === 'function' &&
call.signature
) {
const market_address = this.contract.options.address
// take out the first parameter which is hopefully the seller address
const input_types = call.inputs.slice(1).map(e => e.type)
if (input_types.length != args.length) {
throw 'The number of parameters passed does not match the contract parameters'
}
const call_params = this.web3.eth.abi.encodeParameters(
input_types,
args
)
const selector = call.signature
return { market_address, selector, call_params }
}
}
throw 'Invalid Marketplace contract for getting create parameters'
}
}
export default V00_MarkeplaceAdapter