geth 部署智能合约
by Avadhoot Kulkarni
通过Avadhoot Kulkarni
Ever since Ethereum graced the crypto space with its presence in mid-2015, the revolutionary invention by Canadian-Russian Programmer Vitalik Buterin has given birth to many new decentralised applications (dApps). Along with the myriad of dApps being built, Ethereum’s success is mainly attributed to its implementation of smart contracts.
自从以太坊(Ethereum)在2015年中期为其加密货币领域增光添彩以来,加拿大-俄罗斯程序员Vitalik Buterin的革命性发明催生了许多新的去中心化应用程序(dApps)。 随着无数dApp的建立,以太坊的成功主要归功于其智能合约的实施。
Interestingly enough, the invention of smart contracts dates back to 1996. Computer scientist Nick Szabo drew up the term “smart contracts,” and explains them as follows:
有趣的是,智能合约的发明可以追溯到1996年。计算机科学家Nick Szabo提出了“智能合约”一词,并对其解释如下:
“I call these new contracts “smart”, because they are far more functional than their inanimate paper-based ancestors. No use of artificial intelligence is implied. A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises”
“我称这些新合同为“智能”合同,因为它们比无生命的纸质祖先具有更多的功能。 不暗示不使用人工智能。 智能合约是一组以数字形式指定的承诺,包括各方在其中履行这些承诺的协议”
— Nick Szabo, 1996
尼克·萨博 ( Nick Szabo) ,1996年
His work later went on to inspire many other researchers and scientists, including Vitalik, who created Ethereum.
他的工作后来继续启发其他许多研究人员和科学家,包括创建以太坊的Vitalik。
Before we delve further into the guide, it is important to understand two important concepts.
在深入研究指南之前,重要的是要了解两个重要概念。
The first thing that we need to understand is what the Ethereum Virtual Machine (EVM) is. Its sole purpose is to act as a runtime environment for smart contracts based on Ethereum. Think of it as a global super computer that runs all the smart contracts. As the name suggests, the EVM is virtual and not a physical machine. You can read more about the EVM here.
我们需要了解的第一件事是以太坊虚拟机( EVM )。 它的唯一目的是充当基于以太坊的智能合约的运行时环境。 可以将其视为运行所有智能合约的全球超级计算机。 顾名思义,EVM是虚拟的,而不是物理机。 您可以在此处阅读有关EVM的更多信息 。
The second concept we need to understand is what is gas. In the EVM, gas is a unit of measurement used to assign a fee to each transaction with a smart contract. Each computation that happens in the EVM requires gas. The more complex and tedious it is, the more gas is needed to execute the smart contract.
我们需要了解的第二个概念是什么是天然气 。 在EVM中,天然气是一种计量单位,用于为具有智能合约的每笔交易分配费用。 EVM中发生的每个计算都需要耗费大量精力。 它越复杂且乏味,则执行智能合约所需的气体就越多。
Every transaction specifies the gas price it is willing to pay in ether for each unit of gas, allowing the market to decide the relationship between the price of ether and the cost of computing operations (as measured in gas). It’s the combination of the two, total gas used multiplied by gas price paid, that results in the total fee paid by a transaction.
每笔交易都指定了愿意为每种天然气单位以醚支付的天然气价格,从而允许市场决定以太价格与计算操作成本(以天然气为单位)之间的关系。 它是两者的总和,乘以所用的总天然气乘以所支付的天然气价格,得出交易所支付的总费用。
Fee for transaction = Total gas used * gas price;
Read more about gas here.
在这里阅读更多关于天然气的信息 。
Now that you have basic knowledge about what a smart contract is and how the smart contract runs, we can go straight into how we are going to make our very own smart contract!
既然您已经了解什么是智能合约以及如何运行智能合约,我们就可以直接研究如何制作自己的智能合约!
We’re going to use a tool for this: Pragma. It’s an easy-to-use platform for creating and deploying smart contracts. Sign up here and go the editor:
我们将为此使用工具:Pragma。 它是用于创建和部署智能合约的易于使用的平台。 在这里注册并进入编辑器:
Log in to Metamask. If you haven’t installed MetaMask yet, you can start here.
登录到Metamask。 如果尚未安装MetaMask,则可以从此处开始 。
Switch to the Kovan test network both in Pragma and MetaMask.Just to give you a brief overview about testnets, check out this article.
切换到Pragma和MetaMask中的Kovan测试网络。只需要简要介绍一下测试网络,请查看本文 。
The Ethereum mainnet is the official Ethereum network. It is more secure, and uses Ether, which has real monetary value.
以太坊主网是官方的以太坊网络。 它更安全,并使用具有实际货币价值的以太币。
Testnets are playground Ethereum networks in which the Ether is agreed to have no monetary value. Developers use these playgrounds to test applications before deploying them to the mainnet for their users.
Testnet是以太坊操场网络,其中以太币被认为没有货币价值。 开发人员使用这些游乐场测试应用程序,然后再将其部署到用户的主网上。
To switch between these networks, click on the network name next to the MetaMask icon and select the network. For this tutorial, please choose Kovan.
要在这些网络之间切换,请单击MetaMask图标旁边的网络名称,然后选择网络。 对于本教程,请选择Kovan 。
The following contract will implement the simplest form of a cryptocurrency. It is possible to generate coins out of thin air, but only the person that created the contract is able to do that (it is trivial to implement a different issuance scheme). Furthermore, anyone can send coins to each other without needing to register with a username and password. All you need is an Ethereum keypair.
以下合约将实现最简单形式的加密货币。 可以凭空产生硬币,但是只有创建合同的人才能做到这一点(实现不同的发行方案很简单)。 此外,任何人都可以彼此发送硬币,而无需使用用户名和密码进行注册。 您只需要一个以太坊密钥对。
This code basically lets you mint and send tokens to other accounts.
这段代码基本上可以让您铸造令牌并将令牌发送到其他帐户。
Let’s go through it line by line:
让我们逐行介绍一下:
pragma solidity ^0.4.21;
This indicates that the source code is written for Solidity version 0.4.21 or anything newer that does not break functionality. This is to ensure that the code doesn’t behave differently with the new compiler versions.
这表明源代码是为Solidity版本0.4.21或任何不破坏功能的较新版本编写的。 这是为了确保代码不会与新的编译器版本有所不同。
contract yourToken
Everything related to yourToken goes inside this contract. Essentially, a contract in solidity is the collection of functions and state (code and data) sitting at an address on the Ethereum blockchain.
与yourToken相关的所有内容都包含在此合同中。 本质上,契约是位于以太坊区块链地址上的功能和状态(代码和数据)的集合。
address public minter;
This is the address of the minter. The keyword “public” makes those variables readable from outside.
这是铸币商的地址。 关键字“ public”使这些变量可以从外部读取。
event Sent(address from, address to, uint amount);
Events allow light clients (UI) to react to the changes efficiently.
通过事件,轻客户端(UI)可以有效地响应更改。
function yourToken() public {
minter = msg.sender;
}
Let’s set your Ethereum address as minter of the contract. You’ll need to access the contract through your MetaMask to be able to mint. We’ll go through this again after deploying the contract.
让我们将您的以太坊地址设置为合约的创建者。 您需要通过您的MetaMask访问合同才能创建合同。 部署合同后,我们将再次进行说明。
function mint(address receiver, uint amount) public {
if(msg.sender != minter) return;
balances[receiver]+=amount;
}
This function lets you mint the amount of coins you want to. You can mint as many tokens as you want to. The if condition tells the system to stop executing if you’re not the minter, which is set in yourToken function.
此功能使您可以铸造所需数量的硬币。 您可以根据需要铸造任意数量的令牌。 如果条件不是,则系统会停止执行,如果您不是在标记函数中设置的铸造者。
If you are in fact the minter, it lets you mint the tokens.
如果您实际上是铸造者,则可以铸造令牌。
function send(address receiver, uint amount) public {
if(balances[msg.sender] < amount) return;
balances[msg.sender]-=amount;
balances[receiver]+=amount;
emit Sent(msg.sender, receiver, amount);
}
This is a function that lets one address send the tokens to another address. It takes two parameters: receiver and amount. It reduces the amount from the sender’s address and adds the same amount to receiver’s address. Event Sent, which we declared earlier, is now used to do the transfer. Currently, we have kept the sender as msg.sender, which is the minter, as we do not want to complicate the contract.
此功能允许一个地址将令牌发送到另一个地址。 它有两个参数:接收者和金额。 它减少了发件人地址中的金额,并将相同的金额添加到了收件人地址中。 我们之前已声明的事件发送已用于传输。 目前,由于我们不希望使合同复杂化,因此我们将发件人保留为msg.sender(即铸造者)。
That’s it. Your contract is now ready, so let’s compile it.
而已。 现在您的合同已经准备好了,让我们对其进行编译。
Once the contract is compiled, let’s deploy it on the blockchain. As mentioned earlier, we’ll use Kovan testnet to deploy the contract.
合同编译后,让我们将其部署在区块链上。 如前所述,我们将使用Kovan testnet部署合同。
Check if the smart contract is deployed.
检查是否部署了智能合约。
For the contract I deployed for this tutorial, this is the transaction. You can also see it in Pragma under your contracts.
对于我为本教程部署的合同, 这就是交易 。 您也可以在Pragma的合同下看到它。
There you have it. Your first smart contract, deployed on blockchain. :)
你有它。 您的第一个智能合约,已部署在区块链上。 :)
A lot of new concepts were introduced along with a couple of amazingly helpful tools. It might be a little overwhelming, and that’s okay! Just try to get your head around the concepts and then run with it.
引入了许多新概念以及一些非常有用的工具。 可能有点不知所措,没关系! 只需尝试着领会这些概念,然后再进行实践即可。
Have you created any simple but interesting smart contracts? Post them in the comments and I’ll add them in the post for reference.
您是否创建了任何简单但有趣的智能合约? 将它们发布在评论中,我将它们添加在帖子中以供参考。
Have questions? Add them in the comments or join our telegram group and talk to us directly.
有问题吗? 将他们添加到评论中,或加入我们的电报组并直接与我们联系 。
翻译自: https://www.freecodecamp.org/news/how-to-write-and-deploy-your-first-smart-contract-341d5e2ffb35/
geth 部署智能合约