-
Notifications
You must be signed in to change notification settings - Fork 3
Program Idea
Initial idea for the Smart Contract:
- Contract address is the recipient of the mining rewards from the mining pool.
- Each time the contract receives a payment from the pool it runs a function to calculate the split factor between the costcenter and the beneficiary and distributes the reward accordingly.
Implementation idea:
- When receiving coins the contract calls a
default()
function. This should be the main function that calculates the split factor and stores the distribution to the costcenter and beneficiary. - A function
distribute()
can be called to actually pay out the stored balances to the costcenter and beneficiary. - To set the target cost-covering amount for the costcenter, a function
setTargetAmount(coinvalue)
can be called. -
setMinimalPayout(coinvalue)
will set a minimum fordistribute()
. - A verification method in relevant functions that they can be called by the owner of the smart contract only, or at least by someone who was delegated permissions to call them.
Problem 1:
- When the mining pool pays out the rewards, it does not call a function on our smart contract, instead it just does a
send()
of the amount to the destination address. - In our case, the destination address is not a "normal" wallet, but it's a smart contract.
- Contracts that receive Ether directly (without a function call, i.e. using send or transfer) but do not define a fallback function throw an exception, sending back the Ether. So if we want our contract to receive Ether, we have to implement a payable fallback function.
function() payable { }
Problem 2:
- Send() now does not forward gas anymore. It simply uses the hardcoded stipend (2300 gas) siphoned from the value transfer cost (minimum 9040). It's enough to send ether, but also enough to basically do one additional small logging operation. You can't do another value transfer (it costs minimum 9040 gas) and you can't do things like storing variables. If a send() runs out of gas, it does not throw an error, it simply returns false().
- What if a contract is supposed to do something when money is sent to it? Using send() + fallback function is limited.
- When using send(), you basically have enough gas to log that it received money.
- Logs and their event data are not accessible from within contracts (not even from the contract that created them). If they should to be processed further, this may be done through an Oracle.
Implementation Alternative:
-
The smart contract runs a pretty much empty payable fallback function when receiving the mining reward from the pool. (Actually, better code style is implementing a
receive() external payable { }
function without the function keyword for the contract to receive Ether. Using payable fallback functions for receiving Ether is not recommended, since it would not fail on interface confusions.) -
The received rewards are not processed at the time they are received. Instead, they just "sit" in the wallet address of the smart contract.
-
To execute calculations and the distribution to the costcenter and the beneficiary separate function calls to the smart contract need to be initiated from outside.