Solidity - 转账时不指定金额

发布于 2025-01-19 00:24:08 字数 1283 浏览 3 评论 0原文

我有一个产品列表,每种产品都有一个价格。

我想创建一个可以由用户调用的函数,并自动检查用户的余额是否有足够的资金来购买产品,它将收费从买方转移到合同的余额中,并将价格从买方转移到卖方。

有可能吗?

这个代码显然无法使用,因为必须以某种方式喂食智能合约。

    struct Product {
        uint id_prod;
        address owner_addr;
        uint curr_price;
        bool onSale;
    }

    struct Owner {
        uint id_owner;
        address payable addr;
    }

    // function to receive ethers into the contract
    receive() external payable {
    }

    // function to send ether
    function pay(address payable _to, uint amount) private {
        (bool sent, bytes memory data) = _to.call{value: amount}("");
        require(sent, "Failed to send Ether");
    }

    function buyProduct(uint _id_product) external payable productOnSale(_id_product) buyerHasEnoughMoney(_id_product) alreadyOwner(_id_product){

        Owner storage _owner = getOwnerStruct(_id_product);
        Product storage _product = ProductList[_id_product];
        
        // pay 1 finney to the contract
        pay(payable(address(this)), 10**15);
        pay(_owner.addr, _product.curr_price);


        _product.owner_addr = msg.sender;
    }

我认为解决方法是创建一个应付的功能,在购买者可以指定金额的情况下,以便智能合约收到钱,然后将价格转移给卖方。

我的问题是,是否存在一种方法来避免买方指定金额,并只是调用直接从其余额中获取金钱的功能,持有其余额的费用,并将价格金额转移给卖方。

I have a list of products, each product has a price.

I would like to create a function that can be called by a user and automatically checks whether the user's balance has enough money to buy the product, it transfers a fee from the buyer to the contract's balance and the price from the buyer to the seller.

Is that possible?

This code obviously cannot work, because the smart contract has to be fed, somehow.

    struct Product {
        uint id_prod;
        address owner_addr;
        uint curr_price;
        bool onSale;
    }

    struct Owner {
        uint id_owner;
        address payable addr;
    }

    // function to receive ethers into the contract
    receive() external payable {
    }

    // function to send ether
    function pay(address payable _to, uint amount) private {
        (bool sent, bytes memory data) = _to.call{value: amount}("");
        require(sent, "Failed to send Ether");
    }

    function buyProduct(uint _id_product) external payable productOnSale(_id_product) buyerHasEnoughMoney(_id_product) alreadyOwner(_id_product){

        Owner storage _owner = getOwnerStruct(_id_product);
        Product storage _product = ProductList[_id_product];
        
        // pay 1 finney to the contract
        pay(payable(address(this)), 10**15);
        pay(_owner.addr, _product.curr_price);


        _product.owner_addr = msg.sender;
    }

I think the workaround is to create a payable function where the buyer can specify the amount of money so that the smart contract receives the money, and subsequently, it transfers the price amount to the seller.

My question is if there exists a way to avoid the buyer specifying the amount and just calls a function that directly takes money from their balance, holds a fee to its own balance, and transfers the price amount to the seller.

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

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

发布评论

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

评论(1

岁月无声 2025-01-26 00:24:08

修改函数付款时,请在调用该函数时发送值。看起来您有一个电子商务网站,销售一些产品。因此,当您在Frontend上调用合同功能时:

await contract.methods.buyProduct(
    // id should be in hex format
    hexId,
  ).send({from: account, value:valueOfItem})

提交表格时调用此功能。它将弹出MetAmask,MetAmask将计算气体成本,如果您有足够的余额,则会提交请求,否则MetAmask将返回您的错误

When you modify the function payable you send value when you call the function. Looks like you have an ecommerce website, selling some products. So when you call the contract function on frontend:

await contract.methods.buyProduct(
    // id should be in hex format
    hexId,
  ).send({from: account, value:valueOfItem})

You call this function when you submit the form. it will pop up the metamask, metamask will calculate gas cost and if you have enough balance it will submit the request otherwise metamask will return you error

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