How to solve TypeError issue with the msg.sender in the constructor of a transaction service provider smart contract that doesn't allow for compiling - solidity

I am currently writing a smart contract and I am not able to compile due to an error on owner = msg.sender. I am also using ^0.8.0 . I would like to know what issue has to be resolved for the smart contract to be able to compile. I had tried adding the address function to the owner variable to make the msg.sender into an address payable. I still got the same error.
contract ServicePay{
address payable public owner;
address payable public buyer;
address payable public seller;
uint public amount;
bool public resolved;
uint public expiration;
uint public fee;
constructor(address payable _buyer, address payable _seller, uint _amount, uint _expiration) {
owner = msg.sender; //Where the error is
buyer = _buyer;
seller = _seller;
amount = _amount;
resolved = false;
expiration = _expiration;
fee = _amount * 2 / 100;
}
function release() public {
require(!resolved, "The payment has already been resolved.");
require(msg.sender == owner, "Only the owner can release the funds.");
seller.transfer(amount - fee);
owner.transfer(fee);
resolved = true;
}

Your owner property is of type address payable (extension of type address).
Since Solidity v0.8.0, msg.sender is only of type address (not the payable extension).
You can convert address to address payable:
// within the constructor
owner = payable(msg.sender);

Related

What is missing from this Smart Contract?

I am working on a smart contract, and my goal is that when a certain (variable) amount of eth gets send to the contract, it gets split and payed to three adresses. I currently have this code. Am I missing something? It doesn't work in Remix unfortunately. Thanks in advance for your valuable time!
`pragma solidity ^0.6.0;
contract PaymentSplitter {
address payable addressOne;
address payable addressTwo;
address payable addressThree;
uint256 amount;
constructor(address payable _addressOne = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2, address payable _addressTwo = 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db, address payable _addressThree = 0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB) public {
addressOne = _addressOne;
addressTwo = _addressTwo;
addressThree = _addressThree;
}
function splitPayment(uint256 _amount) public {
amount = _amount;
addressOne.transfer(_amount / 3);
addressTwo.transfer(_amount / 3);
addressThree.transfer(_amount / 3);
}
function getSplitAmount() public view returns (uint256) {
return amount;
}
// This will be invoked when the contract receives a payment
function() external payable {
splitPayment(msg.value);
}
}`
We tried various smart contract, but without any significant results
I modified your smart contract. I put some notes, to understand you some errors in your logic:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract PaymentSplitter {
address payable addressOne;
address payable addressTwo;
address payable addressThree;
uint256 amount;
constructor(address _addressOne, address _addressTwo, address _addressThree) public {
addressOne = payable(_addressOne);
addressTwo = payable(_addressTwo);
addressThree = payable(_addressThree);
}
// NOTE: Avoid passing parameter with value because with this statement 'address(this).balance' I receive the entire amount of Ether stored in smart contract
function splitPayment() public {
// NOTE: I retrieve entire amount stored in smart contract balance after send value to it
uint smartContractBalance = address(this).balance;
// NOTE: Since smart contract balance should be divided into three equal part the amount, I make this operation one time and put the value inside 'amount' variable. Then
// I transfer the equal amount to three accounts.
amount = smartContractBalance / 3;
addressOne.transfer(amount);
addressTwo.transfer(amount);
addressThree.transfer(amount);
}
function getSplitAmount() public view returns (uint256) {
return amount;
}
// This will be invoked when the contract receives a payment
receive() external payable {
splitPayment();
}
}
NOTE: Consider to use address.call{value}() for send amount from smart contract to accounts.
Is this a better contract? A few changes, and different amounts have to be sent to different addresses.
pragma solidity ^0.8.17;
contract Blockbook{
address payable public address1;
address payable public address2;
address payable public address3;
constructor(address payable _address1, address payable _address2, address payable _address3) public {
address1 = _address1;
address2 = _address2;
address3 = _address3;
}
function splitPayment(uint amount) public payable {
address1.transfer(amount * 0.8);
address2.transfer(amount * 0.1);
address3.transfer(amount * 0.1);
}
receive() external payable {
splitPayment(msg.value);
}
function updateAddresses(address payable _address1, address payable _address2, address payable _address3) public {
address1 = _address1;
address2 = _address2;
address3 = _address3;
}
}

Problem sending eth from contract to contract

pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT
contract Client {
address payable private hub;
address payable public owner;
uint256 public balance;
constructor(address payable _hub) {
hub = _hub;
owner = payable(msg.sender);
}
receive() payable external {
balance += msg.value;
}
function withdraw(address payable destAddr) public {
require(msg.sender == owner, "Only owner can withdraw funds");
uint amount = address(this).balance;
destAddr.transfer(amount);
}
function start() public payable {
require(msg.sender == owner, "Only owner can start the process");
uint amount = address(this).balance;
hub.transfer(amount);
balance = 0;
}
function setHub(address payable _new) public {
require(msg.sender == owner, "Only owner can change address");
hub = _new;
}
}
Hi i have a problem, when I deploy this contract and put as input (hub) the other contract, then send eth to this contract, i call the "start" function and throw a gas estimation error.
Someone who can help me pls...
I'm expecting that calling the start function fund will be sent to the other contract that also have a function for receiving eth
receive() payable external {
balance += msg.value;
}
You are getting that gas estimation error because your receive function on the second contract is not empty and the transaction does not have enough gas to execute the code. transfer only sends the amount of gas necessary to execute a transfer of ether (21,000 gas) and nothing more.
You can use call instead of transfer to send ether and set the amount of gas that you want to send. transfer is actually no longer recommended for sending ether.

Need some help resolving smart contract warnings

New solidity programmer here.
I'm trying to create a smart contract where a user can create a Bounty. The creator sets the bounty on the smart contract in the constructor. They can subsequently choose the recipient of the funds after evaluation of some criteria. They can cancel, increase the bounty.
I tested the code out and it appears to work, but I'm getting some warnings in remix IDE that I don't know how to fix.
Could some one show me how its supposed to be done?
contract Bounty {
address payable public owner;
address payable public provider;
uint256 private bounty;
bool isActive;
event IncreaseBounty (uint256 oldBounty, uint256 newBounty);
event Paid(address owner, address payee, uint256 amount);
event Cancel(address owner, uint256 amount);
constructor() payable {
owner = payable(msg.sender);
bounty = msg.value;
isActive = true;
}
function cancel() public {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner can cancel the bounty");
uint256 bountyTemp = bounty;
bounty = 0;
owner.transfer(bountyTemp);
isActive = false;
emit Cancel(msg.sender, bountyTemp);
}
function setAndTransferToProvider(address addy) public {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner release the funds");
provider = payable(addy);
provider.transfer(bounty);
uint256 bountyUsed = bounty;
bounty = 0;
isActive = false;
emit Paid(owner, provider, bountyUsed);
}
function increaseBounty() payable external returns (uint256) {
require(isActive, "contract must be active");
require(owner == msg.sender, "Only the owner can increase the bounty");
uint oldBounty = bounty;
bounty += uint(msg.value);
emit IncreaseBounty(oldBounty, bounty);
return bounty;
}
function getBounty() public view returns (uint256) {
require(isActive, "contract must be active");
return bounty;
}
}
Try to put this line
provider.transfer(bounty);
right before the emit of the events on every function.
You can check this article for more understanding of reentrancy attacks.

solidity - Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested

I am trying to give address as payable but I am getting error at msg.sender and address(_author).transfer(msg.value) . it was showing like Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested. i tried many ways to solve every time i replace the same error. before adding payable to author it was fine, but when added payable to author then it started getting error. In both, msg.sender and msg.value
pragma solidity >=0.4.0 <0.9.0;
contract SocialNetwork {
string public name;
uint public postCount = 0;
mapping(uint => Post) public posts;
struct Post {
uint id;
string content;
uint tipAmount;
address payable author;
}
event PostCreated(
uint id,
string content,
uint tipAmount,
address payable author
);
event PostTipped(
uint id,
string content,
uint tipAmount,
address payable author
);
constructor() public {
name = "Yash university Social Network";
}
function createPost(string memory _content) public {
//REquire Valid content
require(bytes(_content).length > 0);
// InCREMENT the post count
postCount ++;
// Create the post
posts[postCount] = Post(postCount, _content, 0, msg.sender);
// Trigger event
emit PostCreated(postCount, _content, 0, msg.sender);
}
function tipPost(uint _id) public payable {
//fetch the post
Post memory _post = posts[_id];
//fetch the author
address payable _author = _post.author;
//pay the author
address(_author).transfer(msg.value);
//increment the tip post
_post.tipAmount = _post.tipAmount + msg.value;
//update the post
posts[_id] = _post;
//Trigger an event
emit PostTipped(postCount, _post.content, _post.tipAmount, _author);
}
}
You have a few issues in your code:
1- in the Post struct, you defined address as payable:
struct Post {
uint id;
string content;
uint tipAmount;
address payable author;
}
But when you are creating a post, you are passing msg.sender which has address type. Before v0.8.0 msg.sender was payable but since than you have to cast it as payable(msgs.sender). it should be:
function createPost(string memory _content) public {
require(bytes(_content).length > 0);
postCount ++;
posts[postCount] = Post(postCount, _content, 0, payable(msg.sender));
emit PostCreated(postCount, _content, 0, payable(msg.sender));
}
2- in tipPost function you are getting payable address
address payable _author = _post.author;
but then you are casting it with address. In solidity address and payable address are two different things. send and transfer are only available to the payable address type. You dont need this:
address(_author).transfer(msg.value);
instead just
_author.transfer(msg.value);

Type address is not implicitly convertible to expected type address payable. owner = msg.sender

I'm getting this error when compiling. I know it's related to v8 and I need to make them payable, and I did but still doesn't work. can a good samaritan help?
contract FundMe {
mapping(address =>uint256) public addressToAmountFunded;
address payable[] public funders;
address payable public owner;
constructor() public {
owner = msg.sender; //LINE WITH ERROR
}
function fund() public payable {
uint256 minimumUSD = 50 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "you need to spend more ETH my friend");
addressToAmountFunded[msg.sender] += msg.value;
funders.push(msg.sender); //ERROR AS WELL
}
Try wrapping the addresses like bellow:
payable(msg.sender)