Skip to content
How to build Smart Contract on Dragonchain Platform
JavaScript Python Dockerfile C C++ C# Other
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets
smart-contract-templates
.gitignore
README.md
example_main.js
example_main.py
package.json

README.md

Welcome to Dragonchain

Guide to building on Dragonchain

Contents

Audience

This document is for developers and enterprises who are:

  • Interested in Hybrid Blockchains
  • Looking to write Upgradeable Smart Contracts

Design

  • Flexibility: Developers can create smart contracts as docker containers with the ability to update and delete them even after deployment.
  • Simplicity: Dragonchain has official SDKs in Python, NodeJS, Golang and a community SDK in C# helping users interact with their chains.

Prerequisites

  1. Register for a Dragonchain Console account
  2. Download a Dragonchain SDK of your choice: Python or Node.js (more coming soon...)

Smart Contract

Dragonchain Smart Contract

Access Smart Contract Templates

Deploy a Smart Contract

  1. Open up your Terminal/Bash Prompt and clone the smart contract template. Then change into the python_contract directory
$ git clone https://github.com/dragonchain-inc/guide-to-develop-on-dragonchain
$ cd guide-to-develop-on-dragonchain
$ cd smart-contract-templates
$ cd python_contract
  1. Change to your home directory and create a python virtual environment as per best practice (change dragonvenv to any suitable name)
$ virtualenv -p python3 dragonvenv
  1. Activate your virtual environment. (type in 'deactivate' when you want to leave the virtual environment later)*
 $ source dragonvenv/bin/activate
  1. Install the Python SDK to interface with your smart contract
 $ sudo pip3 install dragonchain-sdk
  1. Open up your favourite web browser and register for a DockerHub Account: https://hub.docker.com/signup
    (or login into your existing one)

  2. Click 'Create a Repository'

  3. Enter a 'Name' for your repository, e.g. "interchain"

  4. Now go back to your Terminal, and build your docker image from the Dockerfile in the /python_contract sub-directory

$ docker build -t  any_image_name .
  1. Then push your smart contract image to your newly created docker registry
    (substitute docker_id, repo_name, tag_name with your own values!)
$ docker push docker_id/repo_name:tagname
  • For example, in our case we might do: docker push 19011/interchain:stellar
  1. Go back to your web browser and log into your Dragonchain Console account

  2. Go to the 'Create Chain' section and choose a name for your new hybrid chain, then click create.
    (Leave Level as 'Level 1' )

alt text

  1. Note down the Chain ID of your new chain and keep it secure!
    (In our chain 'misty-silence', the Chain ID happends to be 'fcf62a0f-5904-428a-bc7d-99e974fa89e0'). When using the SDK, you should generate keys: AuthKeyId and AuthKey while on the your created section. Scroll to the bottom of Dragonchain console.

alt text

  1. Click 'View Chain' and navigate to the dashboard of your newly created hybrid chain. Scroll down to the 'MISC' section and click on 'Generate New API Key' on the bottom right.

alt text

  1. Note down the Auth Key ID and Auth Key and keep them secure!
    (The 'Auth Key ID' in our case is 'NNIPQSWIKNYV' and the 'Auth Key' is 'MabBUglfjl87LVLZFJQCahxrKfEQojhc')

alt text

  1. Now go back to the Terminal, and open up the file index.py with your favourite editor. Delete any existing lines of code.
    (We use Atom here but you can use Sublime, VisualStudio, Vim or even EMACS if you really fancy.)
atom index.py
  1. Copy (Ctrl + C), paste (Ctrl + V) and save (Ctrl + S) the following code into your index.py file.
    (In the code we use our sample credentials but please replace them with your own!
    Here we are creating a cryptocurrency of transaction type 'nvidiacoin'. Ensure you enter a semicolon followed by any version number in your image name e.g. stellar:0.0.1)
import json
import dragonchain_sdk
dragonchain_client = dragonchain_sdk.Client(dragonchain_id='your_dc_id', auth_key_id='your_auth_key_id', auth_key='your_authkey')

# Create a Python Cryptocurrency Contract
print(dragonchain_client.create_smart_contract(
    txn_type='nvidiacoin',
    image='interchain:stellar:0.0.1',
    cmd='python',
    args=['-m', 'index'],
    execution_order='parallel',
))

Response from Dragonchain

{
    "ok": true,
    "response": {
        "success": {
            "args": [
                "index"
            ],
            "auth_key_id": null,
            "cmd": "paython",
            "cron": null,
            "dcrn": "SmartContract::L1::AtRest",
            "env": null,
            "execution_order": "parallel",
            "existing_secrets": null,
            "id": "2cb7589b-1218-42b7-b876-7a05392d86c9",
            "image": "image_name",
            "image_digest": null,
            "seconds": null,
            "status": {
                "msg": "Contract creating",
                "state": "Pending",
                "timestamp": "2019-04-05 22:59:52.653891"
            },
            "txn_type": "example_contract",
            "version": "3"
        }
    },
    "status": 202
}

Update a Smart Contract

# Update Python Smart Contract
print(dragonchain_client.update_contract(
    contract_id='<contract_id>',
    image='image_name',
    cmd='python',
    args=['-m', 'index'],
    execution_order='parallel',
    # auth='<docker_auth_token_if_private_repository>'
))

Response from Dragonchain

{
    "ok": true,
    "response": {
        "success": {
            "args": [
                "index"
            ],
            "auth_key_id": null,
            "cmd": "paython",
            "cron": null,
            "dcrn": "SmartContract::L1::AtRest",
            "env": {},
            "execution_order": "parallel",
            "existing_secrets": [],
            "id": "2cb7589b-1218-42b7-b876-7a05392d86c9",
            "image": "image_name",
            "image_digest": null,
            "seconds": null,
            "status": {
                "msg": "",
                "state": "updating",
                "timestamp": "2019-04-05 23:06:35.334432"
            },
            "txn_type": "example_contract",
            "version": "1"
        }
    },
    "status": 202
}

Delete a Smart Contract

# Delete a Smart Contract
print(dragonchain_client.delete_contract('<contract_id>'))
{
    "ok": true,
    "response": {
        "success": {
            "args": [
                "index"
            ],
            "auth_key_id": null,
            "cmd": "paython",
            "cron": null,
            "dcrn": "SmartContract::L1::AtRest",
            "env": {},
            "execution_order": "parallel",
            "existing_secrets": [],
            "id": "2cb7589b-1218-42b7-b876-7a05392d86c9",
            "image": "taban/python_contract:latest",
            "image_digest": null,
            "seconds": null,
            "status": {
                "msg": "",
                "state": "deleting",
                "timestamp": "2019-04-08 16:28:03.427830"
            },
            "txn_type": "example_contract",
            "version": "1"
        }
    },
    "status": 202
}

Transaction

Register a transaction type

print(dragonchain_client.register_transaction_type(transation_type='currency'))

Post a transaction

  1. Posting a transaction to currency transaction
# Currency contract
print(dragonchain_client.create_transaction(transation_type='currency', payload={
    'version': '1',
    'paymentData': {
        'amount': 500,
        'to': 'John Howies',
        'type': 'dining'
    }
}))

Response from Dragonchain

{
    "ok": true,
    "response": {
        "transaction_id": "5b64cd77-6d7e-48ba-8004-5c276ed43da7"
    },
    "status": 201
}

21)Post a transaction to our python_contract under the txn_type='example_contract'

print(dragonchain_client.create_transaction(transaction_type='example_contract', {
    'version': '1',
    'exampleData': {
        'type': 'test',
'message': 'Hello, world!'
    }
}))

Response from Dragonchain

  1. Save the transaction_id returned by your example
{
    "ok": true,
    "response": {
        "transaction_id": "98f1c816-48d0-4db6-94e5-10d175d4d145"
    },
    "status": 201
}

Update a Transaction

print(dragonchain_client.register_transaction_type('currency'))

Query a Transaction

  1. Query the currency transaction

Grab the transaction id returned by currency

# # Query currency
print(dragonchain_client.query_transactions(lucene_query='your_transaction_id'))

Response from Dragonchain

{
    "ok": true,
    "response": {
        "results": [
            {
                "dcrn": "Transaction::L1::FullTransaction",
                "header": {
                    "block_id": "24453725",
                    "dc_id": "9cf00a35-ccb1-4f0b-a3d5-d63237b9139d",
                    "invoker": "",
                    "tag": "",
                    "timestamp": "1554506841",
                    "txn_id": "5b64cd77-6d7e-48ba-8004-5c276ed43da7",
                    "txn_type": "currency"
                },
                "payload": {
                    "paymentData": {
                        "amount": 500,
                        "to": "John Howies",
                        "type": "dining"
                    },
                    "version": "1"
                },
                "proof": {
                    "full": "JB6VLlxFTkvcCOe2sGNy44lVl0Qaas4Q8FUl8fjvf6E=",
                    "stripped": "MEUCIQDncsLMgjawR1oOovBLHI0zPgYkCxuC2gjNDIV6smJ2VAIgHMfXV/jYfaGoeqrdMGmCv+o74lCFVO3jcAKZ18QztDw="
                },
                "version": "1"
            }
        ],
        "total": 1
    },
    "status": 200
}
  1. Query example_contract transaction

Grab the transaction id returned by example_contract

# Query example_contract
print(dragonchain_client.query_transactions(lucene_query='your_transaction_id'))

Response from Dragonchain

{
    "ok": true,
    "response": {
        "results": [
            {
                "dcrn": "Transaction::L1::FullTransaction",
                "header": {
                    "block_id": "24453533",
                    "dc_id": "9cf00a35-ccb1-4f0b-a3d5-d63237b9139d",
                    "invoker": "",
                    "tag": "",
                    "timestamp": "1554505884",
                    "txn_id": "98f1c816-48d0-4db6-94e5-10d175d4d145",
                    "txn_type": "example_contract"
                },
                "payload": {
                    "exampleData": {
"message": "Hello, world!",
                        "type": "test"
                    },
                    "version": "1"
                },
                "proof": {
                    "full": "A0u2d3f6JROLZcGeZaX0ijOJfe16VYJ5bA7IP4XrT+k=",
                    "stripped": "MEUCIQCY0+dh706AqhOYS1hr9CMk7xxP9IBQG8743PP0C2TZNgIgXkQyBO1XmQBWRNlwaQ8vp0EwddAPTBxpmViXlmi6iY4="
                },
                "version": "1"
            }
        ],
        "total": 1
    },
    "status": 200
}

Node example

Go here

Our system also allows developers to add scheduled smart contract execution using a cron expression or seconds between broadcasts. For example, cron='* * * * *' and seconds=59 are both valid. These fields are optional. If you are going to use a scheduler, you can only use a cron or seconds, not both.

Contribute SDK

Currently, Dragonchain supports two SDKs (Python and Node.js), but any language could be used to interface with a chain. If you have any questions, concerns, or would like to contribute to a DragonchainSDK or create your own, please join our community developer forum on Telegram (Community Dragonchain Dev Official).

You can’t perform that action at this time.