[区块链笔记12] 第一个DApp--demo
[区块链笔记12] 第一个DApp--demo
初始化项目git clone cp truffle-config.js truffle.js
编辑InfoContract.sol
pragma solidity ^0.4.23;contract Info { string name; uint age; event doneEvent(string name, uint age); function setInfo(string _name, uint _age) public { name = _name; age = _age; emit doneEvent(name, age); } function getInfo() public view returns (string, uint) { return (name, age); }}
编译合约 truffle compile
打开Ganache,分配一个区块链网络
配置truffle.js文件, 127.0.0.1和7545端口
编辑一个2_info_migration.js文件
const Info = artifacts.require("Info");module.exports = function(deployer) { deployer.deploy(Info);};
部署合约truffle migrate
npm init用npm初始化,因为后续要用npm 来install包
npm install truffle-contract安装truffle-contract,方便和智能合约交互
创建src和js和css文件夹,用于存放html和js和css文件
把web3.min.js和jquery.min.js和truffle-contract.min.js拷贝到js目录下
css目录下创建index.css,内容如下
body { background-color: #F0F0F0;}#info { padding: 20px; background-color: #FFF;}#button { width: 100px;}
编辑src下的index.html