Adds tests for upvoteEffect and Withdraw

This commit is contained in:
Andy Tudhope 2019-04-09 12:21:23 +02:00
parent 8cf59b99ee
commit ccbec568c1
No known key found for this signature in database
GPG Key ID: 02A3DFA93BF26AD2
2 changed files with 107 additions and 4 deletions

View File

@ -123,7 +123,12 @@ contract DAppStore is ApproveAndCallFallBack, BancorFormula {
(result, precision) = BancorFormula.power(mAvailable, decimals, uint32(decimals), uint32(mRate)); (result, precision) = BancorFormula.power(mAvailable, decimals, uint32(decimals), uint32(mRate));
uint mVMinted = result >> precision; uint mVMinted = result >> precision;
uint mEBalance = mBalance - ((mVMinted*mRate)*(mAvailable/mVMinted));
uint temp1 = d.votes_cast * mRate * mAvailable;
uint temp2 = mVMinted * decimals * decimals;
uint mEffect = temp1 / temp2;
uint mEBalance = mBalance - mEffect;
return (mEBalance - d.effective_balance); return (mEBalance - d.effective_balance);
} }
@ -242,9 +247,14 @@ contract DAppStore is ApproveAndCallFallBack, BancorFormula {
if (d.votes_cast > d.votes_minted) { if (d.votes_cast > d.votes_minted) {
d.votes_cast = d.votes_minted; d.votes_cast = d.votes_minted;
} }
d.effective_balance = d.balance - ((d.votes_cast*d.rate)*(d.available/d.votes_minted));
SNT.transferFrom(address(this), d.developer, _amount); uint temp1 = d.votes_cast * d.rate * d.available;
uint temp2 = d.votes_minted * decimals * decimals;
uint effect = temp1 / temp2;
d.effective_balance = d.balance - effect;
require(SNT.transfer(d.developer, _amount), "Transfer failed");
emit Withdraw(_id, d.effective_balance); emit Withdraw(_id, d.effective_balance);
} }

View File

@ -276,4 +276,97 @@ contract("DAppStore", function () {
assert.strictEqual(parseInt(receipt.effective_balance, 10), e_balance); assert.strictEqual(parseInt(receipt.effective_balance, 10), e_balance);
}) })
it("should return correct upvoteEffect for use in UI", async function () {
let id = "0x7465737400000000000000000000000000000000000000000000000000000000";
let amount = 400;
let receipt = await DAppStore.methods.dapps(0).call();
let effect = await DAppStore.methods.upvoteEffect(id,amount).call();
// Mock receiving the SNT
let mBalance = parseInt(receipt.balance, 10) + amount
let max = await DAppStore.methods.max().call();
let decimals = await DAppStore.methods.decimals().call();
let mRate = Math.round(decimals - (mBalance * decimals/max));
let mAvailable = mBalance * mRate;
let mVMinted = Math.round((mAvailable/decimals) ** (decimals/mRate));
// Votes have been cast by this stage, so we need to check how many there are
// and confirm that `upvoteEffect` mocks the effect correctly
let votes_cast = parseInt(receipt.votes_cast, 10);
let mEBalance = Math.round(mBalance - ((votes_cast*mRate/decimals)*(mAvailable/decimals/mVMinted)));
let effect_calc = mEBalance - receipt.effective_balance;
// Confirm that what is returned is (mEBalance - d.effective_balance)
assert.strictEqual(effect_calc, parseInt(effect, 10));
})
it("should throw already in upvoteEffect if you exceed the ceiling", async function () {
let id = "0x7465737400000000000000000000000000000000000000000000000000000000";
let amount = 2000000;
try {
await DAppStore.methods.upvoteEffect(id,amount).call();
} catch (error) {
TestUtils.assertJump(error);
}
})
it("should handle withdrawals correctly", async function () {
let id = "0x7465737400000000000000000000000000000000000000000000000000000000";
let amount = 1000;
let initial = await DAppStore.methods.dapps(0).call();
// Check the DApp Store actually sends SNT to the developer
let before = await SNT.methods.balanceOf(DAppStore.options.address).call();
let before_dev = await SNT.methods.balanceOf(accounts[0]).call();
let receipt_obj = await DAppStore.methods.withdraw(id,amount).send({from: accounts[0]});
let after = await SNT.methods.balanceOf(DAppStore.options.address).call();
let after_dev = await SNT.methods.balanceOf(accounts[0]).call();
let difference = parseInt(before, 10) - parseInt(after, 10);
let difference_dev = parseInt(after_dev, 10) - parseInt(before_dev, 10);
assert.strictEqual(difference, amount)
assert.strictEqual(difference_dev, amount)
let receipt = receipt_obj.events.Withdraw.returnValues;
assert.strictEqual(receipt.id, id);
let goal = parseInt(receipt.newEffectiveBalance, 10);
// Send the SNT
let balance = parseInt(initial.balance, 10) - amount
let max = await DAppStore.methods.max().call();
let decimals = await DAppStore.methods.decimals().call();
let rate = Math.round(decimals - (balance * decimals/max));
let available = balance * rate;
let v_minted = Math.round((available/decimals) ** (decimals/rate));
let v_cast = parseInt(initial.votes_cast, 10);
let e_balance = Math.round(balance - ((v_cast*rate/decimals)*(available/decimals/v_minted)));
assert.strictEqual(e_balance, goal);
// Having withdrawn the SNT, check that it updates the particular DApp's storage values properly
let check = await DAppStore.methods.dapps(0).call();
let withdrawnBalance = parseInt(initial.balance, 10) - amount
assert.strictEqual(parseInt(check.balance, 10), withdrawnBalance);
assert.strictEqual(parseInt(check.rate, 10), rate);
assert.strictEqual(parseInt(check.available, 10), available);
assert.strictEqual(parseInt(check.votes_minted, 10), v_minted);
})
}); });