链上部署智能合约

文章前言

随着区块链技术的发展智能合约已经成为了区块链应用中不可或缺的一部分,以太坊作为一种基于区块链技术的去中心化平台,其智能合约功能更是被广泛应用于各种领域,本文将为大家介绍在以太坊链上部署智能合约的过程和注意事项,希望能够为大家带来一些帮助

智能合约

智能合约(Smart Contract)是一种基于区块链技术的自动化合约,它能够自动执行、管理和验证合约条款,智能合约是一段由计算机代码编写的程序,它被部署在区块链上并且能够自动执行其中定义的条件和操作,智能合约可以实现一系列复杂的业务逻辑,例如:数字货币的转移、贸易的结算、股票的发行和交易、保险的理赔等

智能合约的特点是去中心化、不可篡改和自动执行,由于智能合约被部署在区块链上,因此它不需要中心化的机构来管理和执行合约,也不会受到单点故障的影响,同时智能合约的内容被加密存储在区块链上,保证了合约的不可篡改性,智能合约能够自动执行其中定义的条件和操作,减少了人为干预的可能性,提高了合约的可信度和有效性,智能合约的应用场景非常广泛,尤其在金融、物流、保险、知识产权等领域具有巨大的潜力,智能合约可以帮助企业降低成本、提高效率、增强安全性,同时也能够为消费者提供更加便利和可信的服务

合约部署

智能合约在链上部署要经过以下步骤:

Step 1:编写智能合约

// SPDX-License-Identifier: SimPL-2.0pragma solidity ^0.6.8; 
contract HelloWorld { string public greeting;
constructor() public { greeting = \\\"Hello, World!\\\"; } function setGreeting(string memory _greeting) public { greeting = _greeting; }}

Step 2:将上面的合约代码拷贝到https://remix.ethereum.org编译器中进行编译操作

然后拷贝字节码Bytecode:

608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b61030c806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063ef690cc0146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610231565b5050565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102295780601f106101fe57610100808354040283529160200191610229565b820191906000526020600020905b81548152906001019060200180831161020c57829003601f168201915b505050505081565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027257805160ff19168380011785556102a0565b828001600101855582156102a0579182015b8281111561029f578251825591602001919060010190610284565b5b5090506102ad91906102b1565b5090565b6102d391905b808211156102cf5760008160009055506001016102b7565b5090565b9056fea2646970667358221220995fc5abfc2eef29586818465f505c1afee4b62fdf8c743fd55fb4b32a8f530b64736f6c63430006080033

ABI:

    [  {    \\\"inputs\\\": [],    \\\"stateMutability\\\": \\\"nonpayable\\\",    \\\"type\\\": \\\"constructor\\\"  },  {    \\\"inputs\\\": [],    \\\"name\\\": \\\"greeting\\\",    \\\"outputs\\\": [      {        \\\"internalType\\\": \\\"string\\\",        \\\"name\\\": \\\"\\\",        \\\"type\\\": \\\"string\\\"      }    ],    \\\"stateMutability\\\": \\\"view\\\",    \\\"type\\\": \\\"function\\\"  },  {    \\\"inputs\\\": [      {        \\\"internalType\\\": \\\"string\\\",        \\\"name\\\": \\\"_greeting\\\",        \\\"type\\\": \\\"string\\\"      }    ],    \\\"name\\\": \\\"setGreeting\\\",    \\\"outputs\\\": [],    \\\"stateMutability\\\": \\\"nonpayable\\\",    \\\"type\\\": \\\"function\\\"  }]

    Step 3:在bejson中转义成字符串 

    http://www.bejson.com/jsonviewernew/

    [{\\\\\\\"inputs\\\\\\\":[],\\\\\\\"stateMutability\\\\\\\":\\\\\\\"nonpayable\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"constructor\\\\\\\"},{\\\\\\\"inputs\\\\\\\":[],\\\\\\\"name\\\\\\\":\\\\\\\"greeting\\\\\\\",\\\\\\\"outputs\\\\\\\":[{\\\\\\\"internalType\\\\\\\":\\\\\\\"string\\\\\\\",\\\\\\\"name\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"string\\\\\\\"}],\\\\\\\"stateMutability\\\\\\\":\\\\\\\"view\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"function\\\\\\\"},{\\\\\\\"inputs\\\\\\\":[{\\\\\\\"internalType\\\\\\\":\\\\\\\"string\\\\\\\",\\\\\\\"name\\\\\\\":\\\\\\\"_greeting\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"string\\\\\\\"}],\\\\\\\"name\\\\\\\":\\\\\\\"setGreeting\\\\\\\",\\\\\\\"outputs\\\\\\\":[],\\\\\\\"stateMutability\\\\\\\":\\\\\\\"nonpayable\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"function\\\\\\\"}]

    链上部署智能合约

    Step 4:通过abi创建一个合约对象

    var abi=JSON.parse(\\\'[{\\\\\\\"inputs\\\\\\\":[],\\\\\\\"stateMutability\\\\\\\":\\\\\\\"nonpayable\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"constructor\\\\\\\"},{\\\\\\\"inputs\\\\\\\":[],\\\\\\\"name\\\\\\\":\\\\\\\"greeting\\\\\\\",\\\\\\\"outputs\\\\\\\":[{\\\\\\\"internalType\\\\\\\":\\\\\\\"string\\\\\\\",\\\\\\\"name\\\\\\\":\\\\\\\"\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"string\\\\\\\"}],\\\\\\\"stateMutability\\\\\\\":\\\\\\\"view\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"function\\\\\\\"},{\\\\\\\"inputs\\\\\\\":[{\\\\\\\"internalType\\\\\\\":\\\\\\\"string\\\\\\\",\\\\\\\"name\\\\\\\":\\\\\\\"_greeting\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"string\\\\\\\"}],\\\\\\\"name\\\\\\\":\\\\\\\"setGreeting\\\\\\\",\\\\\\\"outputs\\\\\\\":[],\\\\\\\"stateMutability\\\\\\\":\\\\\\\"nonpayable\\\\\\\",\\\\\\\"type\\\\\\\":\\\\\\\"function\\\\\\\"}]\\\')contract = web3.eth.contract(abi)

    链上部署智能合约

    Step 5:检查coinbase账户余额

      > miner.setEtherbase(eth.accounts[0])true> eth.coinbase\\\"0x84e0d961774b5ae49f2968b51bfce611096adace\\\"> eth.getBalance(eth.coinbase)85999992654807192250> balance=web3.fromWei(eth.getBalance(eth.coinbase),\\\"ether\\\")85.99999265480719225> 

      链上部署智能合约

      注:如果这里的余额大于0,则继续,否则,开始挖矿,等你觉得差不多的时候再停止就OK

      Step 6:解锁coinbase账户并使用coinbase账户来部署合约

      personal.unlockAccount(eth.coinbase,\\\'12345678\\\')

      链上部署智能合约

      Step 7:部署合约

      #命令格式:var bytecode = \\\"0x智能合约字节码\\\"var greeting = \\\"Hello  World!\\\"    var deploy = {from: \\\"0x账户地址\\\",data: bytecode, arguments: [greeting]};    var gas = web3.eth.estimateGas(deploy);var tx = contract.deploy(deploy);
      #执行示例var bytecode = \\\"0x608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b61030c806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063a41368621461003b578063ef690cc0146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610231565b5050565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102295780601f106101fe57610100808354040283529160200191610229565b820191906000526020600020905b81548152906001019060200180831161020c57829003601f168201915b505050505081565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027257805160ff19168380011785556102a0565b828001600101855582156102a0579182015b8281111561029f578251825591602001919060010190610284565b5b5090506102ad91906102b1565b5090565b6102d391905b808211156102cf5760008160009055506001016102b7565b5090565b9056fea2646970667358221220995fc5abfc2eef29586818465f505c1afee4b62fdf8c743fd55fb4b32a8f530b64736f6c63430006080033\\\"var greeting = \\\"Hello Al1ex!\\\" var deploy = {from:\\\"0x84e0d961774b5ae49f2968b51bfce611096adace\\\", data:bytecode,arguments: [greeting]}; var gas = web3.eth.estimateGas(deploy);var contracts = contract.new(deploy);

      链上部署智能合约

      Step 8:查看交易池中的交易信息

      txpool.statuseth.getBlock(\\\"pending\\\",true).transactions

      链上部署智能合约

      Step 9:进行挖矿操作

      miner.start(1);admin.sleepBlocks(1);miner.stop();

      链上部署智能合约

      Step 10:查看合约信息

      contracts

      链上部署智能合约

      Step 11:验证合约是否部署成功

      eth.getCode(contracts.address)

      Step 12:调用合约方法,很多人可能会好奇,这里为啥greeting没有变化?这是因为状态变量和全局变量的差异性所导致的,有兴趣的可以自我构建一个合约来进行验证以下

      文末小结

      本文介绍了以太坊智能合约在以太坊链上的步骤操作流程,主要从合约编写、合约编译、ABI、Bytecode等到链上创建部署合约、调用合约函数等方法

      公链基础安全专题到此结束,后续也会分享其余公链安全的内容,敬情期待~

      原创文章,作者:七芒星实验室,如若转载,请注明出处:https://www.sudun.com/ask/34152.html

      Like (0)
      七芒星实验室的头像七芒星实验室
      Previous 2024年4月7日
      Next 2024年4月7日

      相关推荐

      发表回复

      您的邮箱地址不会被公开。 必填项已用 * 标注