Skip to content

devicehive/devicehive-plugin-ethereum

Repository files navigation

Devicehive plugin ethereum

This plugin allows you to manipulate your custom smart contract via devicehive.

Before the start

You should have ethereum node running somewhere. You also need ethereum account's address and password. With this guide you can run ethereum node locally.

Specify ethereum configuration

Firstly you should specify ethereum configuration. Add ethereum url, account's address and account's password to config file (path - ./src/ethereum-node/config.json).
Note - you can use example to see how it works, just do not change other fields.

Specify plugin configuration

In plugin configuration there are already playground urls, however you can run devicehive locally and write your own. Add plugin topic and access token. Also add user access token, which you can copy from playground.

Simple start

  1. Create plugin (locally or on playground) and specify plugin configuration file
  2. Specify ethereum configuration file (plugin will start with contract in example folder)
  3. Run npm i
  4. Run npm start
  5. Send message from device. See example here.
  6. Wait for plugin receives the message and sends transaction to blockchain.

Logger will notify you about results. You will see message Transaction has been passed successfully, hash: ${transactionHash}.

Configuration

Plugin

You can find plugin configuration in ./config.json Plugin part of configuration you can find here. Also plugin authentication process and options are described here.

PLUGIN_PROPS - plugin's properties. There are only filters at the moment. PLUGIN_PROPS.OVERRIDE - set it to true and plugin's filters will be updated according to config. FILTERS - filters for your plugin. LOGGER - logger configuration.

Examples:

"FILTERS":{
    "notifications": true,
    "commands": false,
    "command_updates": false,
    "names": "blockchain-record"
}

"LOGGER":{
    "level": 0, // 0 - info + error, 1 - error
    "filePath": "./dhe-log.txt"
}

Ethereum smart contract

You can find configuration in ./ethereum-config.json

  • CONTRACT_PATH - path to your smart contract,
  • ETHEREUM_URL - url to ethereum node,
  • CONTRACT_INITIAL_ARGS - arguments for initializing smart contract,
  • ACCOUNT_ADDRESS - ethereum account address
  • ACCOUNT_PASSWORD - ethereum account password
  • GAS_LIMIT - maximum gas limit which can be used by device
  • TIME_PERIOD - time period in minutes, every TIME_PERIOD minutes gas limit will be updated
  • CONTRACT_ADDRESS - contract's address, which plugin will use
  • OVERRIDE_CONTRACT - set it to true, if you want to create new contract
  • ALLOWED_METHODS - methods, which can be used by device

Plugin parameters

After successfull creation of the contract plugin's parameters will be overwritten.
They will store ethereum node url, contract's address, allowed methods and initial transaction's hash.

Message name: blockchain-record Parameters: { "method": "increase", "args": { "amount": 1 }, "options": {} }

Message model

{
    "method":*method_name*,
    "args":{
        "arg1",
        "arg2",
        ...
    },
    "options":{
        "gas", // optional
        "gasPrice", // optional
        "value"" // optional
    }
}

Note: if gas or gasPrice are not specified then plugin will use min amount of gas for transaction.

How to run ethereum node locally

To start ethereum locally:

Note - with this guide you will run dev node, so you will not be working with real ethereum.

  1. Download and install geth.
  2. Open terminal and run geth --mine --minerthreads=1 --networkid=1999 --rpc --rpcaddr "127.0.0.1" --rpcapi="admin,debug,miner,shh,txpool,personal,eth,net,web3" --dev.
  3. Open another terminal window and run geth attach http://localhost:8545.
  4. In second window run personal.newAccount() and enter your password. You will get account's address.
  5. Make sure you have enough ethereum. You can run eth.accounts.forEach(function(account){if(account!==eth.coinbase){eth.sendTransaction({from:eth.coinbase,to:account,value:web3.toWei(1000000)})}}) to increase it on your account. You will see undefined in response. It means that transaction has been finished successfully.

Note - you can check your account's balance by entering web3.fromWei(eth.getBalance(${ACCOUNT_ADDRESS}), "ether") command.