我如何将ETH从帐户钱包转移到智能合约
我正在创建一份智能合约,使人们可以为
我堆叠的每月订阅付费:
如何将Plan.mount从用户钱包转移到智能合约?
function subscribe(uint planId) external {
Plan storage plan = plans[planId];
require(plan.merchant != address(0), 'address not valid');
bool sent = payable(address(this)).send(plan.amount);
require(sent, "tx failed");
emit PaymentSent(
msg.sender,
plan.merchant,
plan.amount, // the monthly amount for Subscription
planId,
block.timestamp
);
subscriptions[msg.sender][planId] = Subscription(
msg.sender,
block.timestamp,
block.timestamp + 4 weeks // next payement
);
emit SubscriptionCreated(msg.sender, planId, block.timestamp);
}
I'm creating a smart contract that allows people to pay for a monthly subscription
I got stacked in this :
how to transfer plan.amount from the user wallet to the smart contract?
function subscribe(uint planId) external {
Plan storage plan = plans[planId];
require(plan.merchant != address(0), 'address not valid');
bool sent = payable(address(this)).send(plan.amount);
require(sent, "tx failed");
emit PaymentSent(
msg.sender,
plan.merchant,
plan.amount, // the monthly amount for Subscription
planId,
block.timestamp
);
subscriptions[msg.sender][planId] = Subscription(
msg.sender,
block.timestamp,
block.timestamp + 4 weeks // next payement
);
emit SubscriptionCreated(msg.sender, planId, block.timestamp);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
subscribe()
功能需要使用付款
修改器才能接受ETH。然后,您可以验证用户在使用msg.value
全局变量调用您的功能时发送了多少。不可能从合同中请求特定金额,因为用户发送交易后 执行合同代码。您始终需要验证用户发送的数量以及调用该功能的事务。
但是,在创建对其元掩体或其他钱包的交易请求时,您可以控制UI上的预定义值。
文档: https:///docs.metamask.io/guide/以太坊provider.html#以ethereum-request-args
The
subscribe()
function needs to use thepayable
modifier in order to accept ETH. Then you can validate how much the user has sent while invoking your function with themsg.value
global variable.It's not possible to request a specific amount from the contract, as the contract code is executed after the user has sent the transaction. You always need to validate how much has the user sent along with the transaction invoking the function.
However you can control the predefined value on the UI, while creating the transaction request to their MetaMask or other wallet.
Docs: https://docs.metamask.io/guide/ethereum-provider.html#ethereum-request-args