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

fix/improve network mgmt #152

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: “<arbitrum|goerli|development>”,
autoSwitchNetwork: <true|false> // optional, defaults to true
})
unicrowSdk.config(
defaultNetwork: “<chainId>”,
autoSwitchNetwork: <true|false> // optional, defaults to false
)
```

For more examples, check out the [SDK Tutorial](https://github.com/unicrowio/sdk-tutorial).
Expand Down
31 changes: 0 additions & 31 deletions src/config/getContractAddress.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/config/getContractsAddresses.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { setupNetwork as config } from "./setup";
export { getContractAddress } from "./getContractAddress";
export { getContractsAddresses } from "./getContractsAddresses";
export { MARKER } from "./marker";
27 changes: 9 additions & 18 deletions src/config/setup.ts
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 3 additions & 3 deletions src/core/approveArbitrator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnicrowArbitrator__factory } from "@unicrowio/ethers-types";

import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import {
ApproveArbitratorParsedPayload,
IApproveArbitrationTransactionCallbacks,
Expand Down Expand Up @@ -31,7 +31,7 @@ export const approveArbitrator = async (
): Promise<ApproveArbitratorParsedPayload> => {
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");
Expand All @@ -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(),
);

Expand Down
6 changes: 3 additions & 3 deletions src/core/approveSettlement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnicrowDispute__factory } from "@unicrowio/ethers-types";
import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import {
ApproveSettlementParsedPayload,
ISettlementApproveTransactionCallbacks,
Expand Down Expand Up @@ -31,7 +31,7 @@ export const approveSettlement = async (
): Promise<ApproveSettlementParsedPayload> => {
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");
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/core/arbitrate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnicrowArbitrator__factory } from "@unicrowio/ethers-types";
import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import {
ArbitrateParsedPayload,
IArbitrateTransactionCallbacks,
Expand Down Expand Up @@ -29,7 +29,7 @@ export const arbitrate = async (
): Promise<ArbitrateParsedPayload> => {
try {
callbacks && callbacks.connectingWallet && callbacks.connectingWallet();
const provider = await getWeb3Provider();
const provider = getWeb3Provider();

if (!provider) {
throw new Error("Error on Arbitrating. Account not connected");
Expand All @@ -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(),
);

Expand Down
6 changes: 3 additions & 3 deletions src/core/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getCurrentWalletAddress,
getWeb3Provider,
} from "../wallet";
import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import {
ChallengeParsedPayload,
IChallengeTransactionCallbacks,
Expand All @@ -24,7 +24,7 @@ export const challenge = async (
callbacks?: IChallengeTransactionCallbacks,
): Promise<ChallengeParsedPayload> => {
callbacks && callbacks.connectingWallet && callbacks.connectingWallet();
const provider = await getWeb3Provider();
const provider = getWeb3Provider();

if (!provider) {
throw new Error("Error on Challenge, Account Not connected");
Expand All @@ -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(),
);

Expand Down
6 changes: 3 additions & 3 deletions src/core/claim.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,7 +21,7 @@ export const claim = async (
callbacks?: IClaimTransactionCallbacks,
): Promise<ClaimParsedPayload> => {
callbacks && callbacks.connectingWallet && callbacks.connectingWallet();
const provider = await getWeb3Provider();
const provider = getWeb3Provider();

if (!provider) {
throw new Error("Error on Claiming, Account Not connected");
Expand All @@ -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(),
);

Expand Down
6 changes: 3 additions & 3 deletions src/core/claimMultiple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import { UnicrowClaim__factory } from "@unicrowio/ethers-types";
import {
MultipleClaimParsedPayload,
Expand Down Expand Up @@ -27,7 +27,7 @@ export const claimMultiple = async (
callbacks?: IClaimTransactionCallbacks,
): Promise<MultipleClaimParsedPayload> => {
callbacks && callbacks.connectingWallet && callbacks.connectingWallet();
const provider = await getWeb3Provider();
const provider = getWeb3Provider();

if (!provider) {
throw new Error("Error on Claiming, Account Not connected");
Expand All @@ -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(),
);

Expand Down
4 changes: 2 additions & 2 deletions src/core/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import { ETH_ADDRESS } from "../helpers";
export const getBalance = async (
tokenAddress: string | null = ETH_ADDRESS,
): Promise<bigint> => {
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);
Expand Down
6 changes: 3 additions & 3 deletions src/core/getEscrowData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import {
ADDRESS_ZERO,
consensus,
Expand Down Expand Up @@ -271,7 +271,7 @@ const parse = (escrowId: number, data: DataStructOutput): any => {
export const getEscrowData = async (
escrowId: number,
): Promise<IGetEscrowData> => {
const provider = await getWeb3Provider();
const provider = getWeb3Provider();

if (!provider) {
throw new Error("Error on Getting Escrow Data, Account Not connected");
Expand All @@ -280,7 +280,7 @@ export const getEscrowData = async (
await autoSwitchNetwork();

const Unicrow = Unicrow__factory.connect(
getContractAddress("unicrow"),
(await getContractsAddresses()).unicrow,
provider,
);

Expand Down
10 changes: 6 additions & 4 deletions src/core/getProtocolFee.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/core/getTokenInfo.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
18 changes: 0 additions & 18 deletions src/core/internal/getBrowserProvider.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/core/offerSettlement.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnicrowDispute__factory } from "@unicrowio/ethers-types";

import { getContractAddress } from "../config";
import { getContractsAddresses } from "../config";
import {
ISettlementOfferTransactionCallbacks,
OfferSettlementParsedPayload,
Expand Down Expand Up @@ -30,7 +30,7 @@ export const offerSettlement = async (
): Promise<OfferSettlementParsedPayload> => {
try {
callbacks && callbacks.connectingWallet && callbacks.connectingWallet();
const provider = await getWeb3Provider();
const provider = getWeb3Provider();

if (!provider) {
throw new Error("Error on Settlement, Account Not connected");
Expand All @@ -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(),
);

Expand Down
Loading
Loading