当用户将某些以太发送到智能合约/DAPP地址时,我是否可以在智能合约中访问该交易?就像被调用的内置方法一样,我可以添加一些代码?
如果可能的话,我理想的工作流程:
- 用户将以太发送到我的DAPP地址。
- 我的智能合同代码中调用了功能。
- 在该功能中,我可以创建一些逻辑,例如将其地址添加到某个状态等。
理想情况下,我想避免用户必须调用合同的特定公共功能,而只是将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:
- User sends Ether to my dApp address.
- A function is invoked in my smart contract code.
- 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?
发布评论
评论(1)
有 Data 事务字段是空的)。
您可以通过
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 throughmsg.value
.