zeppelin连接数据源_使用开放源代码合同(open-zeppelin)创建以太坊令牌

zeppelin连接数据源

by Danny

通过丹尼

使用开放源代码合同(open-zeppelin)创建以太坊令牌 (Create an Ethereum token using open source contracts (open-zeppelin))

I want to show you that creating a best practice token is a simple process. To be honest, we are going to be doing some coding, but it won’t be much.

我想向您展示创建最佳实践令牌是一个简单的过程。 老实说,我们将要进行一些编码,但是不会太多。

We’ll be using Solidity to create our Ethereum token. But don’t worry, there are a lot of open source libraries and contracts to help us in the process.

我们将使用Solidity创建我们的以太坊令牌。 但是不用担心,有很多开源库和合同可以在此过程中为我们提供帮助。

What we want is an ERC-20 compliant token. What that means is that the Ethereum developers have decided a set of functionalities that is necessary for your most common token usages today. There are other types of ERC standards, but we wont dive into it.

我们想要的是符合ERC-20的令牌。 这意味着以太坊开发人员已经决定了当今最常见的令牌使用所必需的一组功能。 还有其他类型的ERC标准,但我们不会深入探讨。

Requirements:

要求:

  • Github

    Github
  • Terminal

    终奌站
  • NodeJS

    节点JS
  • NPM

    NPM
  • Metamask (For initial Account Creation)

    Metamask(用于初始帐户创建)

Alright let’s start coding! The first thing we want to do is download truffleglobally. You can visit their repo at truffle and here’s the following snippet to install:

好吧,让我们开始编码! 我们要做的第一件事是全局下载truffle 。 您可以在松露处访问他们的存储库,以下是要安装的代码段:

npm install -g truffle

*note: make sure you have the latest version of truffle if you installed this prior

*注意 :如果您事先安装了最新版的松露,请确保已安装

Truffle will handle the smart contract compilation, linking, and deployment for us. It’s a library that will make our lives easier for this demonstration.

松露将为我们处理智能合约的编译,链接和部署。 这是一个图书馆,可以使我们的生活更加轻松。

Now we need to create a directory where our project will live. In my case I called it ethereum_token_tutorial.

现在,我们需要创建一个目录,该目录将用于我们的项目。 就我而言,我将其称为ethereum_token_tutorial。

So we have two options here. Either you can clone the repo I have created by following this:

因此,我们在这里有两个选择。 您可以按照以下步骤克隆我创建的存储库:

git clone -b initial_step https://[email protected]/danieljoonlee/ethereum_token_tutorial.git

Or you can do this in your terminal inside of your new directory:

或者,您可以在新目录内的终端中执行此操作:

truffle init

If you followed the second option of doing truffle init. The directory should look like this:

如果遵循第二种选择,即truffle init 。 该目录应如下所示:

etherem_token_tutorial|___contracts| |_____ConvertLib.sol| |_____MetaCoin.sol| |_____Migrations.sol|___migrations| |_____1_initial_migrations.js| |_____2_deploy_contracts.js|___test| |_____TestMetacoin.sol| |_____metacoin.js|___truffle.js

Go ahead and delete ConvertLib.sol , MetaCoin.sol , TestMetacoin.sol , metacoin.js.

继续并删除ConvertLib.solMetaCoin.solTestMetacoin.solmetacoin.js

So your directory should look like this now:

因此您的目录现在应如下所示:

etherem_token_tutorial|___contracts| |_____Migrations.sol|___migrations| |_____1_initial_migrations.js| |_____2_deploy_contracts.js|___test|___truffle.js

Great. Now we’re moving. Truffle helps us compile smart contracts and deploy them. But we deleted our smart contract files other than the migrating helper. Don’t worry, this is where Open-Zeppelin comes in.

大。 现在我们要搬家了。 松露可帮助我们编译和部署智能合约。 但是我们删除了迁移助手以外的智能合约文件。 别担心,这是Open-Zeppelin的用武之地。

Open-Zeppelin is an open source repo where you can find smart contracts with generally best practices, good test coverage, and most likely audited*.

Open-Zeppelin是一个开放源代码回购,您可以在其中找到具有最佳实践,良好的测试覆盖率以及最有可能经过审计*的智能合约。

  • Audit is when you have professional developers review your smart contracts looking for any leaks, bugs, or possibilities of malicious attacks.

    审核是指让专业开发人员查看您的智能合约,以查找任何泄漏,错误或恶意攻击的可能性。

Here’s a link if you’re interested in smart contract attacks: Link

如果您对智能合约攻击感兴趣,请使用以下链接: 链接

For us to use any Open-Zeppelin contracts we need to install it into our repository:

为了让我们使用任何Open-Zeppelin合同,我们需要将其安装到我们的存储库中:

npm init -ynpm install -E zeppelin-solidity

We initialized a package.json with npm init -y. We also installed the package for using the Open-Zeppelin contracts.

我们使用npm init -y初始化了package.json。 我们还安装了使用Open-Zeppelin合同的软件包。

Okay, we’re going to write some Solidity. I did mention in the article earlier that this will not be much code and I wasn’t joking!

好的,我们将编写一些Solidity。 我在前面的文章中确实提到过,这不会是太多代码,而且我不是在开玩笑!

Create a new file in the contracts folder. In my case I named it TestToken.sol

contracts文件夹中创建一个新文件。 就我而言,我将其命名为TestToken.sol

Now your directory should look like this:

现在您的目录应如下所示:

etherem_token_tutorial|___contracts| |_____Migrations.sol| |_____TestToken.sol***(this one is new)|___migrations| |_____1_initial_migrations.js| |_____2_deploy_contracts.js|___test|___truffle.js

In TestToken.sol we need to have the following code:

TestToken.sol我们需要以下代码:

// TestToken.solpragma solidity ^0.4.18;
import "zeppelin-solidity/contracts/token/ERC20/MintableToken.sol";
contract TestToken is MintableToken {    string public constant name = "Test Token";    string public constant symbol = "TT";    uint8 public constant decimals = 18;}

Let’s break this down since it’s quite a bit , even though it’s only a few lines of code.

让我们分解一下,因为它虽然很多,但只有几行代码。

pragma solidity ^0.4.18;

pragma solidity ^0.4.18;

It is required at the top of the file because it specifies the version of Solidity we’re using.

在文件顶部是必需的,因为它指定了我们正在使用的Solidity版本。

import "zeppelin-solidity/contracts/token/ERC20/MintableToken.sol";

The above code snippet is why Open-Zeppelin is so useful. If you know how inheritance works, our contract is inheriting from MintableToken. If you don’t know how inheritance works, MintableToken has a lot of functionalities saved in inMintableToken.sol. We can use these functionalities to create our token. If you visit this MintableToken you’ll notice a ton of functions and even more inheritance. It can be a bit of a rabbit hole, but for this demonstration purpose, I want us to release a token into the testnet.

上面的代码段是为什么Open-Zeppelin如此有用的原因。 如果您知道继承的工作原理,那么我们的合同就是从MintableToken继承的。 如果您不知道继承的工作原理,则MintableToken在inMintableToken.sol中保存了很多功能。 我们可以使用这些功能来创建令牌。 如果您访问此MintableToken,您会注意到大量的函数甚至更多的继承。 这可能有点麻烦,但是出于演示目的,我希望我们将令牌释放到测试网中。

For us, Mintable let’s us create as many tokens as we want, so we won’t be starting with an initial supply. In my next article, we’ll create a nodejs service that will create new tokens, and handle other ERC-20 standard functionalities.

对于我们来说,Mintable让我们创建所需数量的令牌,因此我们不会从初始供应开始。 在我的下一篇文章中,我们将创建一个nodejs服务,该服务将创建新令牌并处理其他ERC-20标准功能。

The next bit of code:

下一段代码:

contract TestToken is MintableToken {    string public constant name = "Test Token";    string public constant symbol = "TT";    uint8 public constant decimals = 18;}

This is where we can customize the token. In my case, I named mine “Test Token”, with the symbol “TT”, and decimals of 18. But why 18 decimals?

这是我们可以自定义令牌的地方。 在我的情况下,我将我的“ Test Token”命名为“ TT”,十进制数为18。但是为什么要18十进制数呢?

Decimals of 18 is fairly standard in the community. So if we have one test token it can potentially look like this 1.111111111111111111.

在社区中,小数点18是相当标准的。 因此,如果我们有一个测试令牌,它可能看起来像这样1.111111111111111111111。

Whelp. That’s all the Solidity coding we need to do for this token. We inherit all the main functionalities for a standardized ERC 20 token from Open-Zeppelin. After that we need to set our constants for the name, symbol, and decimals.

仔。 这就是我们需要为此令牌执行的所有Solidity编码。 我们继承了Open-Zeppelin标准化ERC 20令牌的所有主要功能。 之后,我们需要为名称,符号和小数设置常量。

Before we forget, we should create a Metamask account and get it funded with testnet ethereum.

在忘记之前,我们应该创建一个Metamask帐户,并使用testnet以太坊为其提供资金。

Go ahead and search MetaMask extension for Chrome, or follow this link

继续并搜索MetaMask于Chrome的MetaMask扩展程序,或点击此链接

After you install MetaMask you should see a series of steps. You can read through like terms of service. Eventually you’ll reach:

安装MetaMask后,您应该会看到一系列步骤。 您可以阅读类似的服务条款。 最终您将达到:

Input your password and confirm that password. On clicking create, you will see another screen.

输入您的密码并确认该密码。 单击创建时,您将看到另一个屏幕。

Make sure to save your seed words or copy them down into a text file. We will need those seed words to deploy the token onto the testnet.

确保保存您的种子词或将其复制到文本文件中。 我们将需要这些种子词来将令牌部署到测试网上。

Also more important is to change your test from Mainnet Test Net to Ropsten Test net. It’s on the top left of your MetaMask tab. Here is the drop down:

同样重要的是将您的测试从Mainnet测试网更改为Ropsten测试网。 它在您的MetaMask标签的左上方。 这是下拉列表:

The reason we’re using Ropsten Test Network is because it’s the closest testnet/implementation of the Main Ethereum Network.

我们使用Ropsten测试网络的原因是因为它是以太坊主网络最接近的测试网/实现。

Next you will need to copy your address to clipboard from the ... menu like so:

接下来,您需要将地址从...菜单复制到剪贴板,如下所示:

You should have an address similar to this one copied to your clipboard:

您应该将与该地址相似的地址复制到剪贴板:

address: 0x8EeF4Fe428F8E56d2202170A0bEf62AAc93989dE

This is the address from which we’re going to deploy our token contract. Now one thing you need to know about deploying contracts is that they cost Ethereum, to be specific Gas. We’re going to need to get some testnet Ethereum into our accounts.

这是我们将用来部署令牌合约的地址。 现在,您需要了解的有关部署合同的一件事是,它们要花费以太坊,具体来说就是Gas。 我们将需要在账户中加入一些以太坊测试网。

Now that you have your address go to this Ropsten faucet link:

现在您有了地址,请转到以下Ropsten水龙头链接:

Ethernet FaucetEdit descriptionfaucet.ropsten.be

以太网水龙头 编辑描述 faucet.ropsten.be

Copy and paste your address and soon you should have 1 Ethereum in your MetaMask wallet for your address.

复制并粘贴您的地址,不久您的MetaMask钱包中应该有1个以太坊作为您的地址。

Just one more thing before we start coding our deployment process! We’re going to use a free API called Infura.io:

在开始对部署过程进行编码之前,还有一件事情! 我们将使用一个名为Infura.io的免费API:

Infura — Scalable Blockchain InfrastructureSecure, reliable, and scalable access to Ethereum APIs and IPFS gateways.infura.io

Infura —可扩展的区块链基础架构 对以太坊API和IPFS网关的安全,可靠和可扩展的访问。 信息

Sign up for their services. You should get an email from them or be redirected to a site with your API Key. The one we want specifically is from the Ropsten Network.

注册他们的服务。 您应该从他们那里收到电子邮件,或使用API​​密钥将其重定向到网站。 我们特别想要的是来自Ropsten网络的产品。

Test Ethereum Network (Ropsten)https://ropsten.infura.io/API_KEY

Copy your API_KEY.

复制您的API_KEY。

Almost there! Now let’s start working on our deployment. Let’s head back in our code.

差不多了! 现在,让我们开始进行部署。 让我们回到我们的代码中。

First things first, let’s talk about security. Create a new file in your root directory called .env. Your file structure should now look like this:

首先,让我们谈谈安全性。 在您的根目录中创建一个名为.env的新文件。 您的文件结构现在应如下所示:

etherem_token_tutorial|___contracts| |_____Migrations.sol| |_____TestToken.sol|___migrations| |_____1_initial_migrations.js| |_____2_deploy_contracts.js|___test|___truffle.js|___.env**(new file)

Inside your .env file lets add some environmental variables (these are variables that you can access anywhere in your code directory)

.env文件中,可以添加一些环境变量(这些变量可以在代码目录中的任何位置访问)

//.env fileINFURA_API_KEY=API_KEYMNENOMIC=MNEOMIC_FROM_METAMASK

First add your API_KEY you copied into the file.

首先将您复制的API_KEY添加到文件中。

Remember the Mneomic(seed words) from initializing Metamask chrome extension? We’re going to need that now to deploy the contracts from. If you downloaded or wrote down your Mneomic, now write them down in your .env file MNENOMIC=SOME KEY PHRASE YOU DONT WANT THE PUBLIC TO KNOW.

还记得初始化Metamask chrome扩展时的Mneomic(种子词)吗? 我们现在需要从中部署合同。 如果您下载或记下了Mneomic,请现在将其记入.env文件中MNENOMIC=SOME KEY PHRASE YOU DONT WANT THE PUBLIC TO KNOW..env MNENOMIC=SOME KEY PHRASE YOU DONT WANT THE PUBLIC TO KNOW.

IMPORTANT***

重要***

We added a .env file!!! We need to add a .gitignore file now to avoid adding the .env to a public repository if you ever decide to make the code public!

我们添加了.env文件!!! 如果您决定公开代码,我们现在需要添加.gitignore文件,以避免将.env添加到公共存储库中!

Create a .gitignore file in the same directory as your .env. Now it should look like this:

在与.env相同的目录中创建一个.gitignore文件。 现在看起来应该像这样:

etherem_token_tutorial|___contracts| |_____Migrations.sol| |_____TestToken.sol|___migrations| |_____1_initial_migrations.js| |_____2_deploy_contracts.js|___test|___truffle.js|___.env|___.gitignore**(newfile)

Inside your .gitignore file:

在您的.gitignore文件中:

// .gitignorenode_modules/build/.env

We want to ignore node_modules/ because when we do npm install it will download packages from our package.json. We want to ignore buildbecause later on when we run a script, it will create that directory for us automatically. We also want to ignore .env because it has private information we don’t want to release to the public.

我们想忽略node_modules/因为在进行npm install ,它将从package.json下载软件包。 我们想忽略build因为稍后运行脚本时,它将自动为我们创建该目录。 我们也想忽略.env因为它包含我们不希望向公众发布的私人信息。

Great! Over in our terminal we need to add two more modules.

大! 在我们的终端中,我们需要再添加两个模块。

npm install --save dotenv truffle-hdwallet-provider

Since we’re putting in private information, we need a way to access those variables from .env, and the dotenv package will help us.

由于我们要输入私人信息,因此我们需要一种从.env访问这些变量的.env ,而dotenv软件包将为我们提供帮助。

The second package, truffle-hdwallet-provider is a wallet enabled provider. Without this, we would need to download all the blocks or use a light wallet to make new transactions in the Ethereum network. With the wallet provider and Infura API. We can deploy instantly, also bypassing painful processes.

第二个软件包truffle-hdwallet-provider是启用了钱包的提供程序。 否则,我们将需要下载所有区块或使用轻钱包在以太坊网络中进行新交易。 使用钱包提供商和Infura API。 我们可以立即部署,也可以绕过繁琐的过程。

Over in the truffle.js in our root directory, we need to modify some configurations.

在根目录的truffle.js中,我们需要修改一些配置。

// truffle.jsrequire('dotenv').config();const HDWalletProvider = require("truffle-hdwallet-provider");
module.exports = {  networks: {    development: {      host: "localhost",      port: 7545,      gas: 6500000,      network_id: "5777"    },    ropsten: {        provider: new HDWalletProvider(process.env.MNENOMIC, "https://ropsten.infura.io/" + process.env.INFURA_API_KEY),        network_id: 3,        gas: 4500000    },  }};

The first line indicates we want to use the .env variables in this repo. Generally in most apps, you only need to require this once in the starting config file.

第一行表明我们要在此.env中使用.env变量。 通常,在大多数应用中,您只需在启动配置文件中要求一次。

Most of this is boilerplate. Main section we want to focus on is the ropsten network.

其中大部分是样板。 我们要关注的主要部分是绳索网络。

ropsten: {        provider: new HDWalletProvider(process.env.MNENOMIC, "https://ropsten.infura.io/" + process.env.INFURA_API_KEY),        network_id: 3,        gas: 4500000    },

The provider is our network. In our case, we want to deploy our token into the Ropsten network. Using the HDWalletProvider we pass in two arguments, process.env.MNENOMIC, "https://ropsten.infura.io/" + process.env.INFURA_API_KEY. We access our .env variables by referencing process.env.VARIABLE_NAME_IN_ENV.

提供者是我们的网络。 在我们的案例中,我们希望将令牌部署到Ropsten网络中。 使用HDWalletProvider我们传入两个参数process.env.MNENOMIC, "https://ropsten.infura.io/" + process.env.INFURA_API_KEY 。 我们通过引用process.env.VARIABLE_NAME_IN_ENV访问我们的.env变量。

We set the network_id: 3 because that represents Ropsten. 1 is the main Ethereum net and 2 is an old testnet.

我们将network_id: 3设置为network_id: 3因为它表示Ropsten。 1是主要的以太坊网络, 2是旧的测试网。

Lastly, we set gas: 4500000, which is why we needed the Ethereum originally. We use gas/ethereum any time we need to modify/add something in the Ethereum Network.

最后,我们将gas: 4500000设置为gas: 4500000 ,这就是为什么我们最初需要以太坊的原因。 每当需要在以太坊网络中修改/添加某些内容时,我们都会使用gas/ethereum

Alright, onto the last step before deployment!

好了,部署前的最后一步!

Over in our migrations/2_deploy_contract.js, we need to make some modifications for our contract.

在我们的migrations/2_deploy_contract.js ,我们需要对合同进行一些修改。

// 2_deploy_contract.js
const TestToken = artifacts.require("./TestToken.sol");
module.exports = function(deployer) {  deployer.deploy(TestToken);};

If you named your token contract file something else. You need to replace the TestToken.sol to whatever file you named it.

如果您将令牌合同文件命名为其他名称。 您需要将TestToken.sol替换为您命名的任何文件。

truffle compile

This should create a new folder in your directory:

这应该在您的目录中创建一个新文件夹:

etherem_token_tutorial|___build| |_____contracts|    |_____BasicToken.json|    |_____ERC20.json|    |_____ERC20Basic.json|    |_____Migrations.json|    |_____MintableToken.json|    |_____Ownable.json|    |_____SafeMath.json|    |_____StandardToken.json|    |_____TestToken.json|___contracts| |_____Migrations.sol| |_____TestToken.sol|___migrations| |_____1_initial_migrations.js| |_____2_deploy_contracts.js|___test|___truffle.js|___.env|___.gitignore**(newfile)

In our build folder, we have a bunch of contracts we inherited from the Open-Zeppelin library. If you’d like to know more about ERC-20 standards I’d check out the wiki. If there’s enough people asking for it I can make another blog post on it. For now here’s the link to the wiki.

在我们的build文件夹中,我们有一堆继承自Open-Zeppelin库的合同。 如果您想了解有关ERC-20标准的更多信息,请查看Wiki。 如果有足够的人要求它,我可以在上面发表另一篇博客文章。 现在,这里是Wiki的链接。

Here comes the moment of truth. Now we need to deploy the contracts into the Ropsten network. Enter the following line in your terminal:

关键时刻到了。 现在,我们需要将合​​同部署到Ropsten网络中。 在终端中输入以下行:

truffle migrate --network ropsten

You should get a series of lines in your terminal like:

您应该在终端中看到以下几行:

Using network 'ropsten'.
Running migration: 1_initial_migration.js  Deploying Migrations...  ... 0x7494ee96ad7db4a560b6f3169e0666c3938f9f54208f7972ab902feb049a7f68  Migrations: 0x254466c5b09f141ce1f93689db6257b92133f51aSaving successful migration to network...  ... 0xd6bc06b3bce3d15dee4b733e5d4b09f0adb8f93f75ad980bad078484641d36e5Saving artifacts...Running migration: 2_deploy_contracts.js  Deploying TestToken...  ... 0x7e5c1b37f1e509aea59cd297417efe93eb49fdab2c72fa5c37dd2c63a3ba67b7  TestToken: 0x02ec6cbd89d3a435f8805e60e2703ef6d3147f96Saving successful migration to network...  ... 0x2fd6d699295d371ffd24aed815a13c5a44e01b62ca7dc6c9c24e2014b088a34eSaving artifacts...

This will take some time. Once it’s fully deployed copy the last txid. In my case:

这将需要一些时间。 完全部署后,复制最后一个txid。 就我而言:

0x2fd6d699295d371ffd24aed815a13c5a44e01b62ca7dc6c9c24e2014b088a34e

This will have an address to your token contract. Here is a link to my txid:

这将有您的令牌合同的地址。 这是我的txid的链接:

Ropsten Transaction 0x2fd6d699295d371ffd24aed815a13c5a44e01b62ca7dc6c9c24e2014b088a34eRopsten (ETH) detailed transaction info for 0x2fd6d699295d371ffd24aed815a13c5a44e01b62ca7dc6c9c24e2014b088a34eropsten.etherscan.io

Ropsten交易0x2fd6d699295d371ffd24aed815a13c5a44e01b62ca7dc6c9c24e2014b088a34e Ropsten(ETH)详细的交易信息(0x2fd6d699295d371ffd24aed815a13c5a44e01b62ca7dc6c9c24eropstenio

Which has an address to the contract itself:

其中包含合同本身的地址:

Ropsten Accounts, Address and ContractsThe Ethereum BlockChain Explorer, API and Analytics Platformropsten.etherscan.io

Ropsten账户,地址和合约 以太坊区块链浏览器,API和分析平台 ropsten.etherscan.io

You can get the completed github repo here.

您可以在此处获取完整的github存储库。

This part one of a series of creating a token and interacting with it. In the next blog we will create a simple node microservice. We will use this service to call various functions on your token smart contract, such as minting new tokens, transferring, etc.

创建令牌并与之交互的一系列步骤之一。 在下一个博客中,我们将创建一个简单的节点微服务。 我们将使用此服务在您的令牌智能合约上调用各种功能,例如铸造新令牌,转让等。

If you find any mistakes or typos please let me know! Also I’m always looking for exciting projects in the blockchain space.

如果您发现任何错误或错别字,请告诉我! 另外,我一直在寻找在区块链领域令人兴奋的项目。

If you found this helpful and feel like buying me a beer:

如果您觉得这有帮助,并且想给我买啤酒:

BTC: 3Kxz6zPweuiaVG28W78pX9DoEZVkLhH4nT

BTC:3Kxz6zPweuiaVG28W78pX9DoEZVkLhH4nT

BCH: qqwusc2peyvlh3wgl0tpt3ll4ug9zujfvy9586tgd4

BCH:qqwusc2peyvlh3wgl0tpt3ll4ug9zujfvy9586tgd4

ETH: 0x96Ee87e22D899BDc27EAD4fE3FCA8e9F39176B4C

ETH:0x96Ee87e22D899BDc27EAD4fE3FCA8e9F39176B4C

LTC: MDhqUBtGgVZrDG7TYzzyK2a2b99sHyHaQQ

LTC:MDhqUBtGgVZrDG7TYzzyK2a2b99sHyHaQQ

翻译自: https://www.freecodecamp.org/news/create-an-ethereum-token-using-open-source-contracts-open-zeppelin-1e132e6233ed/

zeppelin连接数据源

你可能感兴趣的:(python,java,区块链,linux,github)