Skip to content

Instantly share code, notes, and snippets.

@amacneil
Created April 5, 2016 17:37
Show Gist options
  • Select an option

  • Save amacneil/693789ee1c510dfd2d6e7401dfbedbc1 to your computer and use it in GitHub Desktop.

Select an option

Save amacneil/693789ee1c510dfd2d6e7401dfbedbc1 to your computer and use it in GitHub Desktop.
contract VCStartup {
string public name = 'VC Startup';
string public symbol = '%';
uint8 public decimals = 6;
uint constant totalShares = 100000000;
// percent owned by each shareholder
mapping (address => uint) public balanceOf;
// array of owners
address[] owners;
// total wei received
uint totalReceived;
event Transfer(address indexed from, address indexed to, uint value);
function VCStartup() {
// contract creator is assignd all shares,
// but will be completely diluted by the first new shareholder
balanceOf[msg.sender] = totalShares;
owners.push(msg.sender);
}
// shareholders can transfer their shares to new owners
function transfer(address to, uint value) {
if (balanceOf[msg.sender] < value || balanceOf[to] + value < balanceOf[to]) {
throw;
}
balanceOf[msg.sender] -= value;
balanceOf[to] += value;
Transfer(msg.sender, to, value);
}
// any funds sent to this contract will be distibuted to existing shareholders,
// then the sender will be given a proportional allocation of shares
function () {
// determine percentage for new shareholder
totalReceived += msg.value;
uint purchasedShares = 0;
bool ownerExists = false;
uint balance = this.balance;
// distribute incoming funds to early investors
for (uint i = 0; i < owners.length; i++) {
address owner = owners[i];
uint dividend = balance * balanceOf[owner] / totalShares;
if (dividend > this.balance) {
// can't send more wei than we own
dividend = this.balance;
}
if (dividend > 0) {
owner.send(dividend);
}
// dilute their holdings
uint dilutedShares = balanceOf[owner] * msg.value / totalReceived;
balanceOf[owner] -= dilutedShares;
purchasedShares += dilutedShares;
// Transfer(owner, msg.sender, dilutedShares);
// if this owner is msg.sender, no need to add them to the owners array
if (owner == msg.sender) {
ownerExists = true;
}
}
// grant remaining shares to our new shareholder
// we do this at the end so that the new shares won't accidentally be diluted
// if they were already an existing shareholder
balanceOf[msg.sender] += purchasedShares;
if (!ownerExists) {
owners.push(msg.sender);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment