Skip to content

Commit

Permalink
Merge pull request #188 from xuhcc/withdraw-check
Browse files Browse the repository at this point in the history
Contracts: Prevent repeated withdrawals when the round is cancelled
  • Loading branch information
xuhcc authored Nov 16, 2020
2 parents 61ad209 + d8bf2d9 commit 75cd3f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions contracts/contracts/FundingRound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ contract FundingRound is Ownable, MACISharedObjs, SignUpGatekeeper, InitialVoice
// Reconstruction of exact contribution amount from VCs may not be possible due to a loss of precision
uint256 amount = contributors[msg.sender].voiceCredits * voiceCreditFactor;
require(amount > 0, 'FundingRound: Nothing to withdraw');
contributors[msg.sender].voiceCredits = 0;
nativeToken.transfer(msg.sender, amount);
emit ContributionWithdrawn(msg.sender);
}
Expand Down
32 changes: 26 additions & 6 deletions contracts/tests/round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use(solidity);

describe('Funding Round', () => {
const provider = waffle.provider;
const [, deployer, coordinator, contributor, recipient] = provider.getWallets()
const [, deployer, coordinator, contributor, anotherContributor, recipient] = provider.getWallets()

const coordinatorPubKey = (new Keypair()).pubKey;
const signUpDuration = 86400 * 7; // Default duration in MACI factory
Expand Down Expand Up @@ -53,6 +53,7 @@ describe('Funding Round', () => {
const Token = await ethers.getContractFactory('AnyOldERC20Token', deployer);
token = await Token.deploy(tokenInitialSupply);
await token.transfer(contributor.address, tokenInitialSupply.div(4))
await token.transfer(anotherContributor.address, tokenInitialSupply.div(4))
await token.transfer(coordinator.address, tokenInitialSupply.div(4))

verifiedUserRegistry = await deployMockContract(deployer, IVerifiedUserRegistryArtifact.abi);
Expand Down Expand Up @@ -611,26 +612,33 @@ describe('Funding Round', () => {

describe('withdrawing funds', () => {
const userPubKey = userKeypair.pubKey.asContractParam()
const anotherUserPubKey = userKeypair.pubKey.asContractParam()
const contributionAmount = UNIT.mul(10)
let tokenAsContributor: Contract;
let fundingRoundAsContributor: Contract;

beforeEach(async () => {
tokenAsContributor = token.connect(contributor);
fundingRoundAsContributor = fundingRound.connect(contributor);
await fundingRound.setMaci(maci.address);
await tokenAsContributor.approve(
await token.connect(contributor).approve(
fundingRound.address,
contributionAmount,
);
)
await token.connect(anotherContributor).approve(
fundingRound.address,
contributionAmount,
)
});

it('allows contributor to withdraw funds', async () => {
it('allows contributors to withdraw funds', async () => {
await fundingRoundAsContributor.contribute(userPubKey, contributionAmount);
await fundingRound.connect(anotherContributor)
.contribute(anotherUserPubKey, contributionAmount)
await fundingRound.cancel();

await expect(fundingRoundAsContributor.withdrawContribution())
.to.emit(fundingRound, 'ContributionWithdrawn')
.withArgs(contributor.address);
await fundingRound.connect(anotherContributor).withdrawContribution()
expect(await token.balanceOf(fundingRound.address))
.to.equal(0);
});
Expand All @@ -646,6 +654,18 @@ describe('Funding Round', () => {
await expect(fundingRoundAsContributor.withdrawContribution())
.to.be.revertedWith('FundingRound: Nothing to withdraw');
});

it('reverts if funds are already withdrawn', async () => {
await fundingRound.connect(contributor)
.contribute(userPubKey, contributionAmount)
await fundingRound.connect(anotherContributor)
.contribute(anotherUserPubKey, contributionAmount)
await fundingRound.cancel()

await fundingRound.connect(contributor).withdrawContribution()
await expect(fundingRound.connect(contributor).withdrawContribution())
.to.be.revertedWith('FundingRound: Nothing to withdraw')
})
});

describe('claiming funds', () => {
Expand Down

0 comments on commit 75cd3f1

Please sign in to comment.