Skip to content

Commit

Permalink
fix: Webapps fail to load when the browser doesn't have MetaMask inst…
Browse files Browse the repository at this point in the history
…alled (#419)

Co-authored-by: Satyajeet Kolhapure <[email protected]>
  • Loading branch information
satyajeetkolhapure and kolhapuresatyajeet authored Nov 22, 2023
1 parent 5bf528a commit b038a7e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
32 changes: 18 additions & 14 deletions sdk/src/VeraxSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (typeof window === "undefined") {
const dotenv = require("dotenv");
dotenv.config({ path: "./.env" });
account = privateKeyToAccount(process.env.PRIVATE_KEY as Hex);
} else {
} else if (typeof window.ethereum !== "undefined") {
window.ethereum.request({ method: "eth_requestAccounts" }).then((result: Address[]) => {
account = result[0];
});
Expand Down Expand Up @@ -57,7 +57,7 @@ export class VeraxSdk {
};

private readonly web3Client: PublicClient;
private readonly walletClient: WalletClient;
private readonly walletClient: WalletClient | undefined;

public attestation: AttestationDataMapper;
public schema: SchemaDataMapper;
Expand All @@ -71,18 +71,22 @@ export class VeraxSdk {
transport: http(),
});

this.walletClient =
conf.mode === SDKMode.BACKEND
? createWalletClient({
chain: conf.chain,
account,
transport: http(),
})
: createWalletClient({
chain: conf.chain,
account,
transport: custom(window.ethereum),
});
if (typeof account === "undefined") {
this.walletClient = undefined;
} else {
this.walletClient =
conf.mode === SDKMode.BACKEND
? createWalletClient({
chain: conf.chain,
account,
transport: http(),
})
: createWalletClient({
chain: conf.chain,
account,
transport: custom(window.ethereum),
});
}

this.attestation = new AttestationDataMapper(conf, this.web3Client, this.walletClient, this);
this.schema = new SchemaDataMapper(conf, this.web3Client, this.walletClient, this);
Expand Down
1 change: 1 addition & 0 deletions sdk/src/dataMapper/AttestationDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export default class AttestationDataMapper extends BaseDataMapper<
}

private async simulateContract(functionName: string, args: unknown[]) {
if (!this.walletClient) throw new Error("Account not available");
try {
const { request } = await this.web3Client.simulateContract({
address: this.conf.attestationRegistryAddress,
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/dataMapper/BaseDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import axios from "axios";
export default abstract class BaseDataMapper<T, TFilter, TOrder> {
protected readonly conf: Conf;
protected readonly web3Client: PublicClient;
protected readonly walletClient: WalletClient;
protected readonly walletClient: WalletClient | undefined;
protected readonly veraxSdk: VeraxSdk;
protected abstract typeName: string;
protected abstract gqlInterface: string;

constructor(_conf: Conf, _web3Client: PublicClient, _walletClient: WalletClient, _veraxSdk: VeraxSdk) {
constructor(_conf: Conf, _web3Client: PublicClient, _walletClient: WalletClient | undefined, _veraxSdk: VeraxSdk) {
this.conf = _conf;
this.web3Client = _web3Client;
this.walletClient = _walletClient;
Expand Down
1 change: 1 addition & 0 deletions sdk/src/dataMapper/ModuleDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export default class ModuleDataMapper extends BaseDataMapper<Module, Module_filt
}

private async simulateContract(functionName: string, args: unknown[]) {
if (!this.walletClient) throw new Error("Account not available");
try {
const { request } = await this.web3Client.simulateContract({
address: this.conf.moduleRegistryAddress,
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/dataMapper/PortalDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
}

private async simulatePortalRegistryContract(functionName: string, args: unknown[]) {
if (!this.walletClient) throw new Error("Account not available");
try {
const { request } = await this.web3Client.simulateContract({
address: this.conf.portalRegistryAddress,
Expand All @@ -225,6 +226,7 @@ export default class PortalDataMapper extends BaseDataMapper<Portal, Portal_filt
}

private async simulatePortalContract(portalAddress: Address, functionName: string, args: unknown[]) {
if (!this.walletClient) throw new Error("Account not available");
try {
const { request } = await this.web3Client.simulateContract({
address: portalAddress,
Expand Down
1 change: 1 addition & 0 deletions sdk/src/dataMapper/SchemaDataMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default class SchemaDataMapper extends BaseDataMapper<Schema, Schema_filt
}

private async simulateContract(functionName: string, args: unknown[]) {
if (!this.walletClient) throw new Error("Account not available");
try {
const { request } = await this.web3Client.simulateContract({
address: this.conf.schemaRegistryAddress,
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/utils/transactionSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Hash, WalletClient } from "viem";

// TODO: Use correct type for request
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function executeTransaction(walletClient: WalletClient, request: any): Promise<Hash> {
export async function executeTransaction(walletClient: WalletClient | undefined, request: any): Promise<Hash> {
if (walletClient === undefined) throw new Error("Account not available");
const hash: Hash = await walletClient.writeContract(request);
console.log(`Transaction sent with hash ${hash}`);
return hash;
Expand Down

0 comments on commit b038a7e

Please sign in to comment.