embark/README.md

586 lines
16 KiB
Markdown
Raw Normal View History

2017-10-10 11:06:14 +00:00
![Embark](https://github.com/iurimatias/embark-framework/raw/develop/logo.png)
2017-10-10 11:01:27 +00:00
2017-10-02 18:07:47 +00:00
[![npm](https://img.shields.io/npm/dm/embark.svg)](https://npmjs.com/package/embark)
2017-10-02 18:07:28 +00:00
[![Gitter](https://img.shields.io/gitter/room/iurimatias/embark-framework.svg)](https://gitter.im/iurimatias/embark-framework)
2016-10-22 22:35:31 +00:00
[![Build
Status](https://travis-ci.org/iurimatias/embark-framework.svg?branch=develop)](https://travis-ci.org/iurimatias/embark-framework)
2016-10-23 01:28:20 +00:00
[![Code Climate](https://codeclimate.com/github/iurimatias/embark-framework/badges/gpa.svg)](https://codeclimate.com/github/iurimatias/embark-framework)
2015-06-22 02:14:22 +00:00
2016-10-22 22:36:57 +00:00
What is Embark
======
2017-02-07 01:37:40 +00:00
Embark is a framework that allows you to easily develop and deploy Decentralized Applications (DApps).
2017-03-22 21:46:12 +00:00
A Decentralized Application is a serverless html5 application that uses one or more decentralized technologies.
2017-02-07 01:37:40 +00:00
2017-03-22 21:46:12 +00:00
Embark currently integrates with EVM blockchains (Ethereum), Decentralized Storages (IPFS), and Decentralized communication platforms (Whisper and Orbit). Swarm is supported for deployment.
2015-06-22 02:14:22 +00:00
With Embark you can:
2017-02-07 01:37:40 +00:00
**Blockchain (Ethereum)**
2015-06-22 02:14:22 +00:00
* Automatically deploy contracts and make them available in your JS code. Embark watches for changes, and if you update a contract, Embark will automatically redeploy the contracts (if needed) and the dapp.
2017-03-05 00:01:05 +00:00
* Contracts are available in JS with Promises.
2015-06-22 02:14:22 +00:00
* Do Test Driven Development with Contracts using Javascript.
2017-03-22 21:46:12 +00:00
* Keep track of deployed contracts; deploy only when truly needed.
2015-10-12 22:33:00 +00:00
* Manage different chains (e.g testnet, private net, livenet)
2017-02-07 01:39:51 +00:00
* Easily manage complex systems of interdependent contracts.
2017-02-07 01:37:40 +00:00
**Decentralized Storage (IPFS)**
2017-03-22 21:46:12 +00:00
* Easily Store & Retrieve Data on the DApp through EmbarkJS. Including uploading and retrieving files.
2017-02-07 01:37:40 +00:00
* Deploy the full application to IPFS or Swarm.
**Decentralized Communication (Whisper, Orbit)**
* Easily send/receive messages through channels in P2P through Whisper or Orbit.
**Web Technologies**
* Integrate with any web technology including React, Foundation, etc..
2017-03-04 20:45:36 +00:00
* Use any build pipeline or tool you wish, including grunt, gulp and webpack.
2015-06-22 10:07:46 +00:00
2016-10-18 09:37:40 +00:00
Table of Contents
======
2016-10-18 09:40:47 +00:00
* [Installation](#installation)
* [Usage Demo](#usage---demo)
* [Dashboard](#dashboard)
* [Creating a new DApp](#creating-a-new-dapp)
* [Libraries and APIs available](#libraries-and-languages-available)
* [Using and Configuring Contracts](#using-contracts)
2016-10-18 09:40:47 +00:00
* [EmbarkJS](#embarkjs)
* [EmbarkJS - Storage (IPFS)](#embarkjs---storage)
2017-01-07 05:03:03 +00:00
* [EmbarkJS - Communication (Whisper/Orbit)](#embarkjs---communication)
2016-10-18 09:43:01 +00:00
* [Testing Contracts](#tests)
* [Working with different chains](#working-with-different-chains)
* [Custom Application Structure](#structuring-application)
* [Deploying to IPFS](#deploying-to-ipfs-and-swarm)
2017-02-07 01:55:09 +00:00
* [Extending Functionality with Plugins](#plugins)
2016-10-18 09:43:01 +00:00
* [Donations](#donations)
2016-10-18 09:37:40 +00:00
2015-05-25 12:21:53 +00:00
Installation
======
2017-10-25 11:08:10 +00:00
Requirements: geth (1.6.7 or higher recommended), node (6.9.1 or higher is recommended) and npm
Optional: testrpc (3.0 or higher) if using the simulator
2016-10-18 09:29:05 +00:00
Further: depending on the dapp stack you choose: [IPFS](https://ipfs.io/)
2015-05-25 12:21:53 +00:00
```Bash
2016-10-16 19:14:48 +00:00
$ npm -g install embark
2016-09-23 22:56:19 +00:00
# If you plan to use the simulator instead of a real ethereum node.
2016-10-19 11:24:47 +00:00
$ npm -g install ethereumjs-testrpc
2015-05-25 12:21:53 +00:00
```
2015-06-22 10:07:46 +00:00
See [Complete Installation Instructions](https://github.com/iurimatias/embark-framework/wiki/Installation).
2016-10-19 13:52:08 +00:00
2017-03-22 21:46:12 +00:00
**updating from Embark 1**
2016-10-19 13:52:08 +00:00
Embark's npm package has changed from ```embark-framework``` to ```embark```, this sometimes can create conflicts. To update first uninstall embark-framework 1 to avoid any conflicts. ```npm uninstall -g embark-framework``` then ```npm install -g embark```
2015-05-25 12:21:53 +00:00
Usage - Demo
======
2017-02-07 00:41:09 +00:00
![Embark Demo screenshot](http://i.imgur.com/a9ddSjn.png)
2015-05-25 12:21:53 +00:00
You can easily create a sample working DApp with the following:
```Bash
$ embark demo
$ cd embark_demo
```
2015-10-12 22:33:00 +00:00
2016-10-20 09:57:50 +00:00
You can run a REAL ethereum node for development purposes:
2015-10-12 22:33:00 +00:00
```Bash
2016-10-20 09:57:50 +00:00
$ embark blockchain
2015-10-12 22:33:00 +00:00
```
2016-10-20 09:57:50 +00:00
Alternatively, to use an ethereum rpc simulator simply run:
2015-05-25 12:21:53 +00:00
```Bash
2016-10-20 09:57:50 +00:00
$ embark simulator
2015-05-25 12:21:53 +00:00
```
2015-10-12 22:33:00 +00:00
2017-03-22 21:46:12 +00:00
By default Embark blockchain will mine a minimum amount of ether and will only mine when new transactions come in. This is quite useful to keep a low CPU. The option can be configured at ```config/blockchain.json```. Note that running a real node requires at least 2GB of free ram, please take this into account if running it in a VM.
2015-05-25 12:21:53 +00:00
Then, in another command line:
```Bash
$ embark run
```
This will automatically deploy the contracts, update their JS bindings and deploy your DApp to a local server at http://localhost:8000
2017-03-22 21:46:12 +00:00
Note that if you update your code, it will automatically be re-deployed, contracts included. There is no need to restart embark, refreshing the page on the browser will do.
2015-05-25 12:21:53 +00:00
2016-10-18 09:36:38 +00:00
Dashboard
=====
Embark 2 comes with a terminal dashboard.
![Dashboard](http://i.imgur.com/s4OQZpu.jpg)
2017-03-22 21:46:12 +00:00
The dashboard will tell you the state of your contracts, the environment you are using, and what Embark is doing at the moment.
2016-10-18 09:36:38 +00:00
**available services**
2017-03-22 21:46:12 +00:00
Available Services will display the services available to your dapp in green. If a service is down, then it will be displayed in red.
2016-10-18 09:36:38 +00:00
**logs and console**
2017-03-22 21:46:12 +00:00
There is a console at the bottom which can be used to interact with contracts or with Embark itself. Type ```help``` to see a list of available commands. More commands will be added with each version of Embark.
2016-10-18 09:36:38 +00:00
2015-05-25 12:21:53 +00:00
Creating a new DApp
======
2017-03-22 21:46:12 +00:00
If you want to create a blank new app:
2016-10-18 09:36:38 +00:00
2015-05-25 12:21:53 +00:00
```Bash
$ embark new AppName
$ cd AppName
```
DApp Structure
======
```Bash
app/
2015-08-31 12:33:15 +00:00
|___ contracts/ #solidity or serpent contracts
2015-06-22 02:14:22 +00:00
|___ html/
|___ css/
|___ js/
2015-05-25 12:21:53 +00:00
config/
2017-03-11 09:04:51 +00:00
|___ blockchain.json #rpc and blockchain configuration
|___ contracts.json #ethereum contracts configuration
|___ storage.json #ipfs configuration
|___ communication.json #whisper/orbit configuration
|___ webserver.json #dev webserver configuration
2016-10-16 19:25:40 +00:00
test/
|___ #contracts tests
2015-06-22 02:14:22 +00:00
```
2015-05-25 12:21:53 +00:00
2017-03-22 21:46:12 +00:00
Solidity/Serpent files in the contracts directory will automatically be deployed with Embark run. Changes in any files will automatically be reflected in app, changes to contracts will result in a redeployment and update of their JS Bindings
2015-05-25 12:21:53 +00:00
Libraries and languages available
======
2017-02-07 01:48:14 +00:00
Embark can build and deploy contracts coded in Solidity. It will make them available on the client side using EmbarkJS and Web3.js.
Further documentation for these can be found below:
* Smart Contracts: [Solidity](https://solidity.readthedocs.io/en/develop/) and [Serpent](https://github.com/ethereum/wiki/wiki/Serpent)
* Client Side: [Web3.js](https://github.com/ethereum/wiki/wiki/JavaScript-API) and [EmbarkJS](#embarkjs)
2015-05-25 12:21:53 +00:00
Using Contracts
======
Embark will automatically take care of deployment for you and set all needed JS bindings. For example, the contract below:
```Javascript
# app/contracts/simple_storage.sol
2017-10-25 11:08:10 +00:00
pragma solidity ^0.4.17;
2015-05-25 12:21:53 +00:00
contract SimpleStorage {
2015-06-22 11:54:22 +00:00
uint public storedData;
2017-10-25 11:08:10 +00:00
function SimpleStorage(uint initialValue) public {
2015-06-22 11:54:22 +00:00
storedData = initialValue;
}
2017-10-25 11:08:10 +00:00
function set(uint x) public {
2015-05-25 12:21:53 +00:00
storedData = x;
}
2017-10-25 11:08:10 +00:00
function get() view returns (uint retVal) {
2015-05-25 12:21:53 +00:00
return storedData;
}
}
```
Will automatically be available in Javascript as:
```Javascript
# app/js/index.js
2017-10-25 11:08:10 +00:00
SimpleStorage.methods.set(100).send({from: web3.eth.defaultAccount});
SimpleStorage.methods.get().call().then(function(value) { console.log(value.toNumber()) });
SimpleStorage.methods.storedData().then(function(value) { console.log(value.toNumber()) });
2015-05-25 12:21:53 +00:00
```
2015-06-22 02:14:22 +00:00
You can specify for each contract and environment its gas costs and arguments:
2016-10-16 19:25:40 +00:00
```Json
# config/contracts.json
{
"development": {
"gas": "auto",
"contracts": {
"SimpleStorage": {
"args": [
100
]
}
}
}
}
2015-06-22 02:14:22 +00:00
```
If you are using multiple contracts, you can pass a reference to another contract as ```$ContractName```, Embark will automatically replace this with the correct address for the contract.
2016-10-18 00:02:33 +00:00
```Json
2016-10-16 20:16:09 +00:00
# config/contracts.json
{
2015-06-22 02:14:22 +00:00
...
2016-10-16 20:16:09 +00:00
"development": {
"contracts": {
"SimpleStorage": {
"args": [
100,
"$MyStorage"
2016-10-16 20:16:09 +00:00
]
},
"MyStorage": {
"args": [
"initial string"
]
},
"MyMainContract": {
"args": [
"$SimpleStorage"
2016-10-16 20:16:09 +00:00
]
}
}
}
...
}
2015-06-22 02:14:22 +00:00
```
2015-07-15 12:06:40 +00:00
You can now deploy many instances of the same contract. e.g
2016-10-17 23:58:52 +00:00
```Json
# config/contracts.json
{
"development": {
"contracts": {
"Currency": {
"deploy": false,
"args": [
100
]
},
"Usd": {
"instanceOf": "Currency",
"args": [
200
]
},
"MyCoin": {
"instanceOf": "Currency",
"args": [
200
]
}
}
}
}
2015-07-15 12:06:40 +00:00
...
```
2017-03-22 21:46:12 +00:00
Contracts addresses can be defined. If an address is defined, Embark uses the defined address instead of deploying the contract.
2015-07-15 12:06:40 +00:00
2016-10-18 00:02:33 +00:00
```Json
# config/contracts.json
{
2015-07-15 12:06:40 +00:00
...
2016-10-18 00:02:33 +00:00
"development": {
"contracts": {
"UserStorage": {
"address": "0x123456"
},
"UserManagement": {
"args": [
2016-10-18 09:13:54 +00:00
"$UserStorage"
2016-10-18 00:02:33 +00:00
]
}
}
}
2015-08-31 12:33:15 +00:00
...
2016-10-18 00:02:33 +00:00
}
2015-08-31 12:33:15 +00:00
```
2017-10-25 11:08:10 +00:00
You can Also specify which versions of solc and web3.js to use:
```Json
# config/contracts.json
{
...
"development": {
"versions": {
"web3.js": "1.0.0-beta",
"solc": "0.4.17"
}
}
...
}
```
You specify which node the contracts should be deploy to and the order of nodes
the dapp should connect to. $WEB3 means the dapp will try to use an existing
web3 object first if available.
```Json
# config/contracts.json
{
...
"development": {
"deployment": {
"host": "localhost",
"port": 8545,
"type": "rpc"
},
"dappConnection": [
"$WEB3",
"http://localhost:8545"
]
}
...
}
```
2016-10-16 19:50:38 +00:00
EmbarkJS
======
2016-10-18 00:09:27 +00:00
EmbarkJS is a javascript library meant to abstract and facilitate the development of DApps.
2016-10-18 09:27:38 +00:00
**promises**
2016-10-18 09:13:54 +00:00
2016-10-18 00:09:27 +00:00
```Javascript
var myContract = new EmbarkJS.Contract({abi: abiObject, address: "0x123"});
2017-10-25 11:08:10 +00:00
myContract.methods.get().call().then(function(value) { console.log("value is " + value.toNumber) });
2016-10-18 00:09:27 +00:00
```
2017-03-07 02:53:36 +00:00
events:
```Javascript
2017-10-25 11:08:10 +00:00
myContract.events.eventName({from: web3.eth.accounts}, 'latest').then(function(event) { console.log(event) });
2017-03-07 02:53:36 +00:00
```
2016-10-18 09:27:38 +00:00
**deployment**
Client side deployment will be automatically available in Embark for existing contracts:
```Javascript
SimpleStorage.deploy([args], {options}).then(function(anotherSimpleStorage) {});
2016-10-18 09:27:38 +00:00
```
or it can be manually definied as
```Javascript
var myContract = new EmbarkJS.Contract({abi: abiObject, code: code});
myContract.deploy([args], {options}).then(function(anotherMyContractObject) {});
```
so you can define your gas as
```Javascript
myContract.deploy([100, "seconde argument"], {gas: 800000}).then(function(anotherMyContractObject) {});
2016-10-18 09:27:38 +00:00
```
2016-10-16 19:50:38 +00:00
EmbarkJS - Storage
======
2016-10-18 09:27:38 +00:00
**initialization**
2017-03-22 21:46:12 +00:00
The current available storage is IPFS. It can be initialized as
2016-10-18 09:27:38 +00:00
```Javascript
EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})
```
**Saving Text**
```Javascript
EmbarkJS.Storage.saveText("hello world").then(function(hash) {});
```
**Retrieving Data/Text**
```Javascript
EmbarkJS.Storage.get(hash).then(function(content) {});
```
**Uploading a file**
```HTML
<input type="file">
```
```Javascript
var input = $("input[type=file"]);
EmbarkJS.Storage.uploadFile(input).then(function(hash) {});
```
**Generate URL to file**
```Javascript
EmbarkJS.Storage.getUrl(hash);
```
2017-03-07 02:53:36 +00:00
note: if not using localhost, the cors needs to be set as ```ipfs --json API.HTTPHeaders.Access-Control-Allow-Origin '["your-host-name-port"]```
2016-10-16 19:50:38 +00:00
EmbarkJS - Communication
======
2016-10-18 09:27:38 +00:00
**initialization**
2017-10-25 11:08:10 +00:00
For Whisper (note: currently requires geth 1.6.0 or higher):
2017-01-07 05:10:12 +00:00
2017-01-08 20:57:23 +00:00
```Javascript
EmbarkJS.Messages.setProvider('whisper')
```
2017-01-07 05:10:12 +00:00
For Orbit:
2017-01-08 20:57:23 +00:00
You'll need to use IPFS from master and run it as: ```ipfs daemon --enable-pubsub-experiment```
2017-01-07 05:10:12 +00:00
then set the provider:
2017-01-08 20:57:23 +00:00
```Javascript
EmbarkJS.Messages.setProvider('orbit', {server: 'localhost', port: 5001})
```
2016-10-18 09:27:38 +00:00
**listening to messages**
```Javascript
2017-01-07 15:42:11 +00:00
EmbarkJS.Messages.listenTo({topic: ["topic1", "topic2"]}).then(function(message) { console.log("received: " + message); })
2016-10-18 09:27:38 +00:00
```
**sending messages**
you can send plain text
```Javascript
2017-01-07 15:42:11 +00:00
EmbarkJS.Messages.sendMessage({topic: "sometopic", data: 'hello world'})
2016-10-18 09:27:38 +00:00
```
or an object
```Javascript
2017-01-07 15:42:11 +00:00
EmbarkJS.Messages.sendMessage({topic: "sometopic", data: {msg: 'hello world'}})
2016-10-18 09:27:38 +00:00
```
2017-01-07 15:42:11 +00:00
note: array of topics are considered an AND. In Whisper you can use another array for OR combinations of several topics e.g ```["topic1", ["topic2", "topic3"]]``` => ```topic1 AND (topic2 OR topic 3)```
2015-06-22 02:14:22 +00:00
Tests
======
2016-10-16 19:25:40 +00:00
You can run specs with ```embark test```, it will run any test files under ```test/```.
2015-06-22 02:14:22 +00:00
2017-03-22 21:46:12 +00:00
Embark includes a testing lib to rapidly run & test your contracts in a EVM.
2015-06-22 02:14:22 +00:00
```Javascript
2015-10-12 22:33:00 +00:00
# test/simple_storage_spec.js
2016-10-16 19:25:40 +00:00
describe("SimpleStorage", function() {
2015-10-12 22:33:00 +00:00
before(function(done) {
2017-03-17 03:01:40 +00:00
this.timeout(0);
2016-10-16 19:25:40 +00:00
var contractsConfig = {
"SimpleStorage": {
args: [100]
}
};
EmbarkSpec.deployAll(contractsConfig, done);
2015-06-22 02:14:22 +00:00
});
2015-10-12 22:33:00 +00:00
it("should set constructor value", function(done) {
SimpleStorage.storedData(function(err, result) {
assert.equal(result.toNumber(), 100);
done();
});
2015-06-22 02:14:22 +00:00
});
2015-10-12 22:33:00 +00:00
it("set storage value", function(done) {
SimpleStorage.set(150, function() {
SimpleStorage.get(function(err, result) {
assert.equal(result.toNumber(), 150);
done();
});
});
2015-06-22 02:14:22 +00:00
});
2016-10-16 19:25:40 +00:00
});
2017-03-17 03:01:40 +00:00
2015-06-22 02:14:22 +00:00
```
2015-10-12 22:33:00 +00:00
Embark uses [Mocha](http://mochajs.org/) by default, but you can use any testing framework you want.
2015-06-22 02:14:22 +00:00
2015-05-25 12:21:53 +00:00
Working with different chains
======
2015-06-22 10:13:04 +00:00
You can specify which environment to deploy to:
2015-05-25 12:21:53 +00:00
2015-06-22 10:13:04 +00:00
```$ embark blockchain livenet```
2015-06-22 10:13:04 +00:00
```$ embark run livenet```
2015-06-22 10:13:04 +00:00
2016-10-18 00:04:52 +00:00
The environment is a specific blockchain configuration that can be managed at config/blockchain.json
2015-05-25 12:21:53 +00:00
2016-10-18 00:04:52 +00:00
```Json
# config/blockchain.json
...
"livenet": {
"networkType": "livenet",
"rpcHost": "localhost",
"rpcPort": 8545,
"rpcCorsDomain": "http://localhost:8000",
"account": {
"password": "config/livenet/password"
2016-10-18 00:04:52 +00:00
}
},
2015-05-25 12:21:53 +00:00
...
```
Structuring Application
======
2017-02-21 02:34:35 +00:00
Embark is quite flexible and you can configure your own directory structure using ```embark.json```
2016-10-18 00:04:52 +00:00
```Json
# embark.json
{
"contracts": ["app/contracts/**"],
"app": {
"css/app.css": ["app/css/**"],
2017-02-07 01:59:36 +00:00
"images/": ["app/images/**"],
2016-10-18 00:04:52 +00:00
"js/app.js": ["embark.js", "app/js/**"],
"index.html": "app/index.html"
},
"buildDir": "dist/",
2017-02-07 01:59:36 +00:00
"config": "config/",
"plugins": {}
2016-10-18 00:04:52 +00:00
}
```
2017-01-14 23:11:43 +00:00
Deploying to IPFS and Swarm
2015-09-01 02:06:30 +00:00
======
2017-01-14 23:11:43 +00:00
To deploy a dapp to IPFS, all you need to do is run a local IPFS node and then run ```embark upload ipfs```.
If you want to deploy to the livenet then after configuring you account on ```config/blockchain.json``` on the ```livenet``` environment then you can deploy to that chain by specifying the environment ```embark ipfs livenet```.
2015-09-01 02:06:30 +00:00
2017-01-14 23:11:43 +00:00
To deploy a dapp to SWARM, all you need to do is run a local SWARM node and then run ```embark upload swarm```.
2016-10-18 09:36:38 +00:00
2017-02-07 01:55:09 +00:00
Plugins
======
2017-03-22 21:46:12 +00:00
It's possible to extend Embark's functionality with plugins. For example, the following is possible:
2017-02-07 01:55:09 +00:00
* plugin to add support for es6, jsx, coffescript, etc (``embark.registerPipeline``)
* plugin to add standard contracts or a contract framework (``embark.registerContractConfiguration`` and ``embark.addContractFile``)
* plugin to make some contracts available in all environments for use by other contracts or the dapp itself e.g a Token, a DAO, ENS, etc.. (``embark.registerContractConfiguration`` and ``embark.addContractFile``)
2017-03-22 21:46:12 +00:00
* plugin to add a libraries such as react or bootstrap (``embark.addFileToPipeline``)
2017-02-07 01:55:09 +00:00
* plugin to specify a particular web3 initialization for special provider uses (``embark.registerClientWeb3Provider``)
* plugin to create a different contract wrapper (``embark.registerContractsGeneration``)
* plugin to add new console commands (``embark.registerConsoleCommand``)
* plugin to add support for another contract language such as viper, LLL, etc (``embark.registerCompiler``)
2017-03-22 21:46:12 +00:00
For more information on how to develop your own plugin, please see the [plugin documentation](http://embark.readthedocs.io/en/latest/plugins.html)
2017-02-07 01:55:09 +00:00
2016-10-18 09:36:38 +00:00
Donations
======
2017-10-14 14:13:30 +00:00
If you like Embark, please consider donating to 0xFA239D14c7117C3D2370B2a4c4238534391fadd9