// inspect-erc3643.ts: read-only checks on a contract that claims to be an ERC-3643 (T-REX) token. // Usage: RPC_URL=https://... FROM_BLOCK= [BLOCK=] npx tsx inspect-erc3643.ts 0xTokenAddress // It only reads: no private key, no signature, no transaction. Partial inventory: it covers the // T-REX reference contracts; custom compliance or extra roles need their own reads. import { createPublicClient, getAddress, http, parseAbi, parseAbiItem, zeroAddress, type Address } from 'viem' const client = createPublicClient({ transport: http(process.env.RPC_URL) }) const token = getAddress(process.argv[2] ?? '') const fromBlock = BigInt(process.env.FROM_BLOCK ?? '0') // many RPCs cap log ranges: start at deployment const abi = parseAbi([ 'function name() view returns (string)', 'function symbol() view returns (string)', 'function totalSupply() view returns (uint256)', 'function owner() view returns (address)', 'function onchainID() view returns (address)', 'function identityRegistry() view returns (address)', 'function compliance() view returns (address)', 'function isAgent(address) view returns (bool)', 'function issuersRegistry() view returns (address)', 'function topicsRegistry() view returns (address)', 'function getTrustedIssuers() view returns (address[])', 'function getTrustedIssuerClaimTopics(address) view returns (uint256[])', 'function getClaimTopics() view returns (uint256[])', 'function getTokenBound() view returns (address)', 'function getModules() view returns (address[])', 'function getImplementationAuthority() view returns (address)', ]) const agentAdded = parseAbiItem('event AgentAdded(address indexed _agent)') const transfer = parseAbiItem('event Transfer(address indexed from, address indexed to, uint256 value)') async function main() { // Pin every read and every log query to one block (finalized by default), so supply and history agree. const blockNumber = process.env.BLOCK ? BigInt(process.env.BLOCK) : (await client.getBlock({ blockTag: 'finalized' })).number const read = (address: Address, functionName: string, args: unknown[] = []) => client.readContract({ address, abi, functionName, args, blockNumber } as never) as Promise const tryRead = (address: Address, functionName: string) => read(address, functionName).catch(() => 'n/a (not exposed)') const kind = async (a: Address) => ((await client.getCode({ address: a, blockNumber })) ?? '0x') === '0x' ? 'EOA' : 'contract' const ownerOf = async (a: Address) => { const o = (await read(a, 'owner')) as Address; return o + ' ' + (await kind(o)) } const supply = (await read(token, 'totalSupply')) as bigint const oid = (await read(token, 'onchainID')) as Address const ir = (await read(token, 'identityRegistry')) as Address const mc = (await read(token, 'compliance')) as Address console.log('block', blockNumber, { name: await read(token, 'name'), symbol: await read(token, 'symbol'), totalSupply: supply }) console.log('token owner ', await ownerOf(token)) console.log('token onchainID ', oid, oid === zeroAddress ? 'NOT SET' : await kind(oid)) // Who decides who counts as "verified" const tir = (await read(ir, 'issuersRegistry')) as Address const ctr = (await read(ir, 'topicsRegistry')) as Address const topics = (await read(ctr, 'getClaimTopics')) as readonly bigint[] const issuers = (await read(tir, 'getTrustedIssuers')) as readonly Address[] console.log('owners: identity registry', await ownerOf(ir), '| topics', await ownerOf(ctr), '| issuers', await ownerOf(tir)) console.log('required claim topics', topics.length ? topics : 'NONE: every registered identity passes isVerified') const covered = new Set() for (const issuer of issuers) { const issuerTopics = (await read(tir, 'getTrustedIssuerClaimTopics', [issuer])) as readonly bigint[] issuerTopics.forEach((x) => covered.add(x)) console.log('trusted issuer', issuer, await kind(issuer), 'topics', issuerTopics) } for (const topic of topics) { if (!covered.has(topic)) console.log('topic', topic, 'has no trusted issuer: isVerified is false for everyone') } // Compliance: custom (non-modular) contracts may not expose these getters, so don't abort on them. console.log('compliance owner', await ownerOf(mc).catch(() => 'n/a (not exposed)')) console.log('compliance bound token', await tryRead(mc, 'getTokenBound'), '| modules', await tryRead(mc, 'getModules')) // Upgrades: a T-REX proxy delegates to an implementation authority whose owner can switch versions. const ia = await tryRead(token, 'getImplementationAuthority') console.log('implementation authority', ia, typeof ia === 'string' && ia.startsWith('0x') ? await ownerOf(ia as Address) : '') // Agents can mint. No public getter lists them, so replay AgentAdded and keep those still active. const added = await client.getLogs({ address: token, event: agentAdded, fromBlock, toBlock: blockNumber }) for (const a of new Set(added.map((l) => l.args._agent).filter((x): x is Address => !!x))) { if (await read(token, 'isAgent', [a])) console.log('active agent', a, await kind(a)) } // Mint history: Transfer from the zero address, minus burns to it, up to the same block. const range = { address: token, event: transfer, fromBlock, toBlock: blockNumber } const mints = await client.getLogs({ ...range, args: { from: zeroAddress } }) const burns = await client.getLogs({ ...range, args: { to: zeroAddress } }) const sum = (logs: typeof mints) => logs.reduce((s, l) => s + (l.args.value ?? 0n), 0n) for (const m of mints) console.log('mint block', m.blockNumber, 'to', m.args.to, 'value', m.args.value) console.log('minted', sum(mints), 'burned', sum(burns), '| matches totalSupply:', sum(mints) - sum(burns) === supply) } main().catch((e) => { console.error(e); process.exit(1) })