Skip to content

Commit

Permalink
feat: zkstack support !wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arthcp committed Nov 18, 2024
1 parent 7f9cbbd commit bf35f4d
Show file tree
Hide file tree
Showing 27 changed files with 1,468 additions and 348 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ POLTER_TESTNET_RPC=' '
POLYGON_AMOY_RPC=' '
OPBNB_RPC=' '
GEIST_RPC=' '

ZERO_SEPOLIA_RPC=' '
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ switchboards.json
devRpcConfig.ts
devRpcConfig.json
prodRpcConfig.ts
prodRpcConfig.json
prodRpcConfig.json

artifacts-zk/
cache_hardhat-zk/
deployments-zk/
3 changes: 2 additions & 1 deletion contracts/examples/Messenger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ contract Messenger is IPlug, Ownable(msg.sender) {
}

function removeGas(address payable receiver_) external onlyOwner {
receiver_.transfer(address(this).balance);
// receiver_.transfer(address(this).balance);
payable(receiver_).call{value: address(this).balance}("");
}

function sendLocalMessage(bytes32 message_) external {
Expand Down
3 changes: 2 additions & 1 deletion contracts/socket/SocketBatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ contract SocketBatcher is AccessControl {
uint256 totalAmount;
for (uint i; i < addresses.length; i++) {
totalAmount += amounts[i];
addresses[i].transfer(amounts[i]);
// addresses[i].transfer(amounts[i]);
payable(addresses[i]).call{value: amounts[i]}("");
}

require(totalAmount == msg.value, "LOW_MSG_VALUE");
Expand Down
14 changes: 13 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-etherscan";
// import "@nomiclabs/hardhat-etherscan";
import "@typechain/hardhat";
import "hardhat-preprocessor";
import "hardhat-deploy";
import "hardhat-abi-exporter";
import "hardhat-change-network";
import "@matterlabs/hardhat-zksync";

import { config as dotenvConfig } from "dotenv";
import type { HardhatUserConfig } from "hardhat/config";
Expand Down Expand Up @@ -340,6 +341,13 @@ const config: HardhatUserConfig = {
chainId: hardhatChainNameToSlug[HardhatChainName.HARDHAT],
},
...liveNetworks,
zeroTestnet: {
url: "https://zerion-testnet-proofs.rpc.caldera.xyz/http",
zksync: true,
ethNetwork: "sepolia",
verifyURL:
"https://api-explorer.zero.network/contract/contract_verification",
},
},
paths: {
sources: "./contracts",
Expand All @@ -362,6 +370,10 @@ const config: HardhatUserConfig = {
},
}),
},
zksolc: {
version: "latest", // Uses latest available in https://github.com/matter-labs/zksolc-bin
settings: {},
},
solidity: {
version: "0.8.19",
settings: {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@aws-sdk/client-s3": "^3.465.0",
"@eth-optimism/sdk": "^3.2.3",
"@ethersproject/bytes": "^5.7.0",
"@matterlabs/hardhat-zksync": "^1.2.0",
"@fxportal/maticjs-fxportal": "^1.1.1",
"@maticnetwork/maticjs": "^3.5.0",
"@maticnetwork/maticjs-ethers": "^1.0.2",
Expand All @@ -41,7 +42,7 @@
"dotenv": "^16.0.3",
"ethereum-waffle": "^3.2.0",
"ethers": "^5.6.6",
"hardhat": "^2.9.5",
"hardhat": "^2.22.7",
"hardhat-abi-exporter": "^2.10.1",
"hardhat-change-network": "^0.0.7",
"hardhat-deploy": "^0.11.20",
Expand All @@ -51,7 +52,8 @@
"prettier-plugin-solidity": "^1.0.0-beta.13",
"ts-node": "^10.7.0",
"typechain": "^8.0.0",
"typescript": "^4.6.4"
"typescript": "^4.6.4",
"zksync-ethers": "5"
},
"scripts": {
"lint": "prettier \"./**\" --write",
Expand Down
26 changes: 26 additions & 0 deletions scripts/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import {
hardhatChainNameToSlug,
HardhatChainName,
chainSlugToHardhatChainName,
zkStackChain,
} from "../../src";

import "@matterlabs/hardhat-zksync-node/dist/type-extensions";
import {
Wallet as zkWallet,
Provider,
} from "@matterlabs/hardhat-zksync/node_modules/zksync-ethers";

const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || "./.env";
dotenvConfig({ path: resolve(__dirname, dotenvConfigPath) });

Expand All @@ -22,6 +29,11 @@ function createReverseEnumMap(enumObj: any) {
return reverseMap;
}

export const chainIdToSlug = (chainId: ChainId) => {
const chainName = chainSlugReverseMap.get(chainId.toString());
return ChainSlug[chainName as keyof typeof ChainSlug];
};

export const rpcKeys = (chainSlug: ChainSlug) => {
const chainName = chainSlugToHardhatChainName[chainSlug].toString();
return `${chainName.toUpperCase()}_RPC`;
Expand All @@ -47,3 +59,17 @@ export const getProviderFromChainSlug = (chainSlug: ChainSlug) => {
export const getProviderFromChainName = (chainName: HardhatChainName) => {
return getProviderFromChainSlug(hardhatChainNameToSlug[chainName]);
};

export const getZkWallet = (chainSlug: ChainSlug) => {
console.log({ chainSlug });
if (!zkStackChain.includes(chainSlug))
throw new Error(`Chain ${chainSlug} is not a zkStack chain`);
if (!process.env.SOCKET_SIGNER_KEY)
throw new Error("SOCKET_SIGNER_KEY not set");

const rpc = getJsonRpcUrl(chainSlug);
console.log({ rpc });
const provider = new Provider(rpc);
console.log({ provider });
return new zkWallet(process.env.SOCKET_SIGNER_KEY, provider);
};
53 changes: 29 additions & 24 deletions scripts/deploy/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DeploymentAddresses, MainnetIds, TestnetIds } from "../../src";
import {
ChainSlug,
DeploymentAddresses,
MainnetIds,
TestnetIds,
} from "../../src";
import { configureRoles } from "./scripts/configureRoles";
import { deployForChains } from "./scripts/deploySocketFor";
import { configureSwitchboards } from "./scripts/configureSwitchboards";
Expand All @@ -8,30 +13,30 @@ import { executionManagerVersion } from "./config/config";

const main = async () => {
try {
const response = await prompts([
{
name: "chainType",
type: "select",
message: "Select chains network type",
choices: [
{
title: "Mainnet",
value: "mainnet",
},
{
title: "Testnet",
value: "testnet",
},
],
},
]);
// const response = await prompts([
// {
// name: "chainType",
// type: "select",
// message: "Select chains network type",
// choices: [
// {
// title: "Mainnet",
// value: "mainnet",
// },
// {
// title: "Testnet",
// value: "testnet",
// },
// ],
// },
// ]);

const chainOptions =
response.chainType === "mainnet" ? MainnetIds : TestnetIds;
let choices = chainOptions.map((chain) => ({
title: chain.toString(),
value: chain,
}));
// const chainOptions =
// response.chainType === "mainnet" ? MainnetIds : TestnetIds;
// let choices = chainOptions.map((chain) => ({
// title: chain.toString(),
// value: chain,
// }));

const chainsResponse = await prompts([
{
Expand Down
8 changes: 7 additions & 1 deletion scripts/deploy/single-click-deploy/integrators/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const updateSDK = async (
);
await updateFile(
"chainSlugToHardhatChainName.ts",
`,\n [ChainSlug.${chainName.toUpperCase()}]: [HardhatChainName.${chainName.toUpperCase()}],\n};\n`,
`,\n [ChainSlug.${chainName.toUpperCase()}]: HardhatChainName.${chainName.toUpperCase()},\n};\n`,
",\n};"
);

Expand Down Expand Up @@ -152,6 +152,12 @@ export const updateSDK = async (
`,\n ChainSlug.${chainName.toUpperCase()},\n];`,
",\n];"
);
} else if (chainTypeInString === ChainType.zkStackChain) {
await updateFile(
"zkStackChain.ts",
`,\n ChainSlug.${chainName.toUpperCase()},\n];`,
",\n];"
);
} else {
await updateFile(
"ethLikeChains.ts",
Expand Down
80 changes: 68 additions & 12 deletions scripts/deploy/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Wallet, utils } from "ethers";
import { network, ethers, run } from "hardhat";

import { ContractFactory, Contract } from "ethers";
import { Address } from "hardhat-deploy/dist/types";
import path from "path";
Expand All @@ -11,10 +10,14 @@ import {
ChainSocketAddresses,
DeploymentAddresses,
DeploymentMode,
zkStackChain,
} from "../../../src";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { overrides } from "../config/config";
import { VerifyArgs } from "../verify";
import { chainIdToSlug, getZkWallet } from "../../constants";
import { Deployer } from "@matterlabs/hardhat-zksync";
import * as hre from "hardhat";

export const deploymentsPath = path.join(__dirname, `/../../../deployments/`);

Expand Down Expand Up @@ -78,23 +81,76 @@ export const getOrDeploy = async (
return contract;
};

// export async function deployContractWithArgs(
// contractName: string,
// args: Array<any>,
// signer: SignerWithAddress | Wallet
// ) {
// try {
// const Contract: ContractFactory = await ethers.getContractFactory(
// contractName
// );
// // gasLimit is set to undefined to not use the value set in overrides
// const contract: Contract = await Contract.connect(signer).deploy(...args, {
// ...(await overrides(await signer.getChainId())),
// });
// await contract.deployed();
// return contract;
// } catch (error) {
// throw error;
// }
// }

export async function deployContractWithArgs(
contractName: string,
args: Array<any>,
signer: SignerWithAddress | Wallet
) {
): Promise<Contract> {
try {
const Contract: ContractFactory = await ethers.getContractFactory(
contractName
);
// gasLimit is set to undefined to not use the value set in overrides
const contract: Contract = await Contract.connect(signer).deploy(...args, {
...(await overrides(await signer.getChainId())),
});
await contract.deployed();
return contract;
const chainId = (await signer.provider.getNetwork()).chainId;
const chainSlug = chainIdToSlug(chainId);
if (zkStackChain.includes(chainSlug)) {
const wallet = getZkWallet(chainSlug);
const deployer = new Deployer(hre, wallet);
const artifact = await deployer
.loadArtifact(contractName)
.catch((error) => {
if (
error?.message?.includes(
`Artifact for contract "${contractName}" not found.`
)
) {
console.error(error.message);
throw `⛔️ Please make sure you have compiled your contracts or specified the correct contract name!`;
} else {
throw error;
}
});
const contract = await deployer.deploy(artifact, args);
const address = await contract.getAddress();
const Contract: ContractFactory = await ethers.getContractFactory(
contractName
);
// console.log(contract);
// contract.address = address;
return Contract.attach(address);
} else {
const Contract: ContractFactory = await ethers.getContractFactory(
contractName
);
const contract: Contract = await Contract.connect(signer).deploy(
...args,
{
...(await overrides(chainSlug)),
}
);
await contract.deployed();
return contract;
}
} catch (error) {
throw error;
// throw error;
console.log(error);
process.exit(1);
}
}

Expand Down
1 change: 1 addition & 0 deletions scripts/rpcConfig/constants/batcherSupportedChainSlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export const batcherSupportedChainSlugs = [
ChainSlug.POLYGON_AMOY,
ChainSlug.OPBNB,
ChainSlug.GEIST,
ChainSlug.ZERO_SEPOLIA,
];
1 change: 1 addition & 0 deletions scripts/rpcConfig/constants/explorers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export const explorers = {
[ChainSlug.POLYGON_AMOY]: "https://amoy.polygonscan.com/",
[ChainSlug.OPBNB]: "https://opbnb.bscscan.com/",
[ChainSlug.GEIST]: "https://geist-mainnet.explorer.alchemy.com/",
[ChainSlug.ZERO_SEPOLIA]: "https://explorer.zero.network/",
};
1 change: 1 addition & 0 deletions scripts/rpcConfig/constants/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export const icons = {
[ChainSlug.POLYGON_AMOY]: "",
[ChainSlug.OPBNB]: "",
[ChainSlug.GEIST]: "",
[ChainSlug.ZERO_SEPOLIA]: "",
};
1 change: 1 addition & 0 deletions scripts/rpcConfig/constants/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ export const rpcs = {
[ChainSlug.POLYGON_AMOY]: checkEnvValue("POLYGON_AMOY_RPC"),
[ChainSlug.OPBNB]: checkEnvValue("OPBNB_RPC"),
[ChainSlug.GEIST]: checkEnvValue("GEIST_RPC"),
[ChainSlug.ZERO_SEPOLIA]: checkEnvValue("ZERO_SEPOLIA_RPC"),
};
3 changes: 3 additions & 0 deletions scripts/rpcConfig/rpcConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
S3Config,
TestnetIds,
TxData,
zkStackChain,
} from "../../src";
import { getSiblings } from "../common";
import { chainOverrides } from "../constants/overrides";
Expand Down Expand Up @@ -76,6 +77,8 @@ const getChainType = (chainSlug: ChainSlug) => {
} else if (arbL3Chains.includes(chainSlug)) {
return ChainType.arbL3Chain;
} else if (polygonCDKChains.includes(chainSlug)) {
return ChainType.zkStackChain;
} else if (zkStackChain.includes(chainSlug)) {
return ChainType.polygonCDKChain;
} else return ChainType.default;
};
Expand Down
1 change: 1 addition & 0 deletions src/enums/chainId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ export enum ChainId {
POLYGON_AMOY = 80002,
OPBNB = 204,
GEIST = 63157,
ZERO_SEPOLIA = 4457845,
}
1 change: 1 addition & 0 deletions src/enums/chainSlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ export enum ChainSlug {
POLYGON_AMOY = ChainId.POLYGON_AMOY,
OPBNB = ChainId.OPBNB,
GEIST = ChainId.GEIST,
ZERO_SEPOLIA = ChainId.ZERO_SEPOLIA,
}
Loading

0 comments on commit bf35f4d

Please sign in to comment.