@blocklet/meta 1.5.19 → 1.5.20
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.
- package/lib/payment.js +12 -32
- package/lib/schema.js +23 -14
- package/package.json +10 -10
package/lib/payment.js
CHANGED
|
@@ -6,52 +6,33 @@ const { toFactoryAddress } = require('@arcblock/did-util');
|
|
|
6
6
|
const { getBlockletPurchaseTemplate } = require('@arcblock/nft/lib/templates');
|
|
7
7
|
|
|
8
8
|
const isFreeBlocklet = (meta) => {
|
|
9
|
-
if (!meta.
|
|
9
|
+
if (!meta.payment) {
|
|
10
10
|
return true;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
if (primaryPrice <= 0 && secondaryPrice.every((x) => x <= 0)) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return false;
|
|
13
|
+
const priceList = (meta.payment.price || []).map((x) => x.value || 0);
|
|
14
|
+
return priceList.every((x) => x === 0);
|
|
20
15
|
};
|
|
21
16
|
|
|
22
|
-
const createShareContract = ({
|
|
17
|
+
const createShareContract = ({ tokens = [], shares = [] }) => {
|
|
23
18
|
const zeroBN = new BN(0);
|
|
24
19
|
const decimals = 1000000; // we only support 6 decimals on share ratio
|
|
25
20
|
const decimalsBN = new BN(decimals);
|
|
26
21
|
|
|
27
22
|
const contract = [];
|
|
28
|
-
const shareSum = shares.reduce((sum, x) => sum + x.
|
|
23
|
+
const shareSum = shares.reduce((sum, x) => sum + x.value, 0);
|
|
29
24
|
if (shareSum > 1) {
|
|
30
|
-
throw new Error('
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// split primary token
|
|
34
|
-
const priceBN = new BN(price);
|
|
35
|
-
if (priceBN.lt(zeroBN)) {
|
|
36
|
-
throw new Error('price must be greater than or equal to zero');
|
|
37
|
-
}
|
|
38
|
-
if (priceBN.gt(zeroBN)) {
|
|
39
|
-
shares.forEach(({ address, share }) => {
|
|
40
|
-
const ratio = new BN(share * decimals);
|
|
41
|
-
const amount = priceBN.mul(ratio).div(decimalsBN);
|
|
42
|
-
contract.push(`transfer('${address}','${amount.toString()}')`);
|
|
43
|
-
});
|
|
25
|
+
throw new Error('payment.share invalid: share sum should not be greater than 1');
|
|
44
26
|
}
|
|
45
27
|
|
|
46
|
-
// split secondary token
|
|
47
28
|
if (Array.isArray(tokens)) {
|
|
48
|
-
tokens.forEach(({ address: tokenAddress, value }) => {
|
|
49
|
-
const valueBN = new BN(
|
|
29
|
+
tokens.forEach(({ address: tokenAddress, value: valueItem }) => {
|
|
30
|
+
const valueBN = new BN(valueItem);
|
|
50
31
|
if (valueBN.lt(zeroBN)) {
|
|
51
32
|
throw new Error('token price must be greater than or equal to zero');
|
|
52
33
|
}
|
|
53
|
-
shares.forEach(({ address,
|
|
54
|
-
const ratio = new BN(
|
|
34
|
+
shares.forEach(({ address, value }) => {
|
|
35
|
+
const ratio = new BN(value * decimals);
|
|
55
36
|
const amount = valueBN.mul(ratio).div(decimalsBN);
|
|
56
37
|
contract.push(`transferToken('${tokenAddress}','${address}','${amount.toString()}')`);
|
|
57
38
|
});
|
|
@@ -62,7 +43,7 @@ const createShareContract = ({ price, shares, tokens = [] }) => {
|
|
|
62
43
|
};
|
|
63
44
|
|
|
64
45
|
// we need to ensure that blocklet purchase factory does not change across changes
|
|
65
|
-
const createNftFactoryItx = ({ meta,
|
|
46
|
+
const createNftFactoryItx = ({ meta, tokens, shares, issuers, serviceUrl }) => {
|
|
66
47
|
const factoryOutput = getBlockletPurchaseTemplate(serviceUrl);
|
|
67
48
|
const itx = {
|
|
68
49
|
name: meta.title || meta.name,
|
|
@@ -71,7 +52,6 @@ const createNftFactoryItx = ({ meta, price, tokens, shares, issuers, serviceUrl
|
|
|
71
52
|
limit: 0,
|
|
72
53
|
trustedIssuers: issuers,
|
|
73
54
|
input: {
|
|
74
|
-
value: price,
|
|
75
55
|
tokens: [...tokens],
|
|
76
56
|
assets: [],
|
|
77
57
|
variables: [],
|
|
@@ -96,7 +76,7 @@ const createNftFactoryItx = ({ meta, price, tokens, shares, issuers, serviceUrl
|
|
|
96
76
|
{
|
|
97
77
|
name: 'mint',
|
|
98
78
|
type: 'contract',
|
|
99
|
-
hook: createShareContract({
|
|
79
|
+
hook: createShareContract({ tokens, shares }),
|
|
100
80
|
},
|
|
101
81
|
],
|
|
102
82
|
};
|
package/lib/schema.js
CHANGED
|
@@ -209,36 +209,45 @@ const createBlockletSchema = (
|
|
|
209
209
|
// This is usually created and maintained by `blocklet publish` command
|
|
210
210
|
nftFactory: Joi.DID().optional().allow('').default(''),
|
|
211
211
|
|
|
212
|
-
// Set the price and
|
|
213
|
-
|
|
214
|
-
// How much
|
|
215
|
-
price: Joi.
|
|
216
|
-
// How much secondary tokens required to purchase this block
|
|
217
|
-
tokens: Joi.array()
|
|
212
|
+
// Set the price and share of the blocklet
|
|
213
|
+
payment: Joi.object({
|
|
214
|
+
// How much price required to purchase this block
|
|
215
|
+
price: Joi.array()
|
|
218
216
|
.max(4)
|
|
219
217
|
.items(
|
|
220
218
|
Joi.object({
|
|
221
|
-
|
|
219
|
+
value: Joi.number().required(),
|
|
222
220
|
address: Joi.DID().required(), // token address
|
|
223
221
|
})
|
|
224
222
|
),
|
|
225
|
-
// List of beneficiaries that
|
|
223
|
+
// List of beneficiaries that share the token earns from blocklet purchase
|
|
226
224
|
// If left empty, blocklet publish workflow will enforce both the developer and the registry account
|
|
227
225
|
// If not, the blocklet publish workflow will enforce the registry account
|
|
228
|
-
// In theory, the developer can split as many
|
|
226
|
+
// In theory, the developer can split as many share as he wants
|
|
229
227
|
// Such as, some developers coauthored the blocklet, and their income get instant settlement on blocklet purchase
|
|
230
|
-
// For performance reasons, we need to set a hard limit for
|
|
231
|
-
|
|
228
|
+
// For performance reasons, we need to set a hard limit for share count
|
|
229
|
+
share: Joi.array()
|
|
232
230
|
.max(4)
|
|
233
231
|
.items(
|
|
234
232
|
Joi.object({
|
|
235
233
|
name: Joi.string().required(),
|
|
236
234
|
address: Joi.DID().required(),
|
|
237
|
-
|
|
235
|
+
value: Joi.number().min(0).max(1).required(),
|
|
238
236
|
})
|
|
239
|
-
)
|
|
237
|
+
)
|
|
238
|
+
.custom((value) => {
|
|
239
|
+
// If share is not empty, the total value should be 1
|
|
240
|
+
if (value.length === 0) {
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
const total = value.reduce((acc, cur) => acc + cur.value, 0);
|
|
244
|
+
if (total !== 1) {
|
|
245
|
+
throw new Error(`invalid share: ${JSON.stringify(value)}`);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}, 'share invalid'),
|
|
240
249
|
})
|
|
241
|
-
.default({ price:
|
|
250
|
+
.default({ price: [], share: [] })
|
|
242
251
|
.optional(),
|
|
243
252
|
|
|
244
253
|
keywords: Joi.alternatives()
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.5.
|
|
6
|
+
"version": "1.5.20",
|
|
7
7
|
"description": "Library to parse/validate/fix blocklet meta",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
"author": "wangshijun <wangshijun2020@gmail.com> (http://github.com/wangshijun)",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@arcblock/did": "^1.13.
|
|
22
|
-
"@arcblock/did-ext": "^1.13.
|
|
23
|
-
"@arcblock/did-util": "^1.13.
|
|
24
|
-
"@arcblock/nft": "^1.13.
|
|
25
|
-
"@ocap/asset": "^1.13.
|
|
26
|
-
"@ocap/mcrypto": "^1.13.
|
|
27
|
-
"@ocap/util": "^1.13.
|
|
28
|
-
"@ocap/wallet": "^1.13.
|
|
21
|
+
"@arcblock/did": "^1.13.64",
|
|
22
|
+
"@arcblock/did-ext": "^1.13.64",
|
|
23
|
+
"@arcblock/did-util": "^1.13.64",
|
|
24
|
+
"@arcblock/nft": "^1.13.64",
|
|
25
|
+
"@ocap/asset": "^1.13.64",
|
|
26
|
+
"@ocap/mcrypto": "^1.13.64",
|
|
27
|
+
"@ocap/util": "^1.13.64",
|
|
28
|
+
"@ocap/wallet": "^1.13.64",
|
|
29
29
|
"ajv": "^7.0.3",
|
|
30
30
|
"debug": "^4.3.3",
|
|
31
31
|
"fs-extra": "^10.0.0",
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"jest": "^27.3.1"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "08771b885d987343780f29b95b71e6e0926b91bf"
|
|
46
46
|
}
|