Skip to content

Commit f8910da

Browse files
committed
feat: migrate document update to evo sdk
1 parent e8912e5 commit f8910da

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Document } 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+
// Default tutorial contract (testnet). Replace or override via DATA_CONTRACT_ID.
8+
const DATA_CONTRACT_ID =
9+
process.env.DATA_CONTRACT_ID ??
10+
'FW3DHrQiG24VqzPY4ARenMgjEPpBNuEQTZckV8hbVCG4';
11+
12+
// Replace with your existing document ID from the Submit Documents tutorial
13+
const DOCUMENT_ID = 'YOUR_DOCUMENT_ID';
14+
15+
try {
16+
// Fetch the existing document to get current revision
17+
const docs = await sdk.documents.query({
18+
dataContractId: DATA_CONTRACT_ID,
19+
documentTypeName: 'note',
20+
where: [['$id', '==', DOCUMENT_ID]],
21+
});
22+
const existingDoc = [...docs.values()][0];
23+
if (!existingDoc) {
24+
throw new Error(`Document ${DOCUMENT_ID} not found`);
25+
}
26+
27+
// Create the replacement document with incremented revision
28+
const document = new Document({
29+
properties: {
30+
message: `Updated Tutorial Test @ ${new Date().toUTCString()}`,
31+
},
32+
documentTypeName: 'note',
33+
dataContractId: DATA_CONTRACT_ID,
34+
ownerId: identity.id,
35+
revision: existingDoc.revision + 1n,
36+
id: DOCUMENT_ID,
37+
});
38+
39+
// Submit the replacement to the platform
40+
await sdk.documents.replace({
41+
document,
42+
identityKey,
43+
signer,
44+
});
45+
46+
console.log('Document updated:\n', document.toJSON());
47+
} catch (e) {
48+
console.error('Something went wrong:\n', e.message);
49+
}

0 commit comments

Comments
 (0)