2015-05-26 14:35:07 +00:00
[![Join the chat at https://gitter.im/iurimatias/embark-framework ](https://badges.gitter.im/Join%20Chat.svg )](https://gitter.im/iurimatias/embark-framework?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
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).
A Decentralized Application is serverless html5 application that uses one or more decentralized technologies.
Embark currently integrates with EVM blockchains (Ethereum), Decentralized Storages (IPFS), and Decentralizaed 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.
* Do Test Driven Development with Contracts using Javascript.
2015-08-31 12:33:15 +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)**
* Easily Store & Retrieve Data on the DApp through EmbarkJS. Includin uploading and retrieving files.
* 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 )
2016-10-23 21:16:22 +00:00
* [Libraries and APIs available ](#libraries-and-languages-available )
2016-10-18 09:40:47 +00:00
* [Using and Configuring Contracts ](#dapp-structure )
* [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 )
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-02-07 00:41:09 +00:00
Requirements: geth (1.5.8 or higher), node (6.9.1 or higher is recommended) and npm
2017-02-07 01:48:14 +00:00
Optional: testrpc (3.0 or higher) if using the simulator or the test functionality.
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:55:21 +00:00
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
**updating from embark 1**
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
2016-10-25 22:34:46 +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 usefull 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
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.
2016-10-18 09:36:38 +00:00
Dashboard
=====
Embark 2 comes with a terminal dashboard.
![Dashboard ](http://i.imgur.com/s4OQZpu.jpg )
The dashboard will tell you the state of your contracts, the enviroment you are using, and what embark is doing at the moment.
**available services**
Available Services will display the services available to your dapp in green, if one of these is down then it will be displayed in red.
**logs and console**
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.
2015-05-25 12:21:53 +00:00
Creating a new DApp
======
2016-10-18 09:36:38 +00:00
If you want to create a blank new app.
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/
2016-10-16 19:25:40 +00:00
|___ blockchain.json #environments configuration
|___ contracts.json #contracts configuration
test/
|___ #contracts tests
2015-06-22 02:14:22 +00:00
```
2015-05-25 12:21:53 +00:00
2015-08-31 12:33:15 +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
2016-10-23 21:16:22 +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.
2016-10-23 21:19:06 +00:00
Further documentation for these can be found below:
2016-10-23 21:16:22 +00:00
* 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-02-15 22:52:25 +00:00
pragma solidity ^0.4.7;
2015-05-25 12:21:53 +00:00
contract SimpleStorage {
2015-06-22 11:54:22 +00:00
uint public storedData;
function SimpleStorage(uint initialValue) {
storedData = initialValue;
}
2015-05-25 12:21:53 +00:00
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}
}
```
Will automatically be available in Javascript as:
```Javascript
# app/js/index.js
SimpleStorage.set(100);
2016-11-03 11:04:07 +00:00
SimpleStorage.get().then(function(value) { console.log(value.toNumber()) });
SimpleStorage.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
]
},
"MyStorage": {
"args": [
"initial string"
]
},
"MyMainContract": {
"args": [
$SimpleStorage
]
}
}
}
...
}
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
...
```
Contracts addresses can be defined, If an address is defined the contract wouldn't be deployed but its defined address will be used instead.
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
```
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 09:27:38 +00:00
methods in EmbarkJS contracts will be converted to 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"});
myContract.get().then(function(value) { console.log("value is " + value.toNumber) });
```
2016-10-18 09:27:38 +00:00
**deployment**
Client side deployment will be automatically available in Embark for existing contracts:
```Javascript
2016-10-18 16:00:56 +00:00
SimpleStorage.deploy().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});
2016-10-18 16:00:56 +00:00
myContract.deploy().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**
The current available storage is IPFS. it can be initialized as
```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);
```
2016-10-16 19:50:38 +00:00
EmbarkJS - Communication
======
2016-10-18 09:27:38 +00:00
**initialization**
2017-01-07 05:10:12 +00:00
For Whisper:
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
2015-09-04 09:18:54 +00:00
Embark includes a testing lib to fastly 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
2015-10-12 22:33:00 +00:00
var assert = require('assert');
2016-11-03 10:31:12 +00:00
var Embark = require('embark');
2015-10-12 22:33:00 +00:00
var EmbarkSpec = Embark.initTests();
2016-10-16 19:25:40 +00:00
var web3 = EmbarkSpec.web3;
2015-10-12 22:33:00 +00:00
2016-10-16 19:25:40 +00:00
describe("SimpleStorage", function() {
2015-10-12 22:33:00 +00:00
before(function(done) {
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
});
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
2017-02-08 11:31:57 +00:00
```$ embark blockchain livenet```
2015-06-22 10:13:04 +00:00
2017-02-08 11:31:57 +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": {
2017-02-08 11:31:57 +00:00
"password": "config/livenet/password"
2016-10-18 00:04:52 +00:00
}
},
2015-05-25 12:21:53 +00:00
...
```
2015-09-01 02:03:02 +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```
2015-09-01 02:03:02 +00:00
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
}
2015-09-01 02:03:02 +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```.
2017-02-08 11:31:57 +00:00
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-02-21 02:34:35 +00:00
It's possible to extend Embarks 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``)
* plugin to add a libraries such as react or boostrap (``embark.addFileToPipeline``)
* 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-02-07 02:39:33 +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
======
If you like Embark please consider donating to 0x8811FdF0F988f0CD1B7E9DE252ABfA5b18c1cDb1