I need a contract that can send funds at 1 time to multiple addresses by percentage. Ex: n disbursements 1@ 20% Another - 14% Another - x% Another - x% and so on.

Ethereum does NOT have this capability. Does EOS?

If you want to send tokens to multiple accounts. You should send the names and the corresponding percentage inside a vector. I have written the code and tested the same with my contract It goes like this:

void samplecontract::percentage(account_name sender,
                                  vector<account_name> &receivers,
                                  vector<double> &percentages,
                                  vector<string> &memos,
                                  string memo)
{
    eosio::token t(N(eosio.token));
    const auto sym_name = eosio::symbol_type(S(4, EOS)).name();
    //get balance of sender first
    const auto my_balance = t.get_balance(sender, sym_name);

    print("\n initiate transacitons  _________________\n");
    for (int i = 0; i < receivers.size(); i++)
    {
        print(name{receivers[i]});
        print("\n");
        print(percentages[i]);
        print(memos[i]);
        //calculating asset to be send 
        eosio::asset tosend = (my_balance * (percentages[i] * 100)) / 10000;
        print("value is _____________", tosend);
        samplecontract::senderaction(sender, receivers[i], tosend, memos[i], 0);
    }

}


void samplecontract::senderaction(account_name sender,
                                      account_name to,
                                      asset quantity,
                                      string memo,
                                      uint64_t delay)
{
    eosio::transaction txn{};
    print(quantity);

    txn.actions.emplace_back(
        eosio::permission_level(sender, N(active)),
        N(eosio.token),
        N(transfer),
        std::make_tuple(sender, to, quantity, memo));
    txn.delay_sec = delay;
    txn.send(eosio::string_to_name(memo.c_str()), sender);

}

I added this as a deferred transaction you can use inline transaction as well. Remember both action should be @abi action. You can also customize the logic of memos , it's up to you. This code working fine in my case.

You can easily execute a transfer action in smart contract. For my current project, I have created an executeTransfer method that I can call from my other code whenever I need to move tokens around.

void executeTransfer(name from, name to, asset quantity) {
    action(
        permission_level{ from, "active"_n},
        "eosio.token"_n,
        "transfer"_n,
        std::make_tuple(from, to, quantity, string("Token Transfer"))
    ).send() ;
}

If you want to disperse tokens to multiple accounts, simply calculate the number of tokens for each account, and call the method multiple times (once for each account you need to send to). Because all smart contract invocations are transactional, if any of the transfers fail, then the entire operation will roll back and no tokens will be sent to anyone.

  • The number of tokens must be expressed as a percentage because the quantity changes based token price. Is this possible? – Fred Jones Nov 8 at 3:47
  • Also it must be done automatically: the contract receives x number of tokens and automatically disperses every time based %. – Fred Jones Nov 8 at 3:50
  • You can pass floats into a method. Once you have the appropriate percentages, you can then calculate the amount to be paid to each account and then call the executeTransfer method with the appropriate values. You cannot send tokens when calling a smart contract method, so you'd either have to send the tokens to the contract, then invoke the dispersal method, or implement the logic inside your contracts transfer method and disperse the tokens from there. – John Haager Nov 8 at 22:50
  • Thanks guys. Much appreciated. – Fred Jones Nov 11 at 17:07

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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