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

feat: Add setBroviderUrl before getProvider #1090

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
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import addFormats from 'ajv-formats';
import addErrors from 'ajv-errors';
import Multicaller from './utils/multicaller';
import { getSnapshots } from './utils/blockfinder';
import getProvider from './utils/provider';
import getProvider, { setBroviderUrl } from './utils/provider';
import { signMessage, getBlockNumber } from './utils/web3';
import { getHash, verify } from './verify';
import gateways from './gateways.json';
Expand Down Expand Up @@ -786,6 +786,7 @@ export default {
sleep,
getNumberWithOrdinal,
voting,
setBroviderUrl,
getProvider,
signMessage,
getBlockNumber,
Expand Down
5 changes: 4 additions & 1 deletion src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export type ProviderOptions = {
broviderUrl?: string;
};

const DEFAULT_BROVIDER_URL = 'https://rpc.snapshot.org';
let DEFAULT_BROVIDER_URL = 'https://rpc.snapshot.org';
export function setBroviderUrl(broviderUrl: string) {
DEFAULT_BROVIDER_URL = broviderUrl;
}

export default function getProvider(
network,
Expand Down
9 changes: 9 additions & 0 deletions test/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const snapshot = require('../');

const provider_DEFAULT = snapshot.utils.getProvider('1');
console.log(provider_DEFAULT.connection.url == 'https://rpc.snapshot.org/1');

snapshot.utils.setBroviderUrl('https://rpc.custom.org');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually use second param in getProvider to pass a different brovider URL, you tried?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried.
I'm using getVp and validate, but I want to use my own provider rpc. https://github.com/snapshot-labs/snapshot-strategies/blob/master/src/utils/vp.ts#L22
https://github.com/snapshot-labs/snapshot-strategies/blob/master/src/validations/basic/index.ts#L11

I use snapshot-strategies package, don't direct add second param in getProvider.
So:

import strategies from '@snapshot-labs/strategies';
import snapshot from "@snapshot-labs/snapshot.js";

// change custom rpc
snapshot.utils.setBroviderUrl('https://rpc.custom.org');
// It use my custom rpc in getVp 
strategies.utils.getVp(...)

const provider_CUSTOM = snapshot.utils.getProvider('56');
console.log(provider_CUSTOM.connection.url == 'https://rpc.custom.org/56');