Here's how you can do it.
- You'll have to bind the parent address (IDK) to the child contract (Thing), so it can send funds back to it. See
createSomething() where we send address(this) when we create a new item.
- Thing will save this second argument as the parent.
- In the
sendEth() method, we'll split 0.2 to the name (I suggest picking a more intuitive variable name here) and 0.8 to the parent. We make sure to check the transfer statuses so they don't fail.
- We have to instruct IDK to receive funds, using the
receive() fallback method.
Note: for transferring tokens, we'll use the call format. More on this here.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Idk {
Thing[] deployedThings;
function createSomething(address _name) public returns (address) {
Thing newThing= new Thing(_name, address(this));
deployedThings.push(newThing);
return address(newThing);
}
function seeThings() public view returns(Thing[] memory){
return deployedThings;
}
// instruct the contract to accept ETH or it will revert without it
receive() payable external {}
}
contract Thing {
address public name;
address public parent;
constructor(address _name, address _parent) {
name= _name;
parent = _parent; // bind the factory/parent address
}
function sendEth() public payable {
require(msg.value==1 ether, "Value has to be 1 ether");
(bool success1, ) = name.call{value: 0.2 ether}("");
require(success1, "First transfer failed");
(bool success2, ) = parent.call{value: 0.8 ether}("");
require(success2, "First transfer failed");
}
}