0

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:

contractObj.deposit.sendTransaction(options)

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

Zulfi.

2 Answers 2

1

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

I would like to add to it by saying that if "myContract.sendTransaction" is replaced with "web3.eth.sendTransaction", the eth is not transferred to myContract unless myContract.address is in the "to" property; although the funds are always withdrawn from accounts[1].

web3.eth.sendTransaction can be run from within the same truffle console environment, and it's results easily followed in ganache.

1
  • Can you format the code to improve its readability?
    – pbsh
    Commented Feb 7, 2022 at 9:44
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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