Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
feat(sdk): fix subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
joeandrews committed Jul 9, 2020
1 parent f48ced0 commit 68f4662
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Web3 from 'web3';
import Web3Service from '~/helpers/Web3Service';
import {
ZkAsset,
Expand All @@ -6,6 +7,7 @@ import {
errorLog,
} from '~/utils/log';
import decodeNoteLogs from './helpers/decodeNoteLogs';
import { getProviderUrl } from '~/utils/network';

const subscribe = async ({
owner,
Expand All @@ -18,10 +20,8 @@ const subscribe = async ({
return null;
}

const {
abi,
subscribe: subscribeOn,
} = Web3Service.eth;
const provider = getProviderUrl(Web3Service.networkId, true);
const { abi, subscribe: subscribeOn } = new Web3(provider).eth;

const eventsTopics = [
ZkAsset.events.createNote,
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/src/config/provider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const infuraProjectId = '09c4eed231c840d5ace14ba5389a1a7c';

export const infuraProviderUrlPattern = 'https://{networkName}.infura.io/v3/{projectId}';
export const infuraHttpProviderUrlPattern = 'https://{networkName}.infura.io/v3/{projectId}';
export const infuraWsProviderUrlPattern = 'wss://{networkName}.infura.io/ws/v3/{projectId}';

export const defaultProviderUrl = 'ws://localhost:8545';
7 changes: 4 additions & 3 deletions packages/extension/src/utils/network/getProviderUrl.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import networks from '~/config/networks';
import {
infuraProjectId,
infuraProviderUrlPattern,
infuraHttpProviderUrlPattern,
infuraWsProviderUrlPattern,
defaultProviderUrl,
} from '~/config/provider';
import {
formatStrPattern,
} from '~/utils/format';

export default function getProviderUrl(networkId, projectId = infuraProjectId) {
export default function getProviderUrl(networkId, ws = false, projectId = infuraProjectId) {
const networkKey = Object.keys(networks)
.find(name => networks[name].id === networkId);
const network = networks[networkKey];
Expand All @@ -20,7 +21,7 @@ export default function getProviderUrl(networkId, projectId = infuraProjectId) {
networkName,
} = network;

return formatStrPattern(infuraProviderUrlPattern, {
return formatStrPattern(ws ? infuraWsProviderUrlPattern : infuraHttpProviderUrlPattern, {
networkName: networkName.toLowerCase(),
projectId,
});
Expand Down

0 comments on commit 68f4662

Please sign in to comment.