编写智能合约
pragma solidity >=0.4.22 <0.7.0;
contract Storage {
uint256 number;
constructor() public {
number = 1;
}
function store(uint256 num) public {
number = num;
}
function retrieve() public view returns (uint256){
return number;
}
}
编译合约
在remix上进行编译
部署合约
ganache-cli
mkdir deploy-proj
cd deploy-proj
npm install web3 -save
编写deploy.js
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
web3.eth.getAccounts()
.then(console.log);
var storageContract = new web3.eth.Contract([{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}]);
storageContract.options.data = '608060405234801561001057600080fd5b50600160008190555060c7806100276000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80632e64cec11460375780636057361d146053575b600080fd5b603d607e565b6040518082815260200191505060405180910390f35b607c60048036036020811015606757600080fd5b81019080803590602001909291905050506087565b005b60008054905090565b806000819055505056fea2646970667358221220dabed7eafe213a90a9645c4a170b5ca337e54115cfafdc710010f039c6de4c0264736f6c63430006060033';
storageContract.deploy({
}).send({
from: '0x0a16B82f1ad3ab455441DCe6Da103c661817b42F',
gas: '4700000'
}).then(function(newContractInstance){
console.log("contract address: ", newContractInstance.options.address)
});
部署:
node deploy.js
创建工程
express -e dapp4
cd dapp4
npm install
npm start
修改app.js
var ejs = require('ejs');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('.html',ejs.__express);
app.set('view engine', 'html');
// app.set('view engine', 'ejs');
修改routes/index.js
var express = require('express');
var router = express.Router();
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
var mycontract = new web3.eth.Contract(
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "retrieve",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "num",
"type": "uint256"
}
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
'0x1C54F0E15980FFB425b596C6469eBeE2b29e3CBF'
);
var init_number = 0;
var modify_number = 0;
mycontract.methods.retrieve().call().then(function(number) {
console.log("init number: ", number);
init_number = number;
});
/* GET home page. */
router.get('/', function(req, res, next) {
mycontract.methods.store(15).send({from: '0x303F88d36ecfcB32AeD6062D3BB8f994cC03e0F8'}).then(function() {
mycontract.methods.retrieve().call().then(function(number) {
modify_number = number;
console.log("modify number : ", number);
res.render('index', { init: init_number, modify: modify_number });
});
});
});
module.exports = router;
编辑view/index.html
<!DOCTYPE html>
<html>
<head>
<title>调用storage的值</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>init number: <%= init %></h1>
<h1>modify number: <%= modify %></h1>
</body>
</html>
运行
ganache-cli
npm_start