@ocap/state 1.27.7 → 1.27.9

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.
@@ -11,7 +11,7 @@ const curveSchema = Joi.alternatives().try(
11
11
  }),
12
12
  Joi.object({
13
13
  type: Joi.string().valid('constant').required(),
14
- fixedPrice: Joi.BN().greater(0).required(),
14
+ fixedPrice: Joi.BN().min(1).required(),
15
15
  }),
16
16
  Joi.object({
17
17
  type: Joi.string().valid('quadratic').required(),
@@ -29,7 +29,7 @@ const stateSchema = Joi.object({
29
29
  reserveBalance: Joi.BN().min(0).required(),
30
30
  status: Joi.string().valid('ACTIVE', 'PAUSED').required(),
31
31
  feeRate: Joi.number().min(0).max(2000).required(),
32
- curve: curveSchema.required(),
32
+ curve: curveSchema.optional().allow(null),
33
33
  context: schemas.context,
34
34
  data: Joi.any().optional().allow(null),
35
35
  }).options({
@@ -2,10 +2,13 @@ const pick = require('lodash/pick');
2
2
  const cloneDeep = require('lodash/cloneDeep');
3
3
  const { toChecksumAddress } = require('@arcblock/did/lib/type');
4
4
  const { toAddress, BN } = require('@ocap/util');
5
- const { Joi } = require('@arcblock/validator');
5
+ const { Joi, schemas } = require('@arcblock/validator');
6
6
 
7
7
  const { create: createStateContext } = require('../contexts/state');
8
8
 
9
+ const isTest = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test' || process.env.CI;
10
+
11
+ // Schema for metadata.value - used independently in token factory protocols
9
12
  const metadataSchema = Joi.object({
10
13
  communityUrl: Joi.string()
11
14
  .uri({ scheme: ['https'], allowRelative: false })
@@ -15,6 +18,44 @@ const metadataSchema = Joi.object({
15
18
  issuer: Joi.string().max(64).required(),
16
19
  }).options({ stripUnknown: true, noDefaults: false });
17
20
 
21
+ const stateSchema = Joi.object({
22
+ address: Joi.DID().prefix().role('ROLE_TOKEN').required(),
23
+ name: Joi.string().min(1).max(32).required(),
24
+ description: Joi.string().min(16).max(128).required(),
25
+ symbol: Joi.string().min(2).max(6).uppercase().required(),
26
+ unit: Joi.string().min(1).max(6).lowercase().required(),
27
+ decimal: Joi.number().min(6).max(18).required(),
28
+ icon: Joi.string().optional().allow(null, ''),
29
+ totalSupply: Joi.BN().min(0).required(),
30
+ initialSupply: Joi.BN().min(0).required(),
31
+ maxTotalSupply: Joi.alternatives().try(Joi.BN().greater(0), Joi.string().valid('')).optional().allow(null),
32
+ foreignToken: schemas.foreignToken.optional().allow(null),
33
+ issuer: Joi.DID().prefix().optional().allow(null, ''),
34
+ website: Joi.string()
35
+ .uri({
36
+ scheme: isTest ? ['http', 'https'] : ['https'],
37
+ allowRelative: false,
38
+ })
39
+ .max(256)
40
+ .optional()
41
+ .allow(null, ''),
42
+ metadata: Joi.object({
43
+ type: Joi.string().valid('json').required(),
44
+ value: metadataSchema.optional(),
45
+ })
46
+ .optional()
47
+ .allow(null),
48
+ tokenFactoryAddress: Joi.DID().prefix().role('ROLE_TOKEN_FACTORY').optional().allow(null, ''),
49
+ spenders: Joi.array().items(Joi.DID().prefix()).max(30).optional().allow(null),
50
+ minters: Joi.array().items(Joi.DID().prefix()).max(30).optional().allow(null),
51
+ type: Joi.string().valid('Token', 'CreditToken', 'BondingCurveToken').optional().allow(null, ''),
52
+ context: schemas.context,
53
+ data: Joi.any().optional().allow(null),
54
+ }).options({
55
+ stripUnknown: true,
56
+ noDefaults: false,
57
+ });
58
+
18
59
  const create = (attrs, context) => {
19
60
  const token = {
20
61
  context: createStateContext(context),
@@ -35,6 +76,9 @@ const create = (attrs, context) => {
35
76
  'website',
36
77
  'metadata',
37
78
  'tokenFactoryAddress',
79
+ 'spenders',
80
+ 'minters',
81
+ 'type',
38
82
  ]),
39
83
  };
40
84
 
@@ -68,15 +112,13 @@ const validate = (state) => {
68
112
 
69
113
  const data = cloneDeep(state);
70
114
 
71
- if (data.metadata?.value) {
72
- const { value, error } = metadataSchema.validate(data.metadata.value, { stripUnknown: true });
73
- if (error) {
74
- throw new Error('INVALID_TOKEN', 'Invalid token metadata');
75
- }
76
- data.metadata.value = value;
115
+ // Validate the entire state with stateSchema
116
+ const { value, error } = stateSchema.validate(data);
117
+ if (error) {
118
+ throw new Error('INVALID_TOKEN', `Invalid token state: ${error.message}`);
77
119
  }
78
120
 
79
- return data;
121
+ return value;
80
122
  };
81
123
 
82
- module.exports = { create, update, metadataSchema };
124
+ module.exports = { create, update, metadataSchema, stateSchema };
package/lib/states/tx.js CHANGED
@@ -358,22 +358,29 @@ const getMintTokenReceipts = (tx, ctx) => {
358
358
  };
359
359
  });
360
360
 
361
- return inputReceipts.concat([
362
- {
363
- address: tx.itxJson.receiver,
364
- changes: [
365
- {
366
- target: tokenAddress,
367
- action: 'mint',
368
- value: `${tx.itxJson.amount}`,
369
- },
370
- ],
371
- },
372
- {
373
- address: owner,
374
- changes: hasFee ? [{ target: reserveAddress, action: 'fee', value: `${ctx.reserveFee}` }] : [],
375
- },
376
- ]);
361
+ return inputReceipts
362
+ .concat([
363
+ {
364
+ address: tx.itxJson.receiver,
365
+ changes: [
366
+ {
367
+ target: tokenAddress,
368
+ action: 'mint',
369
+ value: `${tx.itxJson.amount}`,
370
+ },
371
+ ],
372
+ },
373
+ ])
374
+ .concat(
375
+ hasFee
376
+ ? [
377
+ {
378
+ address: owner,
379
+ changes: [{ target: reserveAddress, action: 'fee', value: `${ctx.reserveFee}` }],
380
+ },
381
+ ]
382
+ : []
383
+ );
377
384
  };
378
385
 
379
386
  const getBurnTokenReceipts = (tx, ctx) => {
@@ -381,7 +388,7 @@ const getBurnTokenReceipts = (tx, ctx) => {
381
388
  const { reserveAddress, tokenAddress, owner } = tokenFactoryState;
382
389
 
383
390
  const fee = new BN(ctx.reserveFee || '0');
384
- const receiveAmount = new BN(ctx.reserveAmount).sub(fee).toString();
391
+ const receiveAmount = new BN(ctx.reserveAmount || '0').sub(fee);
385
392
 
386
393
  const inputReceipts = inputChanges.map((input) => {
387
394
  return {
@@ -396,16 +403,27 @@ const getBurnTokenReceipts = (tx, ctx) => {
396
403
  };
397
404
  });
398
405
 
399
- return inputReceipts.concat([
400
- {
401
- address: tx.itxJson.receiver,
402
- changes: [{ target: reserveAddress, action: 'swap', value: receiveAmount }],
403
- },
404
- {
405
- address: owner,
406
- changes: fee.gt(ZERO) ? [{ target: reserveAddress, action: 'fee', value: `${ctx.reserveFee}` }] : [],
407
- },
408
- ]);
406
+ return inputReceipts
407
+ .concat(
408
+ receiveAmount.gt(ZERO)
409
+ ? [
410
+ {
411
+ address: tx.itxJson.receiver,
412
+ changes: [{ target: reserveAddress, action: 'swap', value: receiveAmount.toString() }],
413
+ },
414
+ ]
415
+ : []
416
+ )
417
+ .concat(
418
+ fee.gt(ZERO)
419
+ ? [
420
+ {
421
+ address: owner,
422
+ changes: [{ target: reserveAddress, action: 'fee', value: fee.toString() }],
423
+ },
424
+ ]
425
+ : []
426
+ );
409
427
  };
410
428
 
411
429
  // Following only exist for legacy support purpose
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.27.7",
6
+ "version": "1.27.9",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -12,18 +12,18 @@
12
12
  "dependencies": {
13
13
  "bloom-filters": "^1.3.9",
14
14
  "lodash": "^4.17.21",
15
- "@arcblock/did": "1.27.7",
16
- "@arcblock/validator": "1.27.7",
17
- "@ocap/contract": "1.27.7",
18
- "@ocap/mcrypto": "1.27.7",
19
- "@ocap/message": "1.27.7",
20
- "@ocap/util": "1.27.7",
21
- "@ocap/wallet": "1.27.7"
15
+ "@arcblock/did": "1.27.9",
16
+ "@arcblock/validator": "1.27.9",
17
+ "@ocap/contract": "1.27.9",
18
+ "@ocap/mcrypto": "1.27.9",
19
+ "@ocap/message": "1.27.9",
20
+ "@ocap/util": "1.27.9",
21
+ "@ocap/wallet": "1.27.9"
22
22
  },
23
23
  "devDependencies": {
24
24
  "jest": "^29.7.0",
25
25
  "start-server-and-test": "^1.14.0",
26
- "@ocap/e2e-test": "1.27.7"
26
+ "@ocap/e2e-test": "1.27.9"
27
27
  },
28
28
  "keywords": [],
29
29
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",