Skip to content

Commit 6ee7e97

Browse files
thephezclaude
andcommitted
feat: migrate contracts tutorials to evo-sdk ESM
Migrate all contract register/retrieve/update tutorials from legacy js-dash-sdk (CJS) to evo-sdk (ESM). Adds new variants (binary, indexed, timestamps, history-retrieve) and works around sdk.contracts.fetch() returning undefined for keepsHistory contracts (dashpay/platform#3165). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ebf1349 commit 6ee7e97

17 files changed

Lines changed: 461 additions & 291 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { DataContract } from '@dashevo/evo-sdk';
2+
import { setupDashClient } from '../setupDashClient.mjs';
3+
4+
const { sdk, keyManager } = await setupDashClient();
5+
const { identity, identityKey, signer } = await keyManager.getAuth();
6+
7+
// Define the document schemas for the contract
8+
const documentSchemas = {
9+
block: {
10+
type: 'object',
11+
properties: {
12+
hash: {
13+
type: 'array',
14+
byteArray: true,
15+
maxItems: 64,
16+
description: 'Store block hashes',
17+
position: 0,
18+
},
19+
},
20+
additionalProperties: false,
21+
},
22+
};
23+
24+
try {
25+
// Get the next identity nonce for contract creation
26+
const identityNonce = await sdk.identities.nonce(identity.id.toString());
27+
28+
// Create the data contract
29+
const dataContract = new DataContract({
30+
ownerId: identity.id,
31+
identityNonce: (identityNonce || 0n) + 1n,
32+
schemas: documentSchemas,
33+
fullValidation: true,
34+
});
35+
36+
// Publish the contract to the platform
37+
const publishedContract = await sdk.contracts.publish({
38+
dataContract,
39+
identityKey,
40+
signer,
41+
});
42+
43+
console.log('Contract registered:\n', publishedContract.toJSON());
44+
} catch (e) {
45+
console.error('Something went wrong:\n', e.message);
46+
}

2-Contracts-and-Documents/contract-register-history.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/contracts-and-documents/register-a-data-contract.html
2+
import { DataContract } from '@dashevo/evo-sdk';
3+
import { setupDashClient } from '../setupDashClient.mjs';
4+
5+
const { sdk, keyManager } = await setupDashClient();
6+
const { identity, identityKey, signer } = await keyManager.getAuth();
7+
8+
const documentSchemas = {
9+
note: {
10+
type: 'object',
11+
properties: {
12+
message: {
13+
type: 'string',
14+
position: 0,
15+
},
16+
},
17+
additionalProperties: false,
18+
},
19+
};
20+
21+
try {
22+
const identityNonce = await sdk.identities.nonce(identity.id.toString());
23+
24+
const dataContract = new DataContract({
25+
ownerId: identity.id,
26+
identityNonce: (identityNonce || 0n) + 1n,
27+
schemas: documentSchemas,
28+
fullValidation: true,
29+
});
30+
31+
// Enable storing of contract history
32+
dataContract.setConfig({
33+
canBeDeleted: false,
34+
readonly: false,
35+
keepsHistory: true,
36+
documentsKeepHistoryContractDefault: false,
37+
documentsMutableContractDefault: true,
38+
});
39+
40+
const publishedContract = await sdk.contracts.publish({
41+
dataContract,
42+
identityKey,
43+
signer,
44+
});
45+
46+
console.log('Contract registered:\n', publishedContract.toJSON());
47+
} catch (e) {
48+
console.error('Something went wrong:\n', e.message);
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { DataContract } from '@dashevo/evo-sdk';
2+
import { setupDashClient } from '../setupDashClient.mjs';
3+
4+
const { sdk, keyManager } = await setupDashClient();
5+
const { identity, identityKey, signer } = await keyManager.getAuth();
6+
7+
// Define the document schemas for the contract
8+
const documentSchemas = {
9+
note: {
10+
type: 'object',
11+
indices: [{
12+
name: 'ownerId',
13+
properties: [{ $ownerId: 'asc' }],
14+
unique: false,
15+
}],
16+
properties: {
17+
message: {
18+
type: 'string',
19+
position: 0,
20+
},
21+
},
22+
additionalProperties: false,
23+
},
24+
};
25+
26+
try {
27+
// Get the next identity nonce for contract creation
28+
const identityNonce = await sdk.identities.nonce(identity.id.toString());
29+
30+
// Create the data contract
31+
const dataContract = new DataContract({
32+
ownerId: identity.id,
33+
identityNonce: (identityNonce || 0n) + 1n,
34+
schemas: documentSchemas,
35+
fullValidation: true,
36+
});
37+
38+
// Publish the contract to the platform
39+
const publishedContract = await sdk.contracts.publish({
40+
dataContract,
41+
identityKey,
42+
signer,
43+
});
44+
45+
console.log('Contract registered:\n', publishedContract.toJSON());
46+
} catch (e) {
47+
console.error('Something went wrong:\n', e.message);
48+
}

2-Contracts-and-Documents/contract-register-minimal.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// See https://docs.dash.org/projects/platform/en/stable/docs/tutorials/contracts-and-documents/register-a-data-contract.html
2+
import { DataContract } from '@dashevo/evo-sdk';
3+
import { setupDashClient } from '../setupDashClient.mjs';
4+
5+
const { sdk, keyManager } = await setupDashClient();
6+
const { identity, identityKey, signer } = await keyManager.getAuth();
7+
8+
// Define the document schemas for the contract
9+
const documentSchemas = {
10+
note: {
11+
type: 'object',
12+
properties: {
13+
message: {
14+
type: 'string',
15+
position: 0,
16+
},
17+
},
18+
additionalProperties: false,
19+
},
20+
};
21+
22+
try {
23+
// Get the next identity nonce for contract creation
24+
const identityNonce = await sdk.identities.nonce(identity.id.toString());
25+
26+
// Create the data contract
27+
const dataContract = new DataContract({
28+
ownerId: identity.id,
29+
identityNonce: (identityNonce || 0n) + 1n,
30+
schemas: documentSchemas,
31+
fullValidation: true,
32+
});
33+
34+
// Publish the contract to the platform
35+
const publishedContract = await sdk.contracts.publish({
36+
dataContract,
37+
identityKey,
38+
signer,
39+
});
40+
41+
console.log('Contract registered:\n', publishedContract.toJSON());
42+
} catch (e) {
43+
console.error('Something went wrong:\n', e.message);
44+
}

2-Contracts-and-Documents/contract-register-nft.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)