This plugin allows you to manipulate your custom smart contract via devicehive.
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.
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.
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.
- Create plugin (locally or on playground) and specify plugin configuration file
- Specify ethereum configuration file (plugin will start with contract in
example
folder) - Run
npm i
- Run
npm start
- Send message from device. See example here.
- 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}
.
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"
}
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
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": {}
}
{
"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.
To start ethereum locally:
Note - with this guide you will run dev node, so you will not be working with real ethereum.
- Download and install
geth
. - 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
. - Open another terminal window and run
geth attach http://localhost:8545
. - In second window run
personal.newAccount()
and enter your password. You will get account's address. - 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 seeundefined
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.