Geth如何部署智能合约
1、打开remix在线工具
https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.25+commit.59dbf8f1.js
2、输入智能合约代码:
pragma solidity ^0.4.0;
contract Multiply{
event Print(uint);
function multiply(uint input) returns (uint) {
Print(input * 7);
return input * 7;
}
}
3、点击start to compile进行编译
4、打开Geth的控制台
F:\Geth>geth --datadir "data" console
5、解锁待部署智能合约的账号:
> personal.unlockAccount(eth.accounts[0])
6、点击Details打开页面,获取待部署的内容
7、在控制台输入6中图里面红色的内容:
> var multiplyContract = web3.eth.contract([{"constant":false,"inputs":[{"name":
"input","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint2
56"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonym
ous":false,"inputs":[{"indexed":false,"name":"","type":"uint256"}],"name":"Print
","type":"event"}]);
undefined
> var multiply = multiplyContract.new(
... {
...... from: web3.eth.accounts[0],
...... data: '0x6080604052348015600f57600080fd5b5060d78061001e6000396000f30
0608060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000
000000000000000600035041663c6888fa181146043575b600080fd5b348015604e57600080fd5b5
06058600435606a565b60408051918252519081900360200190f35b6040805160078302815290516
000917f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da919081900
360200190a150600702905600a165627a7a723058201adda81c9d31f7a3048a87867185d12ef31b8
a1412cb7d049c87afe6bc517b960029',
...... gas: '4700000'
...... }, function (e, contract){
...... console.log(e, contract);
...... if (typeof contract.address !== 'undefined') {
......... console.log('Contract mined! address: ' + contract.address +
' transactionHash: ' + contract.transactionHash);
......... }
...... })
INFO [11-14|17:45:47.680] Submitted contract creation fullhash=0xa3
7f825173c9276a17d1349a8a85710f9b1841bc14d7e87a5071a95307d392d5 contract=0x57Ab4d
AA353800f7580644316195297E0b1C7EbD
null [object Object]
undefined
8、查看multiply 对象内容:
> multiply
{
abi: [{
constant: false,
inputs: [{...}],
name: "multiply",
outputs: [{...}],
payable: false,
stateMutability: "nonpayable",
type: "function"
}, {
anonymous: false,
inputs: [{...}],
name: "Print",
type: "event"
}],
address: undefined,
transactionHash: "0xa37f825173c9276a17d1349a8a85710f9b1841bc14d7e87a5071a95307
d392d5"
}
9、挖矿(需要超过12个块):
> miner.start(1);admin.sleepBlocks(13);miner.stop()
> null [object Object]
Contract mined! address: 0x57ab4daa353800f7580644316195297e0b1c7ebd transaction
ash: 0xa37f825173c9276a17d1349a8a85710f9b1841bc14d7e87a5071a95307d392d5
10、再次查看multiply 对象内容:
> multiply
{
abi: [{
constant: false,
inputs: [{...}],
name: "multiply",
outputs: [{...}],
payable: false,
stateMutability: "nonpayable",
type: "function"
}, {
anonymous: false,
inputs: [{...}],
name: "Print",
type: "event"
}],
address: "0x57ab4daa353800f7580644316195297e0b1c7ebd",
transactionHash: "0xa37f825173c9276a17d1349a8a85710f9b1841bc14d7e87a5071a95307
d392d5",
Print: function(),
allEvents: function(),
multiply: function()
}
11、合约的调用:
> multiply.multiply.call(2)
14
>
(欢迎打赏,一分也是爱)