Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Sort recpient list for the pay method
Browse files Browse the repository at this point in the history
  • Loading branch information
chmanie committed Feb 28, 2023
1 parent 52cfecd commit cd78fa8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
26 changes: 19 additions & 7 deletions examples/node/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { ColonyNetwork, ColonyRpcEndpoint, w } from '../../src';

const provider = new providers.JsonRpcProvider(ColonyRpcEndpoint.Gnosis);

if (!process.env.PRIVATE_KEY) {
throw new Error('Please provide PRIVATE_KEY as an environment variable');
}

const start = async () => {
// Create a new wallet from a private key. This key is stored in an environment variable called `PRIATE_KEY`
const wallet = new Wallet(process.env.PRIVATE_KEY as string).connect(
Expand All @@ -16,17 +20,25 @@ const start = async () => {
const colony = await colonyNetwork.getColony(
'0x364B3153A24bb9ECa28B8c7aCeB15E3942eb4fc5',
);
// Mint 100 of the colony's native token
await colony.mint(w`100`).metaTx();

// Define recipients and amounts to pay out
const recipients = [
'0xf24e6667C49f6272A7EbFdf4e5e7e9BBda0c89Bc',
'0x7E676370a053Dcfa851DCA951E9a9F567503E33B',
'0xD2c5D6D1f16d9B1e8bbD0e2EA784a67dC6aC8969',
'0x3962e6380421c94d69584e69255EAd67C92BFD23',
];
const amounts = [1, 2, 3, 4];

// Mint 10 of the colony's native token
await colony.mint(w`10`).metaTx();
// Claim these tokens for the colony
await colony.claimFunds().metaTx();
// Move all the funds from the Root team to the team with the number 2
await colony.moveFundsToTeam(w`100`, 2).metaTx();
await colony.moveFundsToTeam(w`10`, 2).metaTx();
if (colony.ext.oneTx) {
// Pay an address 50 of the colony's native token from the funds of team 2
await colony.ext.oneTx
.pay('0x0AEFF664e8d75c13801be16bCfE8143Bf422135A', w`50`, 2)
.metaTx();
// Pay a bunch of addresses the respective of the colony's native token from the funds of team 2
await colony.ext.oneTx.pay(recipients, amounts, 2).metaTx();
}
};

Expand Down
32 changes: 22 additions & 10 deletions src/ColonyNetwork/OneTxPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,29 @@ export class OneTxPayment {
const setTeamId = teamId || Id.RootDomain;
const setTokenAddress = tokenAddress
? ([] as string[]).concat(tokenAddress)
: [colonyClient.tokenClient.address];
: Array(setReceipient.length).fill(colonyClient.tokenClient.address);
const setAmount = ([] as BigNumberish[]).concat(amount);

if (Array.isArray(setTokenAddress) && Array.isArray(setAmount)) {
if (setTokenAddress.length !== setAmount.length) {
throw new Error(
'amount and tokenAddress arrays need to have the same size',
);
}
if (recipient.length !== setAmount.length) {
throw new Error('recipient and amount arrays need to have the same size');
}

if (setTokenAddress.length !== setAmount.length) {
throw new Error(
'amount and tokenAddress arrays need to have the same size',
);
}

// The list of recipients has to be orderd in ascending order.
// So we have to reorder the amounts and tokens as well
// TODO: technically, the list of tokens for a user also has to be sorted in ascending order.
// Let's cross that bridge when we get to it
const indices = Array.from(setReceipient.keys());
indices.sort((a, b) => setReceipient[a].localeCompare(setReceipient[b]));
const sortedRecipients = indices.map((i) => setReceipient[i]);
const sortedAmounts = indices.map((i) => setAmount[i]);
const sortedTokens = indices.map((i) => setTokenAddress[i]);

return this.colony.createColonyTxCreator(
this.oneTxPaymentClient,
'makePaymentFundedFromDomain',
Expand All @@ -160,9 +172,9 @@ export class OneTxPayment {
extChildSkillIndex,
userPermissionDomainId,
userChildSkillIndex,
setReceipient,
setTokenAddress,
setAmount,
sortedRecipients,
sortedTokens,
sortedAmounts,
setTeamId,
// Skill associated with this payment. Ignore for now
Id.SkillIgnore,
Expand Down

0 comments on commit cd78fa8

Please sign in to comment.