Writing a wallet setup file
File name
Since wallet setup files play a crucial role, they must follow a strict naming convention. The file name must follow this format:*.setup.{ts,js,mjs}
.
Here are some examples of valid file names:
basic.setup.ts
connected.setup.ts
optimism.setup.ts
multi-user.setup.ts
Import
Let’s start with the import statement. You need to import thedefineWalletSetup
function from @synthetixio/synpress
:
Prepare seed phrase and password
Next, you need to define the seed phrase and password that will be used to import the wallet. You can define them as constants, use environment variables, or do whatever suits your needs.You can replace
dotenv
with any other alternative to load the environmental variables.Define the wallet setup
Finally, you need to define the wallet setup steps. This is done by calling thedefineWalletSetup
function. It takes two arguments, the password, and the setup function.
The setup function is where the magic happens. Contents of this function will be executed in the browser context, which means you can use all browser-related Playwright APIs.
Here’s how you can import a wallet with MetaMask or Phantom:
Building the cache
PREREQUISITE: Before building the cache, you must have relevant Playwright browsers installed on your machine. See this Playwright guide for instructions on how to do it.
synpress
. Here’s how to do it:
[PROJECT_ROOT]/test/wallet-setup
directory. If you want to use a different one, you can pass it as an argument to the CLI:
Run
synpress --help
to see all available options.By default, the browsers will be launched in the headed mode. If you want to run them in the headless mode, you can pass the
--headless
flag to the CLI or set the HEADLESS
environmental variable to true
.How it works
Basics
Understanding how the wallet cache works is crucial for writing wallet setup files. Let’s start with the basics. Here’s the wallet setup file from the previous section:basic.setup.ts
[PROJECT_ROOT]/.cache-synpress
directory.
Each cache is saved in a separate directory named after the hash of the wallet setup file.
You should add the
.cache-synpress
directory to your .gitignore
file.IMPORTANT NOTE: If you intend to use MetaMask or Phantom to send transactions, sign data, or do anything that requires interactions through the pop-up, you must adjust your wallet setup file by including its extension ID.💡 Show details below to see the adjusted wallet setup file
Hash generation
The hash is generated ONLY from the contents of the function you pass to thedefineWalletSetup
function. This means you can import other files, define constants, etc., without affecting the hash.
Based on the wallet setup above, only the following lines will be used to generate the hash:
SEED_PHRASE
constant and inline it like this:
NOTE:
There’s one exception to this rule. All
console.log
and debugger
statements are stripped out before the hash is generated, which means they do NOT affect the hash.To learn more about this, see the Debugging Wallet Setup Files section.Things to keep in mind
Here are a few things that are important to keep in mind when dealing with setup files:- You can do whatever you want in the setup function as long as it’s not affecting the state of a blockchain in any way. This means you can add wallets, networks, change settings, etc., but you cannot send any transactions. However, you can still sign data with imported wallets, which is useful for connecting to dapps. With this, you can start tests with a fully configured wallet connected to a dapp.
- You don’t have to close the browser or any tabs in the setup function. Synpress will handle everything.
- You can have multiple wallet setup files. Synpress will generate a cache for each of them. However, currently, all caches are built in parallel, so if you have a ton of setup files and a slow computer, it might be impossible to build them. We’re working on a solution for this.