Skip to content

Commit

Permalink
fix: add proper types to the provider
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Jun 13, 2024
1 parent d9f07cf commit 429bbcf
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 196 deletions.
12 changes: 3 additions & 9 deletions packages/adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"module": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"scripts": {
"lint": "tsc --build && biome check --no-errors-on-unmatched --files-ignore-unknown=true .",
"lint": "tsc --build && biome check --no-errors-on-unmatched --files-ignore-unknown=true",
"build": "wireit",
"test": "wireit"
"test": "playwright test"
},
"wireit": {
"build": {
Expand All @@ -39,17 +39,11 @@
"output": [
"dist/**"
]
},
"test": {
"command": "playwright test",
"dependencies": [
"../../examples/demo:dev",
"../snap:serve"
]
}
},
"dependencies": {
"@metamask/approval-controller": "^6.0.0",
"@metamask/sdk": "^0.20.5",
"filsnap": "workspace:^",
"iso-filecoin": "^4.0.3"
},
Expand Down
20 changes: 13 additions & 7 deletions packages/adapter/playwright.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { defineConfig } from '@playwright/test'

export default defineConfig({
name: 'adapter',
testDir: './test',
timeout: process.env.CI ? 60 * 1000 : 30 * 1000,
expect: {
timeout: 5000,
},
fullyParallel: false,
forbidOnly: Boolean(process.env.CI),
maxFailures: process.env.CI ? 2 : 1,
retries: process.env.CI ? 2 : 0,
retries: process.env.CI ? 2 : 1,
workers: process.env.CI ? 1 : 2,
reporter: process.env.CI ? [['html'], ['list']] : 'list',
use: {
actionTimeout: 0,
baseURL: 'http://localhost:5173',
trace: 'on-first-retry',
colorScheme: 'dark',
browserName: 'chromium',
},
webServer: [
{
command:
'pnpm --filter filsnap run build && pnpm --filter filsnap exec sirv --port 8081',
url: 'http://localhost:8081/dist/snap.js',
},
{
command: 'pnpm --filter demo exec vite',
url: 'http://localhost:5173',
},
],
})
50 changes: 27 additions & 23 deletions packages/adapter/src/snap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import type { FilSnapMethods, Network, SnapConfig } from 'filsnap'
import { RPC } from 'iso-filecoin/rpc'
import { getRequestProvider } from './get-request-provider'
import type { Provider } from './get-request-provider'
import { getRequestProvider } from './get-request-provider'

export class FilsnapAdapter {
readonly snapId: string
Expand Down Expand Up @@ -58,11 +57,14 @@ export class FilsnapAdapter {
}

const provider = await FilsnapAdapter.getProvider()
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const snaps: Record<string, any> =
(await provider.request({ method: 'wallet_getSnaps' })) ?? {}
const snaps = await provider.request({ method: 'wallet_getSnaps' })

const snap = snaps[snapId]

if (snap == null || 'error' in snap) {
return false
}

const isSnapBlocked = snap?.blocked === true
const isSnapEnabled = snap?.enabled === true
const isVersionMatch = snapVersion === '*' || snap?.version === snapVersion
Expand Down Expand Up @@ -92,25 +94,28 @@ export class FilsnapAdapter {
}

const provider = await FilsnapAdapter.getProvider()
const snaps =
(await provider.request({
method: 'wallet_requestSnaps',
params: {
[snapId]: {
version: snapVersion,
},
const snaps = await provider.request({
method: 'wallet_requestSnaps',
params: {
[snapId]: {
version: snapVersion,
},
})) ?? {}
},
})
const snap = snaps[snapId]

// @ts-expect-error - simple enough check
if (snaps[snapId] == null) {
if (snap == null) {
throw new Error(`Failed to connect to snap ${snapId} ${snapVersion}`)
}
// @ts-expect-error - TS doesn't know that `snaps[snapId]` is not null
const { version } = snaps[snapId]
// @ts-export-error - improve the provider typings
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const adapter = new FilsnapAdapter(snapId, version)

if ('error' in snap) {
throw new Error(
`Failed to connect to snap ${snapId} ${snapVersion} with error ${snap.error.message}`
)
}

const adapter = new FilsnapAdapter(snapId, snap.version)

const result = await adapter.configure(config)

if (result.error) {
Expand Down Expand Up @@ -145,7 +150,6 @@ export class FilsnapAdapter {
network: this.config.network,
})
}

/**
* Configure the snap
*
Expand Down Expand Up @@ -383,8 +387,8 @@ export class FilsnapAdapter {
}
}

const provider = await FilsnapAdapter.getProvider()
try {
const provider = await FilsnapAdapter.getProvider()
await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: config.chainId }],
Expand All @@ -397,7 +401,7 @@ export class FilsnapAdapter {
// This error code indicates that the chain has not been added to MetaMask.
if (err.code === 4902) {
try {
await ethereum.request({
await provider.request({
method: 'wallet_addEthereumChain',
params: [config],
})
Expand Down
97 changes: 97 additions & 0 deletions packages/adapter/test/adapter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { createFixture } from 'metamask-testing-tools'

const SNAP_ID = 'local:http://localhost:8081'
const SNAP_VERSION = '*'
const METAMASK_VERSION = 'v11.16.5'
const RAINBOW_PASSWORD = '12345678'
const RAINBOW_EXTENSION_ID = 'opfgelmcmbiajamepnmloijbpoleiama'

/**
* @param {any} data
*/
async function setupExtraExtensions(data) {
for (const extension of data) {
if (extension.title === 'Rainbow Wallet') {
const page = extension.page
await page.getByTestId('create-wallet-button').click()
await page.getByTestId('skip-button').click()
await page.getByTestId('password-input').fill(RAINBOW_PASSWORD)
await page.getByTestId('confirm-password-input').fill(RAINBOW_PASSWORD)
await page.getByTestId('set-password-button').click()
}
}
}

let fixture = createFixture({
downloadOptions: {
flask: true,
tag: METAMASK_VERSION,
},
})

fixture.test(
'should start connect to snap when flask is installed',
async ({ metamask, page }) => {
await page.getByTestId('connect-snap').click()

const dialog = await metamask.waitForDialog('experimental-area')
await fixture.expect(dialog.getByTestId('experimental-area')).toBeVisible()
}
)

fixture.test(
'should start enabling snap when metamask as an account',
async ({ metamask, page }) => {
await metamask.setup()
await page.getByTestId('connect-snap').click()

const dialog = await metamask.waitForDialog('snaps-connect')
fixture.expect(dialog).toBeDefined()
}
)

fixture = createFixture({
isolated: true,
downloadOptions: {
flask: true,
tag: METAMASK_VERSION,
},
snap: {
id: SNAP_ID,
version: SNAP_VERSION,
},
})

fixture.test(
'should show connect button when snap is installed',
async ({ metamask, page }) => {
await page.getByTestId('connect-snap').click()

const dialog = await metamask.waitForDialog('snap-install')
fixture.expect(dialog).toBeDefined()
}
)

fixture = createFixture({
isolated: true,
downloadOptions: {
flask: true,
extensionsIds: [RAINBOW_EXTENSION_ID],
tag: METAMASK_VERSION,
},
snap: {
id: SNAP_ID,
version: SNAP_VERSION,
},
})

fixture.test(
'should start connect to snap when flask is installed alongside rainbow wallet',
async ({ metamask, page }) => {
await metamask.setupExtraExtensions(setupExtraExtensions)
await page.getByTestId('connect-snap').click()

const dialog = await metamask.waitForDialog('connect')
fixture.expect(dialog).toBeDefined()
}
)
Loading

0 comments on commit 429bbcf

Please sign in to comment.