Skip to content

Commit

Permalink
Merge pull request #38 from PortalNetwork/dev
Browse files Browse the repository at this point in the history
v0.0.9
  • Loading branch information
PhyrexTsai authored Oct 10, 2018
2 parents a92466b + 73ee12c commit 8ff6a33
Show file tree
Hide file tree
Showing 15 changed files with 1,548 additions and 1,030 deletions.
Binary file added assets/deploy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/frontend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/ganache.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/init.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/new.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions build/lib/plugin/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ exports.argv = function (argv) {
case 'bluzelle':
handleBluzelle(kaizenConfig, boilerplate);
break;

case 'nkn':
handleNKN(kaizenConfig);
break;
}
};

Expand Down Expand Up @@ -172,4 +176,27 @@ function handleBluzelle(kaizenConfig, boilerplate) {
console.log('error', error);
handleError(error);
}
}

function handleNKN(kaizenConfig) {
try {
var checkResult = packageChecker(kaizenConfig);

if (checkResult.isValid === false) {
console.error('[ERROR]: please use kaizen new to create new project first');
return;
}

console.log('kaizen: installing nkn sdk...');
cmd.get('npm install nkn-client', function (error) {
if (error) {
handleError(error);
return;
}

updateConfig(kaizenConfig, 'NKN');
});
} catch (error) {
handleError(error);
}
}
22 changes: 22 additions & 0 deletions build/lib/plugin/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exports.argv = function (argv) {
removeBluzelle(configPath, kaizenConfig, ['pn-react-bluzelle', 'pn-vue-bluzelle']);
break;

case 'nkn':
removeNKN(configPath, kaizenConfig, ['NKN']);
break;

default:
console.log("there is no plugin named ".concat(pluginName, " in this project"));
break;
Expand Down Expand Up @@ -72,4 +76,22 @@ function removeBluzelle(configPath, kaizenConfig, pluginKeywords) {
});
fsHelper.updateFileSync(configPath, JSON.stringify(newKaizenConfig));
console.log('plugin removed');
}

function removeNKN(configPath, kaizenConfig, pluginKeywords) {
var newKaizenConfig = _objectSpread({}, kaizenConfig, {
plugins: kaizenConfig.plugins.filter(function (x) {
return pluginKeywords.includes(x) === false;
})
});

cmd.get('npm uninstall nkn-client', function (error) {
if (error) {
console.error('[ERROR]:', error);
return;
}

fsHelper.updateFileSync(configPath, JSON.stringify(newKaizenConfig));
console.log('plugin removed');
});
}
153 changes: 153 additions & 0 deletions docs/Create_Kaizen_Project.md
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)
156 changes: 156 additions & 0 deletions docs/TRUFFLE.md
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)
Loading

0 comments on commit 8ff6a33

Please sign in to comment.