-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from PortalNetwork/dev
v0.0.9
- Loading branch information
Showing
15 changed files
with
1,548 additions
and
1,030 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Create your first dApp based on Kaizen-Cli | ||
|
||
## What is dApp? | ||
`dApp` is abbreviation of decentralized application. In short, it's a web application which is capable of interacting usually within Ethereum blockchain. It will call a smart contract essentially. | ||
|
||
## Prerequisite | ||
|
||
`Kaizen-cli` installed. | ||
|
||
``` | ||
npm install -g kaizen-cli | ||
``` | ||
|
||
## Let's start | ||
|
||
1. Create a boilerplate through `kaizen-cli`. | ||
|
||
``` | ||
kaizen new -n <name> -b <vue|react> | ||
``` | ||
There are two arguments necessary to create a new project: | ||
- `-n` : followed by the desired `name` of project. | ||
- `-b` : choose your preferred front-end framework `vue|react`. | ||
|
||
In the following example, we will create a dApp of React as front-end framework named `dApp`. | ||
``` | ||
kaizen new -n dApp -b react | ||
``` | ||
![newApp](../assets/new.png) | ||
|
||
2. And you will see a new project be created. The directory layout is like: | ||
``` | ||
dApp | ||
├── DOCUMENT.md | ||
├── LICENSE | ||
├── README.md | ||
├── SMART_CONTRACT.md | ||
├── TRUFFLE.md | ||
├── config | ||
│ ├── ... | ||
│ | ||
├── contracts | ||
│ ├── BasicToken.sol | ||
│ ├── ... | ||
│ | ||
├── kaizen.json | ||
├── migrations | ||
│ ├── 1_initial_migration.js | ||
│ └── 2_deploy_contracts.js | ||
├── package.json | ||
├── public | ||
│ ├── favicon.ico | ||
│ ├── index.html | ||
│ └── manifest.json | ||
├── scripts | ||
│ ├── build.js | ||
│ ├── start.js | ||
│ └── test.js | ||
├── src | ||
│ ├── actions | ||
│ │ ├── ... | ||
| | | ||
│ ├── apis | ||
│ │ ├── ... | ||
| | | ||
│ ├── components | ||
│ │ ├── ... | ||
| | | ||
│ ├── constants | ||
│ │ ├── ... | ||
| | | ||
│ ├── container | ||
│ │ ├── ... | ||
| | | ||
│ ├── index.css | ||
│ ├── index.js | ||
│ ├── lib | ||
│ │ ├── ... | ||
| | | ||
│ ├── reducers | ||
│ │ ├── ... | ||
| | | ||
│ ├── registerServiceWorker.js | ||
│ ├── routes.js | ||
│ ├── sagas | ||
│ │ ├── ... | ||
| | | ||
│ └── store | ||
│ ├── ... | ||
| | ||
├── test | ||
│ ├── TestMetacoin.sol | ||
│ ├── metacoin.js | ||
│ └── simpleToken.js | ||
└── truffle.js | ||
``` | ||
|
||
- `kaizen.json` : configuration for publishing dApp | ||
- `contracts/` : storage directory of smart contracts | ||
- `migrations` : script of smart contract deployment(for truffle) | ||
- `/1_initial_migration.js` : Used to deploy Migrations contract which stores the number of the last deployment script applied. The initial migration files rarely need to be changed. | ||
- `/2_deploy_contracts.js` : Used to describe what contract would be deployed. | ||
- `src/` : storage directory of front-end project. | ||
- `test/` : directory of `jest ` test scripts. | ||
- `truffle.js` : configurations of truffle like which network would be connected. | ||
|
||
We will just use the default configuration of all files in the following example. | ||
|
||
3. Change to `dApp/` and install all dependency module. | ||
|
||
``` | ||
cd dApp && npm install && npm install -g ganache-cli truffle | ||
``` | ||
|
||
[Here's](./TRUFFLE.md) a brief introduction of `truffle` and `ganache`. | ||
|
||
4. Take a glance at `./migrations/2_deploy_contracts.js`: | ||
|
||
```js | ||
var ConvertLib = artifacts.require("./ConvertLib.sol"); | ||
var MetaCoin = artifacts.require("./MetaCoin.sol"); | ||
var SimpleToken = artifacts.require("./SimpleToken.sol"); | ||
|
||
module.exports = function(deployer) { | ||
deployer.deploy(ConvertLib); | ||
deployer.link(ConvertLib, MetaCoin); | ||
deployer.deploy(MetaCoin); | ||
deployer.deploy(SimpleToken); | ||
}; | ||
``` | ||
|
||
It describes which contract will be deploy to the blockchain later, you can change to different contracts on your own. | ||
|
||
5. Test on localnet first, open a new console and type: | ||
|
||
``` | ||
ganache-cli | ||
``` | ||
It will boot up the private chain on your `loacalhost:8545`, and it will print the information of the private chain. | ||
![ganache](../assets/ganache.png) | ||
|
||
6. Start to delpoy contracts. | ||
|
||
``` | ||
truffle migrate | ||
``` | ||
![deploy](../assets/deploy.png) | ||
|
||
7. Now, we can interact with the delpoyed contract through front-end. | ||
``` | ||
npm start | ||
``` | ||
![front](../assets/frontend.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# Truffle suite | ||
|
||
`Truffle` is a development environment, testing framework and asset pipeline for Ethereum, aiming to help you compile smart contract, test and deploy. You also need `ganache` to host a local private blockchain to test your contract. | ||
|
||
## Installation | ||
|
||
``` | ||
npm install -g truffle | ||
npm install -g ganache-cli | ||
``` | ||
|
||
## Quick start | ||
|
||
We need to create a truffle project first to use truffle commands within it. Create a new project folder and initialize it with truffle. | ||
|
||
``` | ||
mkdir truffle-example | ||
cd truffle-example | ||
truffle init | ||
``` | ||
![init](../assets/init.png) | ||
|
||
The directory layout: | ||
``` | ||
truffle-example | ||
├── contracts | ||
│ └── Migrations.sol | ||
├── migrations | ||
│ └── 1_initial_migration.js | ||
├── test | ||
│ └── ... | ||
└── truffle.js | ||
``` | ||
- `contracts/` : Smart contracts storage. | ||
- `migrations/` : Scripts of contracts deployment. | ||
- `test/` : Test script storage. | ||
- `truffle.js` : Truffle configuration file. | ||
|
||
Let's take a look at `truffle.js`: | ||
|
||
```js | ||
module.exports = { | ||
// See <http://truffleframework.com/docs/advanced/configuration> | ||
// to customize your Truffle configuration! | ||
}; | ||
``` | ||
There is nothing within it temporarily, let's add some code snippets to it. | ||
|
||
```js | ||
// Remeber to install truffle-hdwallet-provider first. | ||
// npm i -g truffle-hdwallet-provider | ||
var HDWalletProvider = require("truffle-hdwallet-provider"); | ||
|
||
// Provide your wallet mnemonic | ||
var mnemonic = "concert load couple harbor equip island argue ramp clarify fence smart topic"; | ||
|
||
module.exports = { | ||
networks: { | ||
development: { | ||
host: "localhost", | ||
port: 8545, | ||
network_id: "*" // Match any network id | ||
}, | ||
ropsten: { | ||
provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/"), | ||
network_id: 3 // official id of the ropsten network | ||
} | ||
} | ||
}; | ||
``` | ||
We jsut set up the network which our smart contracts will be deployed to. As default, it will choose `development` network which is configured as `localhost:8545` which is the default port `ganache` running on. | ||
|
||
Next, dive into `contracts` folder and create a file named `MetaCoin.sol` with the following code fragment: | ||
|
||
``` js | ||
pragma solidity ^0.4.4; | ||
|
||
import "./ConvertLib.sol"; | ||
|
||
// This is just a simple example of a coin-like contract. | ||
// It is not standards compatible and cannot be expected to talk to other | ||
// coin/token contracts. If you want to create a standards-compliant | ||
// token, see: https://github.com/ConsenSys/Tokens. Cheers! | ||
|
||
contract MetaCoin { | ||
mapping (address => uint) balances; | ||
|
||
event Transfer(address indexed _from, address indexed _to, uint256 _value); | ||
|
||
function MetaCoin() { | ||
balances[tx.origin] = 10000; | ||
} | ||
|
||
function sendCoin(address receiver, uint amount) returns(bool sufficient) { | ||
if (balances[msg.sender] < amount) return false; | ||
balances[msg.sender] -= amount; | ||
balances[receiver] += amount; | ||
Transfer(msg.sender, receiver, amount); | ||
return true; | ||
} | ||
|
||
function getBalanceInEth(address addr) returns(uint){ | ||
return ConvertLib.convert(getBalance(addr),2); | ||
} | ||
|
||
function getBalance(address addr) returns(uint) { | ||
return balances[addr]; | ||
} | ||
} | ||
|
||
``` | ||
It's our first smart contract which allows you create your own token on the blockchain. It includes of the other one contract files, add it to `contracts/` as well. | ||
|
||
`ConvertLib.sol` | ||
```js | ||
pragma solidity ^0.4.4; | ||
|
||
library ConvertLib{ | ||
function convert(uint amount,uint conversionRate) returns (uint convertedAmount) | ||
{ | ||
return amount * conversionRate; | ||
} | ||
} | ||
``` | ||
|
||
The last thing before we start to deploy contract, we must tell truffle what contracts needed to be deployed. Change to `migrations/` folder and create a new file named `2_deploy_contracts.js` | ||
|
||
```js | ||
var ConvertLib = artifacts.require("./ConvertLib.sol"); | ||
var MetaCoin = artifacts.require("./MetaCoin.sol"); | ||
|
||
module.exports = function(deployer) { | ||
deployer.deploy(ConvertLib); | ||
deployer.link(ConvertLib, MetaCoin); | ||
deployer.deploy(MetaCoin); | ||
}; | ||
``` | ||
|
||
Now we can deploy contracts to our `localhost`, we need to boot it up through `gangache`. Open a new terminal and type in: | ||
``` | ||
ganache-cli | ||
``` | ||
![ganache](../assets/ganache.png) | ||
|
||
Start to delpoy contracts: | ||
|
||
``` | ||
truffle migrate | ||
``` | ||
![deploy](../assets/deploy.png) | ||
|
||
You can see the deployed contract address. | ||
|
||
## Reference | ||
[ganache-cli](https://github.com/trufflesuite/ganache-cli) | ||
[truffle](https://github.com/trufflesuite/truffle) |
Oops, something went wrong.