Skip to content

Commit

Permalink
return all contributor funds when cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
darcys22 committed Sep 17, 2024
1 parent 5c59e17 commit b23b938
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
7 changes: 6 additions & 1 deletion contracts/ServiceNodeContribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,12 @@ contract ServiceNodeContribution is Shared {
require(!finalized, "Cannot cancel a finalized node.");
require(!cancelled, "Node has already been cancelled.");
cancelled = true;
removeAndRefundContributor(msg.sender);
uint256 arrayLength = contributorAddresses.length;
address[] memory _contributorAddresses = contributorAddresses;
for (uint256 i = 0; i < arrayLength; i++) {
address entry = _contributorAddresses[i];
removeAndRefundContributor(entry);
}
emit Cancelled(serviceNodeParams.serviceNodePubkey);
}

Expand Down
28 changes: 20 additions & 8 deletions test/unit-js/ServiceNodeContributionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,27 @@ describe("ServiceNodeContribution Contract Tests", function () {
.equal(3);
});

it("Cancel node and check contributors can withdraw", async function() {
it("Cancel node and check contributor funds have been returned", async function() {
const [owner, contributor1, contributor2] = await ethers.getSigners();
await expect(snContribution.connect(owner).cancelNode());

const contributorArray = [contributor1, contributor2];
for (let i = 0; i < contributorArray.length; i++) {
const contributor = contributorArray[i];
await withdrawContributor(sentToken, snContribution, contributor);
}

// Get initial balances
const initialBalance1 = await sentToken.balanceOf(contributor1.address);
const initialBalance2 = await sentToken.balanceOf(contributor2.address);

// Get contribution amounts
const contribution1 = await snContribution.contributions(contributor1.address);
const contribution2 = await snContribution.contributions(contributor2.address);

// Cancel the node
await expect(snContribution.connect(owner).cancelNode())
.to.emit(snContribution, "Cancelled");

// Check final balances
const finalBalance1 = await sentToken.balanceOf(contributor1.address);
const finalBalance2 = await sentToken.balanceOf(contributor2.address);

expect(finalBalance1).to.equal(initialBalance1 + contribution1);
expect(finalBalance2).to.equal(initialBalance2 + contribution2);
});
});
});
Expand Down

0 comments on commit b23b938

Please sign in to comment.