Skip to content

Commit

Permalink
feat: update hooks script
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolveArt committed Oct 8, 2024
1 parent 7534d9c commit e5f2481
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 30 deletions.
2 changes: 1 addition & 1 deletion scripts/configs/pragma_devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"constructor": {
"local_domain": {
"type": "u32",
"value": "23448591"
"value": "6363709"
},
"owner": {
"type": "ContractAddress",
Expand Down
16 changes: 1 addition & 15 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import {
Account,
Contract,
json,
Provider,
CallData,
RpcProvider,
ContractFactory,
ContractFactoryParams
} from "starknet";
import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import { buildAccount } from "./utils";

dotenv.config();

const BUILD_PATH = "../cairo/target/dev/contracts";
const MOCK_BUILD_PATH = "../cairo/target/dev/mocks";
const ACCOUNT_ADDRESS = process.env.ACCOUNT_ADDRESS;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const CONFIGS_DIR = 'configs';
const DEPLOYMENTS_DIR = 'deployments';
const NETWORK = process.env.NETWORK;
Expand Down Expand Up @@ -84,18 +81,7 @@ function findContractFile(name: string, suffix: string): string {
throw new Error(`Contract file not found for ${name} with suffix ${suffix} in either ${BUILD_PATH} or ${MOCK_BUILD_PATH}`);
}

async function buildAccount(): Promise<Account> {
const provider = new RpcProvider({ nodeUrl: process.env.STARKNET_RPC_URL });

if (!PRIVATE_KEY || !ACCOUNT_ADDRESS) {
throw new Error("Private key or account address not set in .env file");
}
if (!NETWORK) {
throw new Error('NETWORK environment variable is not set');
}

return new Account(provider, ACCOUNT_ADDRESS, PRIVATE_KEY);
}

function getCompiledContract(name: string): any {
const contractPath = findContractFile(name, '.contract_class.json');
Expand Down
54 changes: 54 additions & 0 deletions scripts/deployments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from "fs";
import path from "path";

interface ContractAddresses {
[key: string]: string | object;
}

export function getDeployedAddress(
chainName: string,
contractName: string,
): string {
try {
const filePath = path.join(
__dirname,
"deployments",
chainName,
"deployments.json",
);
const fileContents = fs.readFileSync(filePath, "utf8");
const config: ContractAddresses = JSON.parse(fileContents);

let address: string | undefined;

// Handle nested structures
const findAddress = (obj: any, key: string): string | undefined => {
if (obj[key] && typeof obj[key] === "string") {
return obj[key] as string;
}
for (const k in obj) {
if (typeof obj[k] === "object") {
const found = findAddress(obj[k], key);
if (found) return found;
}
}
return undefined;
};

address = findAddress(config, contractName);

if (!address) {
throw new Error(
`Invalid or missing address for contract ${contractName} in deployments.json for chain ${chainName}`,
);
}

return address;
} catch (error) {
console.error(
`Error reading configuration file for chain ${chainName}:`,
error,
);
throw error;
}
}
26 changes: 13 additions & 13 deletions scripts/deployments/PRAGMA_DEVNET/deployments.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"merkleroot_multisig_ism": "0xecdcfc130ef39d1165338615a875176873d4ef2100c7bbf52de2cd5a2b4081",
"messageid_multisig_ism": "0x54668196b83eeaf476b6623a8c7c8bd4646704cccbf09210f89d47541fe0b92",
"domain_routing_ism": "0x14ea7c5eae72dd0f39ff2263ed29d8a01bb48d64e75603f4ded7cc1b6d8dd20",
"noop_ism": "0x769f77987315e20eac6bc8bd426fb6242095e73039e49bead77c88c8c406b2c",
"pausable_ism": "0x59b29192815b814ccd1397e0868cce6874e2b44f096b30732bc1fca6870c3e6",
"aggregation": "0x45caa0f1bab9b1d34f46eaf280fad1970616d90d7acf27b9630596f8f0d6c15",
"protocol_fee": "0xffa8a03e994b7b35a52db403c389c987e11a6b34eda453ca58c3da314e6d50",
"hook": "0x7809aea51191017f541bb09885ef4c84fe6b11725edbd81c5fd8af6a0943dce",
"mailbox": "0x41c20175af14a0bfebfc9ae2f3bda29230a0bceb551844197d9f46faf76d6da",
"merkle_tree_hook": "0xb9a75496355e223652c40fe50d45b5f39b86d3cc5c4f7aed44be6c7f6a8b4c",
"default_fallback_routing_ism": "0x6b632654d261241234331a29f8b24a7838833035ea5142a26a0c7687a71730f",
"trusted_relayer_ism": "0x77559266dd71d2ed75a82608a3400d4d9da71e1b1029013b47f641c431ed5d3",
"validator_announce": "0x2305250e0adab1bafabe58bc905cf94440a55e0fc7664ff095250fe2f8684b8"
"merkleroot_multisig_ism": "0x4d4c61dc1906f485baa9acb1e18ad3c8c3eb90dbb80ff58a6f122fba1d428af",
"messageid_multisig_ism": "0x12aa22b2c080f5a0c03f1f82568fc01f148fd12f27984b704f16f0ee484e72a",
"domain_routing_ism": "0x4751b43d9216331f8fb75c53641d249e8617fbaa065886e861f64e3f7ab5cbe",
"noop_ism": "0x49b253f317a02202df240be49f13ae2cdf27ee3aecb14d56101254c7717b72a",
"pausable_ism": "0x22c77ac9401a85ac12aa88f22324532e8b025c450573cd9d72b2d153cc0c183",
"aggregation": "0x208b5413ed1123604601402067e97ee2fa6ea52af588b5c164aeb0bffa1ad1e",
"protocol_fee": "0x35ab3b732ad84f83a3307c5cce4b4bf9aa19971cdcc390cbcebdee8292489e4",
"hook": "0x35972881d3c9f9bc499030f1aef75b6f4868116031c57b62fdfbbb0a3d8feb3",
"mailbox": "0x684a2d64d270b47df2bb13de2303658854e98ace64e14f233b36789543035ea",
"merkle_tree_hook": "0x2a155aac0327e9a2e84052615577c62f4059245008efa4a7b07fa0dedfa1cb5",
"default_fallback_routing_ism": "0x3dfd7f5d488958499d85fd399c29fce3a346e8af483a853b410896a8d77168",
"trusted_relayer_ism": "0x15e7f24d83121c5e59e57e11a3743ab742ebc674503e8efb67c3b7ccee72e25",
"validator_announce": "0xc452133ef35830db0609efe72de431797ef9ba783baf64f94f800c79f3044"
}
3 changes: 2 additions & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"deploy": "ts-node deploy.ts"
"deploy": "ts-node deploy.ts",
"update-hooks": "ts-node update-hooks.ts"
},
"keywords": [],
"author": "",
Expand Down
52 changes: 52 additions & 0 deletions scripts/update-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Contract } from "starknet";
import { buildAccount } from "./utils";
import { getDeployedAddress } from "./deployments";
import dotenv from "dotenv";

dotenv.config();

const NETWORK = process.env.NETWORK;
const DEFAULT_HOOK = "0x2a155aac0327e9a2e84052615577c62f4059245008efa4a7b07fa0dedfa1cb5";
const REQUIRED_HOOK = "0x2a155aac0327e9a2e84052615577c62f4059245008efa4a7b07fa0dedfa1cb5";

async function updateHooks(): Promise<void> {
try {
if (!NETWORK) {
throw new Error('NETWORK environment variable is not set');
}

const account = await buildAccount();

const mailboxAddress = getDeployedAddress(NETWORK, 'mailbox');
const { abi } = await account.getClassAt(mailboxAddress);
const mailboxContract = new Contract(abi, mailboxAddress, account);

if (DEFAULT_HOOK != undefined) {
console.log(`🧩 Updating default hook ${DEFAULT_HOOK}..`);
const invoke = await mailboxContract.invoke("set_default_hook", [DEFAULT_HOOK])
await account.waitForTransaction(invoke.transaction_hash);

console.log(`⚡️ Transaction hash: ${invoke.transaction_hash}`);
}

if (REQUIRED_HOOK != undefined) {
console.log(`🧩 Updating required hook ${REQUIRED_HOOK}..`);
const invoke = await mailboxContract.invoke("set_required_hook", [REQUIRED_HOOK])
await account.waitForTransaction(invoke.transaction_hash);

console.log(`⚡️ Transaction hash: ${invoke.transaction_hash}`);
}

console.log(`🧩 Hooks updated successfully with ${DEFAULT_HOOK} & ${REQUIRED_HOOK}`);
} catch (error) {
console.error(`⛔ Error updating hooks ${DEFAULT_HOOK} & ${REQUIRED_HOOK}:`, error);
}
}

updateHooks().then(() => {
console.log('Hooks updated successfully');
}
).catch((error) => {
console.error('Error updating hooks:', error);
}
);
22 changes: 22 additions & 0 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Account,
RpcProvider,
} from "starknet";

import dotenv from "dotenv";

dotenv.config();


const ACCOUNT_ADDRESS = process.env.ACCOUNT_ADDRESS;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

export async function buildAccount(): Promise<Account> {
const provider = new RpcProvider({ nodeUrl: process.env.STARKNET_RPC_URL });

if (!PRIVATE_KEY || !ACCOUNT_ADDRESS) {
throw new Error("Private key or account address not set in .env file");
}

return new Account(provider, ACCOUNT_ADDRESS, PRIVATE_KEY);
}

0 comments on commit e5f2481

Please sign in to comment.