@blocklet/meta 1.8.19 → 1.8.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.
Files changed (2) hide show
  1. package/lib/payment/v2.js +44 -6
  2. package/package.json +4 -4
package/lib/payment/v2.js CHANGED
@@ -2,7 +2,7 @@
2
2
  const crypto = require('crypto');
3
3
  const debug = require('debug')('@blocklet/meta:payment');
4
4
  const joinURL = require('url-join');
5
- const axios = require('axios');
5
+ const axios = require('axios').create();
6
6
  const stableStringify = require('json-stable-stringify');
7
7
  const get = require('lodash/get');
8
8
  const pick = require('lodash/pick');
@@ -243,7 +243,9 @@ const getTokenTransfers = ({ priceToken, shares = [], components = [] }) => {
243
243
  const [token] = child.meta.payment.price || [];
244
244
  if (token && token.address !== priceToken.address) {
245
245
  throw new Error(
246
- `component price token is not same with app price token: ${child.meta.title || child.meta.name}`
246
+ `The token address of the component "${
247
+ child.meta.title || child.meta.name
248
+ }" is inconsistent with the blocklet. Component: ${priceToken.address}, Composite Blocklet: ${token.address}`
247
249
  );
248
250
  }
249
251
 
@@ -251,10 +253,6 @@ const getTokenTransfers = ({ priceToken, shares = [], components = [] }) => {
251
253
 
252
254
  parentShareBN = parentShareBN.sub(fromTokenToUnit(childShare, priceToken.decimal));
253
255
 
254
- if (parentShareBN.lt(ZeroBN)) {
255
- throw new Error('Price is not enough for component sharing');
256
- }
257
-
258
256
  const componentContracts = getTokenTransfers({
259
257
  priceToken: { ...priceToken, value: childShare },
260
258
  shares: child.meta.payment.share,
@@ -265,6 +263,13 @@ const getTokenTransfers = ({ priceToken, shares = [], components = [] }) => {
265
263
  }
266
264
  }
267
265
 
266
+ if (parentShareBN.lt(ZeroBN)) {
267
+ const needPrice = fromUnitToToken(fromTokenToUnit(price, priceToken.decimal).sub(parentShareBN));
268
+ throw new Error(
269
+ `Price for composite blocklet must be greater than ${needPrice} because paid components are included.`
270
+ );
271
+ }
272
+
268
273
  shares.forEach(({ name, address: accountAddress, value: ratio }) => {
269
274
  contracts.push({
270
275
  tokenAddress: priceToken.address,
@@ -604,10 +609,43 @@ const verifyNftFactory = async ({ factoryState, signerWallet }) => {
604
609
  return { components };
605
610
  };
606
611
 
612
+ /**
613
+ * Check blocklet and all of components are free
614
+ * Throw Error if not free
615
+ *
616
+ * @param {BlockletMeta} meta
617
+ */
618
+ const checkFreeBlocklet = async (blockletMeta) => {
619
+ if (!isFreeBlocklet(blockletMeta)) {
620
+ return Promise.reject(new Error('blocklet is not free'));
621
+ }
622
+
623
+ const { components } = await getComponents(blockletMeta);
624
+
625
+ const shouldAllComponentFree = (arr) => {
626
+ arr.forEach(({ meta, children }) => {
627
+ if (!isFreeBlocklet(meta) || !isFreeComponent(meta)) {
628
+ // throw new Error(`Found paid component "${meta.title || meta.name}" in free blocklet`);
629
+ throw new Error(
630
+ `Paid component "${meta.title || meta.name}" found in free blocklet "${
631
+ blockletMeta.title || blockletMeta.name
632
+ }", which is forbidden`
633
+ );
634
+ }
635
+ shouldAllComponentFree(children || []);
636
+ });
637
+ };
638
+
639
+ shouldAllComponentFree(components);
640
+
641
+ return true;
642
+ };
643
+
607
644
  module.exports = {
608
645
  createNftFactoryItx,
609
646
  verifyPaymentIntegrity,
610
647
  verifyNftFactory,
648
+ checkFreeBlocklet,
611
649
  version: VERSION,
612
650
  _test: {
613
651
  getPriceTokens,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.8.19",
6
+ "version": "1.8.20",
7
7
  "description": "Library to parse/validate/fix blocklet meta",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,8 +18,8 @@
18
18
  "author": "wangshijun <wangshijun2020@gmail.com> (http://github.com/wangshijun)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.8.19",
22
- "@abtnode/util": "1.8.19",
21
+ "@abtnode/constant": "1.8.20",
22
+ "@abtnode/util": "1.8.20",
23
23
  "@arcblock/did": "1.17.19",
24
24
  "@arcblock/did-ext": "1.17.19",
25
25
  "@arcblock/did-util": "1.17.19",
@@ -53,5 +53,5 @@
53
53
  "express": "^4.18.1",
54
54
  "jest": "^27.5.1"
55
55
  },
56
- "gitHead": "19d3e21ad3658e79a3ec60394d6c77793af7deb3"
56
+ "gitHead": "abe47e26c9583bfe5c4969e19cd36574f436a515"
57
57
  }