Skip to content

Commit

Permalink
feat: implement calculate & predict
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Aug 25, 2021
1 parent 3d60280 commit 0b96dba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
2 changes: 1 addition & 1 deletion dcurve/src/internal/hash/sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GrantMatch } from 'src/types';
* @param distribution - GrantMatch[]
* @returns {string} - sha256 hash of the distribution
*/
export const handle = (distribution: GrantMatch[]) => {
export const handle = (distribution: GrantMatch[]): string => {

let grantIds: number[] = [];
let matches: number[] = [];
Expand Down
102 changes: 61 additions & 41 deletions dcurve/src/internal/main.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,89 @@
'use strict';

import { GrantPredictionArgs, GrantPredictions, GrantsDistribution } from 'src/types';
import { GrantPrediction, GrantPredictionArgs, GrantPredictions, GrantRoundContributions, GrantsDistribution } from 'src/types';
import { addAnonContribution, getGrantMatch } from './utils';

// ========== Actual Calculations ===========
type InitArgs = {
calcAlgo: Function,
hashAlgo: Function
}

export class CLR {
_options = {};
_options: InitArgs;

constructor(options: object) {
constructor(options: InitArgs) {
this._options = options;
}

calculate(contributions: Function): GrantsDistribution {
calculate(grantRoundContributions: GrantRoundContributions): GrantsDistribution {
const calcAlgo = this._options['calcAlgo'];
const hashAlgo = this._options['hashAlgo'];

// calculate distribution based on contributions
const distribution: GrantsDistribution = calcAlgo(
// TODO: invoke calcAlgo with CLRArgs object and grantRoundContributions
);

// generate the hash on the distribution based on selected hashAlgo
distribution.hash = hashAlgo(distribution.distribution);

return distribution;
}

/**
*
* @param contributions
* @param grantId
* @param predicitionPoints
* Calculates the matching amount based on current distribution and predicts
* the match based on predictionPoints
* @param GrantPredictionArgs
*
* @returns GrantPredictions
*/
predict(args: GrantPredictionArgs): GrantPredictions {
const calcAlgo = this._options['calcAlgo'];
const hashAlgo = this._options['hashAlgo'];

// accepts ALL contributions then predicts against each preidicitionPoint on the grantId only
const grantId: number = args.grantId
const predictionPoints: number[] = args.predictionPoints;
const grantRoundContributions: GrantRoundContributions = args.grantRoundContributions;

// calculate distribution based on current contribution
const distribution: GrantsDistribution = calcAlgo(
// TODO: invoke calcAlgo with CLRArgs object and grantRoundContributions
);
const currentGrantMatch = getGrantMatch(grantId, distribution);

let predictions: GrantPrediction[] = [];

// CLR calculation (full/with 0)
// CLR calculation with 1
// 1:predictedAmount = final_match for 1 - final_match for 0
// calculate predicted distribution for each predictionPoint
predictionPoints.forEach(predictionPoint => {

// return [
// {predictionPoint: 1, predictedAmount: 12},
// {predictionPoint: 10, predictedAmount: 30}
// ];
// add anon contribution of value predictionPoint
let newGrantRoundContributions = addAnonContribution(grantId, grantRoundContributions, predictionPoint);

// calculate distribution with anon contribution
const newDistribution: GrantsDistribution = calcAlgo(
// TODO: invoke calcAlgo with CLRArgs object and newGrantRoundContributions
);

const predictedGrantMatch = getGrantMatch(grantId, newDistribution);

const prediction: GrantPrediction = {
predictionPoint: predictionPoint,
predictedGrantMatch: predictedGrantMatch,
predictionDiff: predictedGrantMatch - currentGrantMatch
}

predictions.push(prediction);

});

const grantPredictions: GrantPredictions = {
grantId: grantId,
predictions: predictions
}

return grantPredictions;
}
}

// import { linear } from 'dcurve';
// import { sha256 } from 'dcurve';
//
// const clr = new CLR({
// calcAlgo: (...args:any[]) => {
// return linear(...args)
// },
// hashAlgo: (...args:any[]) => {
// return sha256(...args)
// }
// });
// const distribution = clr.calculate(() => {
//
// // feed in grantRound and a list of addresses to ignore
// return fetch(1, [
// '0x0...', '0x0...',
// ])
// })[0];
// const prediction = clr.predict(() => {
//
// return fetch(...);
// }, 10, [1, 10])

// ======= FOR FUTURE ========

// how to store the distribution curve as opposed to
Expand Down
8 changes: 4 additions & 4 deletions dcurve/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type GrantRoundContributions = {
export type GrantPredictionArgs = {
grantId: number;
predictionPoints: number[];
contributions: Contribution[];
grantRoundContributions: GrantRoundContributions;
};

/**
Expand All @@ -61,12 +61,12 @@ export type GrantPredictionArgs = {
*
* @type GrantPrediction
* @field {predictionPoint} if grant were to recieve an anon contribution
* @field {predictionAmount} new match after adding predicted_amount
* @field {predictedGrantMatch} new match after adding predicted_amount
* @field {predictionDiff} difference between predicted_match and predicted_amount
*/
type GrantPrediction = {
export type GrantPrediction = {
predictionPoint: number;
predictionAmount: number;
predictedGrantMatch: number;
predictionDiff: number;
}

Expand Down

0 comments on commit 0b96dba

Please sign in to comment.