区块链应用开发人员

by Igor Yalovoy

由Igor Yalovoy

每个区块链开发人员都应该了解这些Web3和Metamask用例 (Every blockchain developer should know these Web3 and Metamask use cases)

更新资料 (Update)

On November 2nd MetaMask and other dapp browsers will stop exposing user accounts by default. This will make some code from this paper to break. I will publish updated version with web3 1.0 and new MetaMask interface.

在11月2日,默认情况下,MetaMask和其他dapp浏览器将停止公开用户帐户。 这将使本文的一些代码破裂。 我将发布Web3 1.0和新的MetaMask界面的更新版本。

Metamask is the de facto standard for dApps on the web. It injects a Web3 instance into a window object making it available for JavaScript code.

Metamask是Web上dApp的事实上的标准。 它将Web3实例注入到窗口对象中,使其可用于JavaScript代码。

We are going to use Web3 0.20 version, not Web3 1.0. The code for Web3 1.0 would be different.

我们将使用Web3 0.20版本,而不是Web3 1.0。 Web3 1.0的代码将有所不同。

Every dApp has its mission, but the way they interact with Metamask is similar. In this article, we’ll cover the ten most common practices to handle Web3/Metamask interactions.

每个dApp都有其使命,但是它们与Metamask交互的方式是相似的。 在本文中,我们将介绍处理Web3 / Metamask交互的十种最常见的实践。

1.检测Metamask并实例化Web3 (1. Detect Metamask and instantiate Web3)

According to docs, here’s the best way to do it.

根据docs ,这是最好的方法。

What is going on here? First, we check if Web3 was injected. If it is injected we create a new instance using the injected provider. Why is that? Because we want to use our library version, not the one injected by Metamask.

这里发生了什么? 首先,我们检查是否已注入Web3。 如果被注入,我们将使用注入的提供程序创建一个新实例。 这是为什么? 因为我们要使用我们的库版本,而不是Metamask注入的库版本。

If Web3 is not present, we try to connect to a localhost provider, like ganache.

如果Web3不存在,我们尝试连接到localhost提供程序,例如ganache 。

2.检查元掩码是否已锁定 (2. Check if Metamask is locked)

Metamask can be installed but locked. In order to interact with a user account and send transactions, the user has to unlock Metamask.

可以安装但可以屏蔽Metamask。 为了与用户帐户进行交互并发送交易,用户必须解锁Metamask。

3.检查当前网络 (3. Check the current network)

There are many test networks beyond the main network. Typically your contract is deployed to a certain network. You want to be sure that the user runs Metamask on the same network.

除了主网络之外,还有许多测试网络。 通常,您的合同已部署到某个网络。 您要确保用户在同一网络上运行Metamask。

4.获取当前帐户 (4. Get the current account)

A user may have multiple accounts at Metamask, but they expect the dApp to interact with the current one.

一个用户可能在Metamask上有多个帐户,但是他们希望dApp与当前帐户交互。

You should always grab the account from the Web3 instance. Do not keep and reuse it, because the user may change their account at any time.

您应该始终从Web3实例获取帐户。 请勿保留并重复使用它,因为用户可以随时更改其帐户。

5.获取当前帐户上的余额 (5. Get the balance on the current account)

Here we use the function getAccount from #4 and call getBalance. Easy.

在这里,我们使用#4中的getAccount函数,并调用getBalance 。 简单。

6.检测当前帐户已更改 (6. Detect that the current account has changed)

A user may change their account at any time. You dApp should be ready for that and react properly.

用户可以随时更改其帐户。 您的dApp应该为此做好了准备,并做出正确的React。

7.检测元掩码是否被锁定/解锁 (7. Detect whether Metamask is locked/unlocked)

Similar to #6. A user may lock/unlock anytime. Your dApp should handle it correctly.

类似于#6。 用户可以随时锁定/解锁。 您的dApp应该正确处理它。

8.处理取消/确认 (8. Handle cancel/confirm)

Once a user interacts with your dApp, you have to send a transaction using the Web3 API. A user may press the cancel or confirm button on the Metamask popup. This may lead to UI inconsistency if not handled correctly.

用户与您的dApp互动后,您必须使用Web3 API发送交易。 用户可以按“ Metamask”弹出窗口上的“取消”或“确认”按钮。 如果处理不当,可能会导致UI不一致。

In order to return instantly with the transaction hash, call contract.methodName.sendTransaction.

为了立即使用交易哈希返回,请调用contract.methodName.sendTransaction

9.获取交易收据 (9. Get the transaction receipt)

Once your dApp transaction is mined, a transaction receipt becomes available. Yet there is no event/notification, so we have to implement a poll mechanism.

一旦您的dApp交易被开采,交易收据就变得可用。 但是没有事件/通知,因此我们必须实现轮询机制。

10.收听Web3事件 (10. Listen for Web3 events)

Solidity events are great. They allow switching from ugly polling to just a push mechanism, assuming your contract implements all necessary events. You can completely avoid polling and just react to events. Event callback returns a lot of data, but we are mostly interested in args.

团结事件很棒。 假设您的合同实施了所有必要的事件,它们允许从丑陋的轮询切换为仅推送机制。 您可以完全避免轮询,而只对事件做出React。 事件回调返回了大量数据,但我们对args最为感兴趣。

摘要 (Summary)

Whatever your dApp is about, it still has to perform common tasks, such as detecting Web3, getting the account state and balance, recognizing the current network, and handling transactions and events. We’ve gone over how it can be done using ten code snippets.

无论您的dApp涉及什么,它都必须执行常见任务,例如检测Web3,获取帐户状态和余额,识别当前网络以及处理交易和事件。 我们已经讨论了如何使用十个代码段来完成它。

聚苯乙烯 (P.S.)

A lot of examples here use methods which might throw an error because of Metamask’s state or some variables being undefined at the moment of a call. You should wrap them in try/catch in a production environment. Async/await has been used here for simplicity. It can be replaced with Promise then/catch.

这里的许多示例使用的方法可能会由于Metamask的状态或在调用时未定义某些变量而引发错误。 您应该在生产环境中将它们包装在try/catch中。 为了简单起见,此处使用了异步/等待。 可以用Promise then / catch代替。

社会的 (Social)

  • Connect with me on LinkedIn.

    在LinkedIn上与我联系。

  • Follow me on twitter.

    在Twitter上关注我。

想要更多? (Want More?)

How to Create and Deploy Your Own EOS TokenWe are going to figure out what is EOS token and how you can create and deploy one yourself.hackernoon.comHow Much Does It Cost to Run DApp in 2018You think your AWS or Digital Ocean bill for your website is killing you?hackernoon.comDifference Between Ethereum and EOS TokensEthereum has ERC-20 token and EOS has EOSIO.Token. They serve the same purpose, but are they the same?medium.com

如何创建和部署自己的EOS令牌 我们将弄清楚什么是EOS令牌以及如何创建和部署自己的EOS令牌。 hackernoon.com 在2018年运行DApp 需花费 多少钱 您认为您的AWS或Digital Ocean网站账单正在杀死您? hackernoon.com以太 坊和EOS令牌之间的区别以太 坊具有ERC-20令牌,而EOS具有EOSIO.Token。 它们具有相同的目的,但是是否相同? medium.com

Originally published at ylv.io on October 15, 2018.

最初于ylv.io于2018年10月15日发布。

翻译自: https://www.freecodecamp.org/news/every-blockchain-developer-should-know-these-web3-and-metamask-use-cases-7f93c1f139b1/

区块链应用开发人员

区块链应用开发人员_每个区块链开发人员都应该了解这些Web3和Metamask用例相关推荐

  1. 重庆找Java开发工作_重庆【Java开发程序员】

    重庆[Java开发程序员],提倡一切为了学员就业的办学思想,教学过程中坚持以练习企业项目为主,让学员真正能学到技术,毕业就能适应工作岗位. 重庆[Java开发程序员], Java 编程开发.而且很多软 ...

  2. python 物联网开发板_物联网学什么开发板好?

    可以试试我们的三明治开发板呀!比较容易上手,你可以使用它轻松创建一款通过智能音箱语音或者手机App控制的智能硬件产品,喜欢玩DIY的知友可以体验下. 配合我们的IoT开发平台,即可快速完成智能产品de ...

  3. 生产环境和开发环境_环境部署:开发、测试和线上环境的区别

    点击蓝字 关注我们 软件开发环境(Software Development Environment,SDE)是指在基本硬件和宿主软件的基础上,为支持系统软件和应用软件的工程化开发和维护而使用的一组软件 ...

  4. 安卓app开发方案_「安卓APP开发流程」安卓APP如何开发的?

    21世纪,智能手机走进了人们的生活,现在的智能手机的操作系统基本分为两种,一种是IOS系统(苹果系统).安卓系统,其中,安卓系统是开源的,所以很多品牌商会讲安卓包装成自己的系统,但核心还是一样的,都是 ...

  5. python前端开发招聘_【天津前端开发招聘_最新天津前端开发招聘信息】-前程无忧...

    天津卓众信息技术有限公司天津-西青区0.6-1万/月11-23 学历要求:本科|工作经验:3-4年|公司性质:民营公司|公司规模:少于50人 1.根据产品设计实现产品的页面交互和数据逻辑展示,负责前端 ...

  6. python游戏开发引擎_你的游戏开发第0课

    电子游戏是许多人喜爱甚至沉迷的事情.尤其对于程序员来说,开发游戏是不少人最初学习编程的动力.在之前,我发过一些游戏开发的教程和案例: 爆款游戏<贪吃蛇大作战>的 Python 实现 如何用 ...

  7. 哈尔滨java开发工资_给哈尔滨Java开发初学者的几个学习建议

    对于初学者应该如何学习Java开发技术,纵观中国目前整体行业来说,互联网IT行业成为了拔尖的行业,IT互联网程序开发成了靠自己能力可以多挣一点钱,所以各个行业都在转行,其实互联网不存在饱和,只不过大多 ...

  8. 区块链分叉如何解决_聊聊区块链背后存在的问题(一):分叉

    近年来,比特币和区块链逐渐成为新风口,受到创业者.资本家和市场的青睐.一方面,比特币的价格不断高涨,一度突破2万美元,另一方面区块链融资成爆发式增长,根据猎豹全球智库区块链研究中心发现,2017年,全 ...

  9. react项目开发步骤_成为专业React开发人员的31个步骤

    react项目开发步骤 我为达到可雇用水平而进行的每个项目和课程. (Every single project and course I took to reach a hireable level. ...

最新文章

  1. 对称加密算法DES,3重DES,TDEA,Blowfish,RC5,IDEA,AES。
  2. 阿里云安装LNMP以及更改网站文件和MySQL数据目录
  3. 深度卷积神经网络CNNs的多GPU并行框架 及其在图像识别的应用
  4. IdentityServer4 配置负载均衡
  5. 100个MySQL 的调节和优化的提示
  6. WebBrowser页面与WinForm交互技巧(转)
  7. 在bitbucket.org上创建Git仓库
  8. CentOS6.5 firefox安装flash插件
  9. Linux安装Diamond软件,1.1 Linux下安装diamond
  10. 阿里图标库——批量下载图标
  11. 林淮川孙玄:分布式锁选型背后的架构设计思维【附源码】
  12. Unity基础到入门-导航系统(Navigation)
  13. Android 视频播放 界面变形处理
  14. java网络编程1-查询Internet地址
  15. php学好要多久,零基础php自学要多久
  16. linux下磁盘检查修复命令e2fsck
  17. 有趣又漂亮的可视化图表制作
  18. ARM的memory Compiler总结
  19. 马斯克究竟从特斯拉赚了多少钱?道翰天琼认知智能机器人平台API接口大脑为您揭秘-1。
  20. Flowable(二):数据库详情

热门文章

  1. Git—代码管理、提交及冲突解决流程的思考
  2. 爬虫-06-通用爬虫与聚焦爬虫
  3. python-操作数据库的练习
  4. 关于我使用的angular.js的上传---FileUploader
  5. SQL --几张表公有字段的查询
  6. Hadoop单机模式安装入门(Ubuntu系统)
  7. 【原创】CLEVO P157SM外接鼠标键盘失灵解决:更换硅脂(附带最新跑分数据)
  8. partial is not defined的解决办法
  9. (转)实现自己的http server
  10. Enterprise Vault 2007 Series [PST Migration]