Skip to content

Commit 6ec7c66

Browse files
authored
API key in headers to score-api (#882)
* Score API key * Change case of variable * v0.4.110
1 parent 1a3d8f6 commit 6ec7c66

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@snapshot-labs/snapshot.js",
3-
"version": "0.4.109",
3+
"version": "0.4.110",
44
"repository": "snapshot-labs/snapshot.js",
55
"license": "MIT",
66
"main": "dist/snapshot.cjs.js",

src/utils.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ interface Strategy {
2828
}
2929

3030
export const SNAPSHOT_SUBGRAPH_URL = delegationSubgraphs;
31-
3231
const ENS_RESOLVER_ABI = [
3332
'function text(bytes32 node, string calldata key) external view returns (string memory)'
3433
];
34+
const SCORE_API_KEY = process?.env?.KEYCARD_SECRET || '';
35+
36+
const scoreApiHeaders = {
37+
Accept: 'application/json',
38+
'Content-Type': 'application/json'
39+
};
40+
if (SCORE_API_KEY) scoreApiHeaders['X-API-KEY'] = SCORE_API_KEY;
3541

3642
const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, $data: true });
3743
// @ts-ignore
@@ -215,7 +221,8 @@ export async function getScores(
215221
network: string,
216222
addresses: string[],
217223
snapshot: number | string = 'latest',
218-
scoreApiUrl = 'https://score.snapshot.org/api/scores'
224+
scoreApiUrl = 'https://score.snapshot.org/api/scores',
225+
options: any = { returnValue: 'scores' }
219226
) {
220227
try {
221228
const params = {
@@ -227,11 +234,11 @@ export async function getScores(
227234
};
228235
const res = await fetch(scoreApiUrl, {
229236
method: 'POST',
230-
headers: { 'Content-Type': 'application/json' },
237+
headers: scoreApiHeaders,
231238
body: JSON.stringify({ params })
232239
});
233240
const obj = await res.json();
234-
return obj.result.scores;
241+
return options.returnValue ? obj.result[options.returnValue] : obj.result;
235242
} catch (e) {
236243
return Promise.reject(e);
237244
}
@@ -250,10 +257,7 @@ export async function getVp(
250257
if (!options.url) options.url = 'https://score.snapshot.org';
251258
const init = {
252259
method: 'POST',
253-
headers: {
254-
Accept: 'application/json',
255-
'Content-Type': 'application/json'
256-
},
260+
headers: scoreApiHeaders,
257261
body: JSON.stringify({
258262
jsonrpc: '2.0',
259263
method: 'get_vp',
@@ -264,8 +268,7 @@ export async function getVp(
264268
snapshot,
265269
space,
266270
delegation
267-
},
268-
id: null
271+
}
269272
})
270273
};
271274
const res = await fetch(options.url, init);
@@ -287,10 +290,7 @@ export async function validate(
287290
if (!options.url) options.url = 'https://score.snapshot.org';
288291
const init = {
289292
method: 'POST',
290-
headers: {
291-
Accept: 'application/json',
292-
'Content-Type': 'application/json'
293-
},
293+
headers: scoreApiHeaders,
294294
body: JSON.stringify({
295295
jsonrpc: '2.0',
296296
method: 'validate',
@@ -301,8 +301,7 @@ export async function validate(
301301
network,
302302
snapshot,
303303
params
304-
},
305-
id: null
304+
}
306305
})
307306
};
308307
const res = await fetch(options.url, init);

test/vp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ const delegation = false;
2020

2121
snapshot.utils
2222
.getVp(address, network, strategies, s, space, delegation)
23-
.then((result) => console.log(result));
23+
.then(console.log)
24+
.catch(console.log);

0 commit comments

Comments
 (0)