Skip to content

Commit

Permalink
docs: update contract registration tutorial to include nft example (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
thephez authored Jun 20, 2024
1 parent 37b4eb9 commit 5596210
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/explanations/nft.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Structurally, there is no difference between an NFT contract and a non-NFT contr

NFT contracts will often set document creation restrictions and enable document transfers. Default options for modifying, deleting, and transferring documents can be specified at the contract level and overridden as needed for specific document types.

Once the data contract design is completed, the contract can be registered on the network in preparation for NFT document creation.
Once the data contract design is completed, the contract can be registered on the network in preparation for NFT document creation. See the [contract registration tutorial](../tutorials/contracts-and-documents/register-a-data-contract.md) for example code.

### Minting NFTs

Expand Down
173 changes: 173 additions & 0 deletions docs/tutorials/contracts-and-documents/register-a-data-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ data.
The fifth tab shows a data contract configured to store contract history. This allows all contract
revisions to be retrieved in the future as needed.

The sixth tab shows a data contract configured for creating NFTs. It allows documents to be deleted, transferred, or traded. It also limits document creation to the contract owner. See the [NFT explanation section](../../explanations/nft.md) for more details about NFTs on Dash Platform. **_Note: the JavaScript SDK supports NFT contract registration, but does not currently support trades or transfers._**

> 🚧
>
> Since Platform v0.25.16, each document property must assign `position` value to support [backwards compatibility](https://github.com/dashpay/platform/pull/1594) for contract updates.
Expand Down Expand Up @@ -161,6 +163,82 @@ array of bytes (e.g. a NodeJS Buffer).
}
```
:::

:::{tab-item} 6. NFT Contract
:sync: nft
```json
// To use contract documents as NFTs, configure settings for modifying
// deleting, transferring, trading, or restricting creation as needed
{
"card": {
"type": "object",
"documentsMutable": false, // true = documents can be modified (replaced)
"canBeDeleted": true, // true = documents can be deleted
"transferable": 1, // 0 = transfers disabled; 1 = transfers enabled
"tradeMode": 1, // 0 = no trading; 1 = direct purchases
"creationRestrictionMode": 1, // 0 = anyone can mint; 1 = only contract owner can mint
"properties": {
"name": {
"type": "string",
"description": "Name of the card",
"minLength": 0,
"maxLength": 63,
"position": 0
},
"description": {
"type": "string",
"description": "Description of the card",
"minLength": 0,
"maxLength": 256,
"position": 1
},
"attack": {
"type": "integer",
"description": "Attack power of the card",
"position": 2
},
"defense": {
"type": "integer",
"description": "Defense level of the card",
"position": 3
}
},
"indices": [
{
"name": "owner",
"properties": [
{
"$ownerId": "asc"
}
]
},
{
"name": "attack",
"properties": [
{
"attack": "asc"
}
]
},
{
"name": "defense",
"properties": [
{
"defense": "asc"
}
]
}
],
"required": [
"name",
"attack",
"defense"
],
"additionalProperties": false
}
}
```
:::
::::

> 📘
Expand Down Expand Up @@ -373,6 +451,101 @@ const registerContract = async () => {
return contract;
};

registerContract()
.then((d) => console.log('Contract registered:\n', d.toJSON()))
.catch((e) => console.error('Something went wrong:\n', e))
.finally(() => client.disconnect());
```
:::

:::{tab-item} 6. NFT Contract
:sync: nft
```javascript
const setupDashClient = require('../setupDashClient');

const client = setupDashClient();

const registerContract = async () => {
const { platform } = client;
const identity = await platform.identities.get('an identity ID goes here');

const contractDocuments = {
card: {
type: "object",
documentsMutable: false, // Documents cannot be modified/replaced
canBeDeleted: true, // Documents can be deleted
transferable: 1, // Transfers enabled
tradeMode: 1, // Direct purchase trades enabled
creationRestrictionMode: 1, // Only the contract owner can mint
properties: {
name: {
type: "string",
description: "Name of the card",
minLength: 0,
maxLength: 63,
position: 0
},
description: {
type: "string",
description: "Description of the card",
minLength: 0,
maxLength: 256,
position: 1
},
attack: {
type: "integer",
description: "Attack power of the card",
position: 2
},
defense: {
type: "integer",
description: "Defense level of the card",
position: 3
}
},
indices: [
{
name: "owner",
properties: [
{
$ownerId: "asc"
}
]
},
{
name: "attack",
properties: [
{
attack: "asc"
}
]
},
{
name: "defense",
properties: [
{
defense: "asc"
}
]
}
],
required: [
"name",
"attack",
"defense"
],
additionalProperties: false
}
}

const contract = await platform.contracts.create(contractDocuments, identity);
console.dir({ contract: contract.toJSON() });

// Sign and submit the data contract
await platform.contracts.publish(contract, identity);
return contract;
};

registerContract()
.then((d) => console.log('Contract registered:\n', d.toJSON()))
.catch((e) => console.error('Something went wrong:\n', e))
Expand Down

0 comments on commit 5596210

Please sign in to comment.