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.