Skip to content

Commit 558f8bc

Browse files
committed
Add automatic refund functionality
1 parent 8d5ad10 commit 558f8bc

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

contracts/colony/ColonyFunding.sol

+11
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ contract ColonyFunding is ColonyStorage { // ignore-swc-123
461461
}
462462

463463
function processPayout(uint256 _fundingPotId, address _token, uint256 _payout, address payable _user) private {
464+
refundDomain(_fundingPotId, _token);
465+
464466
IColonyNetwork colonyNetworkContract = IColonyNetwork(colonyNetworkAddress);
465467
address payable metaColonyAddress = colonyNetworkContract.getMetaColony();
466468

@@ -491,4 +493,13 @@ contract ColonyFunding is ColonyStorage { // ignore-swc-123
491493

492494
emit PayoutClaimed(msgSender(), _fundingPotId, _token, remainder);
493495
}
496+
497+
function refundDomain(uint256 _fundingPotId, address _token) private {
498+
FundingPot storage fundingPot = fundingPots[_fundingPotId];
499+
if (fundingPot.payouts[_token] < fundingPot.balance[_token]) {
500+
uint256 domainId = getDomainFromFundingPot(_fundingPotId);
501+
uint256 surplus = sub(fundingPot.balance[_token], fundingPot.payouts[_token]);
502+
moveFundsBetweenPotsFunctionality(_fundingPotId, domains[domainId].fundingPotId, surplus, _token);
503+
}
504+
}
494505
}

test/contracts-network/colony-expenditure.js

+29
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,35 @@ contract("Colony Expenditure", (accounts) => {
685685
expect(potPayout).to.be.zero;
686686
});
687687

688+
it("should automatically reclaim funds if there is excess funding for a token", async () => {
689+
await colony.setExpenditureRecipient(expenditureId, SLOT0, RECIPIENT, { from: ADMIN });
690+
await colony.setExpenditurePayout(expenditureId, SLOT0, token.address, WAD, { from: ADMIN });
691+
692+
const expenditure = await colony.getExpenditure(expenditureId);
693+
await colony.moveFundsBetweenPots(
694+
1,
695+
UINT256_MAX,
696+
1,
697+
UINT256_MAX,
698+
UINT256_MAX,
699+
domain1.fundingPotId,
700+
expenditure.fundingPotId,
701+
WAD.muln(2),
702+
token.address
703+
);
704+
await colony.finalizeExpenditure(expenditureId, { from: ADMIN });
705+
706+
const balanceBefore = await colony.getFundingPotBalance(domain1.fundingPotId, token.address);
707+
await colony.claimExpenditurePayout(expenditureId, SLOT0, token.address);
708+
const balanceAfter = await colony.getFundingPotBalance(domain1.fundingPotId, token.address);
709+
expect(balanceAfter.sub(balanceBefore)).to.eq.BN(WAD);
710+
711+
const potBalance = await colony.getFundingPotBalance(expenditure.fundingPotId, token.address);
712+
const potPayout = await colony.getFundingPotPayout(expenditure.fundingPotId, token.address);
713+
expect(potBalance).to.be.zero;
714+
expect(potPayout).to.be.zero;
715+
});
716+
688717
it("if skill is set, should emit two reputation updates", async () => {
689718
await colony.setExpenditureRecipient(expenditureId, SLOT0, RECIPIENT, { from: ADMIN });
690719
await colony.setExpenditurePayout(expenditureId, SLOT0, token.address, WAD, { from: ADMIN });

0 commit comments

Comments
 (0)