Skip to content

Commit

Permalink
Merge pull request #809 from obvious-inc/candidate-index
Browse files Browse the repository at this point in the history
add number to Candidates [subgraph]
  • Loading branch information
solimander authored Dec 11, 2023
2 parents c12cfaf + 604a98f commit 0568af8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/nouns-subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ type Governance @entity {

"The proposal ID from which vote snapshots are taken at vote start instead of proposal creation"
voteSnapshotBlockSwitchProposalId: BigInt!

"Number of candidates created"
candidates: BigInt!
}

type DynamicQuorumParams @entity {
Expand Down Expand Up @@ -466,6 +469,9 @@ type ProposalCandidate @entity {

"This candidate's versions"
versions: [ProposalCandidateVersion!]! @derivedFrom(field: "proposal")

"This candidate's number"
number: BigInt!
}

type ProposalCandidateVersion @entity(immutable: true) {
Expand Down
2 changes: 2 additions & 0 deletions packages/nouns-subgraph/src/nouns-dao-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { ProposalCandidateContent, ProposalCandidateVersion } from './types/schema';
import {
candidateID,
getCandidateIndex,
getOrCreateCandidateFeedback,
getOrCreateDelegate,
getOrCreateProposalCandidate,
Expand All @@ -33,6 +34,7 @@ export function handleProposalCandidateCreated(event: ProposalCandidateCreated):
candidate.lastUpdatedTimestamp = event.block.timestamp;
candidate.lastUpdatedBlock = event.block.number;
candidate.canceled = false;
candidate.number = getCandidateIndex();

const version = captureProposalCandidateVersion(
event.transaction.hash.toHexString(),
Expand Down
11 changes: 11 additions & 0 deletions packages/nouns-subgraph/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function getGovernanceEntity(): Governance {
governance.delegatedVotes = BIGINT_ZERO;
governance.proposalsQueued = BIGINT_ZERO;
governance.voteSnapshotBlockSwitchProposalId = BIGINT_ZERO;
governance.candidates = BIGINT_ZERO;
}

return governance as Governance;
Expand Down Expand Up @@ -177,7 +178,12 @@ export function getOrCreateProposalCandidate(id: string): ProposalCandidate {
let candidate = ProposalCandidate.load(id);
if (candidate == null) {
candidate = new ProposalCandidate(id);

const governance = getGovernanceEntity();
governance.candidates = governance.candidates.plus(BIGINT_ONE);
governance.save();
}

return candidate;
}

Expand Down Expand Up @@ -233,6 +239,11 @@ export function getOrCreateFork(id: BigInt): Fork {
return fork;
}

export function getCandidateIndex(): BigInt {
const governance = getGovernanceEntity();
return governance.candidates;
}

export function candidateID(proposer: Address, slug: string): string {
return proposer.toHexString().concat('-').concat(slug);
}
Expand Down
39 changes: 39 additions & 0 deletions packages/nouns-subgraph/tests/nouns-dao-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('nouns-dao-data', () => {
assert.bytesEquals(txHash, candidate.createdTransactionHash);
assert.bigIntEquals(blockTimestamp, candidate.createdTimestamp);
assert.bigIntEquals(blockNumber, candidate.createdBlock);
assert.bigIntEquals(BigInt.fromI32(1), candidate.number);

const version = ProposalCandidateVersion.load(candidate.latestVersion)!;
assert.stringEquals(candidate.id, version.proposal);
Expand Down Expand Up @@ -177,5 +178,43 @@ describe('nouns-dao-data', () => {
);
assert.assertNull(signature);
});

test('save a proposal candidade includes candidate index', () => {
const candidate = ProposalCandidate.load(
candidateProposer.toHexString().concat('-').concat(slug),
)!;
assert.stringEquals(candidateProposer.toHexString(), candidate.proposer.toHexString());
assert.stringEquals(slug, candidate.slug);
assert.bytesEquals(txHash, candidate.createdTransactionHash);
assert.bigIntEquals(blockTimestamp, candidate.createdTimestamp);
assert.bigIntEquals(blockNumber, candidate.createdBlock);
assert.bigIntEquals(BigInt.fromI32(1), candidate.number);

const newSlug = 'new slug';

// save new one
const event = createProposalCandidateCreatedEvent(
txHash,
logIndex,
blockTimestamp,
blockNumber,
candidateProposer,
targets,
values,
signatures,
calldatas,
description,
newSlug,
encodedProposalHash,
);

handleProposalCandidateCreated(event);

const candidate2 = ProposalCandidate.load(
candidateProposer.toHexString().concat('-').concat(newSlug),
)!;

assert.bigIntEquals(BigInt.fromI32(2), candidate2.number);
});
});
});

0 comments on commit 0568af8

Please sign in to comment.