igneum/tools/reference-apps/oracle/demo.mjs

40 lines
2.9 KiB
JavaScript

// Reads a Devnet 3 balance from Sepolia. With fixtures/dn3-balance.json: submits its certificate through the verifier
// (stub or real), submits the state root with the full proof, then calls provenBalance and prints the Sepolia-read
// balance beside the fixture's. Without it: the same flow on the synthetic vector, and says so.
import { readFileSync, existsSync } from 'node:fs';
import * as L from './lib.mjs';
const d = L.deployment();
const pub = L.publicClient();
const wallet = L.walletClient(L.deployerAccount());
const abi = L.artifact('IgneumStateOracle').abi;
const read = (functionName, args) => pub.readContract({ address: d.oracle.address, abi, functionName, args });
const fx = L.fixtures + '/dn3-balance.json';
const real = existsSync(fx);
const V = real ? L.vectorFromBalanceFixture(JSON.parse(readFileSync(fx, 'utf8'))) : L.buildSynthetic();
console.log(L.ukTime(), 'UK oracle', d.oracle.address, 'on Sepolia, verifier', await read('verifier'), real ? '' : '(no dn3-balance.json yet: synthetic vector)');
console.log('vector:', V.kind, 'checkpoint', V.certIndex, V.checkpoint.slice(0, 12), 'headers', V.headers.length, 'chain block', V.number, 'address', L.hex0x(V.address));
const c = await L.ensureCertificate(pub, wallet, d, V.certIndex, L.hex0x(L.hexToBytes(V.checkpoint)), V.bitmap, V.signature);
console.log(L.ukTime(), 'UK certificate', V.certIndex, c.already ? 'already recorded on the verifier' : 'recorded: tx ' + c.tx + ' block ' + c.block + ' gas ' + c.gasUsed);
const r = await L.ensureStateRoot(pub, wallet, d, V);
console.log(L.ukTime(), 'UK state root for block', V.number, r.already ? 'already stored' : 'stored: tx ' + r.tx + ' block ' + r.block + ' gas ' + r.gasUsed + ' (' + r.headers + ' headers, ' + r.calldataBytes + ' bytes of calldata)');
const [root, blockHash, carrier, certIndex] = await read('stateRoot', [V.number]);
console.log('stored post_root', root, 'block hash', blockHash.slice(0, 14), 'carrier', carrier.slice(0, 14), 'certificate', certIndex);
const balance = await read('provenBalance', [V.number, L.hex0x(V.address), V.accountProof.map(L.hex0x)]);
const expected = real ? V.balance : V.account.balance;
const ign = w => (Number(w / 10n ** 12n) / 1e6).toFixed(6);
console.log('');
console.log('Sepolia-read balance :', balance.toString(), 'wei =', ign(balance), 'IGN');
console.log('fixture balance :', expected.toString(), 'wei =', ign(expected), 'IGN');
console.log(balance === expected ? 'MATCH' : 'MISMATCH');
if (real && V.storageProofs.length) {
for (const sp of V.storageProofs) {
const v = await read('provenStorage', [V.number, L.hex0x(V.address), L.hex0x(sp.slot), V.accountProof.map(L.hex0x), sp.proof.map(L.hex0x)]);
console.log('slot', L.hex0x(sp.slot).slice(0, 14), 'Sepolia-read', v, 'fixture', sp.value.toString(), BigInt(v) === sp.value ? 'MATCH' : 'MISMATCH');
}
}
console.log('');
console.log('trust():', await read('trust'));
process.exit(balance === expected ? 0 : 1);