Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 26 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import fetch from 'cross-fetch';
import _strategies from './strategies';
import snapshot from '@snapshot-labs/snapshot.js';
import { getDelegations } from './utils/delegation';
import { getVp } from './utils/vp';
import { createHash } from 'crypto';
import { Protocol, Score, Snapshot } from './types';
import { Protocol, Score, Snapshot, VotingPower } from './types';
import { VALID_PROTOCOLS } from './constants';

export function sha256(str) {
Expand Down Expand Up @@ -75,6 +74,31 @@ export async function getScoresDirect(
}
}

export async function getVp(
address: string,
network: string,
strategies: any[],
snapshot: Snapshot,
space: string
): Promise<VotingPower> {
const scores: Score[] = await getScoresDirect(
space,
strategies,
network,
getProvider(network),
[address],
snapshot
);

const vpByStrategy = scores.map((score) => score[address] || 0);

return {
vp: vpByStrategy.reduce((a, b) => a + b, 0),
vp_by_strategy: vpByStrategy,
vp_state: snapshot === 'latest' ? 'pending' : 'final'
};
}

export function customFetch(
url: RequestInfo | URL,
options: RequestInit = {},
Expand Down
60 changes: 0 additions & 60 deletions src/utils/vp.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[` getVp 1`] = `
exports[`utils getVp 1`] = `
{
"vp": 21.55404462002206,
"vp_by_strategy": [
Expand Down
5 changes: 2 additions & 3 deletions test/vp.test.ts → test/integration/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVp } from '../src/utils/vp';
import { getVp } from '../../src/utils';

const address = '0xeF8305E140ac520225DAf050e2f71d5fBcC543e7';
const space = 'cvx.eth';
Expand Down Expand Up @@ -30,10 +30,9 @@ const strategies = [
}
];

describe('', () => {
describe('utils', () => {
it('getVp', async () => {
const scores = await getVp(address, network, strategies, snapshot, space);
expect(scores).toMatchSnapshot();
console.log(scores);
}, 20e3);
});
Loading