[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:
parent
5b6b9e35da
commit
6c8325e683
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue