@arcblock/did-playground 1.4.12 → 1.6.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,575 @@
1
+ import mustache from 'mustache';
2
+
3
+ async function createSwapOrder(api) {
4
+ const res = await api.post('/api/did/swap', {});
5
+ return { tid: res.data.traceId };
6
+ }
7
+
8
+ const getValidPayAmount = (payAmount, price) => {
9
+ if (Number(payAmount) >= 0) {
10
+ return payAmount;
11
+ }
12
+ if (Number(price) >= 0) {
13
+ return price;
14
+ }
15
+ return 1;
16
+ };
17
+
18
+ export const getMessage = (message = '', session) => {
19
+ try {
20
+ return mustache.render(
21
+ message,
22
+ {
23
+ user: session.user || {},
24
+ token: session.token || {},
25
+ balance: session.balance || {},
26
+ },
27
+ {},
28
+ ['(%', '%)']
29
+ );
30
+ } catch (err) {
31
+ console.error('Cannot render message', { message, session });
32
+ return message;
33
+ }
34
+ };
35
+
36
+ // https://github.com/ArcBlock/gatsby-extensions/issues/56
37
+ export const actions = {
38
+ // Currency
39
+ receive_local_token: {
40
+ action: 'receive_token',
41
+ extraParams: props => ({ chain: 'local', amount: props.amount || 1 }),
42
+ },
43
+ receive_foreign_token: {
44
+ action: 'receive_token',
45
+ extraParams: props => ({ chain: 'foreign', amount: props.amount || 1 }),
46
+ },
47
+ send_local_token: {
48
+ action: 'send_token',
49
+ extraParams: props => ({ chain: 'local', amount: props.amount || 1 }),
50
+ },
51
+ send_foreign_token: {
52
+ action: 'send_token',
53
+ extraParams: props => ({ chain: 'foreign', amount: props.amount || 1 }),
54
+ },
55
+ exchange_to_foreign_token: {
56
+ action: 'swap_token',
57
+ onStart: createSwapOrder,
58
+ extraParams: props => ({ action: 'buy', rate: props.exchangeRate, amount: props.amount || 1 }),
59
+ },
60
+ exchange_to_local_token: {
61
+ action: 'swap_token',
62
+ onStart: createSwapOrder,
63
+ extraParams: props => ({ action: 'sell', rate: props.exchangeRate, amount: props.amount || 1 }),
64
+ },
65
+
66
+ exchange_to_foreign_token_v2: {
67
+ action: 'swap_token_v2',
68
+ onStart: createSwapOrder,
69
+ extraParams: props => ({ action: 'buy', rate: props.exchangeRate, amount: props.amount || 1 }),
70
+ },
71
+ exchange_to_local_token_v2: {
72
+ action: 'swap_token_v2',
73
+ onStart: createSwapOrder,
74
+ extraParams: props => ({ action: 'sell', rate: props.exchangeRate, amount: props.amount || 1 }),
75
+ },
76
+
77
+ // Cross chain assets and tokens
78
+ buy_foreign_certificate_with_local_token: {
79
+ action: 'swap_asset',
80
+ onStart: createSwapOrder,
81
+ extraParams: (props, session) => ({
82
+ action: 'buy',
83
+ type: 'certificate',
84
+ pfc: 'local',
85
+ price: props.price || 1,
86
+ name: getMessage(props.name, session),
87
+ desc: getMessage(props.description, session),
88
+ loc: getMessage(props.location, session),
89
+ bg: props.backgroundUrl,
90
+ logo: props.logoUrl,
91
+ }),
92
+ },
93
+ buy_foreign_badge_with_local_token: {
94
+ action: 'swap_asset',
95
+ onStart: createSwapOrder,
96
+ extraParams: (props, session) => ({
97
+ action: 'buy',
98
+ type: 'badge',
99
+ pfc: 'local',
100
+ price: props.price || 1,
101
+ name: getMessage(props.name, session),
102
+ desc: getMessage(props.description, session),
103
+ loc: getMessage(props.location, session),
104
+ bg: props.backgroundUrl,
105
+ logo: props.logoUrl,
106
+ svg: props.svg,
107
+ }),
108
+ },
109
+ buy_foreign_ticket_with_local_token: {
110
+ action: 'swap_asset',
111
+ onStart: createSwapOrder,
112
+ extraParams: (props, session) => ({
113
+ action: 'buy',
114
+ type: 'ticket',
115
+ pfc: 'local',
116
+ price: props.price || 1,
117
+ name: getMessage(props.name, session),
118
+ desc: getMessage(props.description, session),
119
+ loc: getMessage(props.location, session),
120
+ bg: props.backgroundUrl,
121
+ logo: props.logoUrl,
122
+ }),
123
+ },
124
+ sell_foreign_certificate_for_local_token: {
125
+ action: 'swap_asset',
126
+ onStart: createSwapOrder,
127
+ extraParams: (props, session) => ({
128
+ action: 'sell',
129
+ type: 'certificate',
130
+ pfc: 'local',
131
+ price: props.price || 1,
132
+ name: getMessage(props.name, session),
133
+ }),
134
+ },
135
+ sell_foreign_badge_for_local_token: {
136
+ action: 'swap_asset',
137
+ onStart: createSwapOrder,
138
+ extraParams: (props, session) => ({
139
+ action: 'sell',
140
+ type: 'badge',
141
+ pfc: 'local',
142
+ price: props.price || 1,
143
+ name: getMessage(props.name, session),
144
+ }),
145
+ },
146
+ sell_foreign_ticket_for_local_token: {
147
+ action: 'swap_asset',
148
+ onStart: createSwapOrder,
149
+ extraParams: (props, session) => ({
150
+ action: 'sell',
151
+ type: 'ticket',
152
+ pfc: 'local',
153
+ price: props.price || 1,
154
+ name: getMessage(props.name, session),
155
+ }),
156
+ },
157
+
158
+ buy_foreign_certificate_with_local_token_v2: {
159
+ action: 'swap_asset_v2',
160
+ extraParams: (props, session) => ({
161
+ action: 'buy',
162
+ type: 'certificate',
163
+ pfc: 'local',
164
+ price: props.price || 1,
165
+ name: getMessage(props.name, session),
166
+ desc: getMessage(props.description, session),
167
+ loc: getMessage(props.location, session),
168
+ bg: props.backgroundUrl,
169
+ logo: props.logoUrl,
170
+ }),
171
+ },
172
+ buy_foreign_badge_with_local_token_v2: {
173
+ action: 'swap_asset_v2',
174
+ extraParams: (props, session) => ({
175
+ action: 'buy',
176
+ type: 'badge',
177
+ pfc: 'local',
178
+ price: props.price || 1,
179
+ name: getMessage(props.name, session),
180
+ desc: getMessage(props.description, session),
181
+ loc: getMessage(props.location, session),
182
+ bg: props.backgroundUrl,
183
+ logo: props.logoUrl,
184
+ svg: props.svg,
185
+ }),
186
+ },
187
+ buy_foreign_ticket_with_local_token_v2: {
188
+ action: 'swap_asset_v2',
189
+ extraParams: (props, session) => ({
190
+ action: 'buy',
191
+ type: 'ticket',
192
+ pfc: 'local',
193
+ price: props.price || 1,
194
+ name: getMessage(props.name, session),
195
+ desc: getMessage(props.description, session),
196
+ loc: getMessage(props.location, session),
197
+ bg: props.backgroundUrl,
198
+ logo: props.logoUrl,
199
+ }),
200
+ },
201
+ sell_foreign_certificate_for_local_token_v2: {
202
+ action: 'swap_asset_v2',
203
+ extraParams: (props, session) => ({
204
+ action: 'sell',
205
+ type: 'certificate',
206
+ pfc: 'local',
207
+ price: props.price || 1,
208
+ name: getMessage(props.name, session),
209
+ }),
210
+ },
211
+ sell_foreign_badge_for_local_token_v2: {
212
+ action: 'swap_asset_v2',
213
+ extraParams: (props, session) => ({
214
+ action: 'sell',
215
+ type: 'badge',
216
+ pfc: 'local',
217
+ price: props.price || 1,
218
+ name: getMessage(props.name, session),
219
+ }),
220
+ },
221
+ sell_foreign_ticket_for_local_token_v2: {
222
+ action: 'swap_asset_v2',
223
+ extraParams: (props, session) => ({
224
+ action: 'sell',
225
+ type: 'ticket',
226
+ pfc: 'local',
227
+ price: props.price || 1,
228
+ name: getMessage(props.name, session),
229
+ }),
230
+ },
231
+
232
+ buy_local_certificate_with_foreign_token: {
233
+ action: 'swap_asset',
234
+ onStart: createSwapOrder,
235
+ extraParams: (props, session) => ({
236
+ action: 'buy',
237
+ type: 'certificate',
238
+ pfc: 'foreign',
239
+ price: props.price || 1,
240
+ name: getMessage(props.name, session),
241
+ desc: getMessage(props.description, session),
242
+ loc: getMessage(props.location, session),
243
+ bg: props.backgroundUrl,
244
+ logo: props.logoUrl,
245
+ }),
246
+ },
247
+ buy_local_badge_with_foreign_token: {
248
+ action: 'swap_asset',
249
+ onStart: createSwapOrder,
250
+ extraParams: (props, session) => ({
251
+ action: 'buy',
252
+ type: 'badge',
253
+ pfc: 'foreign',
254
+ price: props.price || 0,
255
+ name: getMessage(props.name, session),
256
+ desc: getMessage(props.description, session),
257
+ loc: getMessage(props.location, session),
258
+ bg: props.backgroundUrl,
259
+ logo: props.logoUrl,
260
+ svg: props.svg,
261
+ }),
262
+ },
263
+ buy_local_ticket_with_foreign_token: {
264
+ action: 'swap_asset',
265
+ onStart: createSwapOrder,
266
+ extraParams: (props, session) => ({
267
+ action: 'buy',
268
+ type: 'ticket',
269
+ pfc: 'foreign',
270
+ price: props.price || 1,
271
+ name: getMessage(props.name, session),
272
+ desc: getMessage(props.description, session),
273
+ loc: getMessage(props.location, session),
274
+ bg: props.backgroundUrl,
275
+ logo: props.logoUrl,
276
+ }),
277
+ },
278
+ sell_local_certificate_for_foreign_token: {
279
+ action: 'swap_asset',
280
+ onStart: createSwapOrder,
281
+ extraParams: (props, session) => ({
282
+ action: 'sell',
283
+ type: 'certificate',
284
+ pfc: 'foreign',
285
+ price: props.price || 1,
286
+ name: getMessage(props.name, session),
287
+ }),
288
+ },
289
+ sell_local_badge_for_foreign_token: {
290
+ action: 'swap_asset',
291
+ onStart: createSwapOrder,
292
+ extraParams: (props, session) => ({
293
+ action: 'sell',
294
+ type: 'vc',
295
+ pfc: 'foreign',
296
+ price: props.price || 1,
297
+ name: getMessage(props.name, session),
298
+ }),
299
+ },
300
+ sell_local_ticket_for_foreign_token: {
301
+ action: 'swap_asset',
302
+ onStart: createSwapOrder,
303
+ extraParams: (props, session) => ({
304
+ action: 'sell',
305
+ type: 'ticket',
306
+ pfc: 'foreign',
307
+ price: props.price || 1,
308
+ name: getMessage(props.name, session),
309
+ }),
310
+ },
311
+
312
+ buy_local_certificate_with_foreign_token_v2: {
313
+ action: 'swap_asset_v2',
314
+ extraParams: (props, session) => ({
315
+ pa: getValidPayAmount(props.payAmount, props.price),
316
+ pt: 'token',
317
+ ra: props.receiveAmount || 1,
318
+ rt: 'certificate',
319
+ name: getMessage(props.name, session),
320
+ desc: getMessage(props.description, session),
321
+ loc: getMessage(props.location, session),
322
+ bg: props.backgroundUrl,
323
+ logo: props.logoUrl,
324
+ }),
325
+ },
326
+ buy_local_badge_with_foreign_token_v2: {
327
+ action: 'swap_asset_v2',
328
+ extraParams: (props, session) => ({
329
+ pa: getValidPayAmount(props.payAmount, props.price),
330
+ pt: 'token',
331
+ ra: props.receiveAmount || 1,
332
+ rt: 'badge',
333
+ name: getMessage(props.name, session),
334
+ desc: getMessage(props.description, session),
335
+ loc: getMessage(props.location, session),
336
+ bg: props.backgroundUrl,
337
+ logo: props.logoUrl,
338
+ svg: props.svg,
339
+ }),
340
+ },
341
+ buy_local_ticket_with_foreign_token_v2: {
342
+ action: 'swap_asset_v2',
343
+ extraParams: (props, session) => ({
344
+ pa: getValidPayAmount(props.payAmount, props.price),
345
+ pt: 'token',
346
+ ra: props.receiveAmount || 1,
347
+ rt: 'badge',
348
+ name: getMessage(props.name, session),
349
+ desc: getMessage(props.description, session),
350
+ loc: getMessage(props.location, session),
351
+ bg: props.backgroundUrl,
352
+ logo: props.logoUrl,
353
+ }),
354
+ },
355
+ sell_local_certificate_for_foreign_token_v2: {
356
+ action: 'swap_asset_v2',
357
+ extraParams: (props, session) => ({
358
+ pa: props.payAmount || 1,
359
+ pt: 'certificate',
360
+ ra: getValidPayAmount(props.receiveAmount, props.price),
361
+ rt: 'token',
362
+ name: getMessage(props.name, session),
363
+ }),
364
+ },
365
+ sell_local_badge_for_foreign_token_v2: {
366
+ action: 'swap_asset_v2',
367
+ extraParams: (props, session) => ({
368
+ pa: props.payAmount || 1,
369
+ pt: 'badge',
370
+ ra: getValidPayAmount(props.receiveAmount, props.price),
371
+ rt: 'token',
372
+ name: getMessage(props.name, session),
373
+ }),
374
+ },
375
+ sell_local_ticket_for_foreign_token_v2: {
376
+ action: 'swap_asset_v2',
377
+ extraParams: (props, session) => ({
378
+ pa: props.payAmount || 1,
379
+ pt: 'ticket',
380
+ ra: getValidPayAmount(props.receiveAmount, props.price),
381
+ rt: 'token',
382
+ name: getMessage(props.name, session),
383
+ }),
384
+ },
385
+
386
+ // Exchange Scenarios
387
+ buy_local_certificate_with_local_token: {
388
+ action: 'exchange_assets',
389
+ extraParams: (props, session) => ({
390
+ pa: getValidPayAmount(props.payAmount, props.price),
391
+ pt: 'token',
392
+ ra: props.receiveAmount || 1,
393
+ rt: 'certificate',
394
+ name: getMessage(props.name, session),
395
+ desc: getMessage(props.description, session),
396
+ loc: getMessage(props.location, session),
397
+ bg: props.backgroundUrl,
398
+ logo: props.logoUrl,
399
+ }),
400
+ },
401
+ sell_local_certificate_for_local_token: {
402
+ action: 'exchange_assets',
403
+ extraParams: (props, session) => ({
404
+ pa: props.payAmount || 1,
405
+ pt: 'certificate',
406
+ ra: getValidPayAmount(props.receiveAmount, props.price),
407
+ rt: 'token',
408
+ name: getMessage(props.name, session),
409
+ }),
410
+ },
411
+ buy_local_badge_with_local_token: {
412
+ action: 'exchange_assets',
413
+ extraParams: (props, session) => ({
414
+ pa: getValidPayAmount(props.payAmount, props.price),
415
+ pt: 'token',
416
+ ra: props.receiveAmount || 1,
417
+ rt: 'badge',
418
+ name: getMessage(props.name, session),
419
+ desc: getMessage(props.description, session),
420
+ loc: getMessage(props.location, session),
421
+ bg: props.backgroundUrl,
422
+ logo: props.logoUrl,
423
+ svg: props.svg,
424
+ }),
425
+ },
426
+ sell_local_badge_for_local_token: {
427
+ action: 'exchange_assets',
428
+ extraParams: (props, session) => ({
429
+ pa: props.payAmount || 1,
430
+ pt: 'badge',
431
+ ra: getValidPayAmount(props.receiveAmount, props.price),
432
+ rt: 'token',
433
+ name: getMessage(props.name, session),
434
+ }),
435
+ },
436
+ buy_local_ticket_with_local_token: {
437
+ action: 'exchange_assets',
438
+ extraParams: (props, session) => ({
439
+ pa: getValidPayAmount(props.payAmount, props.price),
440
+ pt: 'token',
441
+ ra: props.receiveAmount || 1,
442
+ rt: 'ticket',
443
+ name: getMessage(props.name, session),
444
+ desc: getMessage(props.description, session),
445
+ loc: getMessage(props.location, session),
446
+ bg: props.backgroundUrl,
447
+ logo: props.logoUrl,
448
+ }),
449
+ },
450
+ sell_local_ticket_for_local_token: {
451
+ action: 'exchange_assets',
452
+ extraParams: (props, session) => ({
453
+ pa: props.payAmount || 1,
454
+ pt: 'ticket',
455
+ ra: getValidPayAmount(props.receiveAmount, props.price),
456
+ rt: 'token',
457
+ name: getMessage(props.name, session),
458
+ }),
459
+ },
460
+ buy_local_ticket_with_local_certificate: {
461
+ action: 'exchange_assets',
462
+ extraParams: (props, session) => ({
463
+ pa: props.payAmount || 1,
464
+ pt: 'certificate',
465
+ ra: props.receiveAmount || 1,
466
+ rt: 'ticket',
467
+ name: getMessage(props.name, session),
468
+ }),
469
+ },
470
+ buy_local_certificate_with_local_ticket: {
471
+ action: 'exchange_assets',
472
+ extraParams: (props, session) => ({
473
+ pa: props.payAmount || 1,
474
+ pt: 'ticket',
475
+ ra: props.receiveAmount || 1,
476
+ rt: 'certificate',
477
+ name: getMessage(props.name, session),
478
+ }),
479
+ },
480
+
481
+ consume_local_asset: {
482
+ action: 'consume_asset',
483
+ extraParams: ({ type, typeUrl, name, did }, session) => ({
484
+ pfc: 'local',
485
+ type,
486
+ tu: typeUrl,
487
+ name: getMessage(name, session),
488
+ did,
489
+ }),
490
+ },
491
+ consume_foreign_asset: {
492
+ action: 'consume_asset',
493
+ extraParams: ({ type, typeUrl, name, did }, session) => ({
494
+ pfc: 'foreign',
495
+ type,
496
+ tu: typeUrl,
497
+ name: getMessage(name, session),
498
+ did,
499
+ }),
500
+ },
501
+ consume_local_asset_by_name: {
502
+ action: 'consume_asset',
503
+ extraParams: ({ name }, session) => ({
504
+ pfc: 'local',
505
+ name: getMessage(name, session),
506
+ }),
507
+ },
508
+ consume_foreign_asset_by_name: {
509
+ action: 'consume_asset',
510
+ extraParams: ({ name }, session) => ({
511
+ pfc: 'foreign',
512
+ name: getMessage(name, session),
513
+ }),
514
+ },
515
+ consume_local_asset_by_did: {
516
+ action: 'consume_asset',
517
+ extraParams: ({ did }) => ({
518
+ pfc: 'local',
519
+ did,
520
+ }),
521
+ },
522
+ consume_foreign_asset_by_did: {
523
+ action: 'consume_asset',
524
+ extraParams: ({ did }) => ({
525
+ pfc: 'foreign',
526
+ did,
527
+ }),
528
+ },
529
+
530
+ claim_signature: {
531
+ action: 'claim_signature',
532
+ extraParams: ({ type }) => ({ type }),
533
+ },
534
+
535
+ consume_email_vc: {
536
+ action: 'consume_vc',
537
+ extraParams: {},
538
+ },
539
+ };
540
+
541
+ export const getActionName = (config, props) => {
542
+ if (typeof config === 'string') {
543
+ return config;
544
+ }
545
+
546
+ if (typeof config.action === 'string') {
547
+ return config.action;
548
+ }
549
+
550
+ if (typeof config.action === 'function') {
551
+ return config.action(props);
552
+ }
553
+
554
+ throw new Error('Cannot determine playground button action');
555
+ };
556
+
557
+ export const getActionParams = (config, props, session) => {
558
+ if (typeof config === 'string') {
559
+ return {};
560
+ }
561
+
562
+ if (!config.extraParams) {
563
+ return {};
564
+ }
565
+
566
+ if (typeof config.extraParams === 'function') {
567
+ return config.extraParams(props, session);
568
+ }
569
+
570
+ if (typeof config.extraParams === 'object') {
571
+ return config.extraParams;
572
+ }
573
+
574
+ return {};
575
+ };