diff --git a/README.md b/README.md index 5bc2e43..fba1a50 100644 --- a/README.md +++ b/README.md @@ -74,17 +74,16 @@ unicrowSdk.ui.release(escrowId) unicrowSdk.ui.claim(escrowId) ``` -### Change network +### Default network and autoswitch -The SDK is by default configured to interact with the Arbitrum One network and asks the user to switch to it automatically when any contract-interacting functions are called. We currently support also Ethereum Goerli* and Unicrow’s private RPC. The automated switch can also be turned off (in such a case, an error is thrown). +To set a default network (for the supported networks, see `wallet/networks.ts`) and (optionally) request a switch when the wallet is connected to a different one: -\*We chose to deploy on Ethereum Goerli instead of Arbitrum Goerli because of larger support for "stablecoins", DEXes, etc. ```js -unicrowSdk.config({ - defaultNetwork: “”, - autoSwitchNetwork: // optional, defaults to true -}) +unicrowSdk.config( + defaultNetwork: “”, + autoSwitchNetwork: // optional, defaults to false +) ``` For more examples, check out the [SDK Tutorial](https://github.com/unicrowio/sdk-tutorial). diff --git a/src/config/getContractAddress.ts b/src/config/getContractAddress.ts deleted file mode 100644 index b295c98..0000000 --- a/src/config/getContractAddress.ts +++ /dev/null @@ -1,31 +0,0 @@ -const contracts = { - // Arbitrum mainnet Unicrow Contract Addresses - arbitrum: { - unicrow: "0x24e9ECC6c56dcD0C875fDF181FA3A4EEf3c5D5F0", - dispute: "0xab32831aA9bBFEB12F1BA7B74eBFf76e45944937", - arbitrator: "0xF1447b0fda912a6857B5d77d7cDE9663266896cf", - claim: "0x40F83551803051676eB7Cb0374176d03db247b97", - }, - // Private RPC development Unicrow Contract Addresses - development: { - unicrow: "0x229A9DEd51df3D9680A8f82a433b13eF7Cfc9978", - dispute: "0x2736A9A556c3259e527978bC249D519b85FB9097", - arbitrator: "0x07525a1F8B01e5806557e9472ba688bCa44BB655", - claim: "0xEc1080E3A2e0a1657107eb5D4b8c233c356eb02f", - }, -}; - -type tGetAddress = "unicrow" | "dispute" | "arbitrator" | "claim"; - -export const getContractAddress = (address: tGetAddress): string => { - const network = globalThis?.defaultNetwork?.name; - - const addressMap = { - unicrow: contracts[network]?.unicrow, - dispute: contracts[network]?.dispute, - arbitrator: contracts[network]?.arbitrator, - claim: contracts[network]?.claim, - }; - - return addressMap[address]; -}; diff --git a/src/config/getContractsAddresses.ts b/src/config/getContractsAddresses.ts new file mode 100644 index 0000000..6dbfcb0 --- /dev/null +++ b/src/config/getContractsAddresses.ts @@ -0,0 +1,14 @@ +import { networks, UnicrowNetwork } from "../wallet/networks"; +import { getNetwork } from "../wallet"; + +export const getContractsAddresses = async (): Promise< + UnicrowNetwork["addresses"] +> => { + let chainId = (await getNetwork())?.chainId; + if (chainId === null) throw new Error(`Wallet not connected.`); + + let network = networks.find((network) => network.chainId === chainId); + if (!network) throw new Error(`Network ${chainId} is not supported yet.`); + + return network.addresses; +}; diff --git a/src/config/index.ts b/src/config/index.ts index c2f6681..e2ab00a 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,3 +1,3 @@ export { setupNetwork as config } from "./setup"; -export { getContractAddress } from "./getContractAddress"; +export { getContractsAddresses } from "./getContractsAddresses"; export { MARKER } from "./marker"; diff --git a/src/config/setup.ts b/src/config/setup.ts index 3dbc050..c6d2d7b 100644 --- a/src/config/setup.ts +++ b/src/config/setup.ts @@ -1,23 +1,14 @@ -import { IConfig } from "../typing"; -import { networks as DefaultNetworks } from "../wallet/networks"; +import { networks } from "../wallet/networks"; -export const setupNetwork = ({ - defaultNetwork = "arbitrum", +export const setupNetwork = ( + chainId = BigInt(42161), autoSwitchNetwork = false, -}: IConfig) => { - globalThis.defaultNetwork = { - name: - defaultNetwork || - DefaultNetworks[defaultNetwork]?.chainName || - DefaultNetworks.arbitrum?.chainName, - displayName: - DefaultNetworks[defaultNetwork]?.displayName || - DefaultNetworks.arbitrum?.displayName, - chainId: defaultNetwork - ? DefaultNetworks[defaultNetwork].chainId - : DefaultNetworks.arbitrum.chainId, - }; +) => { + let network = networks.find((network) => network.chainId === chainId); + if (!network) throw new Error(`Network ${chainId} is not supported yet.`); + + globalThis.defaultNetwork = network; globalThis.autoSwitchNetwork = autoSwitchNetwork; }; -setupNetwork({}); +setupNetwork(); diff --git a/src/core/approveArbitrator.ts b/src/core/approveArbitrator.ts index f097dcd..1967bc6 100644 --- a/src/core/approveArbitrator.ts +++ b/src/core/approveArbitrator.ts @@ -1,6 +1,6 @@ import { UnicrowArbitrator__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { ApproveArbitratorParsedPayload, IApproveArbitrationTransactionCallbacks, @@ -31,7 +31,7 @@ export const approveArbitrator = async ( ): Promise => { try { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Approving the Arbiter. Account not connected"); @@ -43,7 +43,7 @@ export const approveArbitrator = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const crowArbitratorContract = UnicrowArbitrator__factory.connect( - getContractAddress("arbitrator"), + (await getContractsAddresses()).arbitrator, await provider.getSigner(), ); diff --git a/src/core/approveSettlement.ts b/src/core/approveSettlement.ts index 2e4a037..150eb39 100644 --- a/src/core/approveSettlement.ts +++ b/src/core/approveSettlement.ts @@ -1,5 +1,5 @@ import { UnicrowDispute__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { ApproveSettlementParsedPayload, ISettlementApproveTransactionCallbacks, @@ -31,7 +31,7 @@ export const approveSettlement = async ( ): Promise => { try { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Approve Offer, Account Not connected"); @@ -43,7 +43,7 @@ export const approveSettlement = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const CrowDisputeContract = UnicrowDispute__factory.connect( - getContractAddress("dispute"), + (await getContractsAddresses()).dispute, await provider.getSigner(), ); const approveOfferTx = await CrowDisputeContract.approveSettlement( diff --git a/src/core/arbitrate.ts b/src/core/arbitrate.ts index 29252ad..989952a 100644 --- a/src/core/arbitrate.ts +++ b/src/core/arbitrate.ts @@ -1,5 +1,5 @@ import { UnicrowArbitrator__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { ArbitrateParsedPayload, IArbitrateTransactionCallbacks, @@ -29,7 +29,7 @@ export const arbitrate = async ( ): Promise => { try { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Arbitrating. Account not connected"); @@ -41,7 +41,7 @@ export const arbitrate = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const crowArbitratorContract = UnicrowArbitrator__factory.connect( - getContractAddress("arbitrator"), + (await getContractsAddresses()).arbitrator, await provider.getSigner(), ); diff --git a/src/core/challenge.ts b/src/core/challenge.ts index ea5f0af..587f587 100644 --- a/src/core/challenge.ts +++ b/src/core/challenge.ts @@ -5,7 +5,7 @@ import { getCurrentWalletAddress, getWeb3Provider, } from "../wallet"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { ChallengeParsedPayload, IChallengeTransactionCallbacks, @@ -24,7 +24,7 @@ export const challenge = async ( callbacks?: IChallengeTransactionCallbacks, ): Promise => { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Challenge, Account Not connected"); @@ -36,7 +36,7 @@ export const challenge = async ( const walletAddress = await getCurrentWalletAddress(); callbacks && callbacks.connected && callbacks.connected(walletAddress); const smartContract = UnicrowDispute__factory.connect( - getContractAddress("dispute"), + (await getContractsAddresses()).dispute, await provider.getSigner(), ); diff --git a/src/core/claim.ts b/src/core/claim.ts index 681ab17..48140fa 100644 --- a/src/core/claim.ts +++ b/src/core/claim.ts @@ -1,4 +1,4 @@ -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { UnicrowClaim__factory } from "@unicrowio/ethers-types"; import { IClaimTransactionCallbacks, ClaimParsedPayload } from "../typing"; import { @@ -21,7 +21,7 @@ export const claim = async ( callbacks?: IClaimTransactionCallbacks, ): Promise => { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Claiming, Account Not connected"); @@ -33,7 +33,7 @@ export const claim = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const smartContract = UnicrowClaim__factory.connect( - getContractAddress("claim"), + (await getContractsAddresses()).claim, await provider.getSigner(), ); diff --git a/src/core/claimMultiple.ts b/src/core/claimMultiple.ts index cf92c87..89f8223 100644 --- a/src/core/claimMultiple.ts +++ b/src/core/claimMultiple.ts @@ -1,4 +1,4 @@ -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { UnicrowClaim__factory } from "@unicrowio/ethers-types"; import { MultipleClaimParsedPayload, @@ -27,7 +27,7 @@ export const claimMultiple = async ( callbacks?: IClaimTransactionCallbacks, ): Promise => { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Claiming, Account Not connected"); @@ -39,7 +39,7 @@ export const claimMultiple = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const smartContract = UnicrowClaim__factory.connect( - getContractAddress("claim"), + (await getContractsAddresses()).claim, await provider.getSigner(), ); diff --git a/src/core/getBalance.ts b/src/core/getBalance.ts index 63fc133..35a3447 100644 --- a/src/core/getBalance.ts +++ b/src/core/getBalance.ts @@ -15,14 +15,14 @@ import { ETH_ADDRESS } from "../helpers"; export const getBalance = async ( tokenAddress: string | null = ETH_ADDRESS, ): Promise => { - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); const walletAddress = await getCurrentWalletAddress(); if (!(provider && walletAddress)) { throw new Error("Error on Get Balance, Account Not connected"); } - autoSwitchNetwork(); + await autoSwitchNetwork(); if (tokenAddress === ETH_ADDRESS) { return provider.getBalance(walletAddress); diff --git a/src/core/getEscrowData.ts b/src/core/getEscrowData.ts index 9c3c196..c2c05f1 100644 --- a/src/core/getEscrowData.ts +++ b/src/core/getEscrowData.ts @@ -1,4 +1,4 @@ -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { ADDRESS_ZERO, consensus, @@ -271,7 +271,7 @@ const parse = (escrowId: number, data: DataStructOutput): any => { export const getEscrowData = async ( escrowId: number, ): Promise => { - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Getting Escrow Data, Account Not connected"); @@ -280,7 +280,7 @@ export const getEscrowData = async ( await autoSwitchNetwork(); const Unicrow = Unicrow__factory.connect( - getContractAddress("unicrow"), + (await getContractsAddresses()).unicrow, provider, ); diff --git a/src/core/getProtocolFee.ts b/src/core/getProtocolFee.ts index 00e0c41..0f8f263 100644 --- a/src/core/getProtocolFee.ts +++ b/src/core/getProtocolFee.ts @@ -1,7 +1,7 @@ import { Unicrow__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { bipsToPercentage } from "../helpers"; -import { getBrowserProvider } from "./internal/getBrowserProvider"; +import { getWeb3Provider } from "../wallet"; /** * Retrieves information about the protocol fee and returns its percentage. @@ -10,9 +10,11 @@ import { getBrowserProvider } from "./internal/getBrowserProvider"; * @returns The protocol fee in percentage */ export const getProtocolFee = async () => { + let provider = getWeb3Provider(); + const smartContract = Unicrow__factory.connect( - getContractAddress("unicrow"), - getBrowserProvider(), + (await getContractsAddresses()).unicrow, + provider, ); const fee = await smartContract.protocolFee(); diff --git a/src/core/getTokenInfo.ts b/src/core/getTokenInfo.ts index ea9372c..ebd04ec 100644 --- a/src/core/getTokenInfo.ts +++ b/src/core/getTokenInfo.ts @@ -1,11 +1,11 @@ import { ERC20__factory } from "@unicrowio/ethers-types"; import { ETH_ADDRESS, isSameAddress } from "../helpers"; import { IToken } from "../typing"; -import { getBrowserProvider } from "./internal/getBrowserProvider"; +import { getWeb3Provider } from "../wallet"; const fetchTokenInfo = async (tokenAddress: string) => { try { - const provider = getBrowserProvider(); + const provider = getWeb3Provider(); const token = ERC20__factory.connect(tokenAddress, provider); return Promise.all([token.symbol(), token.decimals()]).then((results) => ({ address: tokenAddress, diff --git a/src/core/internal/getBrowserProvider.ts b/src/core/internal/getBrowserProvider.ts deleted file mode 100644 index 0db493e..0000000 --- a/src/core/internal/getBrowserProvider.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { ethers } from "ethers"; - -/** - * Returns the user's provider - * - * @throws Error - * If account is not connected (=no provider given). - * @returns {ethers.providers.BrowserProvider} - */ -export const getBrowserProvider = () => { - const provider = new ethers.BrowserProvider(window.ethereum); - - if (!provider) { - throw new Error("Could not get BrowserProvider"); - } - - return provider; -}; diff --git a/src/core/offerSettlement.ts b/src/core/offerSettlement.ts index efb8243..8cc0d4c 100644 --- a/src/core/offerSettlement.ts +++ b/src/core/offerSettlement.ts @@ -1,6 +1,6 @@ import { UnicrowDispute__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { ISettlementOfferTransactionCallbacks, OfferSettlementParsedPayload, @@ -30,7 +30,7 @@ export const offerSettlement = async ( ): Promise => { try { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Settlement, Account Not connected"); @@ -42,7 +42,7 @@ export const offerSettlement = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const crowDisputeContract = UnicrowDispute__factory.connect( - getContractAddress("dispute"), + (await getContractsAddresses()).dispute, await provider.getSigner(), ); diff --git a/src/core/pay.ts b/src/core/pay.ts index 8f06ac0..204ff8a 100644 --- a/src/core/pay.ts +++ b/src/core/pay.ts @@ -6,14 +6,13 @@ import { validateParameters, parseAmount, } from "../helpers"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { IPaymentProps, IPayTransactionCallbacks, PayParsedPayload, } from "../typing"; import { getBalance } from "./getBalance"; -import { getTokenInfo } from "../core/getTokenInfo"; import { errorHandler } from "./internal/errorHandler"; import { getWeb3Provider, @@ -147,7 +146,7 @@ export const pay = async ( callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Wallet not connected"); } @@ -182,7 +181,7 @@ export const pay = async ( throw new Error(`Insufficient balance: ${balance} < ${solidityAmount}`); } - const unicrowAddress = getContractAddress("unicrow"); + const unicrowAddress = (await getContractsAddresses()).unicrow; if (tokenAddress != ETH_ADDRESS) { const token = ERC20__factory.connect(tokenAddress, providerSigner); diff --git a/src/core/proposeArbitrator.ts b/src/core/proposeArbitrator.ts index d4e3664..73c7bfa 100644 --- a/src/core/proposeArbitrator.ts +++ b/src/core/proposeArbitrator.ts @@ -1,5 +1,5 @@ import { UnicrowArbitrator__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { IProposeArbitrationTransactionCallbacks, ProposalArbitratorParsedPayload, @@ -29,7 +29,7 @@ export const proposeArbitrator = async ( validateAddresses({ arbitrator }); callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Adding Arbitrator. Account not connected"); @@ -38,7 +38,7 @@ export const proposeArbitrator = async ( await autoSwitchNetwork(callbacks); const crowArbitratorContract = UnicrowArbitrator__factory.connect( - getContractAddress("arbitrator"), + (await getContractsAddresses()).arbitrator, await provider.getSigner(), ); diff --git a/src/core/refund.ts b/src/core/refund.ts index 3139a6e..19c900e 100644 --- a/src/core/refund.ts +++ b/src/core/refund.ts @@ -1,4 +1,4 @@ -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { Unicrow__factory } from "@unicrowio/ethers-types"; import { IRefundTransactionCallbacks } from "../typing"; import { @@ -21,7 +21,7 @@ export const refund = async ( callbacks?: IRefundTransactionCallbacks, ): Promise => { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Refund, Account Not connected"); @@ -33,7 +33,7 @@ export const refund = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const smartContract = Unicrow__factory.connect( - getContractAddress("unicrow"), + (await getContractsAddresses()).unicrow, await provider.getSigner(), ); diff --git a/src/core/release.ts b/src/core/release.ts index 2fb3253..5760d7f 100644 --- a/src/core/release.ts +++ b/src/core/release.ts @@ -1,5 +1,5 @@ import { Unicrow__factory } from "@unicrowio/ethers-types"; -import { getContractAddress } from "../config"; +import { getContractsAddresses } from "../config"; import { IReleaseTransactionCallbacks, ReleaseParsedPayload } from "../typing"; import { autoSwitchNetwork, @@ -21,7 +21,7 @@ export const release = async ( callbacks?: IReleaseTransactionCallbacks, ): Promise => { callbacks && callbacks.connectingWallet && callbacks.connectingWallet(); - const provider = await getWeb3Provider(); + const provider = getWeb3Provider(); if (!provider) { throw new Error("Error on Release, Account Not connected"); @@ -33,7 +33,7 @@ export const release = async ( callbacks && callbacks.connected && callbacks.connected(walletAddress); const Unicrow = Unicrow__factory.connect( - getContractAddress("unicrow"), + (await getContractsAddresses()).unicrow, await provider.getSigner(), ); diff --git a/src/helpers/constants.ts b/src/helpers/constants.ts index 99e56aa..71481e0 100644 --- a/src/helpers/constants.ts +++ b/src/helpers/constants.ts @@ -30,13 +30,4 @@ export enum split { export const BUYER = "buyer"; export const SELLER = "seller"; -export const CHAIN_ID = { - arbitrumOne: BigInt(42161), - mainnet: BigInt(1), - arbitrumSepolia: BigInt(421614), - sepolia: BigInt(11155111), - goerli: BigInt(5), - development: BigInt(5777), -}; - export const metamaskUrl = "https://metamask.io/download/"; diff --git a/src/helpers/ensToAddress.ts b/src/helpers/ensToAddress.ts index 4304651..78f1d38 100644 --- a/src/helpers/ensToAddress.ts +++ b/src/helpers/ensToAddress.ts @@ -1,7 +1,7 @@ -import { getBrowserProvider } from "../core/internal/getBrowserProvider"; +import { getWeb3Provider } from "../wallet"; export const ensToAddress = async (ensName: string) => { - const provider = getBrowserProvider(); + const provider = getWeb3Provider(); const address = await provider.resolveName(ensName); return address; }; diff --git a/src/typing/global.d.ts b/src/typing/global.d.ts index 22d9333..2574c63 100644 --- a/src/typing/global.d.ts +++ b/src/typing/global.d.ts @@ -1,4 +1,4 @@ -import { DefaultNetwork } from "config/setup"; +import { UnicrowNetwork } from "../wallet/networks"; declare global { interface Window { @@ -6,18 +6,8 @@ declare global { } } -interface Network { - name: DefaultNetwork; - chainId: number; - rpcUrl: string; -} - declare module globalThis { - var arbitrum: string | undefined; - var goerli: string | undefined; - var development: string | undefined; - var mainnetRPCUrl: string | undefined; - var defaultNetwork: Network = {}; + var defaultNetwork: UnicrowNetwork[]; var autoSwitchNetwork: boolean; } diff --git a/src/typing/index.ts b/src/typing/index.ts index 1753d7c..b1c9c9e 100644 --- a/src/typing/index.ts +++ b/src/typing/index.ts @@ -1340,20 +1340,4 @@ export interface tSplits { export type CalculateFunction = CalculateAmountsInput; -export type DefaultNetwork = "arbitrum" | "goerli" | "development"; - -export type Network = { - rpcUrl: string; -}; - -export type Networks = { - [key in DefaultNetwork]?: Network; -}; - -export interface IConfig { - networks?: Networks; - defaultNetwork?: DefaultNetwork; - autoSwitchNetwork?: boolean; -} - export default {}; diff --git a/src/ui/internal/hooks/useNetworkCheck.tsx b/src/ui/internal/hooks/useNetworkCheck.tsx index c8232a0..f03640a 100644 --- a/src/ui/internal/hooks/useNetworkCheck.tsx +++ b/src/ui/internal/hooks/useNetworkCheck.tsx @@ -7,7 +7,6 @@ import { switchNetwork, } from "wallet"; import { ModalError } from "ui/internal/components/ModalError"; -import { DefaultNetwork } from "typing"; import { metamaskUrl } from "../../../helpers/constants"; export const useNetworkCheck = () => { @@ -38,7 +37,7 @@ export const useNetworkCheck = () => { const onNetworkSwitch = React.useCallback(async () => { setIsCorrectNetwork(await isCorrectNetworkConnected()); if (!isCorrectNetwork) { - await switchNetwork(globalThis.defaultNetwork.name as DefaultNetwork); + await switchNetwork(globalThis.defaultNetwork.chainId); } }, [isCorrectNetwork]); diff --git a/src/wallet/index.ts b/src/wallet/index.ts index ff016e1..bcbc134 100644 --- a/src/wallet/index.ts +++ b/src/wallet/index.ts @@ -1,8 +1,7 @@ import EventEmitter from "events"; import { ethers, BrowserProvider } from "ethers"; import { networks, UnicrowNetwork } from "./networks"; -import { CHAIN_ID } from "../helpers"; -import { DefaultNetwork, IGenericTransactionCallbacks } from "typing"; +import { IGenericTransactionCallbacks } from "typing"; import { config } from "../config"; import { toast } from "../ui/internal/notification/toast"; @@ -90,22 +89,23 @@ export const connect = async (): Promise => { /** * Asks user's web3 wallet to switch to a selected network * - * @param name - Name of one of the configured networks ('arbitrum', 'development', or 'goerli' in standard SDK installation) + * @param chainId - chainId of one of the supported networks (see `networks.ts`) * @returns Name of the network that the wallet was switched to. * @throws Error if no wallet is present or the user rejected adding or switching to the network */ -export const switchNetwork = async (name: DefaultNetwork) => { +export const switchNetwork = async (chainId: bigint) => { if (!isWeb3WalletInstalled()) throw Error("No wallet installed"); - const { chainName, chainId, nativeCurrency, blockExplorerUrls } = - networks[name]; + + let network = networks.find((network) => network.chainId === chainId); + if (!network) throw new Error(`Network ${chainId} is not supported yet.`); registerAccountChangedListener(); const addParams: any = { chainId: ethers.toQuantity(chainId), - chainName, - nativeCurrency, - blockExplorerUrls, + chainName: network.chainName, + nativeCurrency: network.nativeCurrency, + blockExplorerUrls: network.blockExplorerUrls, }; const switchParams: any = { chainId: addParams.chainId }; @@ -143,10 +143,10 @@ export const switchNetwork = async (name: DefaultNetwork) => { const connected = await getNetwork(); if (connected.chainId === chainId) { - config({ defaultNetwork: name }); + config(connected.chainId); } - return name; + return network; }; /** @@ -164,7 +164,7 @@ export const autoSwitchNetwork = async ( if (!isCorrectNetwork) { if (globalThis.autoSwitchNetwork || force) { - await switchNetwork(globalThis.defaultNetwork.name); + await switchNetwork(globalThis.defaultNetwork.chainId); callbacks && callbacks.switchingNetwork && callbacks.switchingNetwork(); } else { throw new Error("Unsupported network"); @@ -173,28 +173,14 @@ export const autoSwitchNetwork = async ( }; /** - * Get parameters of the network that the wallet is connected to + * Get the network the wallet is currently connected to * - * @returns Network info of where the wallet switched to + * @returns Network or null if no provider is available * @throws Error if no wallet is installed or if the user rejected adding or switching to the network */ export const getNetwork = async (): Promise => { - const provider = await getWeb3Provider(); - - let network; - - if (provider !== null) { - network = await provider.getNetwork(); - - if (network.chainId === CHAIN_ID.development) { - network = { - chainId: network.chainId, - name: "development", - }; - } - } - - return network; + const provider = getWeb3Provider(); + return provider !== null ? await provider.getNetwork() : null; }; /** @@ -202,9 +188,7 @@ export const getNetwork = async (): Promise => { * * @returns List and parameters of all configured networks */ -export const getSupportedNetworks: () => { - [name: string]: UnicrowNetwork; -} = () => networks; +export const getSupportedNetworks: () => UnicrowNetwork[] = () => networks; /** * Checks, based on chainId comparison, if the wallet is connected to the default network @@ -281,7 +265,7 @@ export const stopListeningNetwork = () => { _onChangeNetworkCallbacks = []; }; -export const getWeb3Provider = async (): Promise => { +export const getWeb3Provider = (): BrowserProvider | null => { return isWeb3WalletInstalled() ? new BrowserProvider(window.ethereum) : null; }; diff --git a/src/wallet/networks.ts b/src/wallet/networks.ts index 90da14f..9fb7586 100644 --- a/src/wallet/networks.ts +++ b/src/wallet/networks.ts @@ -1,5 +1,3 @@ -import { CHAIN_ID } from "../helpers"; - export interface UnicrowNetwork { chainId: bigint; chainName: string; @@ -9,12 +7,19 @@ export interface UnicrowNetwork { symbol: string; decimals: number; }; - blockExplorerUrls?: string[]; + blockExplorerUrls: string[]; + addresses: { + unicrow: string; + dispute: string; + arbitrator: string; + claim: string; + }; + live: boolean; } -export const networks: { [name: string]: UnicrowNetwork } = { - arbitrum: { - chainId: CHAIN_ID.arbitrumOne, +export const networks: UnicrowNetwork[] = [ + { + chainId: BigInt(42161), chainName: "Arbitrum One", displayName: "Arbitrum", nativeCurrency: { @@ -23,9 +28,16 @@ export const networks: { [name: string]: UnicrowNetwork } = { decimals: 18, }, blockExplorerUrls: ["https://arbiscan.io/"], + addresses: { + unicrow: "0x24e9ECC6c56dcD0C875fDF181FA3A4EEf3c5D5F0", + dispute: "0xab32831aA9bBFEB12F1BA7B74eBFf76e45944937", + arbitrator: "0xF1447b0fda912a6857B5d77d7cDE9663266896cf", + claim: "0x40F83551803051676eB7Cb0374176d03db247b97", + }, + live: true, }, - mainnet: { - chainId: CHAIN_ID.mainnet, + { + chainId: BigInt(1), chainName: "Ethereum", displayName: "Ethereum Mainnet", nativeCurrency: { @@ -34,9 +46,17 @@ export const networks: { [name: string]: UnicrowNetwork } = { decimals: 18, }, blockExplorerUrls: ["https://etherscan.io/"], + // TODO - Update after deployment + addresses: { + unicrow: "", + dispute: "", + arbitrator: "", + claim: "", + }, + live: false, }, - arbitrumSepolia: { - chainId: CHAIN_ID.arbitrumSepolia, + { + chainId: BigInt(421614), chainName: "Arbitrum Sepolia Testnet", displayName: "Arbitrum Sepolia Testnet", nativeCurrency: { @@ -44,10 +64,17 @@ export const networks: { [name: string]: UnicrowNetwork } = { symbol: "ETH", decimals: 18, }, + addresses: { + unicrow: "0x0fD5d4bbdf522E5daA11dcd6557D542c4573011B", + dispute: "0x0BBc45602129cc57ED70d11a2B8a0e160700C7dB", + arbitrator: "0x41415F717351A22DeBD6B3E7382D0BCf74201B8a", + claim: "0x67dBfb5117296a354B464f4591Da570cA96Bc4Fb", + }, blockExplorerUrls: ["https://sepolia.arbiscan.io/"], + live: true, }, - sepolia: { - chainId: CHAIN_ID.sepolia, + { + chainId: BigInt(11155111), chainName: "Ethereum Sepolia Testnet", displayName: "Ehereum Sepolia Testnet", nativeCurrency: { @@ -55,27 +82,13 @@ export const networks: { [name: string]: UnicrowNetwork } = { symbol: "ETH", decimals: 18, }, - blockExplorerUrls: ["https://sepolia.etherscan.io/"], - }, - goerli: { - chainId: CHAIN_ID.goerli, - chainName: "Goerli", - displayName: "Ethereum Goerli Testnet", - nativeCurrency: { - name: "Görli Ether", - symbol: "ETH", - decimals: 18, - }, - blockExplorerUrls: ["https://goerli.etherscan.io"], - }, - development: { - chainId: CHAIN_ID.development, - chainName: "Unicrow Testnet", - displayName: "Unicrow Testnet", - nativeCurrency: { - name: "ETH", - symbol: "ETH", - decimals: 18, + addresses: { + unicrow: "0xEC8eaCfC2Dd1614b7182676A118088a204F69b86", + dispute: "0x335ba877387646815cb189f9883dF98aa0913EAF", + arbitrator: "0x310Da0C6c224C14168e7bBEd632797A79B9eff95", + claim: "0xAF6eC0acE0Ac7CB7865313523cAE83Cfaaa788e0", }, + blockExplorerUrls: ["https://sepolia.etherscan.io/"], + live: true, }, -}; +];