Skip to content

Commit

Permalink
harden network loading
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Feb 2, 2023
1 parent ab6deff commit 3e8ac36
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/account/get-balances.ts
Expand Up @@ -32,11 +32,11 @@ export const getBalances = async (masa: Masa, address?: string) => {
// ETH
masa.config.wallet.provider.getBalance(addressToLoad),
// MASA
loadBalance(addressToLoad, contractAddresses.MASA),
loadBalance(addressToLoad, contractAddresses?.MASA),
// USDC
loadBalance(addressToLoad, contractAddresses.USDC),
loadBalance(addressToLoad, contractAddresses?.USDC),
// WETH
loadBalance(addressToLoad, contractAddresses.WETH),
loadBalance(addressToLoad, contractAddresses?.WETH),
// SBI
masa.contracts.instances.SoulboundIdentityContract.balanceOf(addressToLoad),
// MSN
Expand Down
23 changes: 11 additions & 12 deletions src/contracts/addresses.ts
@@ -1,20 +1,19 @@
import { alfajores, bsctest, goerli, mainnet, mumbai } from "./networks";
import { NetworkName } from "../interface";

export interface Addresses {
[index: string]: {
USDC?: string;
WETH?: string;
MASA?: string;
SoulboundIdentityAddress?: string;
SoulboundCreditScoreAddress?: string;
SoulNameAddress?: string;
SoulStoreAddress?: string;
SoulLinkerAddress?: string;
SoulboundGreenAddress?: string;
};
USDC?: string;
WETH?: string;
MASA?: string;
SoulboundIdentityAddress?: string;
SoulboundCreditScoreAddress?: string;
SoulNameAddress?: string;
SoulStoreAddress?: string;
SoulLinkerAddress?: string;
SoulboundGreenAddress?: string;
}

export const addresses: Addresses = {
export const addresses: Partial<{ [key in NetworkName]: Addresses }> = {
// eth
goerli,
mainnet,
Expand Down
23 changes: 14 additions & 9 deletions src/contracts/load-Identity-contracts.ts
@@ -1,24 +1,28 @@
import {
SoulboundGreen__factory,
SoulboundCreditScore__factory,
SoulboundGreen__factory,
SoulboundIdentity__factory,
SoulLinker__factory,
SoulName__factory,
SoulStore__factory,
} from "@masa-finance/masa-contracts-identity";
import { ethers } from "ethers";
import { IIdentityContracts } from "../interface";
import { IIdentityContracts, NetworkName } from "../interface";
import { addresses } from "./addresses";

export interface LoadContractArgs {
provider?: ethers.providers.Provider;
network?: string;
network?: NetworkName;
}

export const loadIdentityContracts = ({
provider,
network = "goerli",
}: LoadContractArgs): IIdentityContracts => {
if (!addresses[network]) {
throw new Error(`Network ${network} not supported!`);
}

const p =
// take provider as is if supplied
provider ||
Expand All @@ -30,34 +34,35 @@ export const loadIdentityContracts = ({
);

const SoulboundIdentityContract = SoulboundIdentity__factory.connect(
addresses[network].SoulboundIdentityAddress || ethers.constants.AddressZero,
addresses[network]?.SoulboundIdentityAddress ||
ethers.constants.AddressZero,
p
);

const SoulboundCreditScoreContract = SoulboundCreditScore__factory.connect(
addresses[network].SoulboundCreditScoreAddress ||
addresses[network]?.SoulboundCreditScoreAddress ||
ethers.constants.AddressZero,
p
);

const SoulNameContract = SoulName__factory.connect(
addresses[network].SoulNameAddress || ethers.constants.AddressZero,
addresses[network]?.SoulNameAddress || ethers.constants.AddressZero,
p
);

const SoulLinkerContract = SoulLinker__factory.connect(
addresses[network].SoulLinkerAddress || ethers.constants.AddressZero,
addresses[network]?.SoulLinkerAddress || ethers.constants.AddressZero,
p
);

const SoulStoreContract = SoulStore__factory.connect(
addresses[network].SoulStoreAddress || ethers.constants.AddressZero,
addresses[network]?.SoulStoreAddress || ethers.constants.AddressZero,
p
);

const SoulboundGreenContract = SoulboundGreen__factory.connect(
// this might be empty
addresses[network].SoulboundGreenAddress || ethers.constants.AddressZero,
addresses[network]?.SoulboundGreenAddress || ethers.constants.AddressZero,
p
);

Expand Down
6 changes: 3 additions & 3 deletions src/contracts/masa-contracts.ts
Expand Up @@ -79,15 +79,15 @@ export class MasaContracts {
switch (paymentMethod) {
case "utility":
paymentAddress =
addresses[this.masaConfig.network].MASA || constants.AddressZero;
addresses[this.masaConfig.network]?.MASA || constants.AddressZero;
break;
case "stable":
paymentAddress =
addresses[this.masaConfig.network].USDC || constants.AddressZero;
addresses[this.masaConfig.network]?.USDC || constants.AddressZero;
break;
case "weth":
paymentAddress =
addresses[this.masaConfig.network].WETH || constants.AddressZero;
addresses[this.masaConfig.network]?.WETH || constants.AddressZero;
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/interface/masa.ts
Expand Up @@ -33,7 +33,7 @@ export interface MasaArgs {
export interface MasaConfig {
apiUrl: string;
environment: string;
network: string;
network: NetworkName;
wallet: ethers.Signer | ethers.Wallet;
verbose: boolean;
}
Expand Down

0 comments on commit 3e8ac36

Please sign in to comment.