Skip to content
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

fix: migrate to snapshot.js 0.8-beta with ofetch support #735

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@snapshot-labs/keycard": "^0.4.0",
"@snapshot-labs/snapshot-metrics": "^1.3.1",
"@snapshot-labs/snapshot-sentry": "^1.5.2",
"@snapshot-labs/snapshot.js": "^0.7.8",
"@snapshot-labs/snapshot.js": "^0.8.0-beta.0",
"bluebird": "^3.7.2",
"connection-string": "^1.0.1",
"cors": "^2.8.5",
Expand Down
49 changes: 35 additions & 14 deletions src/graphql/operations/vp.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import snapshot from '@snapshot-labs/snapshot.js';
import type { Strategy } from '@snapshot-labs/snapshot.js/dist/voting/types';
import { GraphQLError } from 'graphql';
import db from '../../helpers/mysql';

const scoreAPIUrl = process.env.SCORE_API_URL || 'https://score.snapshot.org';

type Payload = {
voter: string;
network?: string;
strategies?: Strategy[];
snapshot: number | 'latest';
space: string;
options: any;
};

export default async function (_parent, { voter, space, proposal }) {
if (voter === '0x0000000000000000000000000000000000000000' || voter === '') {
return Promise.reject(new Error('invalid address'));
}

const payload: Payload = {
voter,
space,
snapshot: 'latest',
options: { url: scoreAPIUrl }
};

if (proposal) {
const query = `SELECT * FROM proposals WHERE id = ? LIMIT 1`;
const [p] = await db.queryAsync(query, [proposal]);
Expand All @@ -16,15 +34,9 @@ export default async function (_parent, { voter, space, proposal }) {
return Promise.reject(new Error('proposal not found'));
}

return await snapshot.utils.getVp(
voter,
p.network,
JSON.parse(p.strategies),
p.snapshot,
space,
false,
{ url: scoreAPIUrl }
);
payload.network = p.network;
payload.strategies = JSON.parse(p.strategies);
payload.snapshot = p.snapshot;
} else if (space) {
const query = `SELECT settings FROM spaces WHERE id = ? AND deleted = 0 LIMIT 1`;
let [s] = await db.queryAsync(query, [space]);
Expand All @@ -34,11 +46,20 @@ export default async function (_parent, { voter, space, proposal }) {
}

s = JSON.parse(s.settings);

return await snapshot.utils.getVp(voter, s.network, s.strategies, 'latest', space, false, {
url: scoreAPIUrl
});
payload.network = s.network;
payload.strategies = s.strategies;
}

return Promise.reject(new Error('missing argument'));
try {
return await snapshot.utils.getVp(
payload.voter,
payload.network as string,
payload.strategies as Strategy[],
payload.snapshot,
payload.space,
payload.options
);
} catch (e: any) {
throw new GraphQLError('failed to get voting power', null, null, null, null, null, e);
}
}
43 changes: 30 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1322,10 +1322,10 @@
dependencies:
"@sentry/node" "^7.60.1"

"@snapshot-labs/snapshot.js@^0.7.8":
version "0.7.8"
resolved "https://registry.yarnpkg.com/@snapshot-labs/snapshot.js/-/snapshot.js-0.7.8.tgz#9a94b2a6de1331ccee5ffea3877af62dde4c2b33"
integrity sha512-TD0GQ+CKPsQWh7Ir5kELLjjdrvmDtH/UbKVgEx+wJYOMHn6IIfqA2Cw3OqmV2WG8SNqAj85hbalIzCBWMDbZ4A==
"@snapshot-labs/snapshot.js@^0.8.0-beta.0":
version "0.8.0-beta.0"
resolved "https://registry.yarnpkg.com/@snapshot-labs/snapshot.js/-/snapshot.js-0.8.0-beta.0.tgz#ec7d898ab439a6b37800dbc92e562dac9d1ef367"
integrity sha512-0B+qilsqQENmNf9tTXnUQ7/UVX5JLaMSyCTuJzf8+GxVGaZXGEG8TC9GdmL1q8YfJ/jXMWq9TzRjhs39ppZ6bg==
dependencies:
"@ensdomains/eth-ens-namehash" "^2.0.15"
"@ethersproject/abi" "^5.6.4"
Expand All @@ -1338,9 +1338,9 @@
"@ethersproject/wallet" "^5.6.2"
ajv "^8.11.0"
ajv-formats "^2.1.1"
cross-fetch "^3.1.6"
json-to-graphql-query "^2.2.4"
lodash.set "^4.3.2"
ofetch "^1.3.3"

"@tsconfig/node10@^1.0.7":
version "1.0.9"
Expand Down Expand Up @@ -2244,13 +2244,6 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-fetch@^3.1.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.6.tgz#bae05aa31a4da760969756318feeee6e70f15d6c"
integrity sha512-riRvo06crlE8HiqOwIpQhxwdOk4fOeR7FVM/wXoxchFEqMNUjvbs3bfo4OTgMEMHzppd4DxFBDbyySj8Cv781g==
dependencies:
node-fetch "^2.6.11"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -2349,6 +2342,11 @@ depd@~1.1.2:
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

destr@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/destr/-/destr-2.0.2.tgz#8d3c0ee4ec0a76df54bc8b819bca215592a8c218"
integrity sha512-65AlobnZMiCET00KaFFjUefxDX0khFA/E4myqZ7a6Sq1yZtR8+FVIvilVX66vF2uobSumxooYZChiRPCKNqhmg==

[email protected]:
version "1.2.0"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.2.0.tgz#4803735509ad8be552934c67df614f94e66fa015"
Expand Down Expand Up @@ -4375,7 +4373,12 @@ [email protected]:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

node-fetch@^2.6.11, node-fetch@^2.7, node-fetch@^2.7.0:
node-fetch-native@^1.4.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/node-fetch-native/-/node-fetch-native-1.4.1.tgz#5a336e55b4e1b1e72b9927da09fecd2b374c9be5"
integrity sha512-NsXBU0UgBxo2rQLOeWNZqS3fvflWePMECr8CoSWoSTqCqGbVVsvl9vZu1HfQicYN0g5piV9Gh8RTEvo/uP752w==

node-fetch@^2.7, node-fetch@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
Expand Down Expand Up @@ -4500,6 +4503,15 @@ object.values@^1.1.6:
define-properties "^1.1.4"
es-abstract "^1.20.4"

ofetch@^1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/ofetch/-/ofetch-1.3.3.tgz#588cb806a28e5c66c2c47dd8994f9059a036d8c0"
integrity sha512-s1ZCMmQWXy4b5K/TW9i/DtiN8Ku+xCiHcjQ6/J/nDdssirrQNOoB165Zu8EqLMA2lln1JUth9a0aW9Ap2ctrUg==
dependencies:
destr "^2.0.1"
node-fetch-native "^1.4.0"
ufo "^1.3.0"

[email protected], on-finished@^2.3.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.4.1.tgz#58c8c44116e54845ad57f14ab10b03533184ac3f"
Expand Down Expand Up @@ -5552,6 +5564,11 @@ typescript@^4.7.4:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235"
integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==

ufo@^1.3.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.3.1.tgz#e085842f4627c41d4c1b60ebea1f75cdab4ce86b"
integrity sha512-uY/99gMLIOlJPwATcMVYfqDSxUR9//AUcgZMzwfSTJPDKzA1S8mX4VLqa+fiAtveraQUBCz4FFcwVZBGbwBXIw==

unbox-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e"
Expand Down