博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
区块链应用开发人员_每个区块链开发人员都应该了解这些Web3和Metamask用例
阅读量:2520 次
发布时间:2019-05-11

本文共 5124 字,大约阅读时间需要 17 分钟。

区块链应用开发人员

by Igor Yalovoy

由Igor Yalovoy

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

更新资料 (Update)

On 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.

在日,默认情况下,MetaMask和其他dapp浏览器将停止公开用户帐户。 这将使本文的一些代码破裂。 我将发布Web3 1.0和新的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.

是Web上的事实上的标准。 它将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 , here’s the best way to do it.

根据 ,这是最好的方法。

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 .

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

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 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 .

    在上与我联系。

  • Follow me on .

    在上关注我。

想要更多? (Want More?)

Originally published at on October 15, 2018.

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

翻译自:

区块链应用开发人员

转载地址:http://ocewd.baihongyu.com/

你可能感兴趣的文章
[经典算法] 排列组合-N元素集合的M元素子集
查看>>
Codeforces 279D The Minimum Number of Variables 状压dp
查看>>
打分排序系统漫谈2 - 点赞量?点赞率?! 置信区间!
查看>>
valgrind检测linux程序内存泄露
查看>>
MSP430(F149)学习笔记——红外接收
查看>>
cef3的各个接口你知道几个
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>