Skip to content

Commit

Permalink
Adapt to last changes in Staking app
Browse files Browse the repository at this point in the history
Locks now have start date too.
  • Loading branch information
bingen committed Jul 24, 2018
1 parent 9e179dc commit db8d13f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
12 changes: 7 additions & 5 deletions contracts/Curation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ contract Curation is AragonApp {
require(!registry.exists(entryId));

// check locked tokens
uint256 amount = _checkLock(msg.sender, lockId, MAX_UINT64);
uint256 amount = _checkLock(msg.sender, lockId, getTimestamp(), MAX_UINT64);

applications[entryId] = Application({
applicant: msg.sender,
Expand Down Expand Up @@ -153,7 +153,7 @@ contract Curation is AragonApp {
}

// check locked tokens
uint256 amount = _checkLock(msg.sender, lockId, getTimestamp().add(applyStageLen));
uint256 amount = _checkLock(msg.sender, lockId, getTimestamp(), getTimestamp().add(applyStageLen));

// create vote
// TODO: metadata
Expand Down Expand Up @@ -472,13 +472,14 @@ contract Curation is AragonApp {
);
}

function _checkLock(address user, uint256 lockId, uint64 date) internal returns (uint256) {
function _checkLock(address user, uint256 lockId, uint64 startDate, uint64 endDate) internal returns (uint256) {
// get the lock
uint256 amount;
uint8 lockUnit;
uint64 lockStarts;
uint64 lockEnds;
address unlocker;
(amount, lockUnit, lockEnds, unlocker, ) = staking.getLock(msg.sender, lockId);
(amount, lockUnit, lockStarts, lockEnds, unlocker, ) = staking.getLock(msg.sender, lockId);
// check lockId was not used before
require(!usedLocks[user][lockId]);
// check unlocker
Expand All @@ -487,7 +488,8 @@ contract Curation is AragonApp {
require(amount >= minDeposit);
// check time
require(lockUnit == uint8(TimeUnit.Seconds));
require(lockEnds >= date);
require(lockStarts <= startDate);
require(lockEnds >= endDate);

// mark it as used
usedLocks[user][lockId] = true;
Expand Down
63 changes: 42 additions & 21 deletions test/curation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { assertRevert } = require('@aragon/test-helpers/assertThrow')
const getTimestamp = async () => await web3.eth.getBlock(web3.eth.blockNumber).timestamp

const { checkUnlocked, checkMovedTokens } = require('./helpers.js')

Expand Down Expand Up @@ -57,7 +58,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

const createApplication = async () => {
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
const r = await curation.newApplication(data, appLockId, { from: applicant })
const entryId = getEvent(r, "NewApplication", "entryId")

Expand All @@ -79,7 +81,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const data1 = "Test 1"
const data2 = "Test 2"
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// create application
await curation.newApplication(data1, appLockId, { from: applicant })
// repeat
Expand All @@ -92,7 +95,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const lockId1 = 1
const lockId2 = 2
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// create application
await curation.newApplication(data, lockId1, { from: applicant })
// repeat
Expand All @@ -105,7 +109,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const lockId1 = 1
const lockId2 = 2
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// register data - this won't happen with real app, as ACLs will be working!
await registry.add(data)
// create application
Expand All @@ -119,7 +124,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const lockId2 = 2
const emptyData = ""
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// register data - this won't happen with real app, as ACLs will be working!
await registry.add(data)
// create application
Expand All @@ -130,7 +136,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

it('fails creating new Application if Curation is not unlocker', async () => {
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, owner, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, owner, "")
// create application
return assertRevert(async () => {
await curation.newApplication(data, appLockId, { from: applicant })
Expand All @@ -139,7 +146,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

it('fails creating new Application if lock deposit is not enough', async () => {
// mock lock
await staking.setLock(minDeposit - 1, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit - 1, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// create application
return assertRevert(async () => {
await curation.newApplication(data, appLockId, { from: applicant })
Expand All @@ -148,7 +156,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

it('fails creating new Application if lock time unit is not seconds', async () => {
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS + 1, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS + 1, startLock, MAX_UINT64, curation.address, "")
// create application
return assertRevert(async () => {
await curation.newApplication(data, appLockId, { from: applicant })
Expand All @@ -157,7 +166,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

it('fails creating new Application if lock time is not enough', async () => {
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64 - 1, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64 - 1, curation.address, "")
// create application
return assertRevert(async () => {
await curation.newApplication(data, appLockId, { from: applicant })
Expand All @@ -169,7 +179,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const applyAndChallenge = async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand Down Expand Up @@ -205,7 +216,7 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
await curation.setMinDeposit(minDeposit + 1)

// mock lock - no need for lock
await staking.setLock(0, TIME_UNIT_SECONDS, 0, zeroAddress, "")
await staking.setLock(0, TIME_UNIT_SECONDS, 0, 0, zeroAddress, "")
// challenge
const receipt = await curation.challengeApplication(entryId, challengeLockId, { from: challenger })
// no challenge has been created
Expand All @@ -221,7 +232,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails challenging with an already used lock', async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// use challenger's lock for an application
await curation.newApplication("test 2", appLockId, { from: challenger })
// mock vote Id
Expand All @@ -244,7 +256,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails challenging application if Curation is not unlocker', async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), owner, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), owner, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand All @@ -256,7 +269,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails challenging application if lock deposit is not enough', async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit - 1, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit - 1, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand All @@ -268,7 +282,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails challenging application if lock time unit is not seconds', async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS + 1, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS + 1, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand All @@ -280,7 +295,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails challenging application if lock time is not enough', async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen - 1), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen - 1), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand Down Expand Up @@ -360,7 +376,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
await curation.registerUnchallengedApplication(entryId)

// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand Down Expand Up @@ -429,7 +446,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails resolving challenge if vote has not ended', async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand Down Expand Up @@ -533,7 +551,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const entryId = await createApplication()

// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand Down Expand Up @@ -644,7 +663,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
it('fails creating new application', async () => {
const lockId = 1
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await getTimestamp()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
return assertRevert(async () => {
await curation.newApplication("test", lockId)
})
Expand Down Expand Up @@ -723,7 +743,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

it('creates new Application', async () => {
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await getTimestamp()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
// create application
const r = await curation.newApplication(data, appLockId, { from: applicant })
const entryId = getEvent(r, "NewApplication", "entryId")
Expand Down
30 changes: 24 additions & 6 deletions test/mocks/StakingMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "@aragon/apps-staking/contracts/interfaces/IStaking.sol";
contract StakingMock is IStaking {
uint256 amount;
uint8 lockUnit;
uint64 lockStarts;
uint64 lockEnds;
address unlocker;
bytes32 metadata;
Expand All @@ -14,28 +15,45 @@ contract StakingMock is IStaking {
event UnlockedPartial(address indexed account, address indexed unlocker, uint256 indexed lockId, uint256 amount);
event MovedTokens(address indexed from, address indexed to, uint256 amount);

function setLock(uint256 _amount, uint8 _lockUnit, uint64 _lockEnds, address _unlocker, bytes32 _metadata) public {
function setLock(uint256 _amount, uint8 _lockUnit, uint64 _lockStarts, uint64 _lockEnds, address _unlocker, bytes32 _metadata) public {
amount = _amount;
lockUnit = _lockUnit;
lockStarts = _lockStarts;
lockEnds = _lockEnds;
unlocker = _unlocker;
}

function unlock(address acct, uint256 lockId) public {
Unlocked(acct, msg.sender, lockId);
function unlock(address _acct, uint256 _lockId) public {
Unlocked(_acct, msg.sender, _lockId);
}

function moveTokens(address _from, address _to, uint256 _amount) public {
MovedTokens(_from, _to, _amount);
}

function unlockPartialAndMoveTokens(address _from, uint256 _lockId, address _to, uint256 _amount) external {
UnlockedPartial(_from, msg.sender, _lockId, amount);
UnlockedPartial(_from, msg.sender, _lockId, _amount);
MovedTokens(_from, _to, _amount);
}

function getLock(address _acct, uint256 _lockId) public view returns (uint256, uint8, uint64, address, bytes32) {
return (amount, lockUnit, lockEnds, unlocker, metadata);
function getLock(
address acct,
uint256 lockId
)
public
view
returns (
uint256 _amount,
uint8 _lockUnit,
uint64 _lockStarts,
uint64 _lockEnds,
address _unlocker,
bytes32 _metadata,
uint256 _prevUnlockerLockId,
uint256 _nextUnlockerLockId
)
{
return (amount, lockUnit, lockStarts, lockEnds, unlocker, metadata, 0, 0);
}


Expand Down
6 changes: 4 additions & 2 deletions test/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {

const createApplication = async () => {
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, MAX_UINT64, curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, MAX_UINT64, curation.address, "")
const r = await curation.newApplication(data, appLockId, { from: applicant })
const entryId = getEvent(r, "NewApplication", "entryId")

Expand All @@ -36,7 +37,8 @@ contract('Curation', ([owner, applicant, challenger, voter, _]) => {
const applyAndChallenge = async () => {
const entryId = await createApplication()
// mock lock
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, (await curation.getTimestampExt.call()).add(applyStageLen + 1000), curation.address, "")
const startLock = await curation.getTimestampExt.call()
await staking.setLock(minDeposit, TIME_UNIT_SECONDS, startLock, startLock.add(applyStageLen + 1000), curation.address, "")
// mock vote Id
await voting.setVoteId(voteId)
// challenge
Expand Down

0 comments on commit db8d13f

Please sign in to comment.