[WIP] Fix naive refund (#95)

* fix amountToRefund computation bug

* Add new test to ensure refund works. Update old test to be more strict on refund.

* Move test back into main file.
This commit is contained in:
Daniel Ternyak 2018-09-24 17:51:38 -05:00 committed by William O'Beirne
parent 5b6b9e35da
commit 6c8325e683
3 changed files with 27 additions and 4 deletions

View File

@ -38,6 +38,7 @@ contract CrowdFund {
uint public deadline;
uint public raiseGoal;
uint public amountRaised;
uint public frozenBalance;
uint public minimumContributionAmount;
uint public amountVotingForRefund;
address public beneficiary;
@ -213,6 +214,7 @@ contract CrowdFund {
freezeReason = FreezeReason.MAJORITY_VOTING_TO_REFUND;
}
frozen = true;
frozenBalance = address(this).balance;
}
// anyone can refund a contributor if a crowdfund has been frozen
@ -222,8 +224,7 @@ contract CrowdFund {
require(!isRefunded, "Specified address is already refunded");
contributors[refundAddress].refunded = true;
uint contributionAmount = contributors[refundAddress].contributionAmount;
// TODO - maybe don't use address(this).balance
uint amountToRefund = contributionAmount.mul(address(this).balance).div(raiseGoal);
uint amountToRefund = contributionAmount.mul(address(this).balance).div(frozenBalance);
refundAddress.transfer(amountToRefund);
emit Withdrawn(refundAddress, amountToRefund);
}

View File

@ -47,7 +47,11 @@ contract("CrowdFund Deadline", accounts => {
it("allows anyone to refund after time is up and goal is not reached and sets refund reason to 1", async () => {
const fundAmount = raiseGoal / 10;
await crowdFund.contribute({ from: fourthAccount, value: fundAmount });
await crowdFund.contribute({
from: fourthAccount,
value: fundAmount,
gasPrice: 0,
});
assert.equal(
(await crowdFund.contributors(fourthAccount))[0].toNumber(),
fundAmount
@ -59,7 +63,7 @@ contract("CrowdFund Deadline", accounts => {
assert.equal((await crowdFund.getFreezeReason()), 1)
await crowdFund.withdraw(fourthAccount);
const finalBalance = await web3.eth.getBalance(fourthAccount);
assert.ok(finalBalance.greaterThan(initBalance)); // hard to be exact due to the gas usage
assert.ok(finalBalance.equals(initBalance.plus(fundAmount)));
});
it("refunds remaining proportionally when fundraiser has failed", async () => {

View File

@ -412,6 +412,24 @@ contract("CrowdFund", accounts => {
assert.ok(finalBalanceThirdAccount.gt(initBalanceThirdAccount));
});
it("refunds full amounts even if raise goal isn't reached", async () => {
const initialBalance = await web3.eth.getBalance(fourthAccount);
const contribution = raiseGoal / 2;
const receipt = await crowdFund.contribute({
from: fourthAccount,
value: contribution,
gasPrice: 0,
});
await crowdFund.refund({ from: firstTrusteeAccount });
await crowdFund.withdraw(fourthAccount);
const balance = await web3.eth.getBalance(fourthAccount);
const diff = initialBalance.minus(balance);
assert(
balance.equals(initialBalance),
`Expected full refund, but refund was short ${diff.toString()} wei`
);
});
// [END] refund
// [BEGIN] getContributorMilestoneVote