Web3 Contract.Methods.Approve(ContractAddress,value),如何提示用户使用测试帐户地址批准?

发布于 2025-02-02 20:12:04 字数 730 浏览 1 评论 0原文

我能够从开发环境中的客户端机器中调用智能合同方法:

示例:

contract.methods.allowance
contract.methods.approve

我可以看到Ganache UI中的交易从合同返回

。但是,我期望在浏览器中看到的是连接钱包中的提示,可以批准交易,类似于我打电话时得到的提示:

window.ethereum.request({ method: 'eth_requestAccounts' });

window.ethereum.request({ method: 'eth_sendTransaction', params});

我使用的帐户是我使用的帐户,我使用的是我导入的。私钥进入metamask。

我必须误解某些事情,因为如果没有“批准”的所有者,则“批准”功能在概念上没有任何意义,除了客户机器调用“批准”之外,地址的所有者进行了“批准”。这是否与开发人员已经知道私钥是什么事实有关吗?

将MetAmask帐户切换到开发网络之外的帐户(我的实际帐户)时,我会发现“发件人帐户未识别”错误。但是,为什么默认行为不要求未认可的帐户批准交易?

例如,我可以致电:window.Ethereum.Request({方法:'eth_sendtransaction',params});使用“未认可”帐户并批准交易,但我无法拨打SC呼叫并批准该帐户。

I am able to call Smart contract methods from client machine in development env:

example:

contract.methods.allowance
contract.methods.approve

and I can see the transactions in Ganache UI returned from contract.methods.approve

Which is fine. However, what I am expecting to see in the browser is a prompt in the connected wallet to approve the transaction, similar to the prompt I get when calling:

window.ethereum.request({ method: 'eth_requestAccounts' });

or

window.ethereum.request({ method: 'eth_sendTransaction', params});

The account I am using is one I used provided by Ganache UI where I imported the private key into metamask.

I must be misunderstanding something because there is no point in an "approve" feature conceptually if there is no "approving" taking place by the owner of the address apart from the client machine calling "approve". Does this have something to do with the fact the the dev env already knows what the private key is?

When switching metamask account to an account outside of the development network (my actual account), I get "sender account not recognized" error. But why isn't default behaviour to ask the unrecognised account to approve the transaction?

example, I can call: window.ethereum.request({ method: 'eth_sendTransaction', params}); with the "unrecognised" account and approve the transaction but I can't make a SC call and approve it with this account.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-02-09 20:12:04

要回答我自己的问题,答案是:
使用MetAmask“ Window.Etereum”作为提供商,允许其选择网络而不是直接注入RPC。

我还切换到ethersjs并停止使用Web3J。因此,以下内容:

const provider = new ethers.providers.Web3Provider(window.ethereum, "any");

而不是注入HTTP提供商,因为网络没有您的私钥。

To answer my own question the answer is:
Use metamask "window.ethereum" as a provider allowing it to select the network instead of injecting the RPC directly.

I also switched to ethersjs and stopped using web3js. So the following:

const provider = new ethers.providers.Web3Provider(window.ethereum, "any");

Instead of injecting an http provider since the the network doesn't have you private key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文