Skip to content

Commit

Permalink
fix: tally result id collision
Browse files Browse the repository at this point in the history
  • Loading branch information
kittybest committed Jan 10, 2025
1 parent f0ad8f3 commit 9fb0720
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/interface/src/server/api/routers/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ export async function calculateMaciResults(
});
}

const results = tallyData.results.reduce((acc, tally, index) => {
const project = projects[index];
const results = tallyData.results.reduce((acc, tally) => {
/// The tallyResult.id on old version subgraph is 0, 1, 2, ..., and is {tallyAddress}-{id=0, 1, 2, ...} on new version
const [id1, id2] = tally.id.split("-");
const projectIndex = id2 ? Number(id2) : Number(id1);
const project = projects[projectIndex];
if (project) {
acc.set(project.id, { votes: Number(tally.result), voters: 0 });
}
Expand Down
4 changes: 2 additions & 2 deletions packages/subgraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
```

3. Run `pnpm run build`. You can use env variables `NETWORK` and `VERSION` to switch config files.
4. Run `graph auth --studio {key}`. You can find the key in subgraph studio dashboard.
5. Run `pnpm run deploy` to deploy subgraph
4. Run `graph auth --studio {key}`. You can find the key in subgraph studio dashboard; if you're using alchemy graph, skip this step.
5. Run `pnpm run deploy` to deploy subgraph; if you're using alchemy graph, run `pnpm run deploy-alchemy --deploy-key {key}` instead.
1 change: 1 addition & 0 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"prebuild": "pnpm codegen",
"build": "graph build",
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ maci-subgraph",
"deploy-alchemy": "graph deploy maci-subgraph --node https://subgraphs.alchemy.com/api/subgraphs/deploy/ --ipfs https://ipfs.satsuma.xyz",
"create-local": "graph create --node http://localhost:8020/ maci-subgraph",
"remove-local": "graph remove --node http://localhost:8020/ maci-subgraph",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 maci-subgraph --network localhost",
Expand Down
6 changes: 4 additions & 2 deletions packages/subgraph/src/utils/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,12 @@ export const createOrLoadClaim = (index: GraphBN, recipient: Bytes, amount: Grap
};

export const createOrLoadTallyResult = (index: GraphBN, result: GraphBN, tally: Bytes): TallyResult => {
let tallyResult = TallyResult.load(index.toString());
// Create composite key by combining tally address and index
const compositeId = `${tally.toHexString()}-${index.toString()}`;
let tallyResult = TallyResult.load(compositeId);

if (!tallyResult) {
tallyResult = new TallyResult(index.toString());
tallyResult = new TallyResult(compositeId);
tallyResult.result = result;
tallyResult.tally = tally;
tallyResult.save();
Expand Down
2 changes: 1 addition & 1 deletion packages/subgraph/tests/tally/tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("Tally", () => {

handleAddResult(event);

const tallyResult = TallyResult.load(event.params.index.toString())!;
const tallyResult = TallyResult.load(`${DEFAULT_TALLY_ADDRESS.toHexString()}-${event.params.index.toString()}`)!;

assert.fieldEquals("TallyResult", tallyResult.id, "result", event.params.result.toString());
});
Expand Down

0 comments on commit 9fb0720

Please sign in to comment.