Skip to content

Commit f9f762f

Browse files
authored
feat: add example retrieving contract history (#22)
* feat: add example retrieving contract history Also includes register/update for a contract with history * refactor: remove contract config options that can't be modified * refactor: rename contract history * chore: add comment * refactor: rename function * refactor: remove wasm-dpp DPP is included in SDK so wasm-dpp isn't required * style: minor updates * chore: remove url
1 parent 8def945 commit f9f762f

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const Dash = require('dash');
2+
const dotenv = require('dotenv');
3+
dotenv.config();
4+
5+
const clientOpts = {
6+
network: process.env.NETWORK,
7+
wallet: {
8+
mnemonic: process.env.MNEMONIC, // A Dash wallet mnemonic with testnet funds
9+
unsafeOptions: {
10+
skipSynchronizationBeforeHeight: 675000, // only sync from early-2022
11+
},
12+
},
13+
};
14+
const client = new Dash.Client(clientOpts);
15+
16+
const registerContract = async () => {
17+
const { platform } = client;
18+
const identity = await platform.identities.get(process.env.IDENTITY_ID); // Your identity ID
19+
20+
const contractDocuments = {
21+
note: {
22+
type: 'object',
23+
properties: {
24+
message: {
25+
type: 'string',
26+
},
27+
},
28+
additionalProperties: false,
29+
},
30+
};
31+
32+
const contract = await platform.contracts.create(contractDocuments, identity);
33+
contract.setConfig({
34+
canBeDeleted: false,
35+
readonly: false, // Make contract read-only
36+
keepsHistory: true, // Enable storing of contract history
37+
documentsKeepHistoryContractDefault: false,
38+
documentsMutableContractDefault: true,
39+
});
40+
console.dir({ contract: contract.toJSON() });
41+
42+
// Sign and submit the data contract
43+
await platform.contracts.publish(contract, identity);
44+
return contract;
45+
};
46+
47+
registerContract()
48+
.then((d) => console.log('Contract registered:\n', d.toJSON()))
49+
.catch((e) => console.error('Something went wrong:\n', e))
50+
.finally(() => client.disconnect());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Dash = require('dash');
2+
const dotenv = require('dotenv');
3+
dotenv.config();
4+
5+
const client = new Dash.Client({ network: process.env.NETWORK });
6+
7+
const retrieveContractHistory = async () => {
8+
const contractId = process.env.CONTRACT_ID; // Your contract ID
9+
return await client.platform.contracts.history(contractId, 0, 10, 0);
10+
};
11+
12+
retrieveContractHistory()
13+
.then((d) => {
14+
Object.entries(d).forEach(([key, value]) => {
15+
client.platform.dpp.dataContract
16+
.createFromObject(value)
17+
.then((contract) => console.dir(contract.toJSON(), { depth: 5 }));
18+
});
19+
})
20+
.catch((e) => console.error('Something went wrong:\n', e))
21+
.finally(() => client.disconnect());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const Dash = require('dash');
2+
const dotenv = require('dotenv');
3+
dotenv.config();
4+
5+
const clientOpts = {
6+
network: process.env.NETWORK,
7+
wallet: {
8+
mnemonic: process.env.MNEMONIC, // A Dash wallet mnemonic with testnet funds
9+
unsafeOptions: {
10+
skipSynchronizationBeforeHeight: 675000, // only sync from early-2022
11+
},
12+
},
13+
};
14+
const client = new Dash.Client(clientOpts);
15+
16+
const updateContract = async () => {
17+
const { platform } = client;
18+
const identity = await platform.identities.get(process.env.IDENTITY_ID); // Your identity ID
19+
20+
const existingDataContract = await platform.contracts.get(
21+
process.env.CONTRACT_ID,
22+
);
23+
const documentSchema = existingDataContract.getDocumentSchema('note');
24+
console.log(documentSchema);
25+
26+
documentSchema.properties.author = {
27+
type: 'string',
28+
};
29+
30+
existingDataContract.setDocumentSchema('note', documentSchema);
31+
existingDataContract.setConfig({
32+
keepsHistory: true, // Enable storing of contract history
33+
});
34+
35+
console.log(existingDataContract.getDocumentSchema('note'));
36+
console.dir(existingDataContract.toJSON());
37+
// Sign and submit the data contract
38+
await platform.contracts.update(existingDataContract, identity);
39+
return existingDataContract;
40+
};
41+
42+
updateContract()
43+
.then((d) => console.log('Contract updated:\n', d.toJSON()))
44+
.catch((e) => console.error('Something went wrong:\n', e))
45+
.finally(() => client.disconnect());
46+

0 commit comments

Comments
 (0)