Skip to content

Instantly share code, notes, and snippets.

Created February 12, 2018 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/1745d2e49020943d7d6b1860a3bd57ed to your computer and use it in GitHub Desktop.
Save anonymous/1745d2e49020943d7d6b1860a3bd57ed to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.19+commit.c4cbbb05.js&optimize=false&gist=
pragma solidity ^0.4.0;
contract Vaults {
struct Deposit {
bool isCreated;
uint amount;
uint takenInterest;
uint depositBlock;
uint releaseRequestBlock;
address releaseTo;
uint blocksPerInterest;
uint interestFraction;
}
struct Vault {
uint totalOwned;
mapping(address => Deposit) deposits;
}
mapping(address => Vault) vaults;
function Vaults() public { }
function put(address vaultOwner, uint blocksPerInterest, uint interestFraction) public payable {
Vault storage v = vaults[vaultOwner];
// return if deposit already found
if (v.deposits[msg.sender].isCreated){
return;
}
Deposit memory deposit = Deposit({
isCreated: true,
amount: msg.value,
takenInterest: 0,
depositBlock: now,
releaseRequestBlock: 0,
releaseTo: msg.sender,
blocksPerInterest: blocksPerInterest,
interestFraction: interestFraction
});
v.deposits[msg.sender] = deposit;
}
function requestRelease(address vaultOwner, address releaseTo) public {
Vault storage v = vaults[vaultOwner];
Deposit storage deposit = v.deposits[msg.sender];
// return if deposit not found
if (!deposit.isCreated) {
return;
}
// return if already requested
if (deposit.releaseRequestBlock != 0) {
return;
}
deposit.releaseRequestBlock = now;
deposit.releaseTo = releaseTo;
}
function release(address depositOwner) public {
Vault storage v = vaults[msg.sender];
Deposit storage deposit = v.deposits[depositOwner];
// return if deposit not found
if (!deposit.isCreated) {
return;
}
// return if release not requested
if (deposit.releaseRequestBlock == 0) {
return;
}
deposit.isCreated = false;
uint blocks = deposit.releaseRequestBlock - deposit.depositBlock;
uint interestParts = blocks / deposit.blocksPerInterest;
if (blocks % deposit.blocksPerInterest != 0) {
interestParts++;
}
uint interest = deposit.amount * interestParts / deposit.interestFraction;
if (interest > deposit.amount) {
return;
}
uint remain = deposit.amount - interest;
if (v.totalOwned < remain) {
return;
}
deposit.releaseTo.transfer(remain);
v.totalOwned -= remain;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment