File size: 3,740 Bytes
dcc955a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// deploy.js β€” compile and deploy ListingNFT.sol to 0G testnet
//
// Usage:
//   cd contract
//   echo "PRIVATE_KEY=0x..." > .env
//   npm install
//   node deploy.js
//
// Prints the deployed address β€” paste into ../shared.js OG_CHAIN.listingNFT

import { readFileSync, writeFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
import { ethers } from 'ethers';
import solc from 'solc';
import 'dotenv/config';

const __dirname = dirname(fileURLToPath(import.meta.url));

const OG_RPC = 'https://evmrpc-testnet.0g.ai';
const OG_CHAIN_ID = 16602;

function compile() {
  console.log('Compiling ListingNFT.sol…');
  const source = readFileSync(resolve(__dirname, 'ListingNFT.sol'), 'utf8');

  const input = {
    language: 'Solidity',
    sources: { 'ListingNFT.sol': { content: source } },
    settings: {
      outputSelection: { '*': { '*': ['abi', 'evm.bytecode.object'] } },
      optimizer: { enabled: true, runs: 200 },
    },
  };

  const output = JSON.parse(solc.compile(JSON.stringify(input)));

  if (output.errors) {
    for (const err of output.errors) {
      if (err.severity === 'error') {
        console.error(err.formattedMessage);
        process.exit(1);
      }
    }
  }

  const contract = output.contracts['ListingNFT.sol']['ListingNFT'];
  const abi = contract.abi;
  const bytecode = contract.evm.bytecode.object;

  // Save artifacts for the frontend
  writeFileSync(resolve(__dirname, 'ListingNFT.abi.json'), JSON.stringify(abi, null, 2));
  writeFileSync(resolve(__dirname, 'ListingNFT.bytecode'), bytecode);
  console.log('Compiled. ABI + bytecode written to contract/.');

  return { abi, bytecode };
}

async function deploy() {
  const privateKey = process.env.PRIVATE_KEY;
  if (!privateKey) {
    console.error('Missing PRIVATE_KEY in contract/.env');
    console.error('Create one: echo "PRIVATE_KEY=0x..." > .env');
    process.exit(1);
  }

  const { abi, bytecode } = compile();

  console.log('Connecting to 0G testnet…');
  const provider = new ethers.JsonRpcProvider(OG_RPC);
  const wallet = new ethers.Wallet(privateKey, provider);

  const network = await provider.getNetwork();
  if (Number(network.chainId) !== OG_CHAIN_ID) {
    console.warn(`Warning: chainId is ${network.chainId}, expected ${OG_CHAIN_ID}`);
  }

  const balance = await provider.getBalance(wallet.address);
  console.log(`Wallet: ${wallet.address}`);
  console.log(`Balance: ${ethers.formatEther(balance)} 0G`);

  if (balance === 0n) {
    console.error('Wallet has 0 balance. Get testnet tokens at https://faucet.0g.ai');
    process.exit(1);
  }

  console.log('Deploying ListingNFT…');
  const factory = new ethers.ContractFactory(abi, '0x' + bytecode, wallet);
  const contract = await factory.deploy();
  console.log(`Tx submitted: ${contract.deploymentTransaction().hash}`);
  console.log('Waiting for confirmation…');
  await contract.waitForDeployment();
  const addr = await contract.getAddress();

  console.log('');
  console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
  console.log('βœ“ Deployed!');
  console.log('');
  console.log(`Address: ${addr}`);
  console.log(`Explorer: https://chainscan-galileo.0g.ai/address/${addr}`);
  console.log('');
  console.log('Next step:');
  console.log('  Edit ../shared.js and set:');
  console.log(`    OG_CHAIN.listingNFT = '${addr}'`);
  console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}

deploy().catch(e => {
  console.error('Deploy failed:', e);
  process.exit(1);
});