我如何将ETH从帐户钱包转移到智能合约

发布于 2025-01-21 14:47:03 字数 735 浏览 0 评论 0原文

我正在创建一份智能合约,使人们可以为

我堆叠的每月订阅付费:

如何将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 技术交流群。

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

发布评论

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

评论(1

北城孤痞 2025-01-28 14:47:03

subscribe()功能需要使用付款修改器才能接受ETH。然后,您可以验证用户在使用msg.value全局变量调用您的功能时发送了多少。

不可能从合同中请求特定金额,因为用户发送交易后 执行合同代码。您始终需要验证用户发送的数量以及调用该功能的事务。

function subscribe(uint planId) external payable {
    // revert if the sent value is not expected
    require(msg.value == 1 ether, "You need to send 1 ETH");
}

但是,在创建对其元掩体或其他钱包的交易请求时,您可以控制UI上的预定义值。

await window.ethereum.request(
    method: 'eth_sendTransaction',
    [
        from: userAddress,
        to: yourContract,
        data: <invoking the subscribe() function>,
        value: <1 ETH in wei, in hex>
    ]
);

文档: https:///docs.metamask.io/guide/以太坊provider.html#以ethereum-request-args

The subscribe() function needs to use the payable modifier in order to accept ETH. Then you can validate how much the user has sent while invoking your function with the msg.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.

function subscribe(uint planId) external payable {
    // revert if the sent value is not expected
    require(msg.value == 1 ether, "You need to send 1 ETH");
}

However you can control the predefined value on the UI, while creating the transaction request to their MetaMask or other wallet.

await window.ethereum.request(
    method: 'eth_sendTransaction',
    [
        from: userAddress,
        to: yourContract,
        data: <invoking the subscribe() function>,
        value: <1 ETH in wei, in hex>
    ]
);

Docs: https://docs.metamask.io/guide/ethereum-provider.html#ethereum-request-args

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