I have the following contract:

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

If I wanted to send Ether to the above contract using deposit function, I used to wrote:

acc1 = accounts[1]

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

and then:


Now suppose that I have a fallback function instead of deposit function, how can I send Ether to the contract? Somebody please guide me.



From the Truffle docs:

You can trigger the fallback function by sending a transaction to this function:

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

This is promisified like all available contract instance functions, and has the same API as web3.eth.sendTransaction without the callback. The to value will be automatically filled in for you.

First, add the fallback function to your contract:

function () external payable {}

Then in your truffle console run the following commands:

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

You will see something like this:

  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: []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.