Skip to content

Commit

Permalink
Phantom support - getAccountAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
zgz2020 committed Dec 11, 2024
1 parent c0018fd commit a07e85c
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 30 deletions.
11 changes: 4 additions & 7 deletions wallets/phantom/src/cypress/Phantom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Selectors from '../selectors/pages/HomePage'
import type { SettingsSidebarMenus } from '../selectors/pages/HomePage/settings'
import TransactionPage from '../selectors/pages/NotificationPage/transactionPage'
import type { GasSettings } from '../type/GasSettings'
import type { Networks } from '../type/Networks'
import getPlaywrightPhantom from './getPlaywrightPhantom'

/**
Expand Down Expand Up @@ -42,8 +43,8 @@ export default class Phantom {
* Gets the current account address.
* @returns The current account address
*/
async getAccountAddress(): Promise<string> {
return await this.phantomPlaywright.getAccountAddress()
async getAccountAddress(network: Networks): Promise<string> {
return await this.phantomPlaywright.getAccountAddress(network)
}

/**
Expand Down Expand Up @@ -79,11 +80,7 @@ export default class Phantom {
* @param privateKey - The private key to import
* @returns True if the import was successful
*/
async importWalletFromPrivateKey(
network: 'solana' | 'ethereum' | 'base' | 'polygon' | 'bitcoin',
privateKey: string,
walletName?: string
): Promise<boolean> {
async importWalletFromPrivateKey(network: Networks, privateKey: string, walletName?: string): Promise<boolean> {
await this.phantomPlaywright.importWalletFromPrivateKey(network, privateKey, walletName)
return true
}
Expand Down
5 changes: 3 additions & 2 deletions wallets/phantom/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BrowserContext, Page } from '@playwright/test'
import { ensureRdpPort } from '@synthetixio/synpress-core'
import type { SettingsSidebarMenus } from '../selectors/pages/HomePage/settings'
import type { GasSettings } from '../type/GasSettings'
import type { Networks } from '../type/Networks'
import Phantom from './Phantom'
import importPhantomWallet from './support/importPhantomWallet'
import { initPhantom } from './support/initPhantom'
Expand Down Expand Up @@ -106,14 +107,14 @@ export default function configureSynpress(
privateKey,
walletName
}: {
network: 'solana' | 'ethereum' | 'base' | 'polygon' | 'bitcoin'
network: Networks
privateKey: string
walletName?: string
}) => phantom?.importWalletFromPrivateKey(network, privateKey, walletName),

// Account
getAccount: () => phantom?.getAccount(),
getAccountAddress: () => phantom?.getAccountAddress(),
getAccountAddress: (network: Networks) => phantom?.getAccountAddress(network),
addNewAccount: (accountName: string) => phantom?.addNewAccount(accountName),
switchAccount: (accountName: string) => phantom?.switchAccount(accountName),
renameAccount: ({
Expand Down
5 changes: 3 additions & 2 deletions wallets/phantom/src/playwright/Phantom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { BrowserContext, Page } from '@playwright/test'
import { SettingsSidebarMenus } from '../selectors/pages/HomePage/settings'
import type { GasSettings } from '../type/GasSettings'
import type { Networks } from '../type/Networks'
import { PhantomAbstract } from '../type/PhantomAbstract'
import { CrashPage, HomePage, LockPage, NotificationPage, OnboardingPage } from './pages'
import { SettingsPage } from './pages/SettingsPage/page'
Expand Down Expand Up @@ -144,8 +145,8 @@ export class Phantom extends PhantomAbstract {
*
* @returns The account address.
*/
async getAccountAddress(): Promise<string> {
return await this.homePage.getAccountAddress()
async getAccountAddress(network: Networks): Promise<string> {
return await this.homePage.getAccountAddress(network)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Page } from '@playwright/test'
import Selectors from '../../../../selectors/pages/HomePage'
import type { Networks } from '../../../../type/Networks'

// TODO - .getAccountAddress() to be updated for all networks
export default async function getAccountAddress(page: Page): Promise<string> {
export default async function getAccountAddress(network: Networks, page: Page): Promise<string> {
// Copy account address to clipboard
await page.locator(Selectors.accountMenu.accountName).hover()
await page.locator(Selectors.ethereumWalletAddress).click()
await page.locator(Selectors[`${network}WalletAddress`]).click()

// Get clipboard content
const handle = await page.evaluateHandle(() => navigator.clipboard.readText())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Page } from '@playwright/test'
import Selectors from '../../../../selectors/pages/HomePage'
import type { Networks } from '../../../../type/Networks'
import { waitFor } from '../../../utils/waitFor'

export async function importWalletFromPrivateKey(
page: Page,
network: 'solana' | 'ethereum' | 'base' | 'polygon' | 'bitcoin',
network: Networks,
privateKey: string,
walletName?: string
) {
Expand Down
5 changes: 3 additions & 2 deletions wallets/phantom/src/playwright/pages/HomePage/page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Page } from '@playwright/test'
import Selectors from '../../../selectors/pages/HomePage'
import type { SettingsSidebarMenus } from '../../../selectors/pages/HomePage/settings'
import type { Networks } from '../../../type/Networks'
import {
addNewAccount,
getAccountAddress,
Expand Down Expand Up @@ -40,8 +41,8 @@ export class HomePage {
await renameAccount(this.page, currentAccountName, newAccountName)
}

async getAccountAddress() {
return await getAccountAddress(this.page)
async getAccountAddress(network: Networks) {
return await getAccountAddress(network, this.page)
}

async importWalletFromPrivateKey(
Expand Down
4 changes: 4 additions & 0 deletions wallets/phantom/src/selectors/pages/HomePage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ const activityTab = {
const singleToken = '.multichain-token-list-item'

export default {
solanaWalletAddress: createDataTestSelector('account-header-chain-solana:101'),
ethereumWalletAddress: createDataTestSelector('account-header-chain-eip155:1'),
baseWalletAddress: createDataTestSelector('account-header-chain-eip155:8453'),
polygonWalletAddress: createDataTestSelector('account-header-chain-eip155:137'),
bitcoinWalletAddress: createDataTestSelector('account-header-chain-bip122:000000000019d6689c085ae165831e93'),
logo: `button${createDataTestSelector('app-header-logo')}`,
copyAccountAddressButton: createDataTestSelector('address-copy-button-text'),
currentNetwork: `${createDataTestSelector('network-display')} span:nth-of-type(1)`,
Expand Down
1 change: 1 addition & 0 deletions wallets/phantom/src/type/Networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Networks = 'solana' | 'ethereum' | 'base' | 'polygon' | 'bitcoin'
9 changes: 3 additions & 6 deletions wallets/phantom/src/type/PhantomAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SettingsSidebarMenus } from '../selectors/pages/HomePage/settings'
import type { GasSettings } from './GasSettings'
import type { Networks } from './Networks'

export abstract class PhantomAbstract {
/**
Expand Down Expand Up @@ -41,11 +42,7 @@ export abstract class PhantomAbstract {
*
* @param privateKey - The private key to import.
*/
abstract importWalletFromPrivateKey(
network: 'solana' | 'ethereum' | 'base' | 'polygon' | 'bitcoin',
privateKey: string,
walletName?: string
): void
abstract importWalletFromPrivateKey(network: Networks, privateKey: string, walletName?: string): void

/**
* Switches to the account with the given name.
Expand All @@ -57,7 +54,7 @@ export abstract class PhantomAbstract {
/**
* Retrieves the current account address.
*/
abstract getAccountAddress(): void
abstract getAccountAddress(network: Networks): void

/**
* Switches to the network with the given name.
Expand Down
21 changes: 13 additions & 8 deletions wallets/phantom/test/playwright/e2e/getAccountAddress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ const test = testWithSynpress(phantomFixtures(basicSetup))

const { expect } = test

// TODO - .getAccountAddress() and Tests to be updated for all networks
test('should get account address', async ({ context, phantomPage }) => {
test('should get account address for all available networks', async ({ context, phantomPage }) => {
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)

await phantom.importWalletFromPrivateKey(
'ethereum',
'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a'
)
const solanaAccountAddress = await phantom.getAccountAddress('solana')
expect(solanaAccountAddress).toEqual('oeYf6KAJkLYhBuR8CiGc6L4D4Xtfepr85fuDgA9kq96')

const accountAddress = await phantom.getAccountAddress()
const ethereumAccountAddress = await phantom.getAccountAddress('ethereum')
expect(ethereumAccountAddress).toEqual('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266')

expect(accountAddress).toEqual('0xa2ce797cA71d0EaE1be5a7EffD27Fd6C38126801')
const baseAccountAddress = await phantom.getAccountAddress('base')
expect(baseAccountAddress).toEqual('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266')

const polygonAccountAddress = await phantom.getAccountAddress('polygon')
expect(polygonAccountAddress).toEqual('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266')

const bitcoinAccountAddress = await phantom.getAccountAddress('bitcoin')
expect(bitcoinAccountAddress).toEqual('bc1q4qw42stdzjqs59xvlrlxr8526e3nunw7mp73te')
})

0 comments on commit a07e85c

Please sign in to comment.