如何在以太坊智能合约DAPP中访问简单的以太交易事件?

发布于 2025-01-29 14:57:48 字数 280 浏览 4 评论 0 原文

当用户将某些以太发送到智能合约/DAPP地址时,我是否可以在智能合约中访问该交易?就像被调用的内置方法一样,我可以添加一些代码?

如果可能的话,我理想的工作流程:

  1. 用户将以太发送到我的DAPP地址。
  2. 我的智能合同代码中调用了功能。
  3. 在该功能中,我可以创建一些逻辑,例如将其地址添加到某个状态等。

理想情况下,我想避免用户必须调用合同的特定公共功能,而只是将Ether发送到DAPP地址。

如果用户必须调用特定功能,我必须开始考虑

When a user sends some Ether to a smart contract/dApp address, is there a way I can access that transaction in my smart contract? Like a built-in method that's invoked, that I can add some code to?

My ideal work flow, if it's possible:

  1. User sends Ether to my dApp address.
  2. A function is invoked in my smart contract code.
  3. Within that function, I can create some logic, like add their address to some state, etc.

Ideally, I want to avoid a user having to invoke a particular public function of the contract, but instead just send Ether to the dApp address.

If the user has to invoke a particular function, I have to start thinking about services like MEW, or I have to build a web2 frontend app that integrates with the MetaMask browser extension or something. This seems like a lot of work, but is this the way it has to be?

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

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

发布评论

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

评论(1

别理我 2025-02-05 14:57:48

Data 事务字段是空的)。

您可以通过 msg.sender 全局变量获得发件人地址,并通过 msg.value 获得金额。

pragma solidity ^0.8;

contract MyContract {
    mapping (address => uint256) contributions;

    receive() external payable {
        contributions[msg.sender] += msg.value;
    }
}

There is the receive() special function that gets executed when you send ETH to the contract address and not specify any function to execute (i.e. the data field of the transaction is empty).

You can get the sender address through the msg.sender global variable and the amount through msg.value.

pragma solidity ^0.8;

contract MyContract {
    mapping (address => uint256) contributions;

    receive() external payable {
        contributions[msg.sender] += msg.value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文