Skip to content

Commit

Permalink
Use single instance of json rpc provider
Browse files Browse the repository at this point in the history
This *should* work just fine.
  • Loading branch information
mondoreale committed Apr 11, 2024
1 parent 51c1fa6 commit a742df3
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 90 deletions.
2 changes: 0 additions & 2 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export enum PermissionType {
Grant,
}

export const JSON_RPC_URL = 'https://polygon-rpc.com'

export const Layer = {
Modal: uniqueId('modals-'),
Toast: uniqueId('toasts-'),
Expand Down
8 changes: 3 additions & 5 deletions src/features/delegation/helpers/lookup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { JSON_RPC_URL, ZeroAddress } from '$/consts'
import { ZeroAddress } from '$/consts'
import { DelegationAction } from '$/features/delegation'
import { selectMainAccount } from '$/hooks/useMainAccount'
import { Address, OptionalAddress } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import getDelegatedAccessRegistry from '$/utils/getDelegatedAccessRegistry'
import handleError from '$/utils/handleError'
import isSameAddress from '$/utils/isSameAddress'
import { providers } from 'ethers'
import { call, delay, put, race, select } from 'redux-saga/effects'

export default function lookup({
Expand All @@ -27,9 +27,7 @@ export default function lookup({
throw new Error('Timeout')
}),
call(function* () {
const contract = getDelegatedAccessRegistry(
new providers.JsonRpcProvider(JSON_RPC_URL)
)
const contract = getDelegatedAccessRegistry(getJsonRpcProvider())

let [main]: [Address] = yield contract.functions.getMainWalletFor(delegated)

Expand Down
15 changes: 7 additions & 8 deletions src/features/misc/helpers/fetchTokenMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { abi as ERC1155abi } from '$/contracts/tokens/ERC1155Token.sol/ERC1155.json'
import { abi as ERC20abi } from '$/contracts/tokens/ERC20Token.sol/ERC20.json'
import { abi as ERC721abi } from '$/contracts/tokens/ERC721Token.sol/ERC721.json'
import { abi as ERC777abi } from '$/contracts/tokens/ERC777Token.sol/ERC777.json'
import { abi as ERC1155abi } from '$/contracts/tokens/ERC1155Token.sol/ERC1155.json'
import { BigNumber, Contract, providers } from 'ethers'
import { Address } from '$/types'
import { Erc1155, Erc20, Erc721, Erc777 } from '$/types'
import { TokenStandard } from '$/features/tokenGatedRooms/types'
import { MiscAction } from '$/features/misc'
import { call } from 'redux-saga/effects'
import { JSON_RPC_URL } from '$/consts'
import { TokenStandard } from '$/features/tokenGatedRooms/types'
import { Address, Erc1155, Erc20, Erc721, Erc777 } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import { JsonRpcProvider } from '@ethersproject/providers'
import { BigNumber, Contract } from 'ethers'
import { call } from 'redux-saga/effects'

function getURIs(tokenIds: string[], contract: Contract) {
return call(function* () {
Expand Down Expand Up @@ -102,7 +101,7 @@ export default function fetchTokenMetadata({
tokenIds,
tokenStandard,
}: ReturnType<typeof MiscAction.fetchTokenMetadata>['payload']) {
const provider = new providers.JsonRpcProvider(JSON_RPC_URL)
const provider = getJsonRpcProvider()

switch (tokenStandard) {
case TokenStandard.ERC1155:
Expand Down
16 changes: 8 additions & 8 deletions src/features/misc/helpers/fetchTokenStandard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ToastType } from '$/components/Toast'
import { abi as erc165abi } from '$/contracts/tokens/ERC165.json'
import { abi as erc20abi } from '$/contracts/tokens/ERC20Token.sol/ERC20.json'
import { abi as erc777abi } from '$/contracts/tokens/ERC777Token.sol/ERC777.json'
import { Flag } from '$/features/flag/types'
import { MiscAction } from '$/features/misc'
import retoast from '$/features/misc/helpers/retoast'
import { InterfaceId, TokenStandard } from '$/features/tokenGatedRooms/types'
import { selectTokenStandard } from '$/hooks/useTokenStandard'
import { getJsonRpcProvider } from '$/utils'
import { BigNumber, Contract } from 'ethers'
import { call, cancelled, put, select } from 'redux-saga/effects'
import { Flag } from '$/features/flag/types'
import { BigNumber, Contract, providers } from 'ethers'
import { abi as erc20abi } from '$/contracts/tokens/ERC20Token.sol/ERC20.json'
import { abi as erc165abi } from '$/contracts/tokens/ERC165.json'
import { abi as erc777abi } from '$/contracts/tokens/ERC777Token.sol/ERC777.json'
import retoast from '$/features/misc/helpers/retoast'
import { JSON_RPC_URL } from '$/consts'

export default function fetchTokenStandard({
address,
Expand Down Expand Up @@ -37,7 +37,7 @@ export default function fetchTokenStandard({
})
}

const provider = new providers.JsonRpcProvider(JSON_RPC_URL)
const provider = getJsonRpcProvider()

const contract = new Contract(address, erc165abi, provider)

Expand Down
10 changes: 5 additions & 5 deletions src/features/permissions/helpers/removeMember.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ToastType } from '$/components/Toast'
import { JSON_RPC_URL, ZeroAddress } from '$/consts'
import { PermissionsAction } from '$/features/permissions'
import { ZeroAddress } from '$/consts'
import { MiscAction } from '$/features/misc'
import { PermissionsAction } from '$/features/permissions'
import { getJsonRpcProvider } from '$/utils'
import getDelegatedAccessRegistry from '$/utils/getDelegatedAccessRegistry'
import getDisplayUsername from '$/utils/getDisplayUsername'
import handleError from '$/utils/handleError'
import i18n from '$/utils/i18n'
import isSameAddress from '$/utils/isSameAddress'
import setMultiplePermissions from '$/utils/setMultiplePermissions'
import { providers } from 'ethers'
import { call, put } from 'redux-saga/effects'
import type { UserPermissionAssignment } from '@streamr/sdk'
import { call, put } from 'redux-saga/effects'

export default function removeMember({
roomId,
Expand All @@ -21,7 +21,7 @@ export default function removeMember({
const displayName: string = yield getDisplayUsername(member)

try {
const contract = getDelegatedAccessRegistry(new providers.JsonRpcProvider(JSON_RPC_URL))
const contract = getDelegatedAccessRegistry(getJsonRpcProvider())

const [delegatedWallet]: string[] = yield contract.functions.mainToDelegatedWallets(
member
Expand Down
42 changes: 20 additions & 22 deletions src/features/room/helpers/joinRoom.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { TokenStandard } from '$/features/tokenGatedRooms/types'
import { Address } from '$/types'
import delegationPreflight from '$/utils/delegationPreflight'
import getJoinPolicyRegistry from '$/utils/getJoinPolicyRegistry'
import handleError from '$/utils/handleError'
import { call, cancelled, put } from 'redux-saga/effects'
import { BigNumber, Contract, providers } from 'ethers'
import { ToastType } from '$/components/Toast'
import { abi as ERC1155JoinPolicyAbi } from '$/contracts/JoinPolicies/ERC1155JoinPolicy.sol/ERC1155JoinPolicy.json'
import { abi as ERC20JoinPolicyAbi } from '$/contracts/JoinPolicies/ERC20JoinPolicy.sol/ERC20JoinPolicy.json'
import { abi as ERC721JoinPolicyAbi } from '$/contracts/JoinPolicies/ERC721JoinPolicy.sol/ERC721JoinPolicy.json'
import { abi as ERC777JoinPolicyAbi } from '$/contracts/JoinPolicies/ERC777JoinPolicy.sol/ERC777JoinPolicy.json'
import { abi as ERC1155JoinPolicyAbi } from '$/contracts/JoinPolicies/ERC1155JoinPolicy.sol/ERC1155JoinPolicy.json'
import { ToastType } from '$/components/Toast'
import StreamrClient, { Stream } from '@streamr/sdk'
import getRoomMetadata from '$/utils/getRoomMetadata'
import retoast, { RetoastController } from '$/features/misc/helpers/retoast'
import { PermissionsAction } from '$/features/permissions'
import { RoomAction } from '$/features/room'
import waitForPermissions from '$/utils/waitForPermissions'
import { TokenStandard } from '$/features/tokenGatedRooms/types'
import { Address } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import delegationPreflight from '$/utils/delegationPreflight'
import getJoinPolicyRegistry from '$/utils/getJoinPolicyRegistry'
import getRoomMetadata from '$/utils/getRoomMetadata'
import getSigner from '$/utils/getSigner'
import getTransactionalClient from '$/utils/getTransactionalClient'
import getWalletProvider from '$/utils/getWalletProvider'
import handleError from '$/utils/handleError'
import i18n from '$/utils/i18n'
import isSameAddress from '$/utils/isSameAddress'
import tokenIdPreflight from '$/utils/tokenIdPreflight'
import recover from '$/utils/recover'
import i18n from '$/utils/i18n'
import getWalletProvider from '$/utils/getWalletProvider'
import getTransactionalClient from '$/utils/getTransactionalClient'
import retoast, { RetoastController } from '$/features/misc/helpers/retoast'
import getSigner from '$/utils/getSigner'
import { JSON_RPC_URL } from '$/consts'
import tokenIdPreflight from '$/utils/tokenIdPreflight'
import waitForPermissions from '$/utils/waitForPermissions'
import StreamrClient, { Stream } from '@streamr/sdk'
import { BigNumber, Contract } from 'ethers'
import { call, cancelled, put } from 'redux-saga/effects'

const Abi = {
[TokenStandard.ERC1155]: ERC1155JoinPolicyAbi,
Expand Down Expand Up @@ -83,9 +83,7 @@ export default function joinRoom(

const delegatedAccount: Address = yield delegationPreflight(requester)

const policyRegistry = getJoinPolicyRegistry(
new providers.JsonRpcProvider(JSON_RPC_URL)
)
const policyRegistry = getJoinPolicyRegistry(getJsonRpcProvider())

const policyAddress: string = yield policyRegistry.getPolicy(
tokenAddress,
Expand Down
44 changes: 21 additions & 23 deletions src/features/tokenGatedRooms/helpers/createTokenGatePolicy.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import { ToastType } from '$/components/Toast'
import { RoomId } from '$/features/room/types'
import { TokenStandard, TokenType } from '$/features/tokenGatedRooms/types'
import { WalletAction } from '$/features/wallet'
import { Address } from '$/types'
import handleError from '$/utils/handleError'
import preflight from '$/utils/preflight'
import { Contract, providers, BigNumber } from 'ethers'
import { call, cancelled, race, retry, spawn, take } from 'redux-saga/effects'
import { StreamPermission } from '@streamr/sdk'
import { abi as erc20abi } from '$/contracts/Factories/ERC20PolicyFactory.sol/ERC20PolicyFactory.json'
import { abi as erc721abi } from '$/contracts/Factories/ERC721PolicyFactory.sol/ERC721PolicyFactory.json'
import { abi as erc777abi } from '$/contracts/Factories/ERC777PolicyFactory.sol/ERC777PolicyFactory.json'
import { abi as erc1155abi } from '$/contracts/Factories/ERC1155PolicyFactory.sol/ERC1155PolicyFactory.json'
import {
ERC1155PolicyFactoryAddress,
ERC20PolicyFactoryAddress,
ERC721PolicyFactoryAddress,
ERC777PolicyFactoryAddress,
JSON_RPC_URL,
PermissionType,
ZeroAddress,
} from '$/consts'
import { abi as erc1155abi } from '$/contracts/Factories/ERC1155PolicyFactory.sol/ERC1155PolicyFactory.json'
import { abi as erc20abi } from '$/contracts/Factories/ERC20PolicyFactory.sol/ERC20PolicyFactory.json'
import { abi as erc721abi } from '$/contracts/Factories/ERC721PolicyFactory.sol/ERC721PolicyFactory.json'
import { abi as erc777abi } from '$/contracts/Factories/ERC777PolicyFactory.sol/ERC777PolicyFactory.json'
import retoast from '$/features/misc/helpers/retoast'
import { RoomId } from '$/features/room/types'
import { TokenStandard, TokenType } from '$/features/tokenGatedRooms/types'
import { WalletAction } from '$/features/wallet'
import { Address } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import getJoinPolicyRegistry from '$/utils/getJoinPolicyRegistry'
import setMultiplePermissions from '$/utils/setMultiplePermissions'
import { ZeroAddress } from '$/consts'
import getSigner from '$/utils/getSigner'
import getWalletProvider from '$/utils/getWalletProvider'
import handleError from '$/utils/handleError'
import i18n from '$/utils/i18n'
import isSameAddress from '$/utils/isSameAddress'
import preflight from '$/utils/preflight'
import recover from '$/utils/recover'
import i18n from '$/utils/i18n'
import getWalletProvider from '$/utils/getWalletProvider'
import retoast from '$/features/misc/helpers/retoast'
import getSigner from '$/utils/getSigner'
import setMultiplePermissions from '$/utils/setMultiplePermissions'
import { StreamPermission } from '@streamr/sdk'
import { BigNumber, Contract } from 'ethers'
import { call, cancelled, race, retry, spawn, take } from 'redux-saga/effects'

const Factory: Record<
TokenStandard,
Expand Down Expand Up @@ -122,9 +122,7 @@ export default function createTokenGatePolicy({
}
)

const policyRegistry = getJoinPolicyRegistry(
new providers.JsonRpcProvider(JSON_RPC_URL)
)
const policyRegistry = getJoinPolicyRegistry(getJsonRpcProvider())

let policyAddress: Address = ZeroAddress

Expand Down
5 changes: 2 additions & 3 deletions src/utils/getAccountType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { JSON_RPC_URL } from '$/consts'
import { Address } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import getDelegatedAccessRegistry from '$/utils/getDelegatedAccessRegistry'
import { providers } from 'ethers'
import { call } from 'redux-saga/effects'

export enum AccountType {
Expand All @@ -14,7 +13,7 @@ export default function* getAccountType(account: Address) {
let type: AccountType | undefined

yield call(function* () {
const contract = getDelegatedAccessRegistry(new providers.JsonRpcProvider(JSON_RPC_URL))
const contract = getDelegatedAccessRegistry(getJsonRpcProvider())

try {
const [metamaskAccount]: boolean[] = yield contract.functions.isMainWallet(account)
Expand Down
15 changes: 13 additions & 2 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JSON_RPC_URL, StreamRegistryAddress } from '$/consts'
import { StreamRegistryAddress } from '$/consts'
import { JsonRpcProvider } from '@ethersproject/providers'
import { Contract, providers } from 'ethers'
import { StreamRegistryV3 } from '../contracts/StreamRegistryV3.sol/StreamRegistryV3'
import { abi } from '../contracts/StreamRegistryV3.sol/StreamRegistryV3.json'
Expand All @@ -10,9 +11,19 @@ export function getStreamRegistry() {
streamRegistry = new Contract(
StreamRegistryAddress,
abi,
new providers.JsonRpcProvider(JSON_RPC_URL)
getJsonRpcProvider()
) as StreamRegistryV3
}

return streamRegistry
}

let jsonRpcProvider: undefined | JsonRpcProvider

export function getJsonRpcProvider() {
if (!jsonRpcProvider) {
jsonRpcProvider = new providers.JsonRpcProvider('https://polygon-rpc.com')
}

return jsonRpcProvider
}
5 changes: 2 additions & 3 deletions src/utils/isAuthorizedDelegatedAccount.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { JSON_RPC_URL } from '$/consts'
import { Address } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import getDelegatedAccessRegistry from '$/utils/getDelegatedAccessRegistry'
import { providers } from 'ethers'

export default async function isAuthorizedDelegatedAccount(
metamaskAccount: Address,
delegatedAccount: Address
): Promise<boolean> {
const contract = getDelegatedAccessRegistry(new providers.JsonRpcProvider(JSON_RPC_URL))
const contract = getDelegatedAccessRegistry(getJsonRpcProvider())

const [isAuthorized]: boolean[] = await contract.functions.isUserAuthorized(
metamaskAccount,
Expand Down
16 changes: 7 additions & 9 deletions src/utils/preflight.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { BigNumber, providers } from 'ethers'
import { Address } from '$/types'
import networkPreflight from './networkPreflight'
import { ToastType } from '$/components/Toast'
import InsufficientFundsError from '$/errors/InsufficientFundsError'
import { call, put } from 'redux-saga/effects'
import { JSON_RPC_URL } from '$/consts'
import { MiscAction } from '$/features/misc'
import { ToastType } from '$/components/Toast'
import { Address } from '$/types'
import { getJsonRpcProvider } from '$/utils'
import { BigNumber } from 'ethers'
import { call, put } from 'redux-saga/effects'
import networkPreflight from './networkPreflight'

export default function preflight(account: Address) {
return call(function* () {
try {
yield networkPreflight()

const balance: BigNumber = yield new providers.JsonRpcProvider(JSON_RPC_URL).getBalance(
account
)
const balance: BigNumber = yield getJsonRpcProvider().getBalance(account)

if (balance.eq(0)) {
throw new InsufficientFundsError()
Expand Down

0 comments on commit a742df3

Please sign in to comment.