美文网首页程序员分享区块链专题区块链
从0了解区块链-了解以太坊智能合约

从0了解区块链-了解以太坊智能合约

作者: 文博客 | 来源:发表于2021-02-25 11:21 被阅读0次

    继续更新区块链-以太坊的文章,话不多少直接开整。

    本文来源并翻译至:https://github.com/kauriio/archive/blob/main/docs/collections/Ethereum%20101/ethereum-101-part-5-the-smart-contract.md

    智能合约

    快速总览

    智能合约是一个无处不在的术语,在整个大区块链生态系统中的定义各不相同。

    智能合约只是计算机程序。在以太坊的上下文中,智能合约指的是EVM专用的特殊编程语言的源代码:Solidity,Vyper,LLL,Bamboo和Serpent。

    使用构建的专用语言来编写智能合约有很好的理由,但在本文档中不会讨论这些理由。试图做一个总结,由于EVM的极简性和约束性,从头开始构建一种特殊用途的语言比将一种通用语言弯曲成EVM的复杂需求更容易。

    高级语言

    智能合约开发者有多种高级语言可供选择。在前的一节中,我们强调了Ethereum客户端的多样性然而解释了Geth和Parity这两个客户端仍然是最出和最广泛使用的。Ethereum高级语言的状况略相似。Solidity是事实上的、使用最广泛的语言而Vyper则是作为一种有竞争力的替代语言出现的。

    高级语言的表比较

    High-Level Language Syntactic similarities Link to Documentation
    Solidity JavaScript / Java / C++ https://solidity.readthedocs.io/en/latest/
    Vyper Python https://vyper.readthedocs.io/en/latest/
    LLL Lisp https://lll-docs.readthedocs.io/en/latest/
    Bamboo Erlang https://github.com/CornellBlockchain/bamboo
    Serpent, outdated Python https://github.com/ethereum/serpent

    Solidity 简单的例子

    Solidity是Ethereum智能合约的事实上的语言,所以在接下来的几个段落中,我们将只使用Solidity源码。

    下面您将找到一个Solidity合同的源代码(有用的信息被注释在右边的'//'处)。

    pragma solidity 0.5.3; // Solidity version 0.5.3, pragma used to enable certain compiler features or checks
    
    contract HelloWorld // defining the contract
    {
        string greeting; // defining the state string variable 'greeting'
        
        constructor() // constructor function, optional, executed once upon deployment and cannot be called again
        public
        {
            greeting = "Hello, World.";
        }
        
        function printGreeting() // defining a function
        public // this function is callable by anyone 
        view // dictates that this function promises to not modify the state
        returns (string memory) // function returns a string variable from memory
        {
            return greeting;
        }
        
        function setGreeting(string memory _greeting)
        public 
        // notice that this function contains no "view" -- it modifies the state
        {
            greeting = _greeting;
        }
    }
    

    该合同使用Solidity编译器0.5.3版进行编译。 在remix.ethereum.org上尝试一下。

    重现合约,将其部署到JavaScript测试区块链(Javascript虚拟机),并与函数进行交互:

    1. 将上述源代码复制并粘贴到remix.ethereum.org的文本编辑器中

    2. 在“编译”选项卡中,将编译器版本设置为“ 0.5.3 + commit.10d17f24 ...”,以匹配我们的合同版本0.5.3。

    3. 切换到“运行”选项卡,然后将“环境”下拉框调整为“ JavaScript VM”。

    4. 在下面的框中,找到我们的“ Hello World”合同,然后使用浅红色按钮将合同部署到JavaScript测试区块链。

    5. 在正下方的两个方框中,找到“已部署合同”部分。 在其中,您将看到两个按钮:

    • 'printGreeting'将打印当前问候语(由于视图声明,该按钮为蓝色!)
    • 'setGreeting'将当前问候语更改为您选择的字符串,只需确保将其括在引号“”之内即可(由于该函数更改了状态,因此已读取按钮!)

    我们只是使用Remix来编写,部署和与“HelloWorld”智能合约进行交互。Remix是用于智能合约开发的强大工具。

    Vyper 简单的例子

    greeting: public(bytes[32]) # defining greeting state variable, just like Solidity
    
    @public
    def __init__(): # initialization function, same as Solidity's constructor function
        self.greeting = "Hello, World."
        
    @public # function can be called internally and externally
    @constant # function will not change state
    def printGreeting() -> bytes[32]:
        return self.greeting
    
    @public
    def setGreeting(_greeting: bytes[32]): # a state changing function
        self.greeting = _greeting
    
    

    我们将使用MyEtherWallet作为Vyper合约:

    1. 打开两个页签:
    2. 将以上源代码复制并粘贴到Vyper在线编译器中,然后进行编译。
    3. 在MyEtherWallet上,导航至“部署合同”并使用Vyper在线编译器上“字节码”和“ ABI”选项卡的输出填充字段。
    4. 使用MetaMask连接到Rinkeby网络并签署交易。
    5. 使用MyEtherWallet查看状态变量,使用MetaMask使用我们的“setGreeting(bytes [32])”进行状态更改
    6. Gas成本昂贵,但这只是出于示范目的。
    7. 使用十六进制到ASCII文本转换器来转换功能输入:

    工具入门

    有关Kauri的 General Solidity文档

    由活跃的web3开发人员编写的使用真实代码的最新文档。

    Remix Solidity

    From the Remix documentation, verbatim:

    Remix是一个功能强大的开源工具,可帮助您直接从浏览器编写Solidity合同。 Remix用JavaScript编写,支持在浏览器和本地使用。



    Remix还支持智能合约的测试,调试和部署等等。

    Remix 相关文档在Kauri中:

    由活跃的web3开发人员编写的使用真实代码的最新文档。

    链接 & 额外信息:

    Truffle 开发框架

    From the Truffle documentation, verbatim:

    使用Ethereum虚拟机(EVM)为区块链提供世界级的开发环境、测试框架和资产管道,旨在让开发者的生活更加轻松。

    Truffle 相关文档在Kauri中:

    由活跃的web3开发人员编写的使用真实代码的最新文档。

    链接 & 额外信息:

    Embark 框架

    From the Embark documentation, verbatim:

    Embark是一个快速、易用、强大的开发者环境,用于构建和部署去中心化应用,也就是"DApps"。它集成了Ethereum区块链、IPFS和Swarm等去中心化存储以及Whisper等去中心化通信平台。

    Embark的目标是通过提供所有需要的工具并同时保持可扩展性,使构建去中心化应用尽可能简单。

    Embark 相关文档在Kauri中:

    由活跃的web3开发人员编写的使用真实代码的最新文档。

    链接 & 额外信息:

    相关文章

      网友评论

        本文标题:从0了解区块链-了解以太坊智能合约

        本文链接:https://www.haomeiwen.com/subject/wdiyfltx.html