I am testing following contract on Truffle console:
MyContract:
contract MyContract {
address owner;
constructor() public {
owner = msg.sender;
}
function sendTo(address payable receiver, uint amount) public {
receiver.transfer(amount);
}
function() external payable{
}
}
==
MyContractTester:
pragma solidity ^0.5.8;
import './MyContract.sol';
contract MyContractTester {
MyContract public myContract;
address payable attacker;
constructor(address payable mycontract) public {
myContract = MyContract(mycontract);
attacker = msg.sender;
}
function attack(address payable to, uint a) public {
myContract.sendTo(to, a);
}
function() external payable {
}
}
I want to invoke the attack(…) method using truffle console. The first argument is the address of the MyContractTester and the second represents the balance of MyContract.I want to specify the entire balance.
I am doing this on Truffle console:
truffle(development)> const V = await MyContract.new()
truffle(development)> const A = await MyContractTester.new(V.address)
truffle(development)> acc2 = accounts[2]
web3.eth.sendTransaction({to:V.address, from:acc2, value: web3.utils.toWei('11')})
Vbal = await web3.eth.getBalance(V.address)
await A.attack(A.address, web3.utils.toWei(Vbal.toString(),"ether"),{from:accounts[0]}),
but it is generating an exception due to the last statement. However, if I am hard-coding the data like this in my last statement, it works:
await A.attack(A.address, web3.utils.toWei('11',"ether"),{from:accounts[0]})
Somebody please guide me how to write the last statement to specify the entire balanceof MyContract.
Zulfi.