Skip to main content

5. Deployment TugWar Game ke Monad Testnet

Setelah mengembangkan dan testing TugWar Game smart contract, sekarang saatnya untuk men-deploy ke Monad Testnet. Pada bagian ini, kita akan membuat satu script deployment yang sederhana namun lengkap untuk deploy dan verifikasi contract.

Daftar Isi​

  1. Script Deployment Sederhana
  2. Menjalankan Deployment
  3. Interaksi dengan Contract
  4. Package.json Scripts

1. Script Deployment Sederhana​

Membuat Script Deploy dan Verify​

Buat file scripts/deploy.ts:

import { ethers } from "hardhat";
import { TugWarContract } from "../typechain-types";

async function main() {
console.log("šŸŽ® TugWar Game Deployment to Monad Testnet");
console.log("=" .repeat(50));

// Get deployer account
const [deployer] = await ethers.getSigners();
const network = await ethers.provider.getNetwork();

console.log("\nšŸ“‹ Deployment Info:");
console.log(" Deployer:", deployer.address);
console.log(" Network:", network.name);
console.log(" Chain ID:", network.chainId.toString());

// Check balance
const balance = await ethers.provider.getBalance(deployer.address);
console.log(" Balance:", ethers.formatEther(balance), "MON");

if (balance < ethers.parseEther("0.1")) {
console.error("āŒ Insufficient balance for deployment");
console.error(" Required: 0.1 MON minimum");
process.exit(1);
}

console.log("āœ… Pre-deployment checks passed");

// Deploy TugWar Contract
console.log("\nšŸš€ Deploying TugWar Contract...");

const TugWarFactory = await ethers.getContractFactory("TugWarContract");

// Estimate gas
const gasEstimate = await (await TugWarFactory.getDeployTransaction(deployer.address)).gasLimit;
const gasPrice = await ethers.provider.getFeeData();

console.log("⛽ Gas Info:");
console.log(" Estimated gas:", gasEstimate?.toString());
console.log(" Gas price:", ethers.formatUnits(gasPrice.gasPrice || 0, "gwei"), "gwei");

const tugwar: TugWarContract = await TugWarFactory.deploy(deployer.address);

console.log("ā³ Waiting for deployment...");
await tugwar.waitForDeployment();

const contractAddress = await tugwar.getAddress();
const deploymentTx = tugwar.deploymentTransaction();

console.log("āœ… Contract deployed successfully!");
console.log(" Address:", contractAddress);
console.log(" Transaction:", deploymentTx?.hash);
console.log(" Block:", deploymentTx?.blockNumber);

// Verify initial state
console.log("\nšŸ” Verifying initial state...");
try {
const gameInfo = await tugwar.getGameInfo();
console.log(" Rope position:", gameInfo.currentRopePos.toString());
console.log(" Team 1 score:", gameInfo.score1.toString());
console.log(" Team 2 score:", gameInfo.score2.toString());
console.log(" Max score diff:", gameInfo.maxDiff.toString());
console.log(" Owner:", await tugwar.owner());
console.log(" Can start game:", await tugwar.canStartGame());
} catch (error) {
console.warn("āš ļø Could not verify initial state:", error);
}

// Contract verification
console.log("\nšŸ“‹ Contract Verification:");
console.log(" To verify manually, run:");
console.log(` npx hardhat verify --network monadTestnet ${contractAddress} "${deployer.address}"`);

// Save deployment info
const deploymentInfo = {
contractAddress: contractAddress,
deployerAddress: deployer.address,
network: network.name,
chainId: network.chainId.toString(),
transactionHash: deploymentTx?.hash,
blockNumber: deploymentTx?.blockNumber,
timestamp: new Date().toISOString(),
explorerUrl: `https://testnet.monadexplorer.com/address/${contractAddress}`
};

require('fs').writeFileSync(
'deployment.json',
JSON.stringify(deploymentInfo, null, 2)
);

console.log("šŸ“„ Deployment info saved to deployment.json");

// Success summary
console.log("\n" + "šŸŽ‰".repeat(20));
console.log("šŸŽ‰ DEPLOYMENT SUCCESSFUL! šŸŽ‰");
console.log("šŸŽ‰".repeat(20));

console.log("\nšŸ“ Summary:");
console.log(" Contract Address:", contractAddress);
console.log(" Owner:", deployer.address);
console.log(" Explorer:", deploymentInfo.explorerUrl);

console.log("\nšŸŽ® Next Steps:");
console.log(" 1. Verify contract (optional)");
console.log(" 2. Test the game:", `npm run play ${contractAddress}`);
console.log(" 3. Share with players!");

console.log("\nšŸŽÆ Game Commands:");
console.log(" • Pull for Team 1: tugwar.pull(true)");
console.log(" • Pull for Team 2: tugwar.pull(false)");
console.log(" • Check winner: tugwar.getWinStatus()");
console.log(" • Reset game: tugwar.reSet(5) [owner only]");
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error("āŒ Deployment failed:");
console.error(error);
process.exit(1);
});

Script untuk Manual Verification​

Buat file scripts/verify.ts:

import { run } from "hardhat";

async function main() {
const deploymentInfo = require('../deployment.json');
const contractAddress = deploymentInfo.contractAddress;
const constructorArgs = [deploymentInfo.deployerAddress];

console.log("šŸ” Verifying TugWar contract...");
console.log(" Address:", contractAddress);
console.log(" Constructor args:", constructorArgs);

try {
await run("verify:verify", {
address: contractAddress,
constructorArguments: constructorArgs,
contract: "contracts/TugWarContract.sol:TugWarContract"
});

console.log("āœ… Contract verified successfully!");
console.log("🌐 View verified contract:");
console.log(" https://testnet.monadexplorer.com/address/" + contractAddress);

} catch (error: any) {
if (error.message.includes("Already Verified")) {
console.log("ā„¹ļø Contract already verified");
console.log("🌐 View contract:");
console.log(" https://testnet.monadexplorer.com/address/" + contractAddress);
} else {
console.error("āŒ Verification failed:", error.message);
console.log("\nšŸ”§ Manual verification command:");
console.log(`npx hardhat verify --network monadTestnet ${contractAddress} "${constructorArgs[0]}"`);
}
}
}

main().catch(console.error);

2. Menjalankan Deployment​

Deploy ke Monad Testnet​

npx hardhat run scripts/deploy.ts --network monadTestnet

Output Deployment​

šŸŽ® TugWar Game Deployment to Monad Testnet
==================================================

šŸ“‹ Deployment Info:
Deployer: 0x742d35Cc6639C0532fBaBc3CABAa1C2db582aC10
Network: monadTestnet
Chain ID: 10143
Balance: 1.234567 MON
āœ… Pre-deployment checks passed

šŸš€ Deploying TugWar Contract...
⛽ Gas Info:
Estimated gas: 1234567
Gas price: 20.0 gwei
ā³ Waiting for deployment...
āœ… Contract deployed successfully!
Address: 0x123...ABC
Transaction: 0x456...DEF
Block: 12345

šŸ” Verifying initial state...
Rope position: 0
Team 1 score: 0
Team 2 score: 0
Max score diff: 5
Owner: 0x742d35Cc6639C0532fBaBc3CABAa1C2db582aC10
Can start game: true

šŸ“‹ Contract Verification:
To verify manually, run:
npx hardhat verify --network monadTestnet 0x123...ABC "0x742d35Cc6639C0532fBaBc3CABAa1C2db582aC10"

šŸ“„ Deployment info saved to deployment.json

šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰
šŸŽ‰ DEPLOYMENT SUCCESSFUL! šŸŽ‰
šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰šŸŽ‰

šŸ“ Summary:
Contract Address: 0x123...ABC
Owner: 0x742d35Cc6639C0532fBaBc3CABAa1C2db582aC10
Explorer: https://testnet.monadexplorer.com/address/0x123...ABC

šŸŽ® Next Steps:
1. Verify contract (optional)
2. Test the game: npm run play 0x123...ABC
3. Share with players!

šŸŽÆ Game Commands:
• Pull for Team 1: tugwar.pull(true)
• Pull for Team 2: tugwar.pull(false)
• Check winner: tugwar.getWinStatus()
• Reset game: tugwar.reSet(5) [owner only]

Verify Contract (Opsional)​

npx hardhat run scripts/verify.ts --network monadTestnet

Direct Interaction via Console​

npx hardhat console --network monadTestnet

Di console:

// Connect to deployed contract
const tugwar = await ethers.getContractAt("TugWarContract", "0x123...ABC");

// Check game info
const gameInfo = await tugwar.getGameInfo();
console.log("Game Info:", gameInfo);

// Pull for Team 1
const tx1 = await tugwar.pull(true);
await tx1.wait();
console.log("Team 1 pulled!");

// Pull for Team 2
const tx2 = await tugwar.pull(false);
await tx2.wait();
console.log("Team 2 pulled!");

// Check winner
const winner = await tugwar.getWinStatus();
console.log("Winner:", winner); // 0 = ongoing, 1 = team1, 2 = team2

// Get prediction
const prediction = await tugwar.getPrediction();
console.log("Prediction:", prediction);

4. Package.json Scripts​

Update package.json​

{
"scripts": {
"deploy": "hardhat run scripts/deploy.ts --network monadTestnet",
"verify": "hardhat run scripts/verify.ts --network monadTestnet",
"play": "hardhat run scripts/interact.ts --network monadTestnet --",
"console": "hardhat console --network monadTestnet",
"clean": "hardhat clean && rm -f deployment.json"
}
}

Usage Commands​

# Deploy contract
npm run deploy

# Verify contract
npm run verify

# Check game status
npm run play 0x123...ABC

# Interactive console
npm run console

# Clean up
npm run clean

File Structure Hasil​

monad-tugwar-game/
ā”œā”€ā”€ scripts/
│ ā”œā”€ā”€ deploy.ts # Main deployment script
│ ā”œā”€ā”€ verify.ts # Contract verification
│ └── interact.ts # Game interaction
ā”œā”€ā”€ deployment.json # Deployment info
└── package.json # NPM scripts

Kesimpulan​

Selamat! Anda telah membuat deployment system yang sederhana namun lengkap untuk TugWar Game dengan:

āœ… Satu Script Deployment​

  1. deploy.ts - All-in-one deployment dengan validation, gas estimation, dan state verification
  2. Automatic Checks - Balance validation dan pre-deployment verification
  3. State Verification - Memastikan contract ter-deploy dengan benar
  4. Info Saving - Menyimpan deployment info ke deployment.json

āœ… Supporting Scripts​

  1. verify.ts - Contract verification script
  2. interact.ts - Game interaction dan status checking
  3. Console access - Direct contract interaction

āœ… Simple Commands​

npm run deploy    # Deploy contract
npm run verify # Verify contract
npm run play # Check game status
npm run console # Interactive mode

āœ… Production Ready​

  1. Error Handling - Comprehensive error checking
  2. Gas Optimization - Gas estimation dan monitoring
  3. Explorer Integration - Direct links ke Monad Explorer
  4. User Friendly - Clear instructions dan game commands

Deployment system ini memberikan workflow yang sederhana namun professional untuk deploy TugWar Game ke Monad Testnet dengan semua tools yang diperlukan untuk testing dan interaction! šŸŽ®šŸš€