Migrating an Existing App
How to migrate your existing app to Treasure Chain.
This guide assumes you have already deployed your app to an existing Ethereum network.
Setting up a Hardhat project
Abstract has a great guide on how to migrate your existing Hardhat project to ZKsync here.
For a specific example of deploying a contract to Topaz checkout the example guide below.
Setting up a Foundry project
For Foundry projects, we recommend using the foundryup-zksync package. Building for ZKsync and running tests is as simple, however you may run into issues with the zksolc compiler. We currently recommend to use Hardhat instead of Foundry for deploying contracts to Treasure Chain.
Install ZKsync Foundry
If you are on macOS you will need to install libusb
first:
If you are on Windows, you will need to install and use Git BASH or WSL, as your terminal, since foundryup-zksync currently does not support Powershell or Cmd. Windows support is currently provided as best-effort.
Then install the foundryup-zksync package:
Set forge to use zksync compiler
Add --zksync
to your forge commands:
Add zkout to your .gitignore
When building with zksync, output files are now generated in the zkout
directory instead of out
.
Adjust your contracts
You may need to adjust your contracts based off the limitations of ZKsync’s Foundry fork.
Deploy your contracts
Deploy your contract with forge create
and specify the blockchain information:
Deploying to Topaz with Hardhat
Whether your project is setup with Foundry or Hardhat, you can use the following example to deploy your contract to Topaz using hardhat-deploy
which supports ZKsync.
You can also use hardhat-zksync
to deploy a Foundry project. Additionally,
hardhat-deploy
is not maintained by (nor receiving contributions from)
Matter Labs, the creators of ZKsync.
In this example we are deploying a contract using a wallet stored in the AWS Key Management Service (KMS) using the @treasure-dev/hardhat-kms
package.
In the Abstract docs linked above they provide an example using a private key instead of a KMS key.
Install the required packages
This will install the following packages:
dotenv
: To load environment variables from a.env
file.hardhat-deploy
: To deploy contracts to a network.@treasure-dev/hardhat-kms
: To use a KMS key to sign your deployments.hardhat
: The main Hardhat package.@matterlabs/hardhat-zksync
: The zksolc compiler.@matterlabs/hardhat-zksync-verify
: To verify contracts on zkSync.@nomicfoundation/hardhat-foundry
: To use Foundry to compile your contracts. (Needed if you are using Foundry instead of Hardhat)ethers
: Used in the deploy process.zksync-ethers
: Used in the deploy process.ts-node
: To run TypeScript files.typescript
: To compile TypeScript files.
Setup your .env file
In your .env
file you will need to define the following variables:
This should be the profile, region, and ARN of the KMS key you want to use to sign your deployments. You can find the ARN in the AWS console under the KMS key you want to use.
Add a hardhat.config.ts file
This hardhat.config.ts
file is using the network information for Topaz as seen in the network details.
Add a tsconfig.json file
This tsconfig.json
file is used for running your deploy script.
Make sure to include any other typescript files you may have in your project.
Create your deploy script
Create a deploy
directory and add a deploy.ts
file to it.
Replace <YOUR_CONTRACT_NAME>
with the name of your contract.
Add Hardhat scripts to your package.json
Add the following scripts to your package.json
file:
clean
: Removes theartifacts
andcache
directories.compile
: Compiles your contracts for Topaz.deploy:treasure-topaz
: Deploys your contracts to Topaz.
Run your deploy script
Run the following command to deploy your contract:
This will deploy your contract to Topaz using the KMS key you specified in your .env
file.