将 eth 从一个 metamask 钱包稳定地发送到另一个钱包

Sending eth from one metamask wallet to another wallet in solidity

我正在尝试编写一份智能合约,以在与 Injected Web 3 环境混合的情况下,将 eth 从一个 metamask 钱包稳定地转移到另一个 metamask 钱包。我部署了合约,但交易 send_ETH 失败了。谁能帮我弄清楚为什么?非常感谢

pragma solidity >=0.8.0;

// Get the latest ETH/USD price from chainlink price feed
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract myContract {
    address payable[] recipients;
    mapping(address => uint256) public balanceAccount;
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    function send_ETH(address payable recipient, uint256 amount) public {
        //set the minimum amount of dollar to transfer
        uint256 minimumUSD = 0.01 * 10 ** 18;
        amount = minimumUSD;
        require(getConversionRate(amount) >= minimumUSD, "You need to spend more ETH!");
        
        this.invest(amount);
        this.fund(recipient);
    }

    function invest(uint256 amount) payable external{
        //transfer amount ETH from metadata wallet to smart contract

        recordTransaction(address(this), amount, false);
        recordTransaction(owner, amount, true);
        address payable contractAddress = payable(address(this));
        contractAddress.send(amount);
    }

    function fund(address payable recipient) external {
        //transfer amount ETH from this smart contract to the recipient

        recordTransaction(address(this), address(this).balance, true);
        recordTransaction(recipient, address(this).balance, false);
        recipient.send(address(this).balance);
    }

    function recordTransaction(address recipient, uint256 deposit, bool out) private {
        if (out) {
            balanceAccount[recipient] -= deposit;
        } else {
            balanceAccount[recipient] += deposit;
        }
    }


    function getVersion() public view returns (uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
        return priceFeed.version();
    }

    function getPrice() public view returns(uint256){
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
        (,int256 answer,,,) = priceFeed.latestRoundData();
         // ETH/USD rate in 18 digit 
         return uint256(answer * 10000000000);
    }

    function getConversionRate(uint256 ethAmount) public view returns (uint256){
        uint256 ethPrice = getPrice();
        uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
        // the actual ETH/USD conversation rate, after adjusting the extra 0s.
        return ethAmountInUsd;
    }
}

你的函数invest()是错误的

   function invest(uint256 amount) payable external{
        //transfer amount ETH from metadata wallet to smart contract

        recordTransaction(address(this), amount, false);
        recordTransaction(owner, amount, true);

         //these 2 lines below are wrong
        address payable contractAddress = payable(address(this));
        contractAddress.send(amount);
    }

您将该功能标记为可支付,如果您直接与之交互是正确的,但您与 send_eth 交互,因此需要支付才能接收 eth

然后你将合约地址保存为应付地址并做了 contractAddress.send(amount) 这意味着:“合约发送 'amount' 到合约”

send() 函数将钱从合约发送到地址

amount var 也是错误的

所有发送的eth都是直接通过tx信息发送,不通过数据发送

函数正确:

function send_ETH(address payable recipient) payable public {
    //set the minimum amount of dollar to transfer
    uint256 minimumUSD = 0.01 * 1e18;
    require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more ETH!");
    
    this.invest(msg.value);
    this.fund(recipient);
}

   function invest() internal{
        //transfer amount ETH from metadata wallet to smart contract

        recordTransaction(address(this), amount, false);
        recordTransaction(owner, amount, true);
    }

然后在调用 send_eth 时,您必须手动写入要发送的 eth 数量,如果您为此创建一个接口,您可以在 tx 详细信息中写入要发送的 eth 数量,例如:

const transaction = {
 'to': '0x31B98D14007bDEe637298086988A0bBd31184523', //contract address 
 'value': 1, //1 eth
 'gas': 30000,
 'maxFeePerGas': 1000000108,
 'nonce': nonce,
};