neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
Documentation
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/env node
'use strict';

/**
 * Standards Mirror — TestNet deploy + invoke pipeline.
 *
 * Reads docs/standards-mirror/deployments/manifest.json. For each pair:
 *   1. Compiles Solidity via target/release/neo-solc
 *   2. Compiles C# via nccs
 *   3. Deploys both NEFs to Neo N3 TestNet
 *   4. Runs each pair's read + write test cases
 *   5. Records contract hashes + tx ids + assertion results
 *
 * Output:
 *   docs/standards-mirror/deployments/results.json   — machine-readable
 *   docs/standards-mirror/deployments/RESULTS.md     — human-readable summary
 *
 * Env:
 *   NEO_TESTNET_WIF   — required, deployer WIF
 *   NEO_TESTNET_RPC   — optional, default http://seed1t5.neo.org:20332
 */

const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const { wallet, rpc, sc, tx, u, CONST, experimental } = require('@cityofzion/neon-js');

const ROOT = path.resolve(__dirname, '..');
const DEPLOYMENTS_DIR = path.join(ROOT, 'docs/standards-mirror/deployments');
const MANIFEST_PATH = path.join(DEPLOYMENTS_DIR, 'manifest.json');
const RESULTS_JSON = path.join(DEPLOYMENTS_DIR, 'results.json');
const RESULTS_MD = path.join(DEPLOYMENTS_DIR, 'RESULTS.md');

const RPC_URL = process.env.NEO_TESTNET_RPC || 'http://seed1t5.neo.org:20332';
const WIF = process.env.NEO_TESTNET_WIF;
const NEO_SOLC = path.join(ROOT, 'target/release/neo-solc');
const NCCS = path.join(process.env.HOME || '~', '.dotnet/tools/nccs');

const SUPPRESSED_WARNINGS = ['W101', 'W103', 'W113', 'W200', 'W121', 'W111', 'W116', 'W106', 'W105'];

function stripAnsi(s) {
  return String(s || '').replace(/\[[0-9;]*m/g, '');
}

function run(cmd, args, opts = {}) {
  const res = spawnSync(cmd, args, { encoding: 'utf8', ...opts });
  return res;
}

function compileSolidity(pair) {
  const sourcePath = path.join(DEPLOYMENTS_DIR, pair.solidity.source);
  const outPrefix = path.join('/tmp', pair.solidity.outName);

  const args = [
    sourcePath,
    '-I', path.join(ROOT, 'devpack'),
    '--contract', pair.solidity.contract,
    '-o', outPrefix
  ];
  for (const w of SUPPRESSED_WARNINGS) args.push('--Wno', w);

  const res = run(NEO_SOLC, args, { cwd: ROOT });
  if (res.status !== 0) {
    return { ok: false, reason: 'compile failed', stderr: stripAnsi(res.stderr) };
  }
  const nefPath = `${outPrefix}.nef`;
  const manifestPath = `${outPrefix}.manifest.json`;
  if (!fs.existsSync(nefPath) || !fs.existsSync(manifestPath)) {
    return { ok: false, reason: 'compile output missing' };
  }

  if (pair.solidity.clearSupportedStandards) {
    const m = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
    m.supportedstandards = [];
    fs.writeFileSync(manifestPath, JSON.stringify(m, null, 2));
  }

  return { ok: true, nefPath, manifestPath };
}

function compileCsharp(pair) {
  const csprojPath = path.join(DEPLOYMENTS_DIR, pair.csharp.csproj);
  const outDir = path.join('/tmp', `${pair.id}-cs`);
  fs.mkdirSync(outDir, { recursive: true });

  const res = run(NCCS, [csprojPath, '-o', outDir], { cwd: path.dirname(csprojPath) });
  if (res.status !== 0) {
    return { ok: false, reason: 'compile failed', stderr: stripAnsi(res.stderr) };
  }
  const nefName = path.basename(csprojPath).replace('.csproj', '.nef');
  const manifestName = path.basename(csprojPath).replace('.csproj', '.manifest.json');
  const nefPath = path.join(outDir, nefName);
  const manifestPath = path.join(outDir, manifestName);
  if (!fs.existsSync(nefPath) || !fs.existsSync(manifestPath)) {
    return { ok: false, reason: 'compile output missing' };
  }
  return { ok: true, nefPath, manifestPath };
}

function to0x(hex) {
  if (!hex) return hex;
  const s = String(hex);
  return s.startsWith('0x') ? s : `0x${s}`;
}

function strip0x(hex) {
  const s = String(hex || '');
  return s.startsWith('0x') ? s.slice(2) : s;
}

function reverseScriptHash(value) {
  // Application-log stack values come as base64 strings for ByteString/Hash160.
  // We decode base64, then reverse bytes (LE -> BE) to get the display script hash.
  let s = String(value || '');
  if (s.startsWith('0x')) s = s.slice(2);
  let buf;
  // If it's exactly 40 hex chars (20 bytes), treat as hex.
  if (s.length === 40 && /^[0-9a-fA-F]+$/.test(s)) {
    buf = Buffer.from(s, 'hex');
  } else {
    // base64 (typical for application log stack output)
    buf = Buffer.from(s, 'base64');
  }
  return buf.reverse().toString('hex');
}

// Stack-top "ByteString" decoder: returns hex string of the underlying bytes.
function bytestringToHex(stackVal) {
  let s = String(stackVal || '');
  if (s.length === 40 && /^[0-9a-fA-F]+$/.test(s)) return s;
  return Buffer.from(s, 'base64').toString('hex');
}

async function deploy(client, networkConfig, account, nef, manifest, deployData) {
  let dataParam;
  if (deployData === null || deployData === undefined) {
    dataParam = sc.ContractParam.any(null);
  } else if (Array.isArray(deployData)) {
    // Solidity convention: JSON-encoded array of constructor args, even if empty.
    dataParam = sc.ContractParam.string(JSON.stringify(deployData));
  } else {
    dataParam = sc.ContractParam.any(deployData);
  }
  const args = [
    sc.ContractParam.byteArray(u.HexString.fromHex(nef.serialize(), true)),
    sc.ContractParam.string(JSON.stringify(manifest.toJson())),
    dataParam
  ];
  const builder = new sc.ScriptBuilder();
  builder.emitContractCall({
    scriptHash: CONST.NATIVE_CONTRACT_HASH.ManagementContract,
    operation: 'deploy',
    callFlags: sc.CallFlags.All,
    args
  });
  const transaction = new tx.Transaction();
  transaction.script = u.HexString.fromHex(builder.build());
  await experimental.txHelpers.setBlockExpiry(transaction, networkConfig, networkConfig.blocksTillExpiry);
  transaction.addSigner({ account: account.scriptHash, scopes: 'CalledByEntry' });
  try {
    await experimental.txHelpers.addFees(transaction, networkConfig);
  } catch (e) {
    // Detect "Contract Already Exists: 0x..." and surface as a structured error
    // so the caller can fetch the existing contract hash and skip redeploy.
    const m = String(e.message || e).match(/Contract Already Exists:\s*(0x[a-fA-F0-9]+)/);
    if (m) {
      const err = new Error('contract already exists');
      err.contractHash = m[1].slice(2);
      throw err;
    }
    throw e;
  }
  transaction.sign(account, networkConfig.networkMagic);
  return await client.sendRawTransaction(transaction);
}

async function waitForTx(client, txid, timeoutMs = 180000) {
  const started = Date.now();
  while (Date.now() - started < timeoutMs) {
    try {
      const log = await client.getApplicationLog(txid);
      if (log && Array.isArray(log.executions) && log.executions.length > 0) return log;
    } catch {}
    await new Promise((r) => setTimeout(r, 3000));
  }
  throw new Error(`timeout waiting for tx ${txid}`);
}

async function waitForContract(client, contractHash, timeoutMs = 180000) {
  const started = Date.now();
  const want = to0x(contractHash);
  while (Date.now() - started < timeoutMs) {
    try {
      const state = await client.getContractState(want);
      if (state && state.hash) return state;
    } catch {}
    await new Promise((r) => setTimeout(r, 3000));
  }
  throw new Error(`timeout waiting for contract ${want}`);
}

function materialize(value, ctx) {
  if (Array.isArray(value)) return value.map((v) => materialize(v, ctx));
  if (value && typeof value === 'object') {
    const o = {};
    for (const [k, v] of Object.entries(value)) o[k] = materialize(v, ctx);
    return o;
  }
  if (typeof value === 'string' && /^\$[A-Z0-9_]+$/.test(value)) {
    const k = value.slice(1);
    if (!Object.prototype.hasOwnProperty.call(ctx, k)) {
      throw new Error(`unknown template: ${value}`);
    }
    return ctx[k];
  }
  return value;
}

function toContractParam(value) {
  if (value && typeof value === 'object' && !Array.isArray(value)) {
    const t = String(value.type || '').toLowerCase();
    if (t === 'hash160') return sc.ContractParam.hash160(String(value.value));
    if (t === 'publickey') return sc.ContractParam.publicKey(String(value.value));
    if (t === 'integer') return sc.ContractParam.integer(String(value.value));
    if (t === 'boolean') return sc.ContractParam.boolean(Boolean(value.value));
    if (t === 'string') return sc.ContractParam.string(String(value.value));
    if (t === 'bytearray') {
      const raw = String(value.value);
      return raw.startsWith('0x')
        ? sc.ContractParam.byteArray(u.HexString.fromHex(strip0x(raw)))
        : sc.ContractParam.byteArray(raw);
    }
  }
  if (Array.isArray(value)) return sc.ContractParam.array(...value.map(toContractParam));
  if (typeof value === 'number') return sc.ContractParam.integer(value);
  if (typeof value === 'boolean') return sc.ContractParam.boolean(value);
  if (typeof value === 'string') return sc.ContractParam.string(value);
  if (value === null || value === undefined) return sc.ContractParam.any(null);
  throw new Error(`unsupported param: ${typeof value}`);
}

function decodeBase64ToHex(b64) {
  return Buffer.from(b64, 'base64').toString('hex');
}

function decodeBase64ToBigIntLE(b64) {
  const buf = Buffer.from(b64, 'base64');
  let n = 0n;
  for (let i = buf.length - 1; i >= 0; i--) {
    n = (n << 8n) | BigInt(buf[i]);
  }
  return n;
}

function normalizeStackValue(stackTop, expectType) {
  let val = stackTop.value;
  const actualType = stackTop.type;

  if (expectType === 'Integer') {
    if (actualType === 'Integer') return BigInt(String(val));
    if (actualType === 'ByteString') {
      // base64-encoded LE bytes
      return decodeBase64ToBigIntLE(String(val));
    }
    if (actualType === 'Boolean') return BigInt(val ? 1 : 0);
  }

  if (expectType === 'ByteString') {
    if (actualType === 'ByteString') {
      // Compare as hex (manifest's expect.value is hex)
      return decodeBase64ToHex(String(val));
    }
    if (actualType === 'Integer') {
      return String(val);
    }
  }

  if (expectType === 'Hash160') {
    if (actualType === 'Hash160' || actualType === 'ByteString') {
      // base64 in raw stack → reverse for big-endian display form
      const buf = Buffer.from(String(val), 'base64');
      return buf.reverse().toString('hex');
    }
  }

  if (expectType === 'Boolean') {
    if (actualType === 'Boolean') return Boolean(val);
    if (actualType === 'Integer') return BigInt(val) !== 0n;
  }

  return val;
}

function compareExpected(stackTop, expect) {
  if (!expect) return { ok: true };
  if (!stackTop) return { ok: false, reason: 'empty stack' };
  if (!Object.prototype.hasOwnProperty.call(expect, 'value')) return { ok: true };

  const normalizedActual = normalizeStackValue(stackTop, expect.type);

  let expectedNormalized;
  if (expect.type === 'Integer') expectedNormalized = BigInt(String(expect.value));
  else if (expect.type === 'Boolean') expectedNormalized = Boolean(expect.value);
  else expectedNormalized = String(expect.value);

  const actualStr = String(normalizedActual);
  const expectStr = String(expectedNormalized);

  if (actualStr !== expectStr) {
    return { ok: false, reason: `actual=${actualStr} (raw type=${stackTop.type}, value=${stackTop.value}), expected ${expectStr}` };
  }
  return { ok: true };
}

async function invokeRead(client, contractHash, op, args) {
  const params = args.map(toContractParam).map((p) => p.toJson());
  return await client.invokeFunction(to0x(contractHash), op, params);
}

async function invokeWrite(client, networkConfig, account, contractHash, op, args) {
  const params = args.map(toContractParam);
  const builder = new sc.ScriptBuilder();
  builder.emitContractCall({
    scriptHash: contractHash,
    operation: op,
    callFlags: sc.CallFlags.All,
    args: params
  });
  const transaction = new tx.Transaction();
  transaction.script = u.HexString.fromHex(builder.build());
  await experimental.txHelpers.setBlockExpiry(transaction, networkConfig, networkConfig.blocksTillExpiry);
  transaction.addSigner({ account: account.scriptHash, scopes: 'CalledByEntry' });
  await experimental.txHelpers.addFees(transaction, networkConfig);
  transaction.sign(account, networkConfig.networkMagic);
  return await client.sendRawTransaction(transaction);
}

async function deployAndTest({ tag, compile, deployData, tests, pair, ctx, client, account, networkConfig }) {
  if (!compile.ok) {
    process.stdout.write(`  [${tag}]  compile: ${compile.reason}\n`);
    if (compile.stderr) process.stdout.write(`        ${compile.stderr.split('\n').slice(0, 3).join('\n        ')}\n`);
    return { status: 'compile-fail', reason: compile.reason };
  }
  const nef = sc.NEF.fromBuffer(fs.readFileSync(compile.nefPath));
  const cm = sc.ContractManifest.fromJson(JSON.parse(fs.readFileSync(compile.manifestPath, 'utf8')));

  // Precompute contract hash deterministically from (deployer, nef.checksum, manifest.name)
  // to avoid parsing it out of application logs (which return a Contract struct).
  // Sender must be passed as HexString WITHOUT the littleEndian=true flag here —
  // experimental.getContractHash already handles the byte order internally.
  const expectedHash = experimental.getContractHash(
    u.HexString.fromHex(account.scriptHash),
    nef.checksum,
    cm.name
  ).toString();
  let contractHashHex = expectedHash.startsWith('0x') ? expectedHash.slice(2) : expectedHash;
  let deployTx = null;
  let reused = false;

  try {
    const txid = await deploy(client, networkConfig, account, nef, cm, deployData);
    const log = await waitForTx(client, txid);
    const exec = log.executions[0];
    if (!exec || exec.vmstate !== 'HALT') {
      return { status: 'deploy-fail', reason: exec ? exec.exception : 'no execution', deployTx: txid };
    }
    deployTx = txid;
    process.stdout.write(`  [${tag}]  deployed: ${contractHashHex} tx=${txid}\n`);
  } catch (e) {
    if (e.contractHash) {
      reused = true;
      process.stdout.write(`  [${tag}]   reused existing: ${contractHashHex}\n`);
    } else {
      process.stdout.write(`  [${tag}]  deploy: ${e.message || e}\n`);
      return { status: 'deploy-fail', reason: String(e.message || e) };
    }
  }

  const contractAddress = wallet.getAddressFromScriptHash(contractHashHex);
  try {
    await waitForContract(client, contractHashHex);
  } catch (e) {
    return {
      status: 'liveness-fail',
      reason: String(e.message || e),
      contractHash: contractHashHex,
      contractAddress,
      deployTx,
      reused,
      tests: []
    };
  }
  const testResults = await runTests(pair.id, tag, ctx, client, account, networkConfig, contractHashHex, tests || []);
  const failedTests = testResults.filter((t) => t.status !== 'pass').length;
  return {
    status: failedTests === 0 ? 'deployed' : 'test-fail',
    contractHash: contractHashHex,
    contractAddress,
    deployTx,
    reused,
    tests: testResults
  };
}

async function runTests(pairId, kind, ctx, client, account, networkConfig, contractHash, tests) {
  const results = [];
  for (const t of tests) {
    const args = (t.args || []).map((a) => materialize(a, ctx));
    if (t.kind === 'read') {
      try {
        const expect = materialize(t.expect, ctx);
        const res = await invokeRead(client, contractHash, t.operation, args);
        const stackTop = res && res.stack && res.stack[0];
        const cmp = compareExpected(stackTop, expect);
        results.push({
          name: t.name,
          kind: 'read',
          operation: t.operation,
          status: cmp.ok ? 'pass' : 'fail',
          actual: stackTop || null,
          expect: expect || null,
          reason: cmp.ok ? undefined : cmp.reason
        });
        process.stdout.write(`  [${kind}] ${cmp.ok ? '' : ''} ${t.name}\n`);
      } catch (e) {
        results.push({ name: t.name, kind: 'read', operation: t.operation, status: 'fail', reason: String(e.message || e) });
        process.stdout.write(`  [${kind}]  ${t.name}  ${e.message || e}\n`);
      }
    } else if (t.kind === 'write') {
      try {
        const txid = await invokeWrite(client, networkConfig, account, contractHash, t.operation, args);
        const log = await waitForTx(client, txid);
        const exec = log.executions[0];
        const ok = exec && exec.vmstate === 'HALT';
        results.push({
          name: t.name,
          kind: 'write',
          operation: t.operation,
          status: ok ? 'pass' : 'fail',
          txHash: txid,
          vmstate: exec ? exec.vmstate : null,
          gasconsumed: exec ? exec.gasconsumed : null
        });
        process.stdout.write(`  [${kind}] ${ok ? '' : ''} ${t.name}  tx=${txid}\n`);
      } catch (e) {
        results.push({ name: t.name, kind: 'write', operation: t.operation, status: 'fail', reason: String(e.message || e) });
        process.stdout.write(`  [${kind}]  ${t.name}  ${e.message || e}\n`);
      }
    }
  }
  return results;
}

function collectFailures(out) {
  const failures = [];
  for (const pair of out.pairs) {
    for (const which of ['solidity', 'csharp']) {
      const result = pair[which] || {};
      if (result.status && result.status !== 'deployed') {
        failures.push(`${pair.id}/${which}: ${result.status}${result.reason ? ` (${result.reason})` : ''}`);
      }
      for (const test of result.tests || []) {
        if (test.status !== 'pass') {
          failures.push(`${pair.id}/${which}/${test.name || test.operation}: ${test.reason || test.vmstate || 'failed'}`);
        }
      }
    }
  }
  return failures;
}

async function main() {
  if (process.argv.includes('--render-only')) {
    if (!fs.existsSync(RESULTS_JSON)) throw new Error(`missing ${RESULTS_JSON}`);
    const out = JSON.parse(fs.readFileSync(RESULTS_JSON, 'utf8'));
    fs.writeFileSync(RESULTS_MD, renderMarkdown(out));
    process.stdout.write(`Wrote ${RESULTS_MD}\n`);
    return;
  }

  if (!WIF) throw new Error('NEO_TESTNET_WIF required');
  if (!fs.existsSync(NEO_SOLC)) {
    process.stdout.write('Building neo-solc...\n');
    const b = run('cargo', ['build', '--release', '--bin', 'neo-solc'], { cwd: ROOT });
    if (b.status !== 0) throw new Error(`failed to build neo-solc:\n${b.stderr}`);
  }

  const account = new wallet.Account(WIF);
  const client = new rpc.RPCClient(RPC_URL);
  const version = await client.getVersion();
  const networkMagic = version?.protocol?.network;
  if (!networkMagic) throw new Error('failed to read network magic');
  const NEO_TESTNET_MAGIC = 894710606;
  if (networkMagic !== NEO_TESTNET_MAGIC) {
    throw new Error(
      `refusing to deploy: RPC ${RPC_URL} reports network magic ${networkMagic} ` +
      `but this script is hard-pinned to TestNet (${NEO_TESTNET_MAGIC}). ` +
      `Override only after auditing scripts/standards_mirror_testnet.js for any mainnet-unsafe assumptions.`
    );
  }

  const networkConfig = {
    networkMagic,
    rpcAddress: RPC_URL,
    account,
    blocksTillExpiry: 240
  };

  const ctx = {
    DEPLOYER_ADDRESS: account.address,
    DEPLOYER_ADDRESS_HEX: account.scriptHash, // little-endian hex
    DEPLOYER_PUBLIC_KEY: account.publicKey
  };

  const manifest = JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf8'));
  const out = {
    generatedAt: new Date().toISOString(),
    rpc: RPC_URL,
    networkMagic,
    deployer: account.address,
    pairs: []
  };

  for (const pair of manifest.pairs) {
    process.stdout.write(`\n=== ${pair.title} (${pair.id}) ===\n`);
    const pairResult = { id: pair.id, title: pair.title, solidity: {}, csharp: {} };

    pairResult.solidity = await deployAndTest({
      tag: 'sol',
      compile: compileSolidity(pair),
      deployData: pair.solidity.deployData,
      tests: pair.solidity.tests,
      pair, ctx, client, account, networkConfig
    });

    pairResult.csharp = await deployAndTest({
      tag: 'cs ',
      compile: compileCsharp(pair),
      deployData: pair.csharp.deployData,
      tests: pair.csharp.tests,
      pair, ctx, client, account, networkConfig
    });

    out.pairs.push(pairResult);

    // Persist after each pair so partial progress is preserved
    fs.writeFileSync(RESULTS_JSON, JSON.stringify(out, null, 2));
  }

  // Render markdown summary
  const md = renderMarkdown(out);
  fs.writeFileSync(RESULTS_MD, md);
  process.stdout.write(`\nWrote ${RESULTS_JSON}\nWrote ${RESULTS_MD}\n`);

  const failures = collectFailures(out);
  if (failures.length > 0) {
    process.stderr.write(`\n${failures.length} deployment/assertion failure(s):\n`);
    for (const f of failures) process.stderr.write(`- ${f}\n`);
    process.exitCode = 1;
  }
}

function renderMarkdown(out) {
  const lines = [];
  lines.push('# Standards Mirror — TestNet Deployments');
  lines.push('');
  lines.push(`- Generated: \`${out.generatedAt}\``);
  lines.push(`- RPC: \`${out.rpc}\``);
  lines.push(`- Network magic: \`${out.networkMagic}\``);
  lines.push(`- Deployer: \`${out.deployer}\``);
  lines.push('');
  lines.push('## Summary');
  lines.push('');
  lines.push('| Pair | Implementation | Address | Deploy Tx | Tests |');
  lines.push('|---|---|---|---|---|');
  for (const p of out.pairs) {
    for (const which of ['solidity', 'csharp']) {
      const r = p[which];
      const passed = (r.tests || []).filter((t) => t.status === 'pass').length;
      const total = (r.tests || []).length;
      const status = r.status || '-';
      lines.push(
        `| ${p.title} | ${which} | \`${r.contractAddress || '-'}\` | \`${r.deployTx || '-'}\` | ${passed}/${total} (${status}) |`
      );
    }
  }
  lines.push('');
  lines.push('## Failed Assertions');
  lines.push('');
  const failures = [];
  for (const p of out.pairs) {
    for (const which of ['solidity', 'csharp']) {
      const r = p[which] || {};
      if (r.status && r.status !== 'deployed' && (!r.tests || r.tests.length === 0)) {
        failures.push([p.title, which, r.status, '-', r.reason || 'deployment failed']);
      }
      for (const t of r.tests || []) {
        if (t.status !== 'pass') {
          failures.push([p.title, which, t.name || '-', t.operation || '-', t.reason || t.vmstate || 'failed']);
        }
      }
    }
  }

  if (failures.length === 0) {
    lines.push('No deployment or assertion failures were recorded.');
  } else {
    lines.push('| Pair | Implementation | Test | Operation | Reason |');
    lines.push('|---|---|---|---|---|');
    for (const row of failures) {
      lines.push(`| ${row.map(mdCell).join(' | ')} |`);
    }
  }
  lines.push('');
  lines.push('## Full Result Data');
  lines.push('');
  lines.push(
    'The full per-test payload is stored in [`results.json`](https://github.com/r3e-network/neo-devpack-solidity/blob/main/docs/standards-mirror/deployments/results.json).'
  );
  return lines.join('\n');
}

function mdCell(value) {
  const text = String(value || '-')
    .replace(/\r?\n/g, ' ')
    .replace(/[\u0000-\u001f\u007f]/g, ' ')
    .replace(/\uFFFD/g, '')
    .replace(/\s+/g, ' ')
    .trim();
  const alreadyClaimed = text.match(/([A-Za-z][A-Za-z0-9]*: already claimed)/);
  if (alreadyClaimed) {
    return alreadyClaimed[1].replace(/\|/g, '\\|');
  }
  const clipped = text.length > 240 ? `${text.slice(0, 237)}...` : text;
  return clipped.replace(/\|/g, '\\|');
}

main().catch((e) => {
  process.stderr.write(`error: ${e.message || e}\n`);
  process.exit(1);
});