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: create rpc method to link to swap/bridge #6004

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion src/background/messaging/messaging-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { To } from 'react-router-dom';

import { InternalMethods } from '@shared/message-types';
import { sendMessage } from '@shared/messages';
import { RouteUrls } from '@shared/route-urls';
import type { SwapRequestParams } from '@shared/rpc/methods/swap';

Check failure on line 6 in src/background/messaging/messaging-utils.ts

View workflow job for this annotation

GitHub Actions / typecheck

'SwapRequestParams' is declared but its value is never read.

import { popup } from '@background/popup';

Expand All @@ -16,7 +19,7 @@
//
// Playwright does not currently support Chrome extension popup testing:
// https://github.com/microsoft/playwright/issues/5593
async function openRequestInFullPage(path: string, urlParams: URLSearchParams) {
async function openRequestInFullPage(path: string | To, urlParams: URLSearchParams) {
return chrome.tabs.create({
url: chrome.runtime.getURL(`index.html#${path}?${urlParams.toString()}`),
});
Expand Down Expand Up @@ -69,3 +72,10 @@
if (IS_TEST_ENV) return openRequestInFullPage(path, urlParams);
return popup({ url: `/popup.html#${path}?${urlParams.toString()}` });
}

export async function triggerSwapWindowOpen(path: To, urlParams: URLSearchParams) {
console.log('triggerSwapWindowOpen', path, urlParams);

Check failure on line 77 in src/background/messaging/messaging-utils.ts

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement

if (IS_TEST_ENV) return openRequestInFullPage(path, urlParams);
return popup({ url: `/index.html#${path}?${urlParams.toString()}` });
}
5 changes: 5 additions & 0 deletions src/background/messaging/rpc-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WalletRequests, makeRpcErrorResponse } from '@shared/rpc/rpc-methods';

import { queueAnalyticsRequest } from '@background/background-analytics';
import { rpcSignStacksTransaction } from '@background/messaging/rpc-methods/sign-stacks-transaction';
import { rpcSwap } from '@background/messaging/rpc-methods/swap';

import { getTabIdFromPort, listenForOriginTabClose } from './messaging-utils';
import { rpcGetAddresses } from './rpc-methods/get-addresses';
Expand All @@ -22,6 +23,10 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru
await rpcOpen(message, port);
break;
}
case 'swap': {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would call it this message something less terse, to indicate that this isn't our API to initiate/track a swap, rather, just opening that page.

await rpcSwap(message, port);
break;
}
case 'getAddresses': {
await rpcGetAddresses(message, port);
break;
Expand Down
26 changes: 26 additions & 0 deletions src/background/messaging/rpc-methods/swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RouteUrls } from '@shared/route-urls';
import type { SwapRequest } from '@shared/rpc/methods/swap';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';

import { makeSearchParamsWithDefaults, triggerSwapWindowOpen } from '../messaging-utils';
import { trackRpcRequestSuccess } from '../rpc-message-handler';

export async function rpcSwap(message: SwapRequest, port: chrome.runtime.Port) {
const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]);
const { from = 'STX', to } = message?.params || {};

await triggerSwapWindowOpen(
RouteUrls.Swap.replace(':base', from).replace(':quote', to ?? ''),
urlParams
);

void trackRpcRequestSuccess({ endpoint: message.method });

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('swap', {
id: message.id,
result: { message: 'Success' },
})
);
}
12 changes: 12 additions & 0 deletions src/shared/rpc/methods/swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';

export interface SwapRequestParams {
from?: string;
to?: string;
}

export type SwapRequest = RpcRequest<'swap', SwapRequestParams>;

type SwapResponse = RpcResponse<{ message: string }>;

export type Swap = DefineRpcMethod<SwapRequest, SwapResponse>;
2 changes: 2 additions & 0 deletions src/shared/rpc/rpc-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { Open } from './methods/open';
import { SignPsbt } from './methods/sign-psbt';
import { SignStacksMessage } from './methods/sign-stacks-message';
import { SupportedMethods } from './methods/supported-methods';
import { Swap } from './methods/swap';

// Supports BtcKit methods, as well as custom Leather methods
export type WalletMethodMap = BtcKitMethodMap &
Open &
Swap &
SupportedMethods &
SignPsbt &
SignStacksTransaction &
Expand Down
Loading