您可以使用仅MetAmask调用坚固的合同方法吗?
我想在我的应用程序中使用metAmask来让用户支付固定的ETH费用(加油)来调用我的坚固合同中的方法。我查看了metAmask文档,eth_sendtransaction
方法似乎与我需要的东西很近。 eth_sendtransaction
肯定会允许我向用户请求ETH,但是“数据”参数有些混乱。
Metamask文档说:
数据是可选的,但用于定义智能合同创建和交互
,
也用于指定合同方法及其参数。
因此,“数据”代表我的方法及其参数,但是metAmask(或window.Ethereum
,而是)如何知道我要调用的方法的合同?
您通常不需要提供合同地址和ABI/JSON才能与已部署的合同互动吗?简而言之,是否可以独自完成我所描述的事情?还是您必须执行其他客户端设置才能使用eth_sendtransaction
调用方法?
编辑:顺便说一句,泰勒(Tylerh),涉及使用web3.js的答案。也许不要编辑人们的帖子,除非您知道自己在说什么。只是一个想法...
I’m wanting to use Metamask in my app to let users pay a fixed ETH fee (plus gas) to call a method from my Solidity contract. I looked at the Metamask documentation and the eth_sendTransaction
method seems close to what I need; eth_sendTransaction
would certainly allow me to request ETH from a user, but the “data” parameter is a bit confusing.
The Metamask docs say:
data is optional, but used for defining smart contract creation and interaction
and
also used for specifying contract methods and their parameters.
So “data” represents my method and its parameters, but how does Metamask (or window.ethereum
, rather) know the contract whose methods I’m trying to call?
Don’t you normally have to provide a contract address and ABI/JSON in order to interact with a deployed contract? In short, is it possible to do what I’ve described with just Metamask alone? Or do you have to do other client-side setups in order to call a method with eth_sendTransaction
?
Edit: by the way TylerH, the answer involved using web3.js. Maybe don't edit people's posts unless you know what the hell you're talking about. Just a thought...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您将需要合同ABI,以获取您需要包含在合同的数据中的信息。您还需要完成其他一些事情:
首先,您需要确保下载
ethers.js
和@alch/alchemy-web3
npm库中的应用程序。其次,您需要从alchemy
之类的平台中提供一个提供商API键,以便与合同ABI通信。最后,您将需要合同ABI,可以在Etherscan合同部分的底部找到。有很多有关如何在线获取这些东西的信息,因此我不会在此处讨论如何配置它们。一旦有了这些,就可以准备下一步。
我建议在应用程序文件系统中某个地方的公用事业文件中创建此功能,但是这个想法是:
因此,在交易的数据字段中填充的值来自调用我们试图在合同中调用的方法。这是对此进行编码的,然后我们将此信息与其他交易数据一起传递。
这是一个非常简短的描述,说明这是什么,我希望这就是您想要的。如果您需要更多帮助,我总是可以在Twitter @_syndk8上聊天。
Yes you will need the contract abi in order to get the information you need to include in the data that you're passing to the contract. There are also a few other things that you will need to accomplish this:
First you will need to make sure you download the
ethers.js
, and@alch/alchemy-web3
npm libraries into your application. Secondly you will need a provider API key from a platform likeAlchemy
in order to communicate with the contract abi. Lastly, you will need the contract abi which can be found at the bottom of the contract section of etherscan. There is plenty of information on how to obtain these things online, so I won't go over how to configure them here.Once you have these, you are ready for the next step.
I suggest creating this in a utilities file somewhere in your applications file system, but the idea is this:
So the value that is populated in the data field of our transaction comes from calling the method that we are trying to invoke in our contract. This is encoded, and then we pass this information along with the rest of our transaction data.
This is a very short and brief description as to what this does, and I hope this is what you're looking for. If you need any more help I'm always available to chat on Twitter @_syndk8.