// Sources flattened with hardhat v2.1.1 https://hardhat.org
// File contracts/SimpleStream.sol
// https://github.com/austintgriffith/scaffold-eth/tree/simple-stream
//
// 🏰 BuidlGuidl.com
//
pragma solidity >=0.8.0;
//SPDX-License-Identifier: MIT
//import "hardhat/console.sol";
contract SimpleStream {
event Withdraw( address indexed to, uint256 amount, string reason );
event Deposit( address indexed from, uint256 amount, string reason );
address payable public toAddress;// = payable(0xD75b0609ed51307E13bae0F9394b5f63A7f8b6A1);
uint256 public cap;// = 0.5 ether;
uint256 public frequency;// 1296000 seconds == 2 weeks;
uint256 public last;//stream starts empty (last = block.timestamp) or full (block.timestamp - frequency)
constructor(address payable _toAddress, uint256 _cap, uint256 _frequency, bool _startsFull) {
toAddress = _toAddress;
cap = _cap;
frequency = _frequency;
if(_startsFull){
last = block.timestamp - frequency;
}else{
last = block.timestamp;
}
}
function streamBalance() public view returns (uint256){
if(block.timestamp-last > frequency){
return cap;
}
return (cap * (block.timestamp-last)) / frequency;
}
function streamWithdraw(uint256 amount, string memory reason) public {
require(msg.sender==toAddress,"this stream is not for you");
uint256 totalAmountCanWithdraw = streamBalance();
require(totalAmountCanWithdraw>=amount,"not enough in the stream");
uint256 cappedLast = block.timestamp-frequency;
if(last<cappedLast){
last = cappedLast;
}
last = last + ((block.timestamp - last) * amount / totalAmountCanWithdraw);
emit Withdraw( msg.sender, amount, reason );
toAddress.transfer(amount);
}
function streamDeposit(string memory reason) public payable {
emit Deposit( msg.sender, msg.value, reason );
}
receive() external payable { streamDeposit(""); }
}
{
"compilationTarget": {
"SimpleStream.sol": "SimpleStream"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address payable","name":"_toAddress","type":"address"},{"internalType":"uint256","name":"_cap","type":"uint256"},{"internalType":"uint256","name":"_frequency","type":"uint256"},{"internalType":"bool","name":"_startsFull","type":"bool"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"string","name":"reason","type":"string"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"string","name":"reason","type":"string"}],"name":"Withdraw","type":"event"},{"inputs":[],"name":"cap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"frequency","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"last","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"streamBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"reason","type":"string"}],"name":"streamDeposit","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"reason","type":"string"}],"name":"streamWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"toAddress","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]