Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activate votes from any account #140

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/happy-rice-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@celo/contractkit': minor
'@celo/celocli': minor
---

Activate votes from any account, new optional parameter to specify for account in ElectionWrapper:activate
25 changes: 18 additions & 7 deletions packages/cli/src/commands/election/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,45 @@ export default class ElectionVote extends BaseCommand {

static flags = {
...BaseCommand.flags,
from: CustomFlags.address({ required: true, description: "Voter's address" }),
from: CustomFlags.address({
required: true,
description: "Address sending transaction (and voter's address if --for not specified)",
}),
for: CustomFlags.address({
required: false,
description: 'Optional: use this to activate votes for another address',
}),
wait: Flags.boolean({ description: 'Wait until all pending votes can be activated' }),
}

static examples = [
'activate --from 0x4443d0349e8b3075cba511a0a87796597602a0f1',
'activate --from 0x4443d0349e8b3075cba511a0a87796597602a0f1 --for 0x5409ed021d9299bf6814279a6a1411a7e866a631',
'activate --from 0x4443d0349e8b3075cba511a0a87796597602a0f1 --wait',
]

async run() {
const kit = await this.getKit()
const res = await this.parse(ElectionVote)

await newCheckBuilder(this, res.flags.from).isSignerOrAccount().runChecks()
const forAccount = res.flags.for ?? res.flags.from
await newCheckBuilder(this, forAccount).isSignerOrAccount().runChecks()

const election = await kit.contracts.getElection()
const accounts = await kit.contracts.getAccounts()
const account = await accounts.voteSignerToAccount(res.flags.from)
const hasPendingVotes = await election.hasPendingVotes(account)
const signerAccount = await accounts.voteSignerToAccount(forAccount)

const election = await kit.contracts.getElection()
const hasPendingVotes = await election.hasPendingVotes(signerAccount)
if (hasPendingVotes) {
if (res.flags.wait) {
// Spin until pending votes become activatable.
ux.action.start(`Waiting until pending votes can be activated`)
while (!(await election.hasActivatablePendingVotes(account))) {
while (!(await election.hasActivatablePendingVotes(signerAccount))) {
await sleep(1000)
}
ux.action.stop()
}
const txos = await election.activate(account)
const txos = await election.activate(signerAccount, res.flags.for != null)
for (const txo of txos) {
await displaySendTx('activate', txo, { from: res.flags.from })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ BaseWrapperForGoverning.address

### activate

▸ **activate**(`account`): `Promise`\<`CeloTransactionObject`\<`boolean`\>[]\>
▸ **activate**(`account`, `onBehalfOfAccount?`): `Promise`\<`CeloTransactionObject`\<`boolean`\>[]\>

Activates any activatable pending votes.

Expand All @@ -503,14 +503,15 @@ Activates any activatable pending votes.
| Name | Type | Description |
| :------ | :------ | :------ |
| `account` | `string` | The account with pending votes to activate. |
| `onBehalfOfAccount?` | `boolean` | - |

#### Returns

`Promise`\<`CeloTransactionObject`\<`boolean`\>[]\>

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:332](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L332)
[packages/sdk/contractkit/src/wrappers/Election.ts:334](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L334)

___

Expand Down Expand Up @@ -578,7 +579,7 @@ ___

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:444](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L444)
[packages/sdk/contractkit/src/wrappers/Election.ts:451](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L451)

___

Expand Down Expand Up @@ -641,7 +642,7 @@ Retrieves the set of validatorsparticipating in BFT at epochNumber.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:473](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L473)
[packages/sdk/contractkit/src/wrappers/Election.ts:480](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L480)

___

Expand All @@ -657,7 +658,7 @@ Returns the current eligible validator groups and their total votes.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:429](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L429)
[packages/sdk/contractkit/src/wrappers/Election.ts:436](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L436)

___

Expand All @@ -680,7 +681,7 @@ Retrieves GroupVoterRewards at epochNumber.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:485](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L485)
[packages/sdk/contractkit/src/wrappers/Election.ts:492](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L492)

___

Expand Down Expand Up @@ -838,7 +839,7 @@ Retrieves VoterRewards for address at epochNumber.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:519](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L519)
[packages/sdk/contractkit/src/wrappers/Election.ts:526](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L526)

___

Expand All @@ -861,7 +862,7 @@ Retrieves a voter's share of active votes.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:553](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L553)
[packages/sdk/contractkit/src/wrappers/Election.ts:560](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L560)

___

Expand Down Expand Up @@ -949,7 +950,7 @@ ___

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:390](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L390)
[packages/sdk/contractkit/src/wrappers/Election.ts:397](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L397)

___

Expand Down Expand Up @@ -979,7 +980,7 @@ Must pass both `lesserAfterVote` and `greaterAfterVote` or neither.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:365](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L365)
[packages/sdk/contractkit/src/wrappers/Election.ts:372](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L372)

___

Expand All @@ -1001,7 +1002,7 @@ ___

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:341](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L341)
[packages/sdk/contractkit/src/wrappers/Election.ts:348](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L348)

___

Expand Down Expand Up @@ -1042,4 +1043,4 @@ Increments the number of total and pending votes for `group`.

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:417](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L417)
[packages/sdk/contractkit/src/wrappers/Election.ts:424](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L424)
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@

#### Defined in

[packages/sdk/contractkit/src/wrappers/Election.ts:569](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L569)
[packages/sdk/contractkit/src/wrappers/Election.ts:576](https://github.com/celo-org/developer-tooling/blob/master/packages/sdk/contractkit/src/wrappers/Election.ts#L576)
11 changes: 9 additions & 2 deletions packages/sdk/contractkit/src/wrappers/Election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,24 @@ export class ElectionWrapper extends BaseWrapperForGoverning<Election> {

private _activate = proxySend(this.connection, this.contract.methods.activate)

private _activateForAccount = proxySend(this.connection, this.contract.methods.activateForAccount)

/**
* Activates any activatable pending votes.
* @param account The account with pending votes to activate.
*/
async activate(account: Address): Promise<CeloTransactionObject<boolean>[]> {
async activate(
account: Address,
onBehalfOfAccount?: boolean
): Promise<Array<CeloTransactionObject<boolean>>> {
const groups = await this.contract.methods.getGroupsVotedForByAccount(account).call()
const isActivatable = await Promise.all(
groups.map((g) => this.contract.methods.hasActivatablePendingVotes(account, g).call())
)
const groupsActivatable = groups.filter((_, i) => isActivatable[i])
return groupsActivatable.map((g) => this._activate(g))
return groupsActivatable.map((g) =>
onBehalfOfAccount ? this._activateForAccount(g, account) : this._activate(g)
)
}

async revokePending(
Expand Down
Loading