1

I'm trying to create a new coin based on ethereum ... one that will act as a charity. I have the concept of beneficiaries (charities that will get money from the coins owner's account on a regular basis... any time money comes into the owner's address). Here are my questions..

1) How would one properly code the distribution to the charities whenever anyone donates to the owners address? Would I do this by appending some logic to all transfer type functions that looks to see if the transfer is to "this" and if so immediately call my distribution logic? Or... would this better be handled by some sort of event that catches a donation to the owner's account and then triggers the distribution?

2) The contract I have "compiles" right now but since this is my first attempt I just want to make sure that what I'm setting up makes sense. Can somebody give these vars/ functions that I have a 'once over' just as a sanity check? They are just related to distributing the moneys.

mapping (address => Beneficiary) public beneficiary;

struct Beneficiary {
    address walletAddress;   // wallet address of beneficiary
    uint256 balance;
    uint256 verificationKey; // number of accumulated votes
}

// A dynamically-sized array of `Proposal` structs.
Beneficiary[] public beneficiaries;


/* Initializes contract with initial supply tokens to the creator of the contract */
function WOLFFE(
    uint256 initialSupply,
    string tokenName,
    uint8 decimalUnits,
    string tokenSymbol
) token (initialSupply, tokenName, decimalUnits, tokenSymbol) {}

function verifyBeneficiary(address _beneficiaryAddress, uint256 _verificationKey) onlyOwner {
    beneficiary[_beneficiaryAddress].verificationKey = _verificationKey;
}

function creditFunds() onlyOwner {
    //calculate distribution
    uint256 distrubutionAmount = balanceOf[this] / beneficiaries.length;
    //distribute
    for (uint p = 0; p < beneficiaries.length; p++) {
        if (beneficiaries[p].verificationKey != 0) {
            beneficiaries[p].balance += distrubutionAmount;
        }
    }
}

function withdrawFunds() external {
    uint funds =  beneficiary[msg.sender].balance;
    beneficiary[msg.sender].balance = 0;
    if (!msg.sender.send(funds)) {
        beneficiary[msg.sender].balance = funds; // reverting state because send failed
    }
} 
2

It's typically best practice to use pull payments, where each recipient/beneficiary independently requests for their own transfer. In your current code, you are looping over the distributions. This creates the potential for error, for example if during the call of your distribute() function, if your contract runs out of gas in the middle of distributing payments. By having the receiver/beneficiary invoke the withdrawal, that transaction would be limited to their own withdrawal without affecting the rest of the contract.

This may require you to rework your contract a bit, to keep track of funds allocated for each beneficiary and balances, checking whether or not they have withdrawn the funds or not.

For some more background, check out this link:

ConsenSys: Pull Payments

| improve this answer | |
  • awesome. Thank you a ton for that info. I'll check out the link and see what I can come up with. Just outta curiosity, is there a way to get the gas charge before the distribution and factor that into my equation for the disbursementAmount? Reason being I feel like it would be much more user friendly if I could initiate the transaction on my side and the charities didn't really have to do anything. – Josh Aug 5 '17 at 18:10
  • Hi Carlolm, I've taken your advice and updated my contract in the initial post. I still am wondering, however, about what the best way to call my creditFunds function would be. I'd like to make it where anytime anyone sends coins to my main account (owners account) that the creditFunds function is automatically called which would essentially make funds available to my charities as soon as the deposit to the main account has been verified in the blockchain. – Josh Aug 6 '17 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.