Skip to content

Instantly share code, notes, and snippets.

@jigar23
Created June 26, 2018 15:08
Show Gist options
  • Save jigar23/dd58ce7400fe02a91275c1cdda093c5a to your computer and use it in GitHub Desktop.
Save jigar23/dd58ce7400fe02a91275c1cdda093c5a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.23+commit.124ca40d.js&optimize=false&gist=
pragma solidity ^0.4.23;
// Make this a library
contract AdrressArray {
address[] private m_array;
constructor(address[] array) public payable
{
m_array = array;
}
function addValue(address value) public
{
m_array.push(value);
}
function removeValue(address value) public returns (bool)
{
require(m_array.length > 0, "No values added");
bool foundValue = false;
// If found, push the last element to the found index and delete the
// last element
for (uint i = 0; i < m_array.length; i++) {
if (m_array[i] == value) {
m_array[i] = m_array[m_array.length-1];
foundValue = true;
break;
}
}
if (foundValue) {
delete m_array[m_array.length-1];
m_array.length--;
}
return foundValue;
}
function distributeValue(uint totalAmount) public payable
{
require(totalAmount > 0, "Add some balance to distribute");
require(m_array.length > 0, "Add some values to distribute");
uint shareOfAddress = totalAmount/m_array.length;
for (uint i = 0; i < m_array.length; i++) {
address value = m_array[i];
value.transfer(shareOfAddress);
}
}
}
pragma solidity ^0.4.23;
contract Ownable {
address internal m_owner;
event OwnershipRenounced(address indexed owner);
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
constructor() public
{
m_owner = msg.sender;
}
modifier onlyOwner()
{
require(msg.sender == m_owner, "Only Owner allowed to modify contract");
_;
}
function transferOwnership(address new_owner) public onlyOwner
{
_transferOwnership(new_owner);
}
function _transferOwnership(address new_owner) internal {
require(new_owner != address(0));
emit OwnershipTransferred(m_owner, new_owner);
m_owner = new_owner;
}
function renounceOwnership() public onlyOwner
{
emit OwnershipRenounced(m_owner);
m_owner = address(0);
}
}
pragma solidity ^0.4.23;
import "browser/Ownable.sol";
import "browser/Arrays.sol";
contract TimeBasedWill is Ownable {
AdrressArray private m_beneficiaries;
uint private m_expiryTime;
modifier beforeExpiry {
require(now < m_expiryTime, "This action should have been done only BEFORE expiry");
_;
}
modifier afterExpiry {
require(now >= m_expiryTime, "This action can be done only AFTER expiry");
_;
}
constructor(uint expiryDuration) public payable
{
require(address(this).balance > 0, "Please add some initial funds to the Will");
m_expiryTime = now + expiryDuration;
}
function changeExpiry(uint expiryDuration) public
{
m_expiryTime = now + expiryDuration;
}
function approveAddresses(address[] beneficiaries) onlyOwner public
{
m_beneficiaries = new AdrressArray(beneficiaries);
}
function addBeneficiary(address newBeneficiary) public onlyOwner beforeExpiry
{
m_beneficiaries.addValue(newBeneficiary);
}
function removeBeneficiary(address beneficiary) public onlyOwner beforeExpiry
{
m_beneficiaries.removeValue(beneficiary);
}
function claimOwnership() public payable afterExpiry
{
m_beneficiaries.distributeValue(address(this).balance);
}
function addFunds() public payable onlyOwner beforeExpiry {
}
function removeFunds(uint amount) public payable onlyOwner beforeExpiry {
require(amount > 0, "Add some value to remove funds");
require(address(this).balance > amount, "Balance insufficient to remove funds");
m_owner.transfer(amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment