[区块链笔记12] 第一个DApp--demo

网友投稿 239 2022-09-25

[区块链笔记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

Dapp demo