美文网首页
使用truffle框架编写简单的加密货币

使用truffle框架编写简单的加密货币

作者: foochane | 来源:发表于2018-07-06 18:13 被阅读32次

    1 创建项目

    通过truffle init

    $ mkdir EncryptedToken
    $ cd EncryptedToken/
    $ truffle init
    Downloading...
    Unpacking...
    Setting up...
    Unbox successful. Sweet!
    
    Commands:
    
      Compile:        truffle compile
      Migrate:        truffle migrate
      Test contracts: truffle test
    

    2 编写代码

    2.1 在contracts目录下新建EncryptedToken.sol文件代码如下:

    pragma solidity ^0.4.4;
    
    contract EncryptedToken {
        uint256 INITIAL_SUPPLY = 888888; //提供币的总量
    
        mapping (address => uint256) balances;
        constructor() public {
            balances[msg.sender] = INITIAL_SUPPLY;
        }
        // 转账到一个指定的地点
        function transfer(address _to,uint256 _amount) public {
            assert(balances[msg.sender] > _amount);
            balances[msg.sender] -= _amount;
            balances[_to] += _amount;
        }
        //查看指定地址的余额
        function balanceOf(address _owner)  public view returns (uint256) {
            return balances[_owner];
        }
    }
    
    /**
    命令:
    进入控制台:$ truffle develop
    编译:compile
    部署: migrate  
    第二次部署:migrate --reset
    创建实例:let contract = EncryptedToken.deployed().then(instance => contract = instance);
    
    Accounts:
    (0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
    (1) 0xf17f52151ebef6c7334fad080c5704d77216b732
    
    查看余额:
    contract.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57")
    contract.balanceOf("0xf17f52151ebef6c7334fad080c5704d77216b732")
    
    转账:
    contract.transfer("0xf17f52151ebef6c7334fad080c5704d77216b732",888)
     */
    
    

    2.1 在migrations目录下新建2_deloy_encryptedtoken.js文件,代码如下:

    var EncryptedToken = artifacts.require("./EncryptedToken.sol");
    
    module.exports = function(deployer) {
      deployer.deploy(EncryptedToken);
    };
    

    3 代码的编译和部署

    进入控制台:

    $ truffle develop
    Truffle Develop started at http://127.0.0.1:9545/
    
    Accounts:
    (0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
    (1) 0xf17f52151ebef6c7334fad080c5704d77216b732
    (2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
    (3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
    (4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
    (5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
    (6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
    (7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
    (8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
    (9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de
    
    Private Keys:
    (0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
    (1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
    (2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
    (3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
    (4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
    (5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
    (6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
    (7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
    (8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
    (9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5
    
    Mnemonic: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat
    
    ⚠️  Important ⚠️  : This mnemonic was created for you by Truffle. It is not secure.
    Ensure you do not use it on production blockchains, or else you risk losing funds.
    

    编译:

    truffle(develop)> compile
    Compiling ./contracts/EncryptedToken.sol...
    Compiling ./contracts/Migrations.sol...
    Writing artifacts to ./build/contracts
    

    部署:

    truffle(develop)> migrate
    Using network 'develop'.
    
    Running migration: 1_initial_migration.js
      Deploying Migrations...
      ... 0x249aed24bd6a95537a5acb3b5a9ea8c920869b0605ef560c0eb9dfc376bf9a61
      Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
    Saving successful migration to network...
      ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
    Saving artifacts...
    Running migration: 2_deloy_encryptedtoken.js
      Deploying EncryptedToken...
      ... 0x403076503ae9a265651cdbaaf4fc97f26af810a484b0ba35ffed8a2ff3288f3b
      EncryptedToken: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
    Saving successful migration to network...
      ... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0
    Saving artifacts...
    

    4 运行合约

    创建实例:

    truffle(develop)> let contract = EncryptedToken.deployed().then(instance => contract = instance);
    undefined
    

    查看地址0x627306090abab3a6e1400e9345bc60c78a8bef57和0xf17f52151ebef6c7334fad080c5704d77216b732中的余额:

    truffle(develop)> contract.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57")
    BigNumber { s: 1, e: 5, c: [ 888888 ] }
    truffle(develop)> contract.balanceOf("0xf17f52151ebef6c7334fad080c5704d77216b732")
    BigNumber { s: 1, e: 0, c: [ 0 ] }
    

    转账给888个币给0xf17f52151ebef6c7334fad080c5704d77216b732:

    truffle(develop)> contract.transfer("0xf17f52151ebef6c7334fad080c5704d77216b732",888)
    { tx:
       '0x88dd855050af4033c005b758bea3de2d8ce0ef95bf067dad242537f328deb581',
      receipt:
       { transactionHash:
          '0x88dd855050af4033c005b758bea3de2d8ce0ef95bf067dad242537f328deb581',
         transactionIndex: 0,
         blockHash:
          '0x7ec60afc574e62c5d4d024723a31795259261ce20b74837a43e79f6581e1ce9d',
         blockNumber: 5,
         gasUsed: 49147,
         cumulativeGasUsed: 49147,
         contractAddress: null,
         logs: [],
         status: '0x01',
         logsBloom:
          '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
      logs: [] }
    

    再查看两个地址的余额情况:

    truffle(develop)> contract.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57")
    BigNumber { s: 1, e: 5, c: [ 888000 ] }
    truffle(develop)> contract.balanceOf("0xf17f52151ebef6c7334fad080c5704d77216b732")
    BigNumber { s: 1, e: 2, c: [ 888 ] }
    

    5 命令总结

    创建项目:$ truffle init
    进入truffle控制台:$ truffle develop
    编译:compile
    部署: migrate
    第二次部署:migrate --reset
    创建实例:let contract = EncryptedToken.deployed().then(instance => contract = instance);

    相关文章

      网友评论

          本文标题:使用truffle框架编写简单的加密货币

          本文链接:https://www.haomeiwen.com/subject/varbuftx.html