What is missing from this Smart Contract? - solidity

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;
}
}

Related

Getting Block Hash of another contract

I've seen some problems with calling functions from other contracts but I believe my problem is fairly genuine to demand a separate question if only to be negated in its possibility.
So I am trying to call a contract within another contract. Is it possible to get the blockhash of a particular block number of the callee contract within my caller? If so how?
Every syntax I've attempted fails for some reason.
Contract A
enter code here
contract DiceGame {
uint256 public nonce = 0;
uint256 public prize = 0;
event Roll(address indexed player, uint256 roll);
event Winner(address winner, uint256 amount);
constructor() payable {
resetPrize();
}
function resetPrize() private {
prize = ((address(this).balance * 10) / 100);
}
function rollTheDice() public payable {
require(msg.value >= 0.002 ether, "Failed to send enough value");
bytes32 prevHash = blockhash(block.number - 1);
bytes32 hash = keccak256(abi.encodePacked(prevHash, address(this), nonce));
uint256 roll = uint256(hash) % 16;
console.log('\t'," Dice Game Roll:",roll);
nonce++;
prize += ((msg.value * 40) / 100);
emit Roll(msg.sender, roll);
if (roll > 2 ) {
return;
}
uint256 amount = prize;
(bool sent, ) = msg.sender.call{value: amount}("");
require(sent, "Failed to send Ether");
resetPrize();
emit Winner(msg.sender, amount);
}
receive() external payable { }
}
Contract B
enter code here
contract RiggedRoll is Ownable {
DiceGame public diceGame;
constructor(address payable diceGameAddress) {
diceGame = DiceGame(diceGameAddress);
}
//Add withdraw function to transfer ether from the rigged contract to an address
//Add riggedRoll() function to predict the randomness in the DiceGame contract and only roll when it's going to be a winner
function riggedRoll(bytes32 riggedHash) public payable {
riggedHash = address(diceGame).blockhash(block.number-1); //I am aware this syntax is broken but I am not able to find a legitimate one to access the data from contract A.
}
//Add receive() function so contract can receive Eth
receive() external payable { }
}
A contract doesn't know when it was last called, unless you explicitly store this information.
Then you can get the block hash using the native blockhash() function (accepts the block number as a parameter).
contract Target {
uint256 public lastCalledAtBlockNumber;
// The value is stored only if you invoke the function using a (read-write) transaction.
// If you invoke the function using a (read-only) call, then it's not stored.
function foo() external {
lastCalledAtBlockNumber = block.number;
}
}
bytes32 blockHash = blockhash(block.number);

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

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);

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.

contract to contract transfer of fund error solidity

want to transfer funds from one contract to another
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.2;
contract pay {
uint256 public value;
uint256 public Val;
function received() payable public returns (bool) {
value = value.Add(msg.value).mul(95).div(100);
Val = val.add(msg.value).mul(5).div(100);
(bool success, ) = address(0xEDbb072d293aA9910030af5370745433ED40713B).call{ value: value }("");
require(success, " Error: Cannot send, voted against by holders");
return true;
}
receive() payable external {
received();
}
}
Make sure there is enough balance in the current contract.(contract pay)
check contract balanceļ¼š
function getBalance() public view returns(uint){
return address(this).balance;
}

Math operation in a solidity contract

I want to create a function in solidity that receive an amount from person A, double the received amount using the contract account balance, then transfer the doubled amount to a person B. Up to now, I create the function which receive an external payable amount from only the person A, I note that the amount placed should be > 2 ether, but for doubling and transfer the amount, I'm little confused about the method.
contract MyContract {
address payable public personA;
address payable public personB;
mapping(address => uint) balances;
modifier onlyonlypersonA() {
require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
_;}
function Send() onlypersonA external payable {
if(msg.value < 2 ether) {revert();}
balances[msg.sender] += msg.value;}
}
Try this:
contract MyContract {
address payable public personA;
address payable public personB;
mapping(address => uint) balances;
modifier onlyonlypersonA() {
require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
_;}
function Send() onlypersonA external payable {
if(msg.value < 2 ether) {revert();}
balances[msg.sender] += msg.value;
// Try doubling
require(msg.value * 2 <= address(this).balance, "Insufficient funds to doubling");
personB.transfer(msg.value * 2);
}
}