区块链应用开发人员
by Igor Yalovoy
由Igor Yalovoy
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交互的十种最常见的实践。
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 。
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。
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。
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实例获取帐户。 请勿保留并重复使用它,因为用户可以随时更改其帐户。
Here we use the function getAccount
from #4 and call getBalance
. Easy.
在这里,我们使用#4中的getAccount
函数,并调用getBalance
。 简单。
A user may change their account at any time. You dApp should be ready for that and react properly.
用户可以随时更改其帐户。 您的dApp应该为此做好了准备,并做出正确的React。
Similar to #6. A user may lock/unlock anytime. Your dApp should handle it correctly.
类似于#6。 用户可以随时锁定/解锁。 您的dApp应该正确处理它。
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
。
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交易被开采,交易收据就变得可用。 但是没有事件/通知,因此我们必须实现轮询机制。
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
最为感兴趣。
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,获取帐户状态和余额,识别当前网络以及处理交易和事件。 我们已经讨论了如何使用十个代码段来完成它。
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代替。
Connect with me on LinkedIn.
在LinkedIn上与我联系。
Follow me on twitter.
在Twitter上关注我。
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/
区块链应用开发人员