基于以太坊truffle框架实践去中心化竞标商城(一)

论坛 期权论坛 区块链     
赖床的猫   2018-11-20 23:33   5057   0
           
前言:
truffle:是一个基于以太坊快速开发智能合约的框架,省去了很多基础文件及基础代码的构建。官方还有宠物商城等等案例代码。
官方地址:http://truffleframework.com/
操作环境:阿里云的centos 7.0 (windows下试过,但是报错更加离谱)
先啰嗦一下我的观点,原本我以为truffle只是一个部署智能合约的框架,如果只能编写并部署智能合约,那其实用ethereum wallet客户端就可以直接编辑和部署了,相信很多刚入门的小伙伴也会有同样的困惑。truffle不仅仅是编写智能合约,还结合了具体应用,比如:web,可以真真切切的开发以太坊的落地应用。
好,搬好小板凳,准备好你的番茄闹钟,步骤实践约需要1个小时(前提是你的私链环境已搭建ok):
一、预热准备1、安装nodejs,官网下载;
2、安装truffle框架
  1. npm -g truffle
复制代码
3、搭建以太坊私链环境
参见我之前的文章:https://www.jianshu.com/p/52332fa4a24c
二、初始化truffle webpack项目命令:
  1. //创建项目目录mkdir Ebaycd Ebaytruffle unbox webpack //这个时候会自动开始下载项目初始化代码
复制代码
结果如下:


image.png三、编写智能合约代码
  1. cd contractsrm -rf MetaCoin.sol //该文件用不到,删除掉即可vi EbayStore.sol
复制代码
代码如下:
  1. pragma solidity ^0.4.18;contract EbayStore{uint public productIndex;function EbayStore() public{    productIndex =0;}enum ProductStatus{    Open,Sold,Unsold}enum ProductCondition{    New,Used}mapping(address=>mapping(uint=>Product)) stores;mapping(uint=>address) productIdInStore;struct Product{    uint id;    string name;    string category;//分类    string imageLink;    string descLink;    uint auctionStartTime; //开始竞标时间    uint auctionEndTime; //结束竞标时间    uint startPrice;//拍卖价格    address highestBidder;//赢家的钱包地址    uint highestBid; //竞标的价格    address secondHighestBidder; //第二高出价人的钱包地址    uint totalBids; //总共多少人参与竞标    ProductStatus status;    ProductCondition condition;    // mapping(address => mapping(bytes32 => Bid)) bids;}function addProductToStore(string _name, string _category, string _imageLink, string _descLink, uint _auctionStartTime, uint _auctionEndTime, uint _startPrice, uint _productCondition) public{    require(_auctionStartTime < _auctionEndTime);    productIndex ++;    Product memory product = Product(productIndex, _name, _category, _imageLink, _descLink, _auctionStartTime, _auctionEndTime, _startPrice, 0, 0, 0, 0, ProductStatus.Open, ProductCondition(_productCondition));    stores[msg.sender][productIndex] = product;    productIdInStore[productIndex] = msg.sender;}function getProduct(uint _productId) view public returns(uint,string,string,string,string,uint,uint,uint,ProductStatus,ProductCondition){    Product memory product = stores[productIdInStore[_productId]][_productId];    return (product.id, product.name, product.category, product.imageLink, product.descLink, product.auctionStartTime, product.auctionEndTime, product.startPrice, product.status, product.condition);  }}
复制代码
四、启动以太坊节点及RPC启动命令如下:
  1. ./geth --datadir "./chain" --rpcapi "db,eth,net,web3,personal,admin,miner" --nodiscover console 2>>eth_output.log> admin.startRPC() //启动RPC,该方法有多种,还可以在第一句命令上加上rpc启动命令
复制代码
五、编译和部署智能合约在刚才的Ebay目录下进入truffle开发控制台:
  1. truffle developtruffle(develop)> compile  //编译智能合约,生成的文件会自动写到build/contracts目录Compiling ./contracts/EbayStore.sol...Compilation warnings encountered:/opt/go-ethereum/ethereum/truffle/Ebay/contracts/EbayStore.sol:4:2: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead.    function EbayStore() public{ ^ (Relevant source part starts here and spans across multiple lines).Writing artifacts to ./build/contracts
复制代码
再进行部署合约:
  1. truffle(develop)> migrateUsing network 'develop'.Running migration: 1_initial_migration.jsDeploying Migrations... ... 0x9703f0bcfbea1379917db408adaa99bc708a5a8a4f3a48375ef39bda5e9436b6Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0Saving successful migration to network...... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956Saving artifacts...Running migration: 2_deploy_contracts.jsDeploying EbayStore...... 0x4ff35a0843e9a3c9df51963e90212830a1f663ecba21721798880111ee6a5161EbayStore: 0x345ca3e014aaf5dca488057592ee47305d9b3e10Saving successful migration to network...... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0Saving artifacts...
复制代码
六、遇到的问题(解决问题占用时间的2/3):1、启动geth的时候不能启动/无反应或一下提示fatal error: runtime: out of memory
top查看占用内存最多的进程,并且把不用的删除掉
2、truffle在部署migrate的时候,提示geth客户端没有启动/没有启动rpc/没有配置好truffle.js首先一个个来做:
启动geth(第二种方式同时启动geth和rpc):
  1. ./geth --datadir "./chain" --rpc --rpcapi "db,eth,net,web3,personal,admin,miner" --nodiscover console 2>>eth_output.log
复制代码
更改Ebay项目下的truffle.js文件
  1. require('babel-register')module.exports = {  networks: {    development: {      host: '127.0.0.1',      port: 8545, //该端口是默认的rpc监听端口      network_id: '*' // Match any network id    }  }}
复制代码
3、migrate报错Running migration: 1_initial_migration.js
Could not connect to your Ethereum client. Please check that your Ethereum client:
- is running
- is accepting RPC connections (i.e., "--rpc" option is used in geth)
- is accessible over the network
- is properly configured in your Truffle configuration file (truffle.js)
解决办法:
重启,然后先启动私链,再启动truffle develop
4、truffle compile 时而好时而不好解决办法:reboot
         
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:
帖子:
精华:
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP