Skip to content

Commit

Permalink
Upvote cli fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pahor167 committed Oct 25, 2023
1 parent d2adb44 commit 6ad699a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
90 changes: 90 additions & 0 deletions packages/cli/src/commands/governance/upvote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Address } from '@celo/connect'
import { newKitFromWeb3 } from '@celo/contractkit'
import { GovernanceWrapper } from '@celo/contractkit/lib/wrappers/Governance'
import { NetworkConfig, testWithGanache, timeTravel } from '@celo/dev-utils/lib/ganache-test'
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { testLocally } from '../../test-utils/cliUtils'
import Register from '../account/register'
import Lock from '../lockedgold/lock'
import Dequeue from './dequeue'
import Upvote from './upvote'

process.env.NO_SYNCCHECK = 'true'

const expConfig = NetworkConfig.governance

testWithGanache('governance:upvote cmd', (web3: Web3) => {
const minDeposit = web3.utils.toWei(expConfig.minDeposit.toString(), 'ether')
const kit = newKitFromWeb3(web3)
const proposalID = new BigNumber(1)
const proposalID2 = new BigNumber(2)
const proposalID3 = new BigNumber(3)
const proposalID4 = new BigNumber(4)
const proposalID5 = new BigNumber(5)
const proposalID6 = new BigNumber(6)
const proposalID7 = new BigNumber(7)

let accounts: Address[] = []
let governance: GovernanceWrapper

beforeEach(async () => {
accounts = await web3.eth.getAccounts()
kit.defaultAccount = accounts[0]
governance = await kit.contracts.getGovernance()
const dequeueFrequency = (await governance.dequeueFrequency()).toNumber()

await governance
.propose([], 'URL')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })
// this will reset lastDequeue to now
// there is 5 concurrent proposals possible to be dequeued
await testLocally(Dequeue, ['--from', accounts[0]])
await governance
.propose([], 'URL2')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })
await governance
.propose([], 'URL3')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })
await governance
.propose([], 'URL4')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })
await governance
.propose([], 'URL5')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })
await governance
.propose([], 'URL6')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })
await governance
.propose([], 'URL7')
.sendAndWaitForReceipt({ from: accounts[0], value: minDeposit })

await timeTravel(dequeueFrequency, web3)
await testLocally(Register, ['--from', accounts[0]])
await testLocally(Lock, ['--from', accounts[0], '--value', '100'])
})

test('will dequeue proposal if ready', async () => {
await testLocally(Upvote, ['--proposalID', proposalID2.toString(10), '--from', accounts[0]])

const queue = await governance.getQueue()
expect(queue.map((k) => k.proposalID)).toEqual([proposalID7])

const dequeue = await governance.getDequeue()
expect(dequeue).toEqual([
proposalID,
proposalID2,
proposalID3,
proposalID4,
proposalID5,
proposalID6,
])
})

test('can upvote proposal which cannot be dequeued', async () => {
await testLocally(Upvote, ['--proposalID', proposalID7.toString(10), '--from', accounts[0]])

const queue = await governance.getQueue()
expect(queue).toEqual([{ proposalID: proposalID7, upvotes: new BigNumber(100) }])
})
})
40 changes: 39 additions & 1 deletion packages/cli/src/commands/governance/upvote.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GovernanceWrapper } from '@celo/contractkit/src/wrappers/Governance'
import { flags } from '@oclif/command'
import { BaseCommand } from '../../base'
import { newCheckBuilder } from '../../utils/checks'
Expand Down Expand Up @@ -29,6 +30,43 @@ export default class Upvote extends BaseCommand {
.runChecks()

const account = await (await this.kit.contracts.getAccounts()).voteSignerToAccount(signer)
await displaySendTx('upvoteTx', await governance.upvote(id, account), {}, 'ProposalUpvoted')

const consideredProposals = await this.dequeueAllPossibleProposals(governance as any)

if (!consideredProposals.some((k) => k.id === id)) {
await displaySendTx('upvoteTx', await governance.upvote(id, account), {}, 'ProposalUpvoted')
}
}

/**
* Dequeues all possible proposals, returns the ones that were considered to be dequeued.
*/
async dequeueAllPossibleProposals(governance: GovernanceWrapper) {
const concurrentProposals = (await governance.concurrentProposals()).toNumber()
const queue = await governance.getQueue()
const originalLastDequeue = await governance.lastDequeue()

let consideredProposals

for (let index = 0; index < queue.length / concurrentProposals + 1; index++) {
consideredProposals = (
await Promise.all(
queue
.slice(0, concurrentProposals)
.map((p) => p.proposalID)
.map(async (id) => {
const expired = await governance.isQueuedProposalExpired(id)
return { id: id.toString(), expired }
})
)
).filter((k) => k.expired === false)

await displaySendTx('dequeue', governance.dequeueProposalsIfReady(), {})
if (originalLastDequeue !== (await governance.lastDequeue())) {
break
}
}

return consideredProposals ?? []
}
}

0 comments on commit 6ad699a

Please sign in to comment.