dash-platform-sdk 1.5.3 → 1.6.0-dev.2

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 (60) hide show
  1. package/README.md +15 -15
  2. package/bundle.min.js +1 -1
  3. package/package.json +3 -4
  4. package/proto/generated/platform.client.d.ts +20 -60
  5. package/proto/generated/platform.client.js +41 -69
  6. package/proto/generated/platform.d.ts +2330 -1650
  7. package/proto/generated/platform.js +4992 -3467
  8. package/src/constants.d.ts +1 -1
  9. package/src/constants.js +1 -1
  10. package/src/documents/create.d.ts +1 -1
  11. package/src/documents/create.js +2 -2
  12. package/src/documents/createStateTransition.d.ts +2 -2
  13. package/src/documents/createStateTransition.js +6 -6
  14. package/src/documents/getActionFeeAgreement.d.ts +4 -0
  15. package/src/documents/getActionFeeAgreement.js +21 -0
  16. package/src/documents/index.d.ts +99 -4
  17. package/src/documents/index.js +117 -3
  18. package/src/documents/query.js +13 -52
  19. package/src/documents/queryAggregate.d.ts +4 -0
  20. package/src/documents/queryAggregate.js +32 -0
  21. package/src/documents/queryJoined.d.ts +16 -0
  22. package/src/documents/queryJoined.js +44 -0
  23. package/src/documents/queryRanked.d.ts +12 -0
  24. package/src/documents/queryRanked.js +37 -0
  25. package/src/documents/v1.d.ts +33 -0
  26. package/src/documents/v1.js +233 -0
  27. package/src/identities/getIdentityByNonUniquePublicKeyHash.js +6 -10
  28. package/src/names/index.d.ts +4 -1
  29. package/src/names/index.js +5 -2
  30. package/src/names/registerName.d.ts +1 -1
  31. package/src/names/registerName.js +55 -35
  32. package/src/shielded/getMostRecentShieldedAnchor.d.ts +2 -1
  33. package/src/shielded/getMostRecentShieldedAnchor.js +6 -4
  34. package/src/shielded/getShieldedAnchors.d.ts +2 -1
  35. package/src/shielded/getShieldedAnchors.js +6 -4
  36. package/src/shielded/getShieldedEncryptedNotes.d.ts +2 -1
  37. package/src/shielded/getShieldedEncryptedNotes.js +6 -4
  38. package/src/shielded/getShieldedNotesCount.d.ts +2 -1
  39. package/src/shielded/getShieldedNotesCount.js +6 -4
  40. package/src/shielded/getShieldedNullifiers.d.ts +2 -1
  41. package/src/shielded/getShieldedNullifiers.js +6 -4
  42. package/src/shielded/getShieldedPoolState.d.ts +2 -1
  43. package/src/shielded/getShieldedPoolState.js +6 -4
  44. package/src/shielded/index.d.ts +17 -7
  45. package/src/shielded/index.js +22 -12
  46. package/src/stateTransitions/index.d.ts +6 -2
  47. package/src/stateTransitions/index.js +5 -1
  48. package/src/stateTransitions/waitForStateTransitionResult.d.ts +1 -1
  49. package/src/stateTransitions/waitForStateTransitionResult.js +4 -2
  50. package/test/unit/DataContract.spec.js +2 -1
  51. package/test/unit/Document.spec.js +9 -9
  52. package/test/unit/DocumentActionFees.spec.d.ts +1 -0
  53. package/test/unit/DocumentActionFees.spec.js +99 -0
  54. package/test/unit/DocumentQueries.spec.d.ts +1 -0
  55. package/test/unit/DocumentQueries.spec.js +178 -0
  56. package/test/unit/DocumentsV1.spec.d.ts +1 -0
  57. package/test/unit/DocumentsV1.spec.js +75 -0
  58. package/test/unit/Names.spec.js +30 -0
  59. package/test/unit/Node.spec.js +11 -8
  60. package/types.d.ts +20 -1
@@ -1,16 +1,18 @@
1
1
  import { GetShieldedNullifiersRequest } from '../../proto/generated/platform.js';
2
- import { verifyShieldedNullifiersProof } from "pshenmic-dpp";
2
+ import { IdentifierWASM, verifyShieldedNullifiersProof } from 'pshenmic-dpp';
3
3
  import { LATEST_PLATFORM_VERSION } from "../constants.js";
4
4
  import { getQuorumPublicKey } from "../utils/getQuorumPublicKey.js";
5
5
  import bytesToHex from "../utils/bytesToHex.js";
6
6
  import verifyTenderdashProof from "../utils/verifyTenderdashProof.js";
7
- export default async function getShieldedNullifiers(grpcPool, nullifiers) {
7
+ export default async function getShieldedNullifiers(grpcPool, nullifiers, tokenIdentifier) {
8
+ const tokenId = tokenIdentifier != null ? new IdentifierWASM(tokenIdentifier) : undefined;
8
9
  const getShieldedNullifiersRequest = GetShieldedNullifiersRequest.create({
9
10
  version: {
10
11
  oneofKind: 'v0',
11
12
  v0: {
12
13
  nullifiers,
13
- prove: true
14
+ prove: true,
15
+ tokenId: tokenId?.bytes()
14
16
  }
15
17
  }
16
18
  });
@@ -27,7 +29,7 @@ export default async function getShieldedNullifiers(grpcPool, nullifiers) {
27
29
  if (metadata == null) {
28
30
  throw new Error('Metadata not found');
29
31
  }
30
- const { rootHash, nullifiers: nullifiersStatuses } = verifyShieldedNullifiersProof(proof.grovedbProof, nullifiers, true, LATEST_PLATFORM_VERSION);
32
+ const { rootHash, nullifiers: nullifiersStatuses } = verifyShieldedNullifiersProof(proof.grovedbProof, nullifiers, true, LATEST_PLATFORM_VERSION, tokenId);
31
33
  const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
32
34
  const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
33
35
  if (!verify) {
@@ -1,2 +1,3 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
- export default function getShieldedPoolState(grpcPool: GRPCPool): Promise<bigint | undefined>;
2
+ import { IdentifierLike } from 'pshenmic-dpp';
3
+ export default function getShieldedPoolState(grpcPool: GRPCPool, tokenIdentifier?: IdentifierLike): Promise<bigint | undefined>;
@@ -2,14 +2,16 @@ import { GetShieldedPoolStateRequest } from '../../proto/generated/platform.js';
2
2
  import { getQuorumPublicKey } from "../utils/getQuorumPublicKey.js";
3
3
  import bytesToHex from "../utils/bytesToHex.js";
4
4
  import verifyTenderdashProof from "../utils/verifyTenderdashProof.js";
5
- import { verifyShieldedPoolStateProof } from "pshenmic-dpp";
5
+ import { IdentifierWASM, verifyShieldedPoolStateProof } from 'pshenmic-dpp';
6
6
  import { LATEST_PLATFORM_VERSION } from "../constants.js";
7
- export default async function getShieldedPoolState(grpcPool) {
7
+ export default async function getShieldedPoolState(grpcPool, tokenIdentifier) {
8
+ const tokenId = tokenIdentifier != null ? new IdentifierWASM(tokenIdentifier) : undefined;
8
9
  const getShieldedPoolStateRequest = GetShieldedPoolStateRequest.create({
9
10
  version: {
10
11
  oneofKind: 'v0',
11
12
  v0: {
12
- prove: true
13
+ prove: true,
14
+ tokenId: tokenId?.bytes()
13
15
  }
14
16
  }
15
17
  });
@@ -26,7 +28,7 @@ export default async function getShieldedPoolState(grpcPool) {
26
28
  if (metadata == null) {
27
29
  throw new Error('Metadata not found');
28
30
  }
29
- const { rootHash, totalBalance } = verifyShieldedPoolStateProof(proof.grovedbProof, true, LATEST_PLATFORM_VERSION);
31
+ const { rootHash, totalBalance } = verifyShieldedPoolStateProof(proof.grovedbProof, true, LATEST_PLATFORM_VERSION, tokenId);
30
32
  const quorumPublicKey = await getQuorumPublicKey(grpcPool.network, proof.quorumType, bytesToHex(proof.quorumHash));
31
33
  const verify = await verifyTenderdashProof(proof, metadata, rootHash, quorumPublicKey);
32
34
  if (!verify) {
@@ -1,6 +1,6 @@
1
1
  import { GRPCPool } from '../grpcConnectionPool.js';
2
2
  import { ShieldedEncryptedNote, ShieldedNullifierStatus, ShieldedTransitionParamsMap, ShieldedTransitionType } from '../../types.js';
3
- import { RecoveredNoteWASM, ShieldedBuilderWASM, SpendableNoteWASM, StateTransitionWASM } from 'pshenmic-dpp';
3
+ import { IdentifierLike, RecoveredNoteWASM, ShieldedBuilderWASM, SpendableNoteWASM, StateTransitionWASM } from 'pshenmic-dpp';
4
4
  /**
5
5
  * Shielded controller for requesting information about shielded pool
6
6
  *
@@ -33,36 +33,45 @@ export declare class ShieldedController {
33
33
  *
34
34
  * @param startIndex {bigint} - index of the first note to fetch
35
35
  * @param count {number} - amount of notes to fetch
36
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
36
37
  *
37
38
  * @return {Promise<ShieldedEncryptedNote[]>}
38
39
  */
39
- getShieldedEncryptedNotes(startIndex: bigint, count: number): Promise<ShieldedEncryptedNote[]>;
40
+ getShieldedEncryptedNotes(startIndex: bigint, count: number, tokenIdentifier?: IdentifierLike): Promise<ShieldedEncryptedNote[]>;
40
41
  /**
41
42
  * Retrieves the set of valid anchors (note commitment tree roots) currently
42
43
  * accepted by the shielded pool.
43
44
  *
45
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
46
+ *
44
47
  * @return {Promise<Uint8Array[]>}
45
48
  */
46
- getShieldedAnchors(): Promise<Uint8Array[]>;
49
+ getShieldedAnchors(tokenIdentifier?: IdentifierLike): Promise<Uint8Array[]>;
47
50
  /**
48
51
  * Retrieves the most recent anchor (note commitment tree root) of the shielded pool.
49
52
  *
53
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
54
+ *
50
55
  * @return {Promise<Uint8Array>}
51
56
  */
52
- getMostRecentShieldedAnchor(): Promise<Uint8Array | undefined>;
57
+ getMostRecentShieldedAnchor(tokenIdentifier?: IdentifierLike): Promise<Uint8Array | undefined>;
53
58
  /**
54
59
  * Retrieves the total balance currently held in the shielded pool.
55
60
  *
61
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
62
+ *
56
63
  * @return {Promise<bigint>}
57
64
  */
58
- getShieldedPoolState(): Promise<bigint | undefined>;
65
+ getShieldedPoolState(tokenIdentifier?: IdentifierLike): Promise<bigint | undefined>;
59
66
  /**
60
67
  * Retrieves the total count of notes (leaves) in the shielded notes
61
68
  * commitment tree. Useful as a denominator for shielded sync progress.
62
69
  *
70
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
71
+ *
63
72
  * @return {Promise<bigint>}
64
73
  */
65
- getShieldedNotesCount(): Promise<bigint | undefined>;
74
+ getShieldedNotesCount(tokenIdentifier?: IdentifierLike): Promise<bigint | undefined>;
66
75
  /**
67
76
  * Checks the spent status of the given nullifiers in the shielded pool.
68
77
  *
@@ -70,10 +79,11 @@ export declare class ShieldedController {
70
79
  * it has already been spent.
71
80
  *
72
81
  * @param nullifiers {Uint8Array[]} - nullifiers to check
82
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
73
83
  *
74
84
  * @return {Promise<ShieldedNullifierStatus[]>}
75
85
  */
76
- getShieldedNullifiers(nullifiers: Uint8Array[]): Promise<ShieldedNullifierStatus[]>;
86
+ getShieldedNullifiers(nullifiers: Uint8Array[], tokenIdentifier?: IdentifierLike): Promise<ShieldedNullifierStatus[]>;
77
87
  /**
78
88
  * Recovers the notes owned by a wallet from a set of shielded encrypted notes
79
89
  * (as returned by {@link getShieldedEncryptedNotes}) by trial-decrypting each
@@ -49,45 +49,54 @@ export class ShieldedController {
49
49
  *
50
50
  * @param startIndex {bigint} - index of the first note to fetch
51
51
  * @param count {number} - amount of notes to fetch
52
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
52
53
  *
53
54
  * @return {Promise<ShieldedEncryptedNote[]>}
54
55
  */
55
- async getShieldedEncryptedNotes(startIndex, count) {
56
- return await getShieldedEncryptedNotes(this.grpcPool, startIndex, count);
56
+ async getShieldedEncryptedNotes(startIndex, count, tokenIdentifier) {
57
+ return await getShieldedEncryptedNotes(this.grpcPool, startIndex, count, tokenIdentifier);
57
58
  }
58
59
  /**
59
60
  * Retrieves the set of valid anchors (note commitment tree roots) currently
60
61
  * accepted by the shielded pool.
61
62
  *
63
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
64
+ *
62
65
  * @return {Promise<Uint8Array[]>}
63
66
  */
64
- async getShieldedAnchors() {
65
- return await getShieldedAnchors(this.grpcPool);
67
+ async getShieldedAnchors(tokenIdentifier) {
68
+ return await getShieldedAnchors(this.grpcPool, tokenIdentifier);
66
69
  }
67
70
  /**
68
71
  * Retrieves the most recent anchor (note commitment tree root) of the shielded pool.
69
72
  *
73
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
74
+ *
70
75
  * @return {Promise<Uint8Array>}
71
76
  */
72
- async getMostRecentShieldedAnchor() {
73
- return await getMostRecentShieldedAnchor(this.grpcPool);
77
+ async getMostRecentShieldedAnchor(tokenIdentifier) {
78
+ return await getMostRecentShieldedAnchor(this.grpcPool, tokenIdentifier);
74
79
  }
75
80
  /**
76
81
  * Retrieves the total balance currently held in the shielded pool.
77
82
  *
83
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
84
+ *
78
85
  * @return {Promise<bigint>}
79
86
  */
80
- async getShieldedPoolState() {
81
- return await getShieldedPoolState(this.grpcPool);
87
+ async getShieldedPoolState(tokenIdentifier) {
88
+ return await getShieldedPoolState(this.grpcPool, tokenIdentifier);
82
89
  }
83
90
  /**
84
91
  * Retrieves the total count of notes (leaves) in the shielded notes
85
92
  * commitment tree. Useful as a denominator for shielded sync progress.
86
93
  *
94
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
95
+ *
87
96
  * @return {Promise<bigint>}
88
97
  */
89
- async getShieldedNotesCount() {
90
- return await getShieldedNotesCount(this.grpcPool);
98
+ async getShieldedNotesCount(tokenIdentifier) {
99
+ return await getShieldedNotesCount(this.grpcPool, tokenIdentifier);
91
100
  }
92
101
  /**
93
102
  * Checks the spent status of the given nullifiers in the shielded pool.
@@ -96,11 +105,12 @@ export class ShieldedController {
96
105
  * it has already been spent.
97
106
  *
98
107
  * @param nullifiers {Uint8Array[]} - nullifiers to check
108
+ * @param tokenIdentifier {IdentifierLike=} - token id to query the token shielded pool instead of the credits pool
99
109
  *
100
110
  * @return {Promise<ShieldedNullifierStatus[]>}
101
111
  */
102
- async getShieldedNullifiers(nullifiers) {
103
- return await getShieldedNullifiers(this.grpcPool, nullifiers);
112
+ async getShieldedNullifiers(nullifiers, tokenIdentifier) {
113
+ return await getShieldedNullifiers(this.grpcPool, nullifiers, tokenIdentifier);
104
114
  }
105
115
  /**
106
116
  * Recovers the notes owned by a wallet from a set of shielded encrypted notes
@@ -20,9 +20,13 @@ export declare class StateTransitionsController {
20
20
  /**
21
21
  * Waits for a given state transition to finalize in the network (usually 1-3 sec)
22
22
  *
23
+ * Returns the owner's credit balance after the transition executed, if the proof
24
+ * carries it (protocol v14+ owned, fee-paying transitions: document and token batches,
25
+ * contract create and update, identity update), otherwise undefined
26
+ *
23
27
  * @param stateTransition {StateTransitionWASM}
24
28
  *
25
- * @return {Promise<void>}
29
+ * @return {Promise<bigint | undefined>}
26
30
  */
27
- waitForStateTransitionResult(stateTransition: StateTransitionWASM): Promise<void>;
31
+ waitForStateTransitionResult(stateTransition: StateTransitionWASM): Promise<bigint | undefined>;
28
32
  }
@@ -24,9 +24,13 @@ export class StateTransitionsController {
24
24
  /**
25
25
  * Waits for a given state transition to finalize in the network (usually 1-3 sec)
26
26
  *
27
+ * Returns the owner's credit balance after the transition executed, if the proof
28
+ * carries it (protocol v14+ owned, fee-paying transitions: document and token batches,
29
+ * contract create and update, identity update), otherwise undefined
30
+ *
27
31
  * @param stateTransition {StateTransitionWASM}
28
32
  *
29
- * @return {Promise<void>}
33
+ * @return {Promise<bigint | undefined>}
30
34
  */
31
35
  async waitForStateTransitionResult(stateTransition) {
32
36
  return await waitForStateTransitionResult(this.grpcPool, stateTransition);
@@ -1,3 +1,3 @@
1
1
  import { StateTransitionWASM } from 'pshenmic-dpp';
2
2
  import { GRPCPool } from '../grpcConnectionPool.js';
3
- export default function waitForStateTransitionResult(grpcPool: GRPCPool, stateTransition: StateTransitionWASM): Promise<void>;
3
+ export default function waitForStateTransitionResult(grpcPool: GRPCPool, stateTransition: StateTransitionWASM): Promise<bigint | undefined>;
@@ -14,7 +14,8 @@ export default async function waitForStateTransitionResult(grpcPool, stateTransi
14
14
  oneofKind: 'v0',
15
15
  v0: {
16
16
  stateTransitionHash: hexToBytes(txHash),
17
- prove: true
17
+ prove: true,
18
+ requestUserBalance: false
18
19
  }
19
20
  }
20
21
  });
@@ -51,7 +52,7 @@ export default async function waitForStateTransitionResult(grpcPool, stateTransi
51
52
  coreHeight: coreChainLockedHeight,
52
53
  epoch
53
54
  };
54
- const { rootHash, result } = verifyStateTransitionResult(proof.grovedbProof, stateTransition, blockInfo, knownContracts, LATEST_PLATFORM_VERSION);
55
+ const { rootHash, result, ownerBalance } = verifyStateTransitionResult(proof.grovedbProof, stateTransition, blockInfo, knownContracts, LATEST_PLATFORM_VERSION);
55
56
  if (result == null) {
56
57
  throw new Error('State transition result was null');
57
58
  }
@@ -60,6 +61,7 @@ export default async function waitForStateTransitionResult(grpcPool, stateTransi
60
61
  if (!verify) {
61
62
  throw new Error('Failed to verify wait for state transition result query');
62
63
  }
64
+ return ownerBalance;
63
65
  }
64
66
  else if (v0.result.oneofKind === 'error') {
65
67
  const { code, message, data } = v0.result.error;
@@ -61,7 +61,8 @@ describe('DataContract', () => {
61
61
  test('should be able to create data contract with optional params', async () => {
62
62
  const dataContract = sdk.dataContracts.create(ownerIdentifier, identityNonce, schema, true, undefined, config);
63
63
  expect(dataContract).toEqual(expect.any(DataContractWASM));
64
- expect(dataContract.getConfig()).toEqual(config);
64
+ // platform v14 upgrades the contract config to format version 2
65
+ expect(dataContract.getConfig()).toEqual({ ...config, $formatVersion: '2' });
65
66
  });
66
67
  test('should be able to create data contract create transition', async () => {
67
68
  const dataContract = sdk.dataContracts.create(ownerIdentifier, identityNonce, schema);
@@ -21,7 +21,7 @@ describe('Document', () => {
21
21
  };
22
22
  });
23
23
  test('should be able to create document', async () => {
24
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision);
24
+ const document = sdk.documents.create(dataContract, documentType, data, identity, BigInt(1), revision);
25
25
  expect(document).toEqual(expect.any(DocumentWASM));
26
26
  });
27
27
  test('should be able to get documents', async () => {
@@ -73,14 +73,14 @@ describe('Document', () => {
73
73
  });
74
74
  describe('should be able to create state transition', () => {
75
75
  test('should be able to create a create transition', async () => {
76
- const document = sdk.documents.create(dataContract, documentType, data, identity);
77
76
  const identityContractNonce = BigInt(1);
77
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce);
78
78
  const stateTransition = sdk.documents.createStateTransition(document, 'create', { identityContractNonce });
79
79
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
80
80
  });
81
81
  test('should be able to create a contested document transition', async () => {
82
- const document = sdk.documents.create(dataContract, documentType, data, identity);
83
82
  const identityContractNonce = BigInt(1);
83
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce);
84
84
  // 0.2 Dash typically
85
85
  const contestedFee = BigInt(20000000000);
86
86
  const indexName = 'parentNameAndLabel';
@@ -89,8 +89,8 @@ describe('Document', () => {
89
89
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
90
90
  });
91
91
  test('should be able to create a document with token payment info', async () => {
92
- const document = sdk.documents.create(dataContract, documentType, data, identity);
93
92
  const identityContractNonce = BigInt(1);
93
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce);
94
94
  const tokenPaymentInfo = {
95
95
  tokenContractId: '6hVQW16jyvZyGSQk2YVty4ND6bgFXozizYWnPt753uW5',
96
96
  tokenContractPosition: 0,
@@ -102,34 +102,34 @@ describe('Document', () => {
102
102
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
103
103
  });
104
104
  test('should be able to create a replace transition', async () => {
105
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
106
105
  const identityContractNonce = BigInt(1);
106
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
107
107
  const stateTransition = sdk.documents.createStateTransition(document, 'replace', { identityContractNonce });
108
108
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
109
109
  });
110
110
  test('should be able to create a delete transition', async () => {
111
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
112
111
  const identityContractNonce = BigInt(1);
112
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
113
113
  const stateTransition = sdk.documents.createStateTransition(document, 'delete', { identityContractNonce });
114
114
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
115
115
  });
116
116
  test('should be able to create a transfer transition', async () => {
117
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
118
117
  const identityContractNonce = BigInt(1);
118
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
119
119
  const recipientId = '6hVQW16jyvZyGSQk2YVty4ND6bgFXozizYWnPt753uW5';
120
120
  const stateTransition = sdk.documents.createStateTransition(document, 'transfer', { identityContractNonce, recipientId });
121
121
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
122
122
  });
123
123
  test('should be able to create an updatePrice transition', async () => {
124
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
125
124
  const identityContractNonce = BigInt(1);
125
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
126
126
  const price = BigInt(10000000);
127
127
  const stateTransition = sdk.documents.createStateTransition(document, 'updatePrice', { identityContractNonce, price });
128
128
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
129
129
  });
130
130
  test('should be able to create a purchase transition', async () => {
131
- const document = sdk.documents.create(dataContract, documentType, data, identity, revision + BigInt(1));
132
131
  const identityContractNonce = BigInt(1);
132
+ const document = sdk.documents.create(dataContract, documentType, data, identity, identityContractNonce, revision + BigInt(1));
133
133
  const amount = BigInt(10000000);
134
134
  const stateTransition = sdk.documents.createStateTransition(document, 'purchase', { identityContractNonce, amount });
135
135
  expect(stateTransition).toEqual(expect.any(StateTransitionWASM));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,99 @@
1
+ import { AgreedFeeMultiplierWASM, BatchTransitionWASM, DocumentActionFeeAgreementWASM } from 'pshenmic-dpp';
2
+ import { DashPlatformSDK } from '../../src/DashPlatformSDK.js';
3
+ const DPNS = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec';
4
+ const owner = 'QMfCRPcjXoTnZa9sA9JR2KWgGxZXMRJ4akgS3Uia1Qv';
5
+ // a document type that charges the contract owner 0.0001 Dash (10000000 credits) per created note
6
+ // and 0.00005 Dash per purchase. Fixed pricing charges the amounts as written, `feeMultiplier`
7
+ // pricing (the default) scales them by the epoch fee multiplier
8
+ function noteSchema(pricing) {
9
+ return {
10
+ note: {
11
+ type: 'object',
12
+ properties: {
13
+ message: { type: 'string', maxLength: 100, position: 0 }
14
+ },
15
+ additionalProperties: false,
16
+ transferable: 1,
17
+ tradeMode: 1,
18
+ actionFees: {
19
+ pricing,
20
+ create: { owner: 10000000 },
21
+ purchase: { owner: 5000000 }
22
+ }
23
+ }
24
+ };
25
+ }
26
+ // reads the agreement a state transition carries
27
+ function agreementOf(stateTransition) {
28
+ const [batched] = BatchTransitionWASM.fromStateTransition(stateTransition).transitions;
29
+ const transition = batched.toTransition();
30
+ return transition.createTransition.base.actionFeeAgreement;
31
+ }
32
+ let sdk;
33
+ let fixedContract;
34
+ let multipliedContract;
35
+ describe('Document action fees', () => {
36
+ beforeAll(() => {
37
+ sdk = new DashPlatformSDK({ network: 'testnet' });
38
+ fixedContract = sdk.dataContracts.create(owner, 1n, noteSchema('fixed'));
39
+ multipliedContract = sdk.dataContracts.create(owner, 2n, noteSchema('feeMultiplier'));
40
+ });
41
+ test('should be able to derive the agreement to a fixed fee', () => {
42
+ // the fee multiplier of the current epoch (sdk.node.getEpochsInfo(1, false)), 1000 permille = x1
43
+ const feeMultiplier = new AgreedFeeMultiplierWASM(1000n, 0);
44
+ const agreement = DocumentActionFeeAgreementWASM.forDocumentTypeAction(fixedContract, 'note', 'Create', feeMultiplier);
45
+ expect(agreement?.owner).toEqual(10000000n);
46
+ expect(agreement?.moderators).toEqual(0n);
47
+ // a fixed fee does not follow the multiplier, so the agreement names none
48
+ expect(agreement?.pricing).toEqual('fixed');
49
+ expect(agreement?.feeMultiplier).toEqual(undefined);
50
+ });
51
+ test('should be able to derive the agreement to a fee priced by the fee multiplier', () => {
52
+ // accept the multiplier to grow by up to 20% before the transition executes
53
+ const feeMultiplier = new AgreedFeeMultiplierWASM(1000n, 20);
54
+ const agreement = DocumentActionFeeAgreementWASM.forDocumentTypeAction(multipliedContract, 'note', 'Purchase', feeMultiplier);
55
+ expect(agreement?.owner).toEqual(5000000n);
56
+ expect(agreement?.pricing).toEqual('feeMultiplier');
57
+ expect(agreement?.feeMultiplier?.knownPermille).toEqual(1000n);
58
+ expect(agreement?.feeMultiplier?.increaseTolerancePercent).toEqual(20);
59
+ });
60
+ test('should get no agreement for an action without a fee', () => {
61
+ const feeMultiplier = new AgreedFeeMultiplierWASM(1000n, 0);
62
+ expect(DocumentActionFeeAgreementWASM.forDocumentTypeAction(fixedContract, 'note', 'Delete', feeMultiplier)).toEqual(undefined);
63
+ });
64
+ test('should be able to check an agreement the way the network does', () => {
65
+ const agreement = DocumentActionFeeAgreementWASM.forDocumentTypeAction(multipliedContract, 'note', 'Create', new AgreedFeeMultiplierWASM(1000n, 20));
66
+ // the multiplier of the executing epoch may be up to 20% higher than the known one
67
+ expect(agreement.verify(multipliedContract, 'note', 'Create', 1200n)).toEqual(true);
68
+ expect(agreement.verify(multipliedContract, 'note', 'Create', 1201n)).toEqual(false);
69
+ // the agreement must name the fee the document type declares for the action
70
+ expect(agreement.verify(multipliedContract, 'note', 'Purchase', 1000n)).toEqual(false);
71
+ });
72
+ test('should be able to create a transition with an agreement', () => {
73
+ const document = sdk.documents.create(fixedContract.id, 'note', { message: 'hello' }, owner, 1n);
74
+ const feeMultiplier = new AgreedFeeMultiplierWASM(1000n, 0);
75
+ const actionFeeAgreement = DocumentActionFeeAgreementWASM.forDocumentTypeAction(fixedContract, 'note', 'Create', feeMultiplier);
76
+ const stateTransition = sdk.documents.createStateTransition(document, 'create', { identityContractNonce: 1n, actionFeeAgreement });
77
+ expect(agreementOf(stateTransition)?.owner).toEqual(10000000n);
78
+ });
79
+ test('should be able to create a transition with an agreement as a plain object', () => {
80
+ const document = sdk.documents.create(multipliedContract.id, 'note', { message: 'hello' }, owner, 1n);
81
+ const stateTransition = sdk.documents.createStateTransition(document, 'create', {
82
+ identityContractNonce: 1n,
83
+ actionFeeAgreement: {
84
+ owner: 10000000n,
85
+ moderators: 0n,
86
+ // omit for a fixed fee
87
+ feeMultiplier: { knownPermille: 1000n, increaseTolerancePercent: 10 }
88
+ }
89
+ });
90
+ const agreement = agreementOf(stateTransition);
91
+ expect(agreement?.pricing).toEqual('feeMultiplier');
92
+ expect(agreement?.feeMultiplier?.increaseTolerancePercent).toEqual(10);
93
+ });
94
+ test('should be able to get the agreement for a contract on the network', async () => {
95
+ // reads the contract and the current epoch fee multiplier, DPNS declares no action fees
96
+ const agreement = await sdk.documents.getActionFeeAgreement(DPNS, 'domain', 'create', 10);
97
+ expect(agreement).toEqual(undefined);
98
+ });
99
+ });
@@ -0,0 +1 @@
1
+ export {};