Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

chore: Update documentation and Readme #202

Merged
merged 10 commits into from
Dec 6, 2022
Merged
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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ $ yarn add @chainsafe/dappeteer
## Usage

```js
import puppeteer from 'puppeteer';
import dappeteer from '@chainsafe/dappeteer';
import D from '@chainsafe/dappeteer';

async function main() {
const [metamask, page] = await dappeteer.bootstrap(puppeteer, { metaMaskVersion: 'v10.15.0' });
const { page, dappeteer } = await D.bootstrap({ metaMaskVersion: 'v10.15.0' });
Tbaut marked this conversation as resolved.
Show resolved Hide resolved

// you can change the network if you want
await metamask.switchNetwork('ropsten');
await dappeteer.switchNetwork('goerli');

// you can import a token
await metamask.addToken({
tokenAddress: '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa',
symbol: 'KAKI',
});
// go to a dapp and do something that prompts MetaMask to add a Token
await page.goto('http://my-dapp.com');
const addToken = await page.$('#add-token');

// ✔️
await dappeteer.acceptAddToken();

// go to a dapp and do something that prompts MetaMask to confirm a transaction
await page.goto('http://my-dapp.com');
const payButton = await page.$('#pay-with-eth');
await payButton.click();

// 🏌
await metamask.confirmTransaction();
await dappeteer.confirmTransaction();
}

main();
Expand Down
86 changes: 56 additions & 30 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ For additional information read root [readme](../README.md)
- [lock](#lock)
- [unlock](#unlock)
- [switchNetwork](#switchNetwork)
- [addNetwork](#addNetwork)
- [addToken](#addToken)
- [acceptAddNetwork](#acceptAddNetwork)
- [rejectAddNetwork](#rejectAddNetwork)
- [acceptAddToken](#acceptAddToken)
- [rejectAddToken](#rejectAddToken)
- [confirmTransaction](#confirmTransaction)
- [sign](#sign)
- [signTypedData](#signTypedData)
- [approve](#approve)
- [helpers](#helpers)
- [getTokenBalance](#getTokenBalance)
Expand All @@ -36,7 +39,7 @@ interface OfficialOptions {

type Path = string | { download: string; extract: string; };
```
or
or
```typescript
interface CustomOptions {
metamaskPath: string;
Expand All @@ -60,14 +63,36 @@ type Step = (page: Page, options?: Options) => void;
```

<a name="bootstrap"><a/>
## `dappeteer.bootstrap(puppeteerLib: typeof puppeteer, options: OfficialOptions & MetaMaskOptions): Promise<[Dappeteer, Page, Browser]>`
## `dappeteer.bootstrap(options: DappeteerLaunchOptions & MetaMaskOptions): Promise<{
dappeteer: Dappeteer;
Tbaut marked this conversation as resolved.
Show resolved Hide resolved
browser: DappeteerBrowser;
page: DappeteerPage;
}>`
```typescript
interface OfficialOptions {
metaMaskVersion: 'latest' | string;
type DappeteerLaunchOptions = {
metaMaskVersion?:
| typeof RECOMMENDED_METAMASK_VERSION
BeroBurny marked this conversation as resolved.
Show resolved Hide resolved
| "latest"
| "local"
| string;
metaMaskLocation?: Path;
metaMaskPath?: string;
//install flask (canary) version of metamask.
metaMaskFlask?: boolean;
//fallbacks to installed dependency and prefers playwright if both are installed
automation?: "puppeteer" | "playwright";
browser: "chrome";
puppeteerOptions?: Omit<Parameters<typeof puppeteerLaunch>[0], "headless">;
playwrightOptions?: Omit<PlaywrightLaunchOptions, "headless">;
};

type MetaMaskOptions = {
seed?: string;
password?: string;
showTestNets?: boolean;
};
```
it runs `dappeteer.launch` and `dappeteer.setup` and return array with dappetter, page and browser
it runs `dappeteer.launch` and `dappeteer.setup` and returns an object with dappeteer, page and browser.

<a name="getMetaMask"></a>
## `dappeteer.getMetaMaskWindow(browser: Browser, version?: string): Promise<Dappeteer>`
Expand Down Expand Up @@ -95,30 +120,27 @@ it unlocks the MetaMask extension. It can only be used in you locked/signed out

<a name="switchNetwork"></a>
## `metamask.switchNetwork(network: string): Promise<void>`
it changes the current selected network. `networkName` can take the following values: `"main"`, `"ropsten"`, `"rinkeby"`, `"kovan"`, `"localhost"`.
it changes the current selected network. `networkName` can take the following values: `"mainnet"`, `"goerli"`, `"sepolia"`, `"ropsten"`, `"rinkeby"`, `"kovan"`, `"localhost"`.

<a name="addNetwork"></a>
## `metamask.addNetwork(options: AddNetwork): Promise<void>`
```typescript
interface AddNetwork {
networkName: string;
rpc: string;
chainId: number;
symbol: string;
}
```
it adds a custom network to MetaMask.
<a name="acceptAddNetwork"></a>
## `metamask.acceptAddNetwork(shouldSwitch?: boolean): Promise<void>`
Tbaut marked this conversation as resolved.
Show resolved Hide resolved

<a name="addToken"></a>
## `metamask.addToken(tokenAddress: string): Promise<void>`
```typescript
interface AddToken {
tokenAddress: string;
symbol?: string;
decimals?: number;
}
```
it adds a custom token to MetaMask.
commands MetaMask to accept a Network addition. For this to work MetaMask has to be in a Network addition state (basically prompting the user to accept/reject a Network addition). You can optionnaly tell Metamask to switch to this network by passing the `true` parameter (default to `false`).

<a name="rejectAddNetwork"></a>
## `metamask.rejectAddNetwork(): Promise<void>`

commands MetaMask to reject a Network addition. For this to work MetaMask has to be in a Network addition state (basically prompting the user to accept/reject a Network addition).

<a name="acceptAddToken"></a>
## `metamask.acceptAddToken(): Promise<void>`

commands MetaMask to accept a Token addition. For this to work MetaMask has to be in a Token addition state (basically prompting the user to accept/reject a Token addition).

<a name="rejectAddToken"></a>
## `metamask.rejectAddToken(): Promise<void>`

commands MetaMask to reject a Token addition. For this to work MetaMask has to be in a Token addition state (basically prompting the user to accept/reject a Token addition).

<a name="confirmTransaction"></a>
## `metamask.confirmTransaction(options?: TransactionOptions): Promise<void>`
Expand All @@ -129,13 +151,17 @@ interface TransactionOptions {
priority?: number;
}
```
commands MetaMask to submit a transaction. For this to work MetaMask has to be in a transaction confirmation state (basically promting the user to submit/reject a transaction). You can (optionally) pass an object with `gas` and/or `gasLimit`, by default they are `20` and `50000` respectively.
commands MetaMask to submit a transaction. For this to work MetaMask has to be in a transaction confirmation state (basically prompting the user to submit/reject a transaction). You can (optionally) pass an object with `gas` and/or `gasLimit`, by default they are `20` and `50000` respectively.


<a name="sign"></a>
## `metamask.sign(): Promise<void>`
commands MetaMask to sign a message. For this to work MetaMask must be in a sign confirmation state.

<a name="signTypedData"></a>
## `metamask.signTypedData(): Promise<void>`
commands MetaMask to sign a message. For this to work MetaMask must be in a sign typed data confirmation state.

<a name="approve"></a>
## `metamask.approve(): Promise<void>`
enables the app to connect to MetaMask account in privacy mode
Expand Down
4 changes: 2 additions & 2 deletions docs/JEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Ethereum', () => {
await page.goto('https://ethereum.org/en/');
});

it('should be titled "Google"', async () => {
it('should be titled "Home | ethereum.org"', async () => {
await expect(page.title()).resolves.toMatch('Home | ethereum.org');
});
});
Expand All @@ -28,7 +28,7 @@ To configure Dappeteer to use custom config values as `metamaskVersion` or own `

**example of `dappeteer.config.js`**
``` js
/** @type {import('@chainsafe/dappeteer').DappateerJestConfig} */
/** @type {import('@chainsafe/dappeteer').DappeteerJestConfig} */

const config = {
dappeteer: {
Expand Down
2 changes: 1 addition & 1 deletion src/metamask/switchNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DappeteerPage } from "../page";
// TODO: validate - for now works fine as it is.
export const switchNetwork =
(page: DappeteerPage) =>
async (network: string = "main"): Promise<void> => {
async (network: string = "mainnet"): Promise<void> => {
await page.bringToFront();
await openNetworkDropdown(page);

Expand Down