I am new to Solidity and I've come up with this issue.
I want to develop a contract called senderContract {}
that can receive ether from any address and then automatically transfer these funds to another contract called receiverContract {}
. In this second contract, you will find two functions, the first one (manualTransfer()
) is working correctly and allows to introduce manually the contract contract address to send the ether. But the second one (autoTransfer()
) is not working.
I've discovered that the constructor()
in the first contract changes the value of the variable recipient
once the second contract es deployed. How is it possible that a variable in a constructor changes from its initialized value? Is the contructor not supposed to be executed just once?
Is there any reason for this action not being possible or am I just not writing the correct code? I leave the code here:
pragma solidity >= 0.7.0 < 0.9.0;
contract receiverContract {
event Log(uint);
address public recipient;
constructor () {
recipient = address(this);
}
fallback () external payable {
emit Log(gasleft());
}
receive () external payable {}
function getThisAddress() public view returns(address) {
return address(this);
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract senderContract is receiverContract {
function manualTransfer (address payable _to) public payable {
_to.transfer(msg.value);
}
function autoTransfer () public payable {
payable(recipient).transfer(msg.value);
}
}
Thank you in advance!