-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add base calculator #24
Changes from 6 commits
c318827
24c3ecd
fb5c976
43a8180
562932d
6c11906
c9bbc0a
b13d3ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import fs from "fs"; | ||
import { linearQF, Contribution, Calculation } from "pluralistic"; | ||
|
||
type AugmentedResult = Calculation & { | ||
projectName: string; | ||
payoutAddress: string; | ||
}; | ||
|
||
export default class Calculator { | ||
private baseDataPath: string; | ||
private chainId: string; | ||
private roundId: string; | ||
|
||
constructor(baseDataPath: string, chainId: string, roundId: string) { | ||
this.baseDataPath = baseDataPath; | ||
this.chainId = chainId; | ||
this.roundId = roundId; | ||
} | ||
|
||
calculate() { | ||
const rawContributions = this.parseJSONFile( | ||
`${this.chainId}/rounds/${this.roundId}/votes.json` | ||
); | ||
const projects = this.parseJSONFile(`${this.chainId}/projects.json`); | ||
const applications = this.parseJSONFile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
`${this.chainId}/rounds/${this.roundId}/projects.json` | ||
); | ||
|
||
const contributions: Array<Contribution> = rawContributions.map( | ||
(raw: any) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we type this instead of using any? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a RawContribution interface but it's not changing a lot. We would probably need to throw an exception if a field is missing. |
||
contributor: raw.voter, | ||
recipient: raw.projectId, | ||
amount: raw.amountUSD, | ||
}) | ||
); | ||
|
||
const results = linearQF(contributions, 333000, { | ||
gravityblast marked this conversation as resolved.
Show resolved
Hide resolved
|
||
minimumAmount: 1, | ||
ignoreSaturation: true, | ||
}); | ||
|
||
const augmented: Array<AugmentedResult> = []; | ||
for (const id in results) { | ||
const calc = results[id]; | ||
const project = projects.find((p: any) => p.id === id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its more readable with names such as |
||
const application = applications.find((a: any) => a.id === id); | ||
|
||
augmented.push({ | ||
totalReceived: calc.totalReceived, | ||
sumOfSqrt: calc.sumOfSqrt, | ||
matched: calc.matched, | ||
projectName: project?.metadata?.title, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it good to consider that these values may not exist, but if the project or metadata do not exist - do we want to handle that a particular way? |
||
payoutAddress: application?.payoutAddress, | ||
}); | ||
} | ||
|
||
return augmented; | ||
} | ||
|
||
parseJSONFile(path: string) { | ||
const fullPath = `${this.baseDataPath}/${path}`; | ||
const data = fs.readFileSync(fullPath, { | ||
encoding: "utf8", | ||
flag: "r", | ||
}); | ||
|
||
return JSON.parse(data); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While not strictly necessary, I think checking constructor arguments would be a great piece of mind, e.g.