From 2bf813adf6d69c7d57dd4311877af3aef599785c Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Mon, 23 Sep 2024 01:41:39 -0300 Subject: [PATCH] update script --- README.md | 2 +- smartcontracts/deploy.py | 87 +++++++++++++------ .../blockexplorer/address/[address]/page.tsx | 2 +- 3 files changed, 62 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index ca3aba9..346b62f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Fullstack Web3 Template v2.3.0 +# Fullstack Web3 Template v2.3.1 ## QuickStart diff --git a/smartcontracts/deploy.py b/smartcontracts/deploy.py index f774eb0..19ac103 100644 --- a/smartcontracts/deploy.py +++ b/smartcontracts/deploy.py @@ -5,6 +5,8 @@ from dataclasses import dataclass, field from json import dumps, load from typing import List +import json +import glob @dataclass @@ -26,40 +28,71 @@ class Contract: TRANSACTIONS_PATH = f"broadcast/{CONTRACT_SCRIPT_NAME}/{CHAIN_ID}/run-latest.json" TARGET_DIR = "../ui/contracts/deployedContracts.ts" +CONTRACTS = [] + def abi_path(name) -> str: return f"artifacts/{name}.sol/{name}.json" -with open(TRANSACTIONS_PATH) as deployed_contracts: - json_file = load(deployed_contracts) - transactions = json_file["transactions"] - contracts: List[Contract] = [] - - for contract in transactions: - if contract["transactionType"] == "CREATE": - name, address = contract["contractName"], contract["contractAddress"] - with open(abi_path(name)) as full_abi_json: - abi = load(full_abi_json)["abi"] - contracts.append(Contract(name, address, abi)) - - -typescript_content = f""" -import {{ GenericContractsDeclaration }} from "~~/utils/fwt/contract"; +def updateABI(): + with open(TRANSACTIONS_PATH) as deployed_contracts: + json_file = load(deployed_contracts) + transactions = json_file["transactions"] + contracts: List[Contract] = [] + + for contract in transactions: + if contract["transactionType"] == "CREATE": + name, address = contract["contractName"], contract["contractAddress"] + CONTRACTS.append(name) + with open(abi_path(name)) as full_abi_json: + abi = load(full_abi_json)["abi"] + contracts.append(Contract(name, address, abi)) + + typescript_content = f""" + import {{ GenericContractsDeclaration }} from "~~/utils/fwt/contract"; + + const deployedContracts = {{ + {CHAIN_ID}: {dumps({ + contract.name: { + "address": contract.address, + "abi": contract.abi, + } + for contract in contracts + })} + }} as const; + + export default deployedContracts satisfies GenericContractsDeclaration; + """ -const deployedContracts = {{ - {CHAIN_ID}: {dumps({ - contract.name: { - "address": contract.address, - "abi": contract.abi, + with open(TARGET_DIR, "w") as ts_file: + ts_file.write(typescript_content) + + +def get_metadata(): + BUILD_INFO = glob.glob("artifacts/build-info/*.json")[0] + with open(BUILD_INFO) as build_info: + json_file = load(build_info) + contracts_data = json_file["output"]["contracts"] + + # Filtrar os contratos relevantes + filtered_contracts = { + contract_path: { + name: contracts_data[contract_path][name] + for name in contracts_data[contract_path] + if name in CONTRACTS + } + for contract_path in contracts_data + if any(name in contracts_data[contract_path] for name in CONTRACTS) } - for contract in contracts - })} -}} as const; -export default deployedContracts satisfies GenericContractsDeclaration; -""" + # Atualiza a estrutura original com os contratos filtrados + json_file["output"]["contracts"] = filtered_contracts + + # Salvar o conteúdo filtrado de volta ao arquivo original + with open(BUILD_INFO, "w") as build_info: + json.dump(json_file, build_info, indent=4) -with open(TARGET_DIR, "w") as ts_file: - ts_file.write(typescript_content) +updateABI() +get_metadata() diff --git a/ui/app/blockexplorer/address/[address]/page.tsx b/ui/app/blockexplorer/address/[address]/page.tsx index 6cd2df4..e7afb17 100644 --- a/ui/app/blockexplorer/address/[address]/page.tsx +++ b/ui/app/blockexplorer/address/[address]/page.tsx @@ -50,7 +50,7 @@ const getContractData = async (address: string) => { "..", "..", "..", - "hardhat", + "smartcontracts", "artifacts", "build-info", );