Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data contract utility update #205

Merged
merged 12 commits into from
Aug 14, 2024
4 changes: 3 additions & 1 deletion packages/data-contract/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SKIP_SYNCHRONIZATION_BEFORE_HEIGHT=1000000
MNEMONIC=""
OWNER_IDENTIFIER=""
OWNER_IDENTIFIER=""
DOCUMENT_NAME=""
CONTRACT_ID=""
38 changes: 36 additions & 2 deletions packages/data-contract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,46 @@ before launching, you need to enter your data in the `.env` file
- Node.js 20+

Insert your data in .env
```
npm install
```
---

## Register Identifier
We can register new identifier by this command
```
npm run identity:register
```
**Required env vars:**
* `MNEMONIC`
* `OWNER_IDENTIFIER`
---

### Start
## Deploy Contract
We can deploy data-contract from `schema.json` by this command
```
npm run dataContract:deploy
```
**Required env vars:**
* `MNEMONIC`
* `OWNER_IDENTIFIER`
---

## Push Document
We can push document from `document.json` by this command
```
npm install
npm run dataContract:deploy
```
**Required env vars:**
* `MNEMONIC`
* `OWNER_IDENTIFIER`
* `DOCUMENT_NAME`
* `CONTRACT_ID`
---

## Env Vars
* `MNEMONIC` - wallet mnemonic
* `OWNER_IDENTIFIER` - identifier which be used for interaction with blockchain
* `DOCUMENT_NAME` - name for document
* `CONTRACT_ID` - contract id for push
* `SKIP_SYNCHRONIZATION_BEFORE_HEIGHT` - core property
4 changes: 4 additions & 0 deletions packages/data-contract/document.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Example1",
"identifier": "1234567890123456789012345678901234567890123"
}
37 changes: 0 additions & 37 deletions packages/data-contract/index.js

This file was deleted.

4 changes: 3 additions & 1 deletion packages/data-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dataContract:deploy": "node index.js",
"dataContract:deploy": "node ./src/actions/deployContract.js",
"identity:register": "node ./src/actions/registerIdentity.js",
"document:push": "node ./src/actions/pushDocument.js",
"lint": "standard ."
},
"repository": {
Expand Down
31 changes: 31 additions & 0 deletions packages/data-contract/src/actions/deployContract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require('dotenv').config()
const schema = require('../../schema.json')

const { initClient } = require('../utils')

async function deployContract () {
console.log('Deploying Contract')

if (!process.env.MNEMONIC) {
throw new Error('Mnemonic not setted')
}

if (!process.env.OWNER_IDENTIFIER) {
throw new Error('No identity in env :(')
}

const client = initClient()

const identity = await client.platform.identities.get(process.env.OWNER_IDENTIFIER)

console.log(`Using: ${identity.toJSON().id}`)

const contract = await client.platform.contracts.create(schema, identity)
const deployedContract = await client.platform.contracts.publish(contract, identity)

console.log('All Done!')
console.log(`Contract deployed at: ${deployedContract.getDataContract().getId()}`)
console.log(`Used id: ${identity.toJSON().id}`)
}

deployContract().catch(console.error)
45 changes: 45 additions & 0 deletions packages/data-contract/src/actions/pushDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require('dotenv').config()
const { initClient } = require('../utils')
const doc = require('../../document.json')

async function pushDocument () {
console.log('Client initialization')

if (!process.env.MNEMONIC) {
throw new Error('Mnemonic not setted')
}

if (!process.env.OWNER_IDENTIFIER) {
throw new Error('No identity in env :(')
}

if (!process.env.CONTRACT_ID) {
throw new Error('No contract ID in env')
}

if (!process.env.DOCUMENT_NAME) {
throw new Error('No document name in env')
}

const { platform } = initClient()

const identity = await platform.identities.get(process.env.OWNER_IDENTIFIER)

const document = await platform.documents.create(
`contract.${process.env.DOCUMENT_NAME}`,
identity,
doc
)
const documentBatch = {
create: [document],
replace: [],
delete: []
}

console.log('Broadcasting Document')
await platform.documents.broadcast(documentBatch, identity)

console.log('Done', '\n', `Document at: ${document.getId()}`)
}

pushDocument().catch(console.error)
19 changes: 19 additions & 0 deletions packages/data-contract/src/actions/registerIdentity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require('dotenv').config()
const { initClient } = require('../utils')

async function createIdentity () {
console.log('Creating Identity')

if (!process.env.MNEMONIC) {
throw new Error('Mnemonic not setted')
}

const client = initClient()

pshenmic marked this conversation as resolved.
Show resolved Hide resolved
const identity = await client.platform.identities.register()

console.log('Done', '\n', `Identity: ${identity.toJSON().id}`)
return identity
}

createIdentity().catch(console.error)
27 changes: 27 additions & 0 deletions packages/data-contract/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Dash = require('dash')

function initClient () {
const options = {
network: 'testnet',
wallet: {
mnemonic: process.env.MNEMONIC
}
}

if (process.env.SKIP_SYNCHRONIZATION_BEFORE_HEIGHT) {
options.wallet.unsafeOptions =
{ skipSynchronizationBeforeHeight: Number(process.env.SKIP_SYNCHRONIZATION_BEFORE_HEIGHT) }
}

if (process.env.CONTRACT_ID) {
options.apps = {
contract: {
contractId: process.env.CONTRACT_ID
}
}
}

return new Dash.Client(options)
}

module.exports = { initClient }
Loading