Ether

Truffle 控制台:使用備份功能將 Ether 發送到合約

  • February 6, 2022

我有以下契約:

pragma solidity ^0.5.8;
contract  Victim {
  address owner;
  constructor() public{  
     owner = msg.sender;
  }
 
  function deposit()  payable public {}
}

如果我想使用存款功能將乙太幣發送到上述合約,我曾經寫過:

acc1 = accounts[1]

options = { from: acc1, to : victim.address, value: web3.utils.toWei('99', 'ether')}

進而:

contractObj.deposit.sendTransaction(options)

現在假設我有一個回退函式而不是存款函式,我怎樣才能將乙太幣發送到合約?有人請指導我。

祖爾菲。

松露文件

您可以通過向此函式發送事務來觸發回退函式:

const result = instance.sendTransaction({...});

web3.eth.sendTransaction這與所有可用的合約實例函式一樣被承諾,並且具有與沒有回調相同的 API 。該to值將自動為您填寫。

首先,將回退功能添加到您的合約中:

function () external payable {}

然後在您的 truffle 控制台中執行以下命令:

truffle(development)> const myContract = await Victim.deployed()
truffle(development)> myContract.sendTransaction({from: accounts[1], value: web3.utils.toWei("1", 'ether')})

你會看到這樣的東西:

{
 tx: '0x22b2161c3d2df23fa3665e17818f18e0ec1aa85e8651b58173eb98c5129422e3',
 receipt: {
   transactionHash: '0x22b2161c3d2df23fa3665e17818f18e0ec1aa85e8651b58173eb98c5129422e3',
   transactionIndex: 0,
   blockHash: '0xfad483d89d468baf3f11169b0c7f2ac8d98eb6c0e9ce8d446447997e90fc0813',
   blockNumber: 5,
   from: '0xed3e91355793f627432d8033df7d9f410a00d77e',
   to: '0x7f17856af4f34e7de93683642fc6e2d4bff44f3a',
   gasUsed: 21040,
   cumulativeGasUsed: 21040,
   contractAddress: null,
   logs: [],
   status: true,
   logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
   rawLogs: []
 },
 logs: []
}

引用自:https://ethereum.stackexchange.com/questions/94446