Avatar
0
monkey Enlightened
monkey Enlightened
Tạo ethereum smart contract đơn giản trong mạng private
  1. tạo file genesis.json
    {
        "config": {
            "chainId": 15,
            "homesteadBlock": 0,
            "eip150Block": 0,
            "eip155Block": 0,
            "eip158Block": 0,
            "byzantiumBlock": 0,
            "constantinopleBlock": 0
        },
        "difficulty": "400000",
        "gasLimit": "2100000",
        "alloc": {}
    }
    
  1. khởi tạo blockchain: <code>geth --datadir . init genesis.json</code>
  2. khởi động node <code>geth --syncmode fast --allow-insecure-unlock --cache 512 --ipcpath ~/Library/Ethereum/geth.ipc --networkid 1234 --datadir . --http --http.api "personal,eth,net,web3,miner" --http.corsdomain "*" --nodiscover</code>
  3. tạo account đầu tiên: <code>geth --datadir . account new</code>
  4. khởi động geth console: <code>geth attach http://localhost:8545/</code>
  5. 1 chạy lệnh để kiểm tra balance: <code>eth.getBalance(eth.accounts[0])</code>
  6. 2 chạy lệnh để unlock account: <code>personal.unlockAccount(eth.accounts[0])</code>
  7. 3 chạy lệnh để set ethbase: <code>miner.setEtherbase(eth.accounts[0])</code>
  8. 4 start miner: <code>miner.start()</code>
  9. tạo thư mục truffle: <code>mkdir truffle</code>
  10. <code>cd truffle</code>
  11. khởi tạo module, chạy lệnh: <code>truffle init</code>. Kết quả chúng ta có thự mục với cấu trúc:
<img src="https://stackask.com/wp-content/uploads/2021/11/Screen-Shot-2021-11-03-at-8.41.12-AM.png" alt="" class="alignnone size-full wp-image-2938" />
  1. Cập nhật file <code>Migrations.sol</code>
    // SPDX-License-Identifier: MIT
    pragma solidity &gt;=0.4.22 &lt;0.9.0;
    
    contract Migrations {
        
        string public message;
        uint public last_completed_migration;
        
        function Hello() public{
            message = "Hello World!";
        }
    
        function setCompleted(uint completed) public{
            last_completed_migration = completed;
        }
    }
    
  1. Cập nhât file <code>truffle-config.js</code>
    module.exports = {
        networks: {
            development: {
                host: "localhost",
                port: 8545,
                network_id: "*",
                gas: 1192085,
                from: "0x3c31b4b9d6c24bad29498f99aaa6914231362c7f"
            }
        }
    };
    
  1. Chạy lệnh: <pre>truffle compile</pre>
  2. Chạy lệnh: <pre>truffle migrate</pre>
  • Answer
blockchain ethereum
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
Các link tham khảo:
  1. <a href="https://medium.com/datawallet-blog/how-to-deploy-a-local-private-ethereum-blockchain-c2b497f068f4" target="_blank" rel="nofollow ugc">https://medium.com/datawallet-blog/how-to-deploy-a-local-private-ethereum-blockchain-c2b497f068f4
  2. <a href="https://www.edureka.co/blog/ethereum-smart-contract-project" target="_blank" rel="nofollow ugc">https://www.edureka.co/blog/ethereum-smart-contract-project
  3. <a href="https://github.com/ethereum/go-ethereum/issues/20676" target="_blank" rel="nofollow ugc">https://github.com/ethereum/go-ethereum/issues/20676
  4. <a href="https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts" target="_blank" rel="nofollow ugc">https://www.trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts
  5. <a href="https://ethereum.stackexchange.com/questions/87245/error-returned-error-invalid-opcode-shr/103943" target="_blank" rel="nofollow ugc">https://ethereum.stackexchange.com/questions/87245/error-returned-error-invalid-opcode-shr/103943
  6. <a href="https://stackoverflow.com/questions/67366053/geth-private-network-return-error-invalid-opcode-selfbalance-when-executing-a" target="_blank" rel="nofollow ugc">https://stackoverflow.com/questions/67366053/geth-private-network-return-error-invalid-opcode-selfbalance-when-executing-a
  • 0
  • Reply