Skip to content

Commit 08e8480

Browse files
TodmyChaituVR
andauthored
feat(config): add ability to define brovider URL (#886)
* feat(brovider): add extra param for configuring brovider * fix(lint): fix lint issues * fix(options): change namings * Update src/utils.ts * Update src/utils.ts * v0.5.6 --------- Co-authored-by: Chaitanya <[email protected]>
1 parent 645d1ba commit 08e8480

File tree

6 files changed

+54
-36
lines changed

6 files changed

+54
-36
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.5.5",
3+
"version": "0.5.6",
44
"repository": "snapshot-labs/snapshot.js",
55
"license": "MIT",
66
"main": "dist/snapshot.cjs.js",

src/sign/eip1271.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export async function verifyOldVersion(
4848
return returnValue.toLowerCase() === magicValue.toLowerCase();
4949
}
5050

51-
export async function verify(address, sig, hash, network = '1') {
52-
const provider = getProvider(network);
51+
export async function verify(address, sig, hash, network = '1', options = {}) {
52+
const provider = getProvider(network, options);
5353
if (await verifyDefault(address, sig, hash, provider)) return true;
5454
return await verifyOldVersion(address, sig, hash, provider);
5555
}

src/sign/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function getHash(data) {
77
return _TypedDataEncoder.hash(domain, types, message);
88
}
99

10-
export async function verify(address, sig, data, network = '1') {
10+
export async function verify(address, sig, data, network = '1', options = {}) {
1111
const { domain, types, message } = data;
1212

1313
const hash = getHash(data);
@@ -23,5 +23,5 @@ export async function verify(address, sig, data, network = '1') {
2323
}
2424

2525
// console.log('Check EIP1271 signature');
26-
return await verifyEIP1271(address, sig, hash, network);
26+
return await verifyEIP1271(address, sig, hash, network, options);
2727
}

src/utils.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ export async function multicall(
104104
const multicallAbi = [
105105
'function aggregate(tuple(address target, bytes callData)[] calls) view returns (uint256 blockNumber, bytes[] returnData)'
106106
];
107-
const multi = new Contract(
108-
networks[network].multicall,
109-
multicallAbi,
110-
provider
111-
);
107+
const multicallAddress =
108+
options?.multicallAddress || networks[network].multicall;
109+
const multi = new Contract(multicallAddress, multicallAbi, provider);
112110
const itf = new Interface(abi);
113111
try {
114112
const max = options?.limit || 500;
@@ -184,8 +182,8 @@ export function getUrl(uri, gateway = gateways[0]) {
184182
return uri;
185183
}
186184

187-
export async function getJSON(uri) {
188-
const url = getUrl(uri);
185+
export async function getJSON(uri, options: any = {}) {
186+
const url = getUrl(uri, options.gateways);
189187
return fetch(url).then((res) => res.json());
190188
}
191189

@@ -323,28 +321,33 @@ export function validateSchema(schema, data) {
323321
export async function getEnsTextRecord(
324322
ens: string,
325323
record: string,
326-
network = '1'
324+
network = '1',
325+
options: any = {}
327326
) {
328327
const ensResolvers =
329-
networks[network].ensResolvers || networks['1'].ensResolvers;
328+
options.ensResolvers ||
329+
networks[network].ensResolvers ||
330+
networks['1'].ensResolvers;
330331
const ensHash = namehash(ensNormalize(ens));
331-
const provider = getProvider(network);
332+
const provider = getProvider(network, options);
332333

333334
const result = await multicall(
334335
network,
335336
provider,
336337
ENS_RESOLVER_ABI,
337-
ensResolvers.map((address: any) => [address, 'text', [ensHash, record]])
338+
ensResolvers.map((address: any) => [address, 'text', [ensHash, record]]),
339+
options
338340
);
339341
return result.flat().find((r: string) => r) || '';
340342
}
341343

342344
export async function getSpaceUri(
343345
id: string,
344-
network = '1'
346+
network = '1',
347+
options: any = {}
345348
): Promise<string | null> {
346349
try {
347-
return await getEnsTextRecord(id, 'snapshot', network);
350+
return await getEnsTextRecord(id, 'snapshot', network, options);
348351
} catch (e) {
349352
console.log(e);
350353
return null;
@@ -353,16 +356,18 @@ export async function getSpaceUri(
353356

354357
export async function getEnsOwner(
355358
ens: string,
356-
network = '1'
359+
network = '1',
360+
options: any = {}
357361
): Promise<string | null> {
358362
const registryAddress = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e';
359-
const provider = getProvider(network);
363+
const provider = getProvider(network, options);
360364
const ensRegistry = new Contract(
361365
registryAddress,
362366
['function owner(bytes32) view returns (address)'],
363367
provider
364368
);
365-
const ensNameWrapper = networks[network].ensNameWrapper;
369+
const ensNameWrapper =
370+
options.ensNameWrapper || networks[network].ensNameWrapper;
366371
const ensHash = namehash(ensNormalize(ens));
367372
let owner = await ensRegistry.owner(ensHash);
368373
// If owner is the ENSNameWrapper contract, resolve the owner of the name
@@ -379,9 +384,10 @@ export async function getEnsOwner(
379384

380385
export async function getSpaceController(
381386
id: string,
382-
network = '1'
387+
network = '1',
388+
options: any = {}
383389
): Promise<string | null> {
384-
const spaceUri = await getSpaceUri(id, network);
390+
const spaceUri = await getSpaceUri(id, network, options);
385391
if (spaceUri) {
386392
let isUriAddress = isAddress(spaceUri);
387393
if (isUriAddress) return spaceUri;
@@ -392,15 +398,18 @@ export async function getSpaceController(
392398
isUriAddress = isAddress(address);
393399
if (isUriAddress) return address;
394400
}
395-
return await getEnsOwner(id, network);
401+
return await getEnsOwner(id, network, options);
396402
}
397403

398404
export async function getDelegatesBySpace(
399405
network: string,
400406
space: string,
401-
snapshot = 'latest'
407+
snapshot = 'latest',
408+
options: any = {}
402409
) {
403-
if (!delegationSubgraphs[network]) {
410+
const subgraphUrl =
411+
options.subgraphUrl || SNAPSHOT_SUBGRAPH_URL[network];
412+
if (!subgraphUrl) {
404413
return Promise.reject(
405414
`Delegation subgraph not available for network ${network}`
406415
);
@@ -432,10 +441,7 @@ export async function getDelegatesBySpace(
432441
while (true) {
433442
params.delegations.__args.skip = page * PAGE_SIZE;
434443

435-
const pageResult = await subgraphRequest(
436-
delegationSubgraphs[network],
437-
params
438-
);
444+
const pageResult = await subgraphRequest(subgraphUrl, params);
439445
const pageDelegations = pageResult.delegations || [];
440446
result = result.concat(pageDelegations);
441447
page++;

src/utils/blockfinder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { subgraphRequest } from '../utils';
33
let cache: Record<string, any> = {};
44
let expirationTime = 0;
55

6-
export async function getSnapshots(network, snapshot, provider, networks) {
6+
export async function getSnapshots(
7+
network,
8+
snapshot,
9+
provider,
10+
networks,
11+
options: any = {}
12+
) {
713
// If snapshot is latest, return all latest
814
const snapshots = {};
915
networks.forEach((n) => (snapshots[n] = 'latest'));
@@ -39,7 +45,7 @@ export async function getSnapshots(network, snapshot, provider, networks) {
3945
number: true
4046
}
4147
};
42-
const url = 'https://blockfinder.snapshot.org';
48+
const url = options.blockFinderUrl || 'https://blockfinder.snapshot.org';
4349
const data = await subgraphRequest(url, query);
4450
data.blocks.forEach((block) => (snapshots[block.network] = block.number));
4551
cache[cacheKey] = snapshots;

src/utils/provider.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import {
66
const providers = {};
77
const batchedProviders = {};
88

9-
export default function getProvider(network) {
10-
const url = `https://rpc.snapshot.org/${network}`;
9+
export default function getProvider(
10+
network,
11+
{ broviderUrl = 'https://rpc.snapshot.org' } = {}
12+
) {
13+
const url = `${broviderUrl}/${network}`;
1114
if (!providers[network])
1215
providers[network] = new StaticJsonRpcProvider(
1316
{
@@ -20,8 +23,11 @@ export default function getProvider(network) {
2023
return providers[network];
2124
}
2225

23-
export function getBatchedProvider(network) {
24-
const url = `https://rpc.snapshot.org/${network}`;
26+
export function getBatchedProvider(
27+
network,
28+
{ broviderUrl = 'https://rpc.snapshot.org' } = {}
29+
) {
30+
const url = `${broviderUrl}/${network}`;
2531
if (!batchedProviders[network])
2632
batchedProviders[network] = new JsonRpcBatchProvider({
2733
url,

0 commit comments

Comments
 (0)