http://www.7klian.com

利用Geth和Web3.js陈设您的第一个私有以太坊智能合约

  “contracts”: {
Version: 0.7.1+commit.f4a555be.Linux.g++
true    <–   make sure you are mining or miner.start(1)
    name: “owner”,
  getOwner: function(),

  }
> ownerContractInstance.setOwner(eth.accounts[1], { from: eth.accounts[0] })
}
coinbase: 0x6be8a6cad397746efc5033e27e82477e27c9afec
Starts the test runner.yarn eject

然后,您想将一个菜单选项下移到Solidity Compiler。只需单击蓝色的Compile Owner.sol按钮。

“0xb2c455aa1ac281180b4faca7b8896092169ccd55”
  blockHash: “0x94a0e3c9c9d562c34061147e1ef21f9260178693df124ff81cba37f1818fa8a7”,
> var deployedBinary = eth.getCode(ownerContractAddress)
“string”
  transactionHash: “0x38391d46e37e79b7764df2ba31cc32ff45457e2a33a5826b8af15fce9a0e5cf2”,
  }],

我展开了合约,您可以从哪里看到合约中的两种要领。带输入字段的“ getter” getOwner和“ setter” changeOwner。我单击getOwner按钮并看到陈设的地点:0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

Unlock account 0x6be8a6cad397746efc5033e27e82477e27c9afec
{“contracts”:{“Owner.sol:Owner”:{“abi”:”[{“inputs”:[],”stateMutability”:”nonpayable”,”type”:”constructor”},{“anonymous”:false,”inputs”:[{“indexed”:true,”internalType”:”address”,”name”:”oldOwner”,”type”:”address”},{“indexed”:true,”internalType”:”address”,”name”:”newOwner”,”type”:”address”}],”name”:”OwnerSet”,”type”:”event”},{“inputs”:[{“internalType”:”address”,”name”:”newOwner”,”type”:”address”}],”name”:”changeOwner”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”getOwner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”owner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”}]”,”bin”:”608060405234801561001057600080fd5b5060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102f3806100db6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063893d20e8146100465780638da5cb5b1461007a578063a6f9dae1146100ae575b600080fd5b61004e6100f2565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b61008261011b565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100f0600480360360208110156100c457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061013f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610200576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea2646970667358221220ba9a494e8dd99908b8c962ed752cac0bf679199c5c8dad87d13f5f9d9088c39c64736f6c63430007010033″}},”version”:”0.7.1+commit.f4a555be.Linux.g++”}
> var deployTransactionObject = { from: eth.coinbase, data: “0x” + ownerContractCompiled.contracts[“Owner.sol:Owner”].bin, gas: 1000000 }
任何人都可以检索合约二进制文件。
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing “SPDX-License-Identifier: <SPDX-License>” to each source file. Use “SPDX-License-Identifier: UNLICENSED” for non-open-source code. Please see https://spdx.org for more information.
  }, {
apt-get install jq -y
  blockNumber: 4061,
    name: “getOwner”,
    type: “function”
  owner: function()
  inputs: [],

  }],
    address: “0xb2c455aa1ac281180b4faca7b8896092169ccd55”,

在Ubuntu中利用solc编译Owner.sol
我但愿您凭据以下方法编译Owner.sol合约。
instance: Geth/v1.9.21-stable-0287d548/linux-amd64/go1.15
}
~/dapp# cat Owner.json | jq .
Solidity是以太坊智能合约的主要编译器之一。你可以像这样在Ubuntu中安装它。
> var ownerContractCompiled = {“contracts”:{“Owner.sol:Owner”:{“abi”:”[{“inputs”:[],”stateMutability”:”nonpayable”,”type”:”constructor”},{“anonymous”:false,”inputs”:[{“indexed”:true,”internalType”:”address”,”name”:”oldOwner”,”type”:”address”},{“indexed”:true,”internalType”:”address”,”name”:”newOwner”,”type”:”address”}],”name”:”OwnerSet”,”type”:”event”},{“inputs”:[{“internalType”:”address”,”name”:”newOwner”,”type”:”address”}],”name”:”changeOwner”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”getOwner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”owner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”}]”,”bin”:”608060405234801561001057600080fd5b5060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102f3806100db6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063893d20e8146100465780638da5cb5b1461007a578063a6f9dae1146100ae575b600080fd5b61004e6100f2565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b61008261011b565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100f0600480360360208110156100c457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061013f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610200576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea2646970667358221220ba9a494e8dd99908b8c962ed752cac0bf679199c5c8dad87d13f5f9d9088c39c64736f6c63430007010033″}},”version”:”0.7.1+commit.f4a555be.Linux.g++”}
  allEvents: function(),
    stateMutability: “nonpayable”,
  constructor() {
利用React.js或Node.js库(Web3.js)与
您的智能合约举办交互。
        emit OwnerSet(owner, newOwner);
“0x6be8a6cad397746efc5033e27e82477e27c9afec”   <–   you will have your own address
区块链中有生意业务收据。

单击橙色Deploy后,您将看到陈设在deployed Contracts下的合约。你也会留意到它有本身的以太坊地点。

–> Owner.sol
  gasUsed: 287352,
  anonymous: false,
    type: “function”
> eth.getTransactionReceipt(ownerContractInstance.transactionHash);
  transactionIndex: 0
在本教程中,我将利用Remix的示例合约之一(Owner.sol)。在较高级别上,此合约会将合约的所有者配置为陈设它的人。有一个“ getter”可以检索当前所有者是谁,尚有一个“ setter”可以答允当前所有者配置另一个所有者。很是简朴,但可以到达目标。
    blockHash: “0x94a0e3c9c9d562c34061147e1ef21f9260178693df124ff81cba37f1818fa8a7”,
contract Owner {
  removed: false,
Passphrase:
    }

智能合约根基上是利用中的以太坊虚拟机(EVM)运行的措施。建设以太坊地点时,会分派一个以太坊地点,而且每个交互利用一个事务。合约/应用措施将在区块链中具有状态,而且与之交互时状态将产生变革。重要的是,一旦合约被发送到区块链,它就永远不会改变(它是不行变的)。您可以通过变动再次上载同一个合约,但前一个合约将保持稳定,而且它们将在互相不知情的环境下并行运行。在智能合约进入主网之前,质量担保和测试至关重要。
> var ownerContract = eth.contract(JSON.parse(ownerContractCompiled.contracts[“Owner.sol:Owner”].abi))
    inputs: [{…}, {…}],
> personal.unlockAccount(eth.coinbase)
  }
> eth.coinbase
    require(msg.sender == owner, “Caller is not owner”);
    outputs: [],
该库可用于Node.js和React.js。您可以像这样与
Geth节点RPC举办交互。
  transactionHash: “0x38391d46e37e79b7764df2ba31cc32ff45457e2a33a5826b8af15fce9a0e5cf2”,

此刻将鼠标移到Deploy&runtransactions菜单。只需单击橙色Deploy按钮。

  abi: [{
    stateMutability: “view”,
  inputs: [],
利用一些示例合约从头Remix加载。我们正在利用的一个在上面称为Owner.sol。我删除了本教程中包括的注释。我将表明如何与之互动。

  data: “0x”,
~/dapp# cat Owner.json
此刻您大概会碰着的一个问题是,Remix中的默认Owner.sol合约利用的编译器≥0.4.22而且<0.7.0。我的Ubuntu系统上安装的solc编译器是0.7.1
  logIndex: 0,
  inputs: [{…}],
~/geth# ./2020-attach.sh
    type: “constructor”

我想改变合约的所有者。在顶部,我移动到第二个帐户,以便可以或许复制它,然后移回第一个帐户,即所有者。

}
  event OwnerSet(address indexed oldOwner, address indexed newOwner);
    type: “event”
0   <–   this means the contract is the same as the contract deployed

}
合约已陈设并正常事情。您将不但愿一遍又一各处陈设合约以与之集成。这就是您与现有合约举办交互的方法。
  to: null,
  changeOwner: function(),
  from: “0x6be8a6cad397746efc5033e27e82477e27c9afec”,

此刻从0x5B3帐户运行changeOwner要领,将新帐户配置为0xAb8帐户。

凭听说明,假如您将目次变动为“ mediumtutorial”和“ yarn start”,它将打开
您的默认欣赏器,您应该会看到。
3983
此刻,对付下一部门,,您将需要一个以太坊Geth节点来安装智能合约。
> var ownerContractAddress = eth.getTransactionReceipt(ownerContractInstance.transactionHash).contractAddress
let web3 = new Web3(‘ws://localhost:8546’);

> typeof ownerContractCompiled.contracts[“Owner.sol:Owner”].abi
  root: “0xb37e7b1b1e55b358585677fe3cad149e859540f8981c0d9c57d8adc5af536c20”,
contract Owner {
solc, the solidity compiler commandline interface
{
> eth.mining
    outputs: [{…}],
    constructor() public {
modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
> eth.blockNumber
 如何建设以太坊智能合约?
    emit OwnerSet(address(0), owner);
    }
    outputs: [{…}],
    “Owner.sol:Owner”: {
        return owner;
      “abi”: “[{“inputs”:[],”stateMutability”:”nonpayable”,”type”:”constructor”},{“anonymous”:false,”inputs”:[{“indexed”:true,”internalType”:”address”,”name”:”oldOwner”,”type”:”address”},{“indexed”:true,”internalType”:”address”,”name”:”newOwner”,”type”:”address”}],”name”:”OwnerSet”,”type”:”event”},{“inputs”:[{“internalType”:”address”,”name”:”newOwner”,”type”:”address”}],”name”:”changeOwner”,”outputs”:[],”stateMutability”:”nonpayable”,”type”:”function”},{“inputs”:[],”name”:”getOwner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”},{“inputs”:[],”name”:”owner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”stateMutability”:”view”,”type”:”function”}]”,
  }, {
~/dapp# solc –combined-json abi,bin Owner.sol > Owner.json
    return owner;

因此这只是Remix的1分钟先容。我发起您本身实验一下。实验将changeOwner配置为无效地点,可能实验使changeOwner不作为所有者,然后看看会产生什么。
> var ownerContractInstance = ownerContract.at(ownerContractAddress)
> ownerContractInstance
  function changeOwner(address newOwner) public isOwner {
  modifier isOwner() {
  function getOwner() external view returns (address) {
    _;
mediumtutorial % yarn add web3
{
pragma solidity >=0.4.22 <0.7.0;
  address: “0xb2c455aa1ac281180b4faca7b8896092169ccd55”,
}
请留意,合约已陈设,我们的两种要领都在这里。
        _;
> (“0x” + ownerContractCompiled.contracts[“Owner.sol:Owner”].bin.substring(2 + ownerContractCompiled.contracts[“Owner.sol:Owner”].bin.length – deployedBinary.length)).localeCompare(deployedBinary);
  },
因此,让我们开始建设一个很是根基的React.js应用措施。我将利用“ create-react-app”东西建设默认应用措施。
yarn startHappy hacking!
        emit OwnerSet(address(0), owner);
# solc –version
at block: 3981 (Tue Sep 22 2020 22:44:03 GMT+0100 (BST))

我们将从文件欣赏器开始在左侧菜单中举办操纵。您只需在这里选择Owner.sol。

    stateMutability: “nonpayable”,
apt-get install solc
  transactionHash: “0x38391d46e37e79b7764df2ba31cc32ff45457e2a33a5826b8af15fce9a0e5cf2”,
function changeOwner(address newOwner) public isOwner {
    }
function getOwner() external view returns (address) {

请留意,Remix提供了几个可利用的资金帐户。每个账户有100以太币资金。)我将利用从0x5B3开始的第一个帐户来陈设合约。

    }
% create-react-app mediumtutorialCreating a new React app in /Repos/React/mediumtutorial.*** INSTALL PROCESS ***Success! Created mediumtutorial at /Repos/React/mediumtutorialInside that directory, you can run several commands:yarn start
    type: “function”
{
  }
    modifier isOwner() {
> var ownerContractInstance = ownerContract.new(deployTransactionObject)
> ownerContractInstance.getOwner()
    emit OwnerSet(owner, newOwner);
Welcome to the Geth JavaScript console!
  }

> ownerContractAddress
  address public owner;

  logs: [{
Starts the development server.yarn build
% cd mediumtutorial
    name: “changeOwner”,
Removes this tool and copies build dependencies, configuration files and scripts into the app directory. If you do this, you can’t go back!We suggest that you begin by typing:cd mediumtutorial
  blockNumber: 4061,
  “version”: “0.7.1+commit.f4a555be.Linux.g++”
true
    }
    name: “OwnerSet”,
为了利便地读取这个JSON输出,我发起安装“jq”。
  logsBloom: “0x00000000000000000000000000000000000000000400000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000002000000000000000800000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000004000000000000000000000000000000000”,
  contractAddress: “0xb2c455aa1ac281180b4faca7b8896092169ccd55”,
      “bin”: “608060405234801561001057600080fd5b5060008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102f3806100db6000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063893d20e8146100465780638da5cb5b1461007a578063a6f9dae1146100ae575b600080fd5b61004e6100f2565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b61008261011b565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6100f0600480360360208110156100c457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061013f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610200576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260138152602001807f43616c6c6572206973206e6f74206f776e65720000000000000000000000000081525060200191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a73560405160405180910390a3806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505056fea2646970667358221220ba9a494e8dd99908b8c962ed752cac0bf679199c5c8dad87d13f5f9d9088c39c64736f6c63430007010033”
我此刻要毗连到我的事情节点…
    owner = newOwner;
    event OwnerSet(address indexed oldOwner, address indexed newOwner);
这就是我们会见合约要领的方法。
  OwnerSet: function(),
    owner = msg.sender;
    stateMutability: “view”,
  }, {

对付0.7.1中的合约,语法略有差异。请在下面利用我变动后的Owner.sol。您还可以将Remix中的编译器版本变动为0.7.1,并也利用此合约。
Bundles the app into static files for production.yarn test
要陈设合约,请执行以下操纵。
address private owner;
此刻你可以在这里提供合约地点。我们正在利用一个变量,但您也可以提供实际地点。请留意以前利用的“.at”而不是“.new”。这意味着您但愿检索现有合约,而不是建设新合约。
        owner = msg.sender;
datadir: /root/.ethereum/net2020
  }, {

快速Remix先容
    inputs: [],
pragma solidity >=0.4.22 <0.8.0;
        require(msg.sender == owner, “Caller is not owner”);
  transactionIndex: 0
  cumulativeGasUsed: 287352,

此刻我再次运行getOwner要领,并正确地将所有者变动为0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2

        owner = newOwner;
  topics: [“0x342827c97908e5e2f71151c08502a66d44b6f758e3ac2f1de95f02eb95f0a735”, “0x0000000000000000000000000000000000000000000000000000000000000000”, “0x0000000000000000000000000000000000000000000000000000000000000000”],

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读