added subpages for helloworld and clash, added game contract to config.json for usage in frontend

This commit is contained in:
averel10
2025-11-14 22:49:06 +01:00
parent d3b512aa95
commit 15bf0155bb
8 changed files with 678 additions and 275 deletions

View File

@@ -10,11 +10,21 @@ dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
const artifactPath = path.join(
__dirname,
"../../artifacts/contracts/testcontract.sol/HelloWorld.json"
);
interface ContractDeploymentConfig {
name: string;
artifactPath: string;
deployArgs?: unknown[];
configKeys: {
address: string;
abi: string;
};
}
async function deployContract(
signer: ethers.Signer,
config: ContractDeploymentConfig
): Promise<{ address: string; abi: unknown }> {
const artifactPath = path.join(__dirname, "../../artifacts/contracts", config.artifactPath);
if (!fs.existsSync(artifactPath)) {
throw new Error(`Artifact not found at ${artifactPath}. Please compile the contracts first.`);
@@ -24,37 +34,69 @@ async function main() {
const contractABI = artifact.abi;
const contractBytecode = artifact.bytecode;
const factory = new ethers.ContractFactory(contractABI, contractBytecode, signer);
console.log(`Deploying ${config.name} contract...`);
const deployedContract = await factory.deploy(...(config.deployArgs || []));
await deployedContract.waitForDeployment();
console.log(`${config.name} deployed to:`, deployedContract.target);
return {
address: deployedContract.target as string,
abi: contractABI
};
}
async function main() {
// Create provider and signer
const provider = new ethers.JsonRpcProvider(process.env.API_URL);
const signer = new ethers.Wallet(process.env.WALLET_PRIVATE_KEY!, provider);
// Create contract factory
const HelloWorldFactory = new ethers.ContractFactory(
contractABI,
contractBytecode,
signer
);
const config: any = {};
const deployedContracts: any = {};
// Deploy the contract
console.log("Deploying HelloWorld contract...");
const hello_world = await HelloWorldFactory.deploy("Hello World!");
// Wait for deployment to complete
await hello_world.waitForDeployment();
console.log("Contract deployed to address:", hello_world.target);
// Load existing config if it exists
const configPath = path.join(__dirname, "../../../config.json");
if (fs.existsSync(configPath)) {
Object.assign(config, JSON.parse(fs.readFileSync(configPath, "utf8")));
}
// Save contract address to config.json
const configPath = path.join(__dirname, "../../../config.json");
let config = {};
if (fs.existsSync(configPath)) {
config = JSON.parse(fs.readFileSync(configPath, "utf8"));
}
(config as any).CONTRACT_ADDRESS = hello_world.target;
(config as any).API_URL = process.env.API_URL;
(config as any).ABI = contractABI;
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
console.log("✓ Contract address saved to config.json");
// Define contracts to deploy
const contractsToDeploy: ContractDeploymentConfig[] = [
{
name: "HelloWorld",
artifactPath: "testcontract.sol/HelloWorld.json",
deployArgs: ["Hello World!"],
configKeys: {
address: "CONTRACT_ADDRESS",
abi: "ABI"
}
},
{
name: "Game",
artifactPath: "Game.sol/Game.json",
deployArgs: [],
configKeys: {
address: "GAME_CONTRACT_ADDRESS",
abi: "GAME_ABI"
}
}
];
// Deploy all contracts
for (const contractConfig of contractsToDeploy) {
const deployed = await deployContract(signer, contractConfig);
config[contractConfig.configKeys.address] = deployed.address;
config[contractConfig.configKeys.abi] = deployed.abi;
}
// Save configuration
config.API_URL = process.env.API_URL;
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
console.log("\n✓ All contracts deployed successfully!");
console.log("✓ Configuration saved to config.json");
}
main()