簡體   English   中英

如何在solidity中使用傳遞函數

[英]How to use transfer function in solidity

我創建了智能合約將 ETH 從一個賬戶轉移到另一個賬戶,發生 ETH 扣除但發送到另一個地址(未指定)。 請幫我解決這個問題。

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.12;

contract ETH{
    address public  buyer;
    uint public amt;
    address public seller;
    constructor(){
        seller = msg.sender;
    }
    function add(address payable _buyer) payable public{
        buyer = _buyer;
        payable(buyer).transfer(amt);
    }
    function bal() view public returns(uint){
        return buyer.balance;
    }
    function s() payable public returns(uint){
        return msg.value;
    }
}

amt的默認值為 0,並且您的代碼不會在任何地方設置此屬性(設置為非零值)。

這有效地使您的.transfer(amt)函數向buyer發送 0 ETH,並將您與交易一起發送的所有 ETH 保留在合同中。

如果要重定向發送的金額,則有msg.value全局變量,它反映了與交易一起發送的當前值。

payable(buyer).transfer(msg.value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM