【问题标题】:Sending eth from contract to wallet address将 eth 从合约发送到钱包地址
【发布时间】:2022-01-02 03:34:36
【问题描述】:

我部署了这个合约,旨在将以太币从自己发送到另一个帐户


pragma solidity ^0.8.0;

contract Contract {

    address public owner;
    address public admin;

    constructor(address _admin) public {
        owner = msg.sender;
        admin = _admin;
    }

    modifier onlyOwner(address sender) {
        require(
            sender == admin,
            "Only the admin of the contract can perform this operation."
        );_;
    }

    function sendTo (
        address toAddress
    ) public payable onlyOwner(msg.sender) {
        payable(toAddress).transfer(msg.value);
    }
}

我尝试在客户端与它进行交互:

var contract = new web3.eth.Contract(abi, Address, null);

const transaction = {
            from: mainAcct,
            to: dummyAcct,
            value: '10000000000000000',
            gas: 30000
};

await contract.methods.sendTo(dummyAcct).send(
            transaction , function(err, hash){
            if(!err){
                console.log("Transaction hash:", hash);
            }else{
                console.log(err);
            }
});
} catch (e) {
                console.log(e);
}

为什么我会在控制台中收到此错误:

Error: Transaction has been reverted by the EVM

任何帮助将不胜感激!

K

【问题讨论】:

  • 您是从mainAcct 地址还是从其他地址部署合约?
  • 是的,来自 mainAcct
  • 好的,一个后续问题,因为我一开始误读了你的代码逻辑。在部署过程中,您向构造函数传递了什么值? mainAcct 也是?
  • 是的,两者都是 mainAcct!

标签: solidity web3 web3js


【解决方案1】:

看来您需要先为合同注资,然后才能发出金额 我做了类似下面的事情 hre 是安全帽对象,但是在部署时会有办法添加资金

const waveContractFactory = await hre.ethers.getContractFactory('contract_name');
  const waveContract = await waveContractFactory.deploy({
    value: hre.ethers.utils.parseEther('0.1'),
  });

【讨论】:

    【解决方案2】:

    问题可能是转帐,转帐限制了gas,这可能是错误的原因,请尝试使用调用而不是转帐,查看此网页以了解如何使用它https://solidity-by-example.org/sending-ether/

    【讨论】:

    • 就是这样,我得增加gas server端
    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 2023-03-12
    • 2021-11-14
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    相关资源
    最近更新 更多