Skip to content

Conference dApp

changwu edited this page Mar 8, 2016 · 7 revisions

dApp

  1. The application must be completely open-source, operate autonomously, and with no entity controlling the token majority. Changes to the application must be adopted by consensus.
  2. Data must be cryptographically stored in a distributed blockchain to avoid central failure points.
  3. The application must use a cryptographic token for access to the application and as reward to network supporters.
  4. The application must generate tokens according to an algorithm that values contributions to the system.
  • DAO - Decentralized Autonomous Organization

A DAO is an organization that is able to operate without human intervention and does not depend on a single point of distribution. A DAO is programmed and stored on a blockchain where it’s conditions are checked and actions are performed based on the protocol of the network. A DAO is the basis of decentralized applications.

  • Ethereum
    • 打造 blockchain 技術應用平台, 並且建立圖零完備的語言, 讓開發者可以在平台上容易開發 dApp
  • MaidSafe
    • 將 data 打散切割並加密後存到每個人的 disk, 企圖打造分散式網路儲存, 來保護個人資料, 另外導入 safecoin 來強化使用者的動機, 維護該網路
  • BURST

參考文章

環境版本

$ geth version
Geth
Version: 1.3.5
Protocol Versions: [63 62 61]
Network Id: 1
Go Version: go1.6
OS: darwin
GOPATH=/Users/user/golang
GOROOT=/usr/local/opt/go/libexec

$ solc --version
solc, the solidity compiler commandline interface
Version: 0.2.2-32f3a653/Release-Darwin/appleclang/Interpreter linked to libethereum-1.2.1-6b652590/Release-Darwin/appleclang/Interpreter

$ Truffle version
Truffle v0.3.1

$ pip freeze | grep testrpc
eth-testrpc==0.1.27

Conference

檔案結構

$ tree .
.
├── app
│   ├── images
│   ├── index.html
│   ├── javascripts
│   │   └── app.js
│   └── stylesheets
│       └── app.css
├── contracts
│   └── Conference.sol
├── environments
│   ├── development
│   │   ├── config.json
│   │   └── contracts
│   │       └── Conference.sol.js
│   ├── production
│   │   └── config.json
│   ├── staging
│   │   └── config.json
│   └── test
│       └── config.json
├── test
│   └── metacoin.js
└── truffle.json

12 directories, 11 files

1. truffle init 建立目錄

$ mkdir conference
$ truffle init

2. 編寫 Contract

$ sublime ~/ethereum/conference/contracts/Conference.sol

contract Conference {
    address public organizer;
    mapping (address => uint) public registrantsPaid;
    uint public numRegistants;
    uint public quota;

    event Deposit(address _from, uint _amount);
    event Refund(address _from, uint _amount);

    function Conference() {
        organizer = msg.sender;
        quota = 500;
        numRegistants = 0;
    }

    function buyTicket() public returns (bool success) {
        if (numRegistants >= quota) {
            return false;
        }
        registrantsPaid[msg.sender] = msg.value;
        numRegistants++;
        Deposit(msg.sender, msg.value);
        return true;
    }

    function changeQuota(uint newquota) public {
        if (msg.sender != organizer) {
            return;
        }
        quota = newquota;
    }

    function refundTicket(address recipient, uint amount) public {
        if (msg.sender != organizer) {
            return;
        }
        if (registrantsPaid[recipient] == amount) {
            address myAddress = this;
            if (myAddress.balance >= amount) {
                recipient.send(amount);
                registrantsPaid[recipient] = 0;
                numRegistants--;
                Refund(recipient, amount);
            }
        }
    }

    function destroy() {
        if (msg.sender == organizer) {
            suicide(organizer);
        }
    }
}

3. 編輯設定檔, 設定 Contract 名稱

$ sublime ~/ethereum/conference/truffle.json

{
  "build": {
    "index.html": "index.html",
    "app.js": [
      "javascripts/app.js"
    ],
    "app.css": [
      "stylesheets/app.css"
    ],
    "images/": "images/"
  },
  "deploy": [
    "Conference"
  ],
  "rpc": {
    "host": "localhost",
    "port": 8545
  }
}

4. 在其他窗口啟動 ethereal node

$ testrpc

5. 佈署 app

編譯

$ truffle compile
Compiling ./contracts/Conference.sol...
Writing contracts to ./environments/development/contracts

佈署

$ truffle deploy
Using environment development.
Compiling ./contracts/Conference.sol...
Sending Conference.sol to the network...
Writing built contract files to ./environments/development/contracts

測試

撰寫測試

$ sublime ~/ethereum/conference/test/conference.js

contract('Conference', function(accounts) {
  it("should assert true", function(done) {
    var conference = Conference.at(Conference.deployed_address);
    assert.isTrue(true);
    done();
  });
});

運行測試

$ truffle test
Using environment test.
Compiling contracts...


  Contract: Conference
    ✓ should assert true


  1 passing (2s)

測試初始變數是否設定正確

$ sublime ~/ethereum/conference/test/conference.js

contract('Conference', function(accounts) {
  console.log(accounts);
  var owner_account = accounts[0];
  var sender_account = accounts[1];

  it("Initial conference settings should match", function(done) {
    var c = Conference.at(Conference.deployed_address);

    Conference.new({from: owner_account}).then(
      function(conference) {
        conference.quota.call().then(
          function(quota) {
            assert.equal(quota, 500, "Quota doesn't match!");
        }).then(
          function() {
            return conference.numRegistrants.call();
        }).then(
          function(num) {
            assert.equal(num, 0, "Registrants doesn't match!");
            return conference.organizer.call();
        }).then(
          function(organizer) {
            assert.equal(organizer, owner_account, "Owner doesn't match!");
            done();
        }).catch(done);
    }).catch(done);
  });
});
$ truffle test
Using environment test.
Compiling contracts...
[ '0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1',
  '0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e',
  '0xdceceaf3fc5c0a63d195d69b1a90011b7b19650d',
  '0x598443f1880ef585b21f1d7585bd0577402861e5',
  '0x13cbb8d99c6c4e0f2728c7d72606e78a29c4e224',
  '0x77db2bebba79db42a978f896968f4afce746ea1f',
  '0x24143873e0e0815fdcbcffdbe09c979cbf9ad013',
  '0x10a1c1cb95c92ec31d3f22c66eef1d9f3f258c6b',
  '0xe0fc04fa2d34a66b779fd5cee748268032a146c0',
  '0x90f0b1ebbba1c1936aff7aaf20a7878ff9e04b6c' ]


  Contract: Conference
    ✓ Initial conference settings should match (280ms)


  1 passing (2s)

Clone this wiki locally