Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zkSync era #1

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,10 @@ dist
.tern-port

.nvmrc

# hardhat
artifacts-zk/
cache-zk/

# zksync local setup
local-setup/
53 changes: 53 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import '@matterlabs/hardhat-zksync-solc'
import '@matterlabs/hardhat-zksync-verify'
import 'hardhat-dependency-compiler'
import { task } from 'hardhat/config'
import {deployV3} from './index'

task('deploy-v3')
.addParam('privateKey', 'Private key used to deploy all contracts')
.addParam('jsonRpc', 'JSON RPC URL where the program should be deployed')
.addParam('weth9Address', 'Address of the WETH9 contract on this chain')
.addParam('nativeCurrencyLabel', 'Native currency label, e.g. ETH')
.addParam('ownerAddress', 'Contract address that will own the deployed artifacts after the script runs')
.addOptionalParam('state', 'Path to the JSON file containing the migrations state (optional)', './state.json')
.addOptionalParam('v2CoreFactoryAddress', 'The V2 core factory address used in the swap router (optional)')
.addOptionalParam('gasPrice', 'The gas price to pay in GWEI for each transaction (optional)')
.addOptionalParam('confirmations', 'How many confirmations to wait for after each transaction (optional)', '2')
.setAction(async (taskArgs) => {
await deployV3(taskArgs)
})

export default {
networks: {
hardhat: {
allowUnlimitedContractSize: false,
zksync: true,
},
},
solidity: {
version: '0.7.6',
settings: {
optimizer: {
enabled: true,
},
metadata: {
bytecodeHash: 'none',
},
},
},
zksolc: {
version: "1.3.10",
compilerSource: "binary",
settings: {
metadata: {
bytecodeHash: 'none',
},
},
},
dependencyCompiler: {
paths: [
'v3-periphery-1_3_0/contracts/NonfungibleTokenPositionDescriptor.sol',
],
},
}
263 changes: 124 additions & 139 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,161 +1,146 @@
import { program } from 'commander'
import { Wallet } from '@ethersproject/wallet'
import { JsonRpcProvider, TransactionReceipt } from '@ethersproject/providers'
import { Wallet, Provider } from 'zksync-web3'
import { TransactionReceipt } from '@ethersproject/providers'
import { AddressZero } from '@ethersproject/constants'
import { getAddress } from '@ethersproject/address'
import fs from 'fs'
import deploy from './src/deploy'
import { MigrationState } from './src/migrations'
import {MigrationState, StepOutput} from './src/migrations'
import { asciiStringToBytes32 } from './src/util/asciiStringToBytes32'
import { version } from './package.json'

program
.requiredOption('-pk, --private-key <string>', 'Private key used to deploy all contracts')
.requiredOption('-j, --json-rpc <url>', 'JSON RPC URL where the program should be deployed')
.requiredOption('-w9, --weth9-address <address>', 'Address of the WETH9 contract on this chain')
.requiredOption('-ncl, --native-currency-label <string>', 'Native currency label, e.g. ETH')
.requiredOption(
'-o, --owner-address <address>',
'Contract address that will own the deployed artifacts after the script runs'
)
.option('-s, --state <path>', 'Path to the JSON file containing the migrations state (optional)', './state.json')
.option('-v2, --v2-core-factory-address <address>', 'The V2 core factory address used in the swap router (optional)')
.option('-g, --gas-price <number>', 'The gas price to pay in GWEI for each transaction (optional)')
.option('-c, --confirmations <number>', 'How many confirmations to wait for after each transaction (optional)', '2')

program.name('npx @uniswap/deploy-v3').version(version).parse(process.argv)

if (!/^0x[a-zA-Z0-9]{64}$/.test(program.privateKey)) {
console.error('Invalid private key!')
process.exit(1)
}

let url: URL
try {
url = new URL(program.jsonRpc)
} catch (error) {
console.error('Invalid JSON RPC URL', (error as Error).message)
process.exit(1)
}

let gasPrice: number | undefined
try {
gasPrice = program.gasPrice ? parseInt(program.gasPrice) : undefined
} catch (error) {
console.error('Failed to parse gas price', (error as Error).message)
process.exit(1)
}

let confirmations: number
try {
confirmations = parseInt(program.confirmations)
} catch (error) {
console.error('Failed to parse confirmations', (error as Error).message)
process.exit(1)
}

let nativeCurrencyLabelBytes: string
try {
nativeCurrencyLabelBytes = asciiStringToBytes32(program.nativeCurrencyLabel)
} catch (error) {
console.error('Invalid native currency label', (error as Error).message)
process.exit(1)
}

let weth9Address: string
try {
weth9Address = getAddress(program.weth9Address)
} catch (error) {
console.error('Invalid WETH9 address', (error as Error).message)
process.exit(1)
}

let v2CoreFactoryAddress: string
if (typeof program.v2CoreFactoryAddress === 'undefined') {
v2CoreFactoryAddress = AddressZero
} else {

export async function deployV3(args: any) {
if (!/^0x[a-zA-Z0-9]{64}$/.test(args.privateKey)) {
console.error('Invalid private key!')
process.exit(1)
}

let url: URL
try {
url = new URL(args.jsonRpc)
} catch (error) {
console.error('Invalid JSON RPC URL', (error as Error).message)
process.exit(1)
}

let gasPrice: number | undefined
try {
v2CoreFactoryAddress = getAddress(program.v2CoreFactoryAddress)
gasPrice = args.gasPrice ? parseInt(args.gasPrice) : undefined
} catch (error) {
console.error('Invalid V2 factory address', (error as Error).message)
console.error('Failed to parse gas price', (error as Error).message)
process.exit(1)
}
}

let ownerAddress: string
try {
ownerAddress = getAddress(program.ownerAddress)
} catch (error) {
console.error('Invalid owner address', (error as Error).message)
process.exit(1)
}
let confirmations: number
try {
confirmations = parseInt(args.confirmations)
} catch (error) {
console.error('Failed to parse confirmations', (error as Error).message)
process.exit(1)
}

const wallet = new Wallet(program.privateKey, new JsonRpcProvider({ url: url.href }))
let nativeCurrencyLabelBytes: string
try {
nativeCurrencyLabelBytes = asciiStringToBytes32(args.nativeCurrencyLabel)
} catch (error) {
console.error('Invalid native currency label', (error as Error).message)
process.exit(1)
}

let state: MigrationState
if (fs.existsSync(program.state)) {
let weth9Address: string
try {
state = JSON.parse(fs.readFileSync(program.state, { encoding: 'utf8' }))
weth9Address = getAddress(args.weth9Address)
} catch (error) {
console.error('Failed to load and parse migration state file', (error as Error).message)
console.error('Invalid WETH9 address', (error as Error).message)
process.exit(1)
}
} else {
state = {}
}

let finalState: MigrationState
const onStateChange = async (newState: MigrationState): Promise<void> => {
fs.writeFileSync(program.state, JSON.stringify(newState))
finalState = newState
}

async function run() {
let step = 1
const results = []
const generator = deploy({
signer: wallet,
gasPrice,
nativeCurrencyLabelBytes,
v2CoreFactoryAddress,
ownerAddress,
weth9Address,
initialState: state,
onStateChange,
})

for await (const result of generator) {
console.log(`Step ${step++} complete`, result)
results.push(result)

// wait 15 minutes for any transactions sent in the step
await Promise.all(
result.map(
(stepResult): Promise<TransactionReceipt | true> => {
if (stepResult.hash) {
return wallet.provider.waitForTransaction(stepResult.hash, confirmations, /* 15 minutes */ 1000 * 60 * 15)
} else {
return Promise.resolve(true)
}
}
)
)

let v2CoreFactoryAddress: string
if (typeof args.v2CoreFactoryAddress === 'undefined') {
v2CoreFactoryAddress = AddressZero
} else {
try {
v2CoreFactoryAddress = getAddress(args.v2CoreFactoryAddress)
} catch (error) {
console.error('Invalid V2 factory address', (error as Error).message)
process.exit(1)
}
}

return results
}
let ownerAddress: string
try {
ownerAddress = getAddress(args.ownerAddress)
} catch (error) {
console.error('Invalid owner address', (error as Error).message)
process.exit(1)
}

run()
.then((results) => {
console.log('Deployment succeeded')
console.log(JSON.stringify(results))
console.log('Final state')
console.log(JSON.stringify(finalState))
process.exit(0)
})
.catch((error) => {
const wallet = new Wallet(args.privateKey, new Provider({url: url.href}))

let state: MigrationState
if (fs.existsSync(args.state)) {
try {
state = JSON.parse(fs.readFileSync(args.state, {encoding: 'utf8'}))
} catch (error) {
console.error('Failed to load and parse migration state file', (error as Error).message)
process.exit(1)
}
} else {
state = {}
}

let finalState: MigrationState
const onStateChange = async (newState: MigrationState): Promise<void> => {
fs.writeFileSync(args.state, JSON.stringify(newState))
finalState = newState
}

async function run() {
let step = 1
const results = []
const generator = deploy({
signer: wallet,
gasPrice,
nativeCurrencyLabelBytes,
v2CoreFactoryAddress,
ownerAddress,
weth9Address,
initialState: state,
onStateChange,
})

for await (const result of generator) {
console.log(`Step ${step++} complete`, result)
results.push(result)

// wait 15 minutes for any transactions sent in the step
await Promise.all(
result.map(
(stepResult): Promise<TransactionReceipt | true> => {
if (stepResult.hash) {
return wallet.provider.waitForTransaction(stepResult.hash, confirmations, /* 15 minutes */ 1000 * 60 * 15)
} else {
return Promise.resolve(true)
}
}
)
)
}

return results
}

let results: StepOutput[][]
try {
results = await run()
} catch (error) {
console.error('Deployment failed', error)
console.log('Final state')
console.log(JSON.stringify(finalState))
console.log(JSON.stringify(finalState!))
process.exit(1)
})
}

console.log('Deployment succeeded')
console.log(JSON.stringify(results))
console.log('Final state')
console.log(JSON.stringify(finalState!))
process.exit(0)
}
Loading