-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add-aa-example
- Loading branch information
Showing
9 changed files
with
203 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This script will: | ||
// 1. Send ETH to the pension account | ||
|
||
import { ethers } from "ethers"; | ||
import { Provider, Wallet } from "zksync-web3"; | ||
import dotenv from "dotenv"; | ||
import { getDeployedContractDetailsFromVars, config } from "../deploy/utils"; | ||
|
||
dotenv.config(); | ||
|
||
const NETWORK = "zkSyncLocalnet"; | ||
const RPC_URL = config.L2RpcUrl; | ||
const PRIVATE_KEY = config.firstWalletPrivateKey; | ||
|
||
async function main() { | ||
const provider = new Provider(RPC_URL); | ||
const mainWallet = new Wallet(PRIVATE_KEY, provider); | ||
|
||
// Load the contract address from vars.json | ||
const paAddress = getDeployedContractDetailsFromVars(NETWORK, "PensionAccount").address; | ||
// send 100 ETH to the paAddress | ||
const balance = await provider.getBalance(paAddress); | ||
const tx = await mainWallet.sendTransaction({ | ||
to: paAddress, | ||
value: ethers.utils.parseEther("100"), | ||
}); | ||
await tx.wait(); | ||
const newBalance = await provider.getBalance(paAddress); | ||
console.log(`Sent 100 ETH to Pension Account at: ${paAddress}`); | ||
console.log(`Balance before: ${ethers.utils.formatEther(balance)} ETH`); | ||
console.log(`Balance after: ${ethers.utils.formatEther(newBalance)} ETH`); | ||
} | ||
|
||
main() | ||
.then(() => process.exit(0)) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// This script will: | ||
// 1. Attempt to send ETH from the pension contract to another address. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { utils, Wallet } from "zksync-web3"; | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
import { Deployer } from "@matterlabs/hardhat-zksync-deploy"; | ||
import { config, saveContractToVars } from "./utils"; | ||
|
||
const KEY = config.firstWalletPrivateKey; | ||
|
||
export default async function (hre: HardhatRuntimeEnvironment) { | ||
// Private key of the account used to deploy | ||
const wallet = new Wallet(KEY); | ||
const deployer = new Deployer(hre, wallet); | ||
const pensionAccountFactoryArtifact = await deployer.loadArtifact("PensionAccountFactory"); | ||
const paArtifact = await deployer.loadArtifact("PensionAccount"); | ||
|
||
// Getting the bytecodeHash of the account | ||
const bytecodeHash = utils.hashBytecode(paArtifact.bytecode); | ||
|
||
const factory = await deployer.deploy( | ||
pensionAccountFactoryArtifact, | ||
[bytecodeHash], | ||
undefined, | ||
[ | ||
// Since the factory requires the code of the multisig to be available, | ||
// we should pass it here as well. | ||
paArtifact.bytecode, | ||
] | ||
); | ||
|
||
console.log(`Pension Account factory address: ${factory.address}`); | ||
saveContractToVars(hre.network.name, "PensionAccountFactory", factory.address); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { utils, Wallet, Provider } from "zksync-web3"; | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
import { config, getDeployedContractDetailsFromVars, createMockAddress, saveContractToVars } from "./utils"; | ||
import { ethers } from "ethers"; | ||
|
||
const KEY = config.firstWalletPrivateKey; | ||
const OWNER_KEY = config.secondWalletPrivateKey; | ||
|
||
export default async function (hre: HardhatRuntimeEnvironment) { | ||
const provider = new Provider(hre.network.config.url); | ||
const wallet = new Wallet(KEY, provider); | ||
const walletOwner = new Wallet(OWNER_KEY, provider); | ||
const factoryAddress = getDeployedContractDetailsFromVars(hre.network.name, "PensionAccountFactory").address; | ||
const paFactoryArtifact = await hre.artifacts.readArtifact("PensionAccountFactory"); | ||
|
||
const paFactory = new ethers.Contract(factoryAddress, paFactoryArtifact.abi, wallet); | ||
|
||
// Contract constructor args | ||
const dex = createMockAddress("decentralizedex"); // "0xdex0000000000000000000000000000000000000" | ||
const doge = createMockAddress("dogecoin"); // "0xdoge000000000000000000000000000000000000" | ||
const pepe = createMockAddress("pepecoin"); // "0xpepe000000000000000000000000000000000000" | ||
const shib = createMockAddress("shibainucoin"); // "0xshib0000000000000000000000000000000000000" | ||
const btc = createMockAddress("bitcoin"); // "0xbtc00000000000000000000000000000000000000" | ||
|
||
// For the simplicity of the tutorial, we will use zero hash as salt | ||
const salt = ethers.constants.HashZero; | ||
|
||
// deploy account with dex and token addresses | ||
const tx = await paFactory.deployPensionAccount(salt, walletOwner.address, dex, doge, pepe, shib, btc, { gasLimit: 10000000 }); | ||
await tx.wait(); | ||
|
||
// Getting the address of the deployed contract account | ||
const abiCoder = new ethers.utils.AbiCoder(); | ||
let contractAddress = utils.create2Address( | ||
factoryAddress, | ||
await paFactory.pensionAccountBytecodeHash(), | ||
salt, | ||
abiCoder.encode(["address", "address", "address", "address", "address", "address"], [walletOwner.address, dex, doge, pepe, shib, btc]) | ||
); | ||
saveContractToVars(hre.network.name, "PensionAccount", contractAddress); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const JSON_FILE_PATH = path.join(__dirname, "vars.json"); | ||
|
||
export function saveContractToVars(network: string, contractName: string, contractAddress: string, varsPath = JSON_FILE_PATH) { | ||
console.log(`Saving ${contractName} to vars.json`); | ||
const config = JSON.parse(fs.readFileSync(varsPath, "utf-8")); | ||
|
||
if (!config[network]) { | ||
config[network] = { deployed: [] }; | ||
} | ||
|
||
const deployedContracts = config[network].deployed; | ||
const existingContractIndex = deployedContracts.findIndex((contract: { name: string; }) => contract.name === contractName); | ||
|
||
if (existingContractIndex === -1) { | ||
console.log(`Adding ${contractName} to vars.json`); | ||
deployedContracts.push({ | ||
name: contractName, | ||
address: contractAddress, | ||
}); | ||
} else { | ||
console.log(`Updating ${contractName} in vars.json`); | ||
deployedContracts[existingContractIndex].address = contractAddress; | ||
} | ||
|
||
fs.writeFileSync(varsPath, JSON.stringify(config, null, 2)); | ||
} | ||
|
||
export function getDeployedContractDetailsFromVars(network: string, contractName: string, varsPath = JSON_FILE_PATH) { | ||
const config = JSON.parse(fs.readFileSync(varsPath, "utf-8")); | ||
const deployedContracts = config[network].deployed; | ||
const existingContract = deployedContracts.find((contract: { name: string; }) => contract.name === contractName); | ||
|
||
if (!existingContract) { | ||
throw new Error(`Contract ${contractName} not found in vars.json`); | ||
} | ||
|
||
return existingContract; | ||
} | ||
|
||
export const config = { | ||
L2RpcUrl: "http://127.0.0.1:8011", | ||
firstWalletPrivateKey: "0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110", | ||
firstWalletAddress: "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049", | ||
secondWalletPrivateKey: "0xac1e735be8536c6534bb4f17f06f6afc73b2b5ba84ac2cfb12f7461b20c0bbe3", | ||
secondWalletAddress: "0xa61464658AfeAf65CccaaFD3a512b69A83B77618", | ||
}; | ||
|
||
export function createMockAddress(base: string) { | ||
const baseHex = base.replace(/[^0-9A-Fa-f]/g, ''); // Remove non-hex characters | ||
const paddingLength = 40 - baseHex.length; // Calculate padding length | ||
return '0x' + baseHex + '0'.repeat(paddingLength); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"zkSyncLocalnet": { | ||
"deployed": [ | ||
{ | ||
"name": "PensionAccountFactory", | ||
"address": "0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021" | ||
}, | ||
{ | ||
"name": "PensionAccount", | ||
"address": "0x7A4C681d869bfF421d752B696cd8eC6eD6c7EfBa" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters