Skip to content

feat: conditional qr modal ep #5799

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

Open
wants to merge 7 commits into
base: v2.0
Choose a base branch
from
Open
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
54 changes: 35 additions & 19 deletions providers/ethereum-provider/src/EthereumProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class EthereumProvider implements IEthereumProvider {
this.signer.logger.error(error);
throw error;
} finally {
if (this.modal) this.modal.closeModal();
if (this.rpc.showQrModal) this.modal?.closeModal();
}
}

Expand Down Expand Up @@ -394,7 +394,7 @@ export class EthereumProvider implements IEthereumProvider {
this.signer.logger.error(error);
throw error;
} finally {
if (this.modal) this.modal.closeModal();
if (this.rpc.showQrModal) this.modal?.closeModal();
}
}

Expand Down Expand Up @@ -433,6 +433,18 @@ export class EthereumProvider implements IEthereumProvider {
return this.signer.session;
}

public updateConfigOptions(opts: Partial<EthereumProviderOptions>) {
const chains = opts.chains?.map((chain) => this.formatChainId(chain)) || this.rpc.chains;
const optionalChains =
opts.optionalChains?.map((chain) => this.formatChainId(chain)) || this.rpc.optionalChains;
this.rpc = {
...this.rpc,
...opts,
chains,
optionalChains,
};
}

// ---------- Protected --------------------------------------------- //

protected registerEventListeners() {
Expand Down Expand Up @@ -597,24 +609,28 @@ export class EthereumProvider implements IEthereumProvider {
});
this.registerEventListeners();
await this.loadPersistedSession();
if (this.rpc.showQrModal) {
let WalletConnectModalClass;
await this.loadWalletConnectModal();
}

protected async loadWalletConnectModal() {
if (this.modal) return;
// old try catch block from the time the modal was optional
let WalletConnectModalClass;
try {
const { WalletConnectModal } = await import("@walletconnect/modal");
WalletConnectModalClass = WalletConnectModal;
} catch {
throw new Error("To use QR modal, please install @walletconnect/modal package");
}
if (WalletConnectModalClass) {
try {
const { WalletConnectModal } = await import("@walletconnect/modal");
WalletConnectModalClass = WalletConnectModal;
} catch {
throw new Error("To use QR modal, please install @walletconnect/modal package");
}
if (WalletConnectModalClass) {
try {
this.modal = new WalletConnectModalClass({
projectId: this.rpc.projectId,
...this.rpc.qrModalOptions,
});
} catch (e) {
this.signer.logger.error(e);
throw new Error("Could not generate WalletConnectModal Instance");
}
this.modal = new WalletConnectModalClass({
projectId: this.rpc.projectId,
...this.rpc.qrModalOptions,
});
} catch (e) {
this.signer.logger.error(e);
throw new Error("Could not generate WalletConnectModal Instance");
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions providers/ethereum-provider/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,56 @@ describe("EthereumProvider", function () {
await walletClient.client?.core.relayer.transportClose();
});

describe("config options", () => {
it("should set options", () => {
const cacheConfig = provider.rpc;
// validate that we can update individual options
provider.updateConfigOptions({ showQrModal: true });
expect(provider.rpc.showQrModal).to.be.true;
provider.updateConfigOptions({ showQrModal: false });
expect(provider.rpc.showQrModal).to.be.false;
provider.updateConfigOptions({
chains: [42],
});
expect(provider.rpc.chains).to.be.eql(["eip155:42"]);
provider.updateConfigOptions({
optionalChains: [3, 1],
});
expect(provider.rpc.optionalChains).to.be.eql(["eip155:3", "eip155:1"]);
provider.updateConfigOptions({
methods: ["eth_sendTransaction"],
});
expect(provider.rpc.methods).to.be.eql(["eth_sendTransaction"]);
provider.updateConfigOptions({
optionalMethods: ["eth_sendTransaction"],
});
expect(provider.rpc.optionalMethods).to.be.eql(["eth_sendTransaction"]);
provider.updateConfigOptions({
events: ["accountsChanged"],
});
expect(provider.rpc.events).to.be.eql(["accountsChanged"]);
provider.updateConfigOptions({
optionalEvents: ["accountsChanged"],
});
expect(provider.rpc.optionalEvents).to.be.eql(["accountsChanged"]);
provider.updateConfigOptions({
rpcMap: {
42: "https://kovan.poa.network",
},
});
// validate updating individual options doesn't affect other options
expect(provider.rpc.rpcMap).to.be.eql({ 42: "https://kovan.poa.network" });
expect(provider.rpc.chains).to.be.eql(["eip155:42"]);
expect(provider.rpc.optionalChains).to.be.eql(["eip155:3", "eip155:1"]);
expect(provider.rpc.methods).to.be.eql(["eth_sendTransaction"]);
expect(provider.rpc.optionalMethods).to.be.eql(["eth_sendTransaction"]);
expect(provider.rpc.events).to.be.eql(["accountsChanged"]);
expect(provider.rpc.optionalEvents).to.be.eql(["accountsChanged"]);
expect(provider.rpc.rpcMap).to.be.eql({ 42: "https://kovan.poa.network" });
provider.rpc = cacheConfig;
});
});

it("chainChanged", async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
// change to Kovan
Expand Down
Loading