Skip to content

Commit

Permalink
compact with multi rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
zkcarter committed Nov 30, 2023
1 parent 61d7288 commit 59aa292
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 45 deletions.
3 changes: 2 additions & 1 deletion etc/EXAMPLE.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"network": {
"name": "devnet",
"url": "http://127.0.0.1",
"declareUrl": "http://127.0.0.1",
"deployUrl": "http://127.0.0.1",
"accounts": {
"deployer": {
"address": "",
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": "module",
"dependencies": {
"commander": "^11.0.0",
"starknet": "^5.24.3"
"starknet": "5.24.3"
},
"scripts": {
"build": "node scripts/build.js build",
Expand Down
9 changes: 9 additions & 0 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ const NOTICE_PERIOD = "NoticePeriod";
// command
const COMMAND_BUILD = "scarb --release build";

// connetion type
const DECLARE = "declare";
const DEPLOY = "deploy";

export var connectionType = {
DECLARE,
DEPLOY
}

export var UpgradeStatus = {
IDLE,
NOTICE_PERIOD
Expand Down
9 changes: 6 additions & 3 deletions scripts/deploy_faucet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { program } from "commander";
import fs from "fs";
import { connectStarknet, buildFaucetTokenConstructorArgs, getClassHashFromError, getContractClass, getDeployLog } from "./utils.js";
import { logName, contractPath } from "./constants.js"
import { logName, contractPath, connectionType } from "./constants.js"

program
.command("deployFaucetToken")
Expand All @@ -25,7 +25,7 @@ async function deploy_faucet_token(options) {
console.log("symbol:", symbol);
console.log("decimals:", decimals);

let { provider, deployer, governor, netConfig} = await connectStarknet();
let { provider, deployer, governor, netConfig} = await connectStarknet(connectionType.DECLARE);

// declare faucet token
const {sierraContract, casmContract} = getContractClass(contractPath.FAUCET_TOKEN);
Expand All @@ -38,7 +38,7 @@ async function deploy_faucet_token(options) {
classHash = declareResponse.class_hash;
console.log('✅ Faucet Token Contract declared with classHash = ', classHash);
} catch (error) {
if (!error.message.includes('is already declared.')) {
if (!error.message.includes('StarkFelt(\\')) {
throw error;
}

Expand All @@ -50,11 +50,14 @@ async function deploy_faucet_token(options) {
console.log('✅ Faucet Token Contract already declared with classHash =', classHash);
}
}
} else {
console.log('✅ Faucet Token Contract already declared with classHash =', classHash);
}
deployLog[logName.DEPLOY_LOG_FAUCET_TOKEN_CLASS_HASH] = classHash;
fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2));

// deploy faucet token
({ provider, deployer, governor, netConfig } = await connectStarknet(connectionType.DEPLOY));
const constructorArgs = buildFaucetTokenConstructorArgs(sierraContract.abi, name, symbol, decimals);
const deployResponse = await deployer.deployContract({ classHash: classHash, constructorCalldata: constructorArgs });
await provider.waitForTransaction(deployResponse.transaction_hash);
Expand Down
19 changes: 8 additions & 11 deletions scripts/deploy_multicall.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { program } from "commander";
import fs from "fs";
import { logName, contractPath } from "./constants.js";
import { logName, contractPath, connectionType } from "./constants.js";
import { connectStarknet, buildMulticallConstructorArgs, getClassHashFromError, getContractClass, getDeployLog } from "./utils.js";

program
Expand All @@ -16,20 +16,20 @@ program.parse();

async function deploy_multicall(options) {
const { deployLogPath, deployLog } = getDeployLog(logName.DEPLOY_MULTICALL_LOG_PREFIX);
let { provider, deployer, governor, netConfig } = await connectStarknet();
let { provider, deployer, governor, netConfig } = await connectStarknet(connectionType.DECLARE);

// declare multicall contract
const {sierraContract, casmContract} = getContractClass(contractPath.MULTICALL);

let classHash = deployLog[logName.DEPLOY_LOG_MULTICALL_CLASS_HASH];
if (!(logName.DEPLOY_LOG_FAUCET_TOKEN_CLASS_HASH in deployLog)) {
if (!(logName.DEPLOY_LOG_MULTICALL_CLASS_HASH in deployLog)) {
try {
const declareResponse = await deployer.declare({ contract: sierraContract, casm: casmContract });
await provider.waitForTransaction(declareResponse.transaction_hash);
classHash = declareResponse.class_hash;
console.log('✅ Multicall Contract declared with classHash = ', classHash);
} catch (error) {
if (!error.message.includes('is already declared.')) {
if (!error.message.includes('StarkFelt(\\')) {
throw error;
}

Expand All @@ -41,22 +41,19 @@ async function deploy_multicall(options) {
console.log('✅ Multicall Contract already declared with classHash =', classHash);
}
}
} else {
console.log('✅ Multicall Contract already declared with classHash =', classHash);
}
deployLog[logName.DEPLOY_LOG_MULTICALL_CLASS_HASH] = classHash;
fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2));

// deploy multicall
({ provider, deployer, governor, netConfig } = await connectStarknet(connectionType.DEPLOY));
const constructorArgs = buildMulticallConstructorArgs(sierraContract.abi);
const deployResponse = await deployer.deployContract({ classHash: classHash, constructorCalldata: constructorArgs });
let tx = await provider.waitForTransaction(deployResponse.transaction_hash);
while (tx.block_number === undefined) {
await new Promise(resolve => setTimeout(resolve, 60000));
tx = await provider.waitForTransaction(deployResponse.transaction_hash);
}
console.log('tx block number', tx.block_number);
await provider.waitForTransaction(deployResponse.transaction_hash);
console.log('✅ Multicall Contract deployed at =', deployResponse.contract_address);
deployLog[logName.DEPLOY_LOG_MULTICALL] = deployResponse.contract_address;
deployLog[logName.DEPLOY_LOG_MULTICALL_TX_HASH] = deployResponse.transaction_hash;
deployLog[logName.DEPLOY_LOG_MULTICALL_BLOCK_NUMBER] = tx.block_number;
fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2));
}
6 changes: 4 additions & 2 deletions scripts/deploy_zklink.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Contract } from "starknet";
import fs from "fs";
import { program } from "commander";
import { logName, contractPath } from "./constants.js"
import { logName, contractPath, connectionType } from "./constants.js"
import { connectStarknet, getDeployLog, buildVerifierConstructorArgs, buildZklinkConstructorArgs, buildGateKeeperConstructorArgs, declare_zklink, getContractClass } from "./utils.js";


Expand All @@ -21,7 +21,7 @@ program
async function deploy_zklink(options) {
const log = getDeployLog(logName.DEPLOY_ZKLINK_LOG_PREFIX);
const { deployLogPath, deployLog } = log;
let { provider, deployer, governor, netConfig } = await connectStarknet();
let { provider, deployer, governor, netConfig } = await connectStarknet(connectionType.DECLARE);

// declare contracts
const declare_options = {
Expand Down Expand Up @@ -49,6 +49,8 @@ async function deploy_zklink(options) {
deployLog[logName.DEPLOY_LOG_VALIDATOR] = options.validator;
fs.writeFileSync(deployLogPath, JSON.stringify(deployLog, null, 2));

({ provider, deployer, governor, netConfig } = await connectStarknet(connectionType.DEPLOY));

const {sierraContract: gatekeeperContractSierra, casmContract: gatekeeperContractCasm} = getContractClass(contractPath.GATEKEEPER);
const {sierraContract: verifierContractSierra, casmContract: verifierContractCasm} = getContractClass(contractPath.VERIFIER);
const {sierraContract: zklinkContractSierra, casmContract: zklinkContractCasm} = getContractClass(contractPath.ZKLINK);
Expand Down
6 changes: 3 additions & 3 deletions scripts/interact.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { program } from "commander";
import fs from "fs";
import { Contract, json } from "starknet";
import { contractPath, logName } from "./constants.js";
import { contractPath, logName, connectionType } from "./constants.js";
import { connectStarknet, getDeployLog, getContractClass } from "./utils.js";

program
Expand All @@ -26,7 +26,7 @@ program
program.parse();

async function add_token(options) {
let { provider, deployer, governor, netConfig} = await connectStarknet();
let { provider, deployer, governor, netConfig} = await connectStarknet(connectionType.DEPLOY);
const { deployLogPath, deployLog } = getDeployLog(logName.DEPLOY_ZKLINK_LOG_PREFIX);

const zklinkAddress = options.zklink === undefined ? deployLog[logName.DEPLOY_LOG_ZKLINK] : options.zklink;
Expand All @@ -52,7 +52,7 @@ async function add_token(options) {
}

async function add_bridge(options) {
let { provider, deployer, governor, netConfig} = await connectStarknet();
let { provider, deployer, governor, netConfig} = await connectStarknet(connectionType.DEPLOY);
const { deployLogPath, deployLog } = getDeployLog(logName.DEPLOY_ZKLINK_LOG_PREFIX);

const bridgeAddress = options.bridge;
Expand Down
4 changes: 2 additions & 2 deletions scripts/upgrade_zklink.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Contract, json} from "starknet";
import fs from "fs";
import { program } from "commander";
import { logName, contractPath, UpgradeStatus } from "./constants.js"
import { logName, contractPath, UpgradeStatus, connectionType } from "./constants.js"
import { connectStarknet, getDeployLog, declare_zklink, getContractClass } from "./utils.js"

program
Expand All @@ -19,7 +19,7 @@ program.parse();
async function upgrade_zklink(options) {
const log = getDeployLog(logName.DEPLOY_ZKLINK_LOG_PREFIX);
const { deployLog, deployLogPath } = log;
let { provider, deployer, governor, netConfig} = await connectStarknet();
let { provider, deployer, governor, netConfig} = await connectStarknet(connectionType.DECLARE);

const upgradeVerifier = options.upgradeVerifier;
const upgradeZklink = options.upgradeZklink;
Expand Down
26 changes: 17 additions & 9 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import fs from "fs";
import { RpcProvider, CallData, Account, json } from "starknet";
import { exec } from "child_process";
import { logName, contractPath } from "./constants.js";
import { logName, contractPath, connectionType } from "./constants.js";


function buildProvider(networkConfig) {
let provider = new RpcProvider({ nodeUrl: networkConfig.url });
console.log(`✅ Connected to ${networkConfig.name}`);
function buildProvider(networkConfig, type) {
let url = undefined;
if (type === connectionType.DEPLOY) {
url = networkConfig.deployUrl;
console.log('Connecting Starknet for deployment...');
} else if (type === connectionType.DECLARE) {
url = networkConfig.declareUrl;
console.log('Connecting Starknet for declaration...');
}
let provider = new RpcProvider({ nodeUrl: url });
console.log(`✅ Connected to ${url}`);
return provider;
}

export async function connectStarknet() {
export async function connectStarknet(type) {
// read config json file
const netName = process.env.NET === undefined ? "EXAMPLE" : process.env.NET;
let netConfig = await fs.promises.readFile(`./etc/${netName}.json`, "utf-8");
netConfig = JSON.parse(netConfig);

// create provider
const provider = buildProvider(netConfig.network);
const provider = buildProvider(netConfig.network, type);

const deployerConfig = netConfig.network.accounts.deployer;
const governorConfig = netConfig.network.accounts.governor;
Expand Down Expand Up @@ -93,7 +101,7 @@ export async function declare_zklink(provider, deployer, log, options) {
gatekeeperContractClassHash = gatekeeperDeclareResponse.class_hash;
console.log('✅ Gatekeeper Contract declared with classHash = ', gatekeeperContractClassHash);
} catch (error) {
if (!error.message.includes('is already declared.')) {
if (!error.message.includes('StarkFelt(\\')) {
throw error;
}
gatekeeperContractClassHash = getClassHashFromError(error);
Expand Down Expand Up @@ -124,7 +132,7 @@ export async function declare_zklink(provider, deployer, log, options) {
verifierContractClassHash = verifierDeclareResponse.class_hash;
console.log('✅ Verifier Contract declared with classHash = ', verifierContractClassHash);
} catch (error) {
if (!error.message.includes('is already declared.')) {
if (!error.message.includes('StarkFelt(\\')) {
throw error;
}

Expand Down Expand Up @@ -154,7 +162,7 @@ export async function declare_zklink(provider, deployer, log, options) {
zklinkContractClassHash = zklinkDeclareResponse.class_hash;
console.log('✅ Zklink Contract declared with classHash = ', zklinkContractClassHash);
} catch (error) {
if (!error.message.includes('is already declared.')) {
if (!error.message.includes('StarkFelt(\\')) {
throw error;
}

Expand Down

0 comments on commit 59aa292

Please sign in to comment.