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

feat: fetch call status via bundler url #5171

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 38 additions & 0 deletions providers/universal-provider/src/providers/eip155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { getChainId, getGlobal, getRpcUrl } from "../utils";
import EventEmitter from "events";
import { PROVIDER_EVENTS } from "../constants";
import { formatJsonRpcRequest } from "@walletconnect/jsonrpc-utils";

class Eip155Provider implements IProvider {
public name = "eip155";
Expand Down Expand Up @@ -45,6 +46,8 @@ class Eip155Provider implements IProvider {
return parseInt(this.getDefaultChain()) as unknown as T;
case "wallet_getCapabilities":
return (await this.getCapabilities(args)) as unknown as T;
case "wallet_getCallsStatus":
return (await this.getCallStatus(args)) as unknown as T;
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this result in a unhandled promise rejection if the call throws ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it as http calls are try/catched. Only if the request can't be sent to thte wallet and/or is rejected by the wallet it would throw

default:
break;
}
Expand Down Expand Up @@ -198,6 +201,41 @@ class Eip155Provider implements IProvider {
}
return capabilities;
}

private async getCallStatus(args: RequestParams) {
const session = this.client.session.get(args.topic);
if (
session?.sessionProperties &&
Object.keys(session.sessionProperties).includes("bundler_url")
) {
const bundlerUrl = session.sessionProperties.bundler_url;
try {
return await this.getUserOperationReceipt(bundlerUrl, args);
} catch (error) {
console.warn("Failed to fetch call status from bundler", error, bundlerUrl);
}
}
if (this.namespace.methods.includes(args.request.method)) {
return await this.client.request(args as EngineTypes.RequestParams);
}

throw new Error("Fetching call status not approved by the wallet.");
}

private async getUserOperationReceipt(bundlerUrl: string, args: RequestParams) {
const url = new URL(bundlerUrl);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(
formatJsonRpcRequest("eth_getUserOperationReceipt", [args.request.params?.[0]]),
),
});

return await response.json();
ganchoradkov marked this conversation as resolved.
Show resolved Hide resolved
}
}

export default Eip155Provider;
198 changes: 198 additions & 0 deletions providers/universal-provider/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,204 @@ describe("UniversalProvider", function () {
expect(provider.client.core.relayer.subscriber.subscriptions.size).to.eql(1);
});
});
describe("call status", () => {
it("should get call status request to wallet when bundler id is not provided", async () => {
const dapp = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "dapp",
});
const wallet = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "wallet",
});
const chains = ["eip155:1"];
await testConnectMethod(
{
dapp,
wallet,
},
{
requiredNamespaces: {
eip155: {
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
optionalNamespaces: {
eip155: {
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
namespaces: {
eip155: {
accounts: chains.map((chain) => `${chain}:${walletAddress}`),
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
},
);
const testResult = { result: "test result " };
await Promise.all([
new Promise<void>((resolve) => {
wallet.client.on("session_request", async (event) => {
expect(event.params.request.method).to.eql("wallet_getCallsStatus");
await wallet.client.respond({
topic: event.topic,
response: formatJsonRpcResult(event.id, testResult),
});
resolve();
});
}),
new Promise<void>(async (resolve) => {
const result = await dapp.request({
method: "wallet_getCallsStatus",
params: ["test params"],
});
expect(result).to.eql(testResult);
resolve();
}),
]);
});
it("should get call status request to bundler when bundler url is provided", async () => {
const dapp = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "dapp",
});
const wallet = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "wallet",
});
const chains = ["eip155:1"];
await testConnectMethod(
{
dapp,
wallet,
},
{
requiredNamespaces: {
eip155: {
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
optionalNamespaces: {
eip155: {
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
namespaces: {
eip155: {
accounts: chains.map((chain) => `${chain}:${walletAddress}`),
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
sessionProperties: { bundler_url: "http://localhost:3000" },
},
);
const testResult = { result: "test result " };
// @ts-ignore
dapp.rpcProviders.eip155.getUserOperationReceipt = (bundlerUrl: string, args: any) => {
expect(bundlerUrl).to.eql("http://localhost:3000");
expect(args.request.method).to.eql("wallet_getCallsStatus");
return testResult;
};
await Promise.all([
new Promise<void>(async (resolve) => {
const result = await dapp.request({
method: "wallet_getCallsStatus",
params: ["test params"],
});
expect(result).to.eql(testResult);
resolve();
}),
]);
});
it("should get call status request to bundler and wallet when bundler url fails", async () => {
const dapp = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "dapp",
});
const wallet = await UniversalProvider.init({
...TEST_PROVIDER_OPTS,
name: "wallet",
});
const chains = ["eip155:1"];
await testConnectMethod(
{
dapp,
wallet,
},
{
requiredNamespaces: {
eip155: {
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
optionalNamespaces: {
eip155: {
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
namespaces: {
eip155: {
accounts: chains.map((chain) => `${chain}:${walletAddress}`),
chains,
methods: ["wallet_getCallsStatus"],
events,
},
},
sessionProperties: { bundler_url: "http://localhost:3000" },
},
);
const testResult = { result: "test result " };
// @ts-ignore
dapp.rpcProviders.eip155.getUserOperationReceipt = (bundlerUrl: string, args: any) => {
throw new Error("Failed to fetch call status from bundler");
};
await Promise.all([
new Promise<void>((resolve) => {
wallet.client.on("session_request", async (event) => {
expect(event.params.request.method).to.eql("wallet_getCallsStatus");
await wallet.client.respond({
topic: event.topic,
response: formatJsonRpcResult(event.id, testResult),
});
resolve();
});
}),
new Promise<void>(async (resolve) => {
const result = await dapp.request({
method: "wallet_getCallsStatus",
params: ["test params"],
});
expect(result).to.eql(testResult);
resolve();
}),
]);
});
it("should receive rejection on get call status request when no bundler url or method is not approved", async () => {
await expect(
provider.request({
method: "wallet_getCallsStatus",
params: ["test params"],
}),
).rejects.toThrowError("Fetching call status not approved by the wallet.");
});
});
describe("caip validation", () => {
it("should reload after restart", async () => {
const dapp = await UniversalProvider.init({
Expand Down
3 changes: 2 additions & 1 deletion providers/universal-provider/test/shared/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface TestConnectParams {
relays?: RelayerTypes.ProtocolOptions[];
pairingTopic?: string;
qrCodeScanLatencyMs?: number;
sessionProperties?: SessionTypes.Struct["sessionProperties"];
}

export async function testConnectMethod(
Expand All @@ -39,9 +40,9 @@ export async function testConnectMethod(
relays: params?.relays || undefined,
pairingTopic: params?.pairingTopic || undefined,
};

const approveParams: Omit<EngineTypes.ApproveParams, "id"> = {
namespaces: params?.namespaces || TEST_NAMESPACES,
sessionProperties: params?.sessionProperties,
};

// We need to kick off the promise that binds the listener for `session_proposal` before `A.connect()`
Expand Down
Loading