// IdleCredits, the contract that holds prepaid credits on Elysium (onchain/contracts/IdleCredits.sol). // This file only reads it: what it holds, who its operator is, and what one address has in it. // Nothing here signs or sends. A wallet sends its own deposits and withdrawals (the relayer carries them). // // What is deployed, and what is not wired yet: // deployed the contract itself, on Elysium, at ELYSIUM_CREDITS.address // not wired the rental engine does not read these balances, charge them or seal receipts yet. // A balance held here cannot be spent on a rental today. It can always be withdrawn. import { Interface, formatUnits, getAddress } from "ethers"; /** The deployment, as written by onchain/scripts/deploy-elysium.mjs (public information only). */ export const ELYSIUM_CREDITS = { address: "0x107a77F5B01dDB8bacb647785c5ea9C493917baA", deployer: "0x957b4Dfa58ae17675D2432C6965653556FD87285", txHash: "0xef2d62f3e0513ebc67fb0db0615a2564f3bb21a45367d253101bf5199f1be216", inboxTxHash: "0x7a84ab77607fce780de04ea41a255bbe23dd4abc9c9efd594a69d52032d85c11", block: 13116695, // The day the explorer accepted the source as matching the deployed code (onchain/scripts/verify-elysium.mjs // answered "Pass - Verified"). A fact recorded here, not read at request time: the explorer page shows it. sourceVerifiedOn: "2026-11-29", }; export const CREDITS_ABI = [ "function usdc() view returns (address)", "function eqnvda() view returns (address)", "function operator() view returns (address)", "function pendingOperator() view returns (address)", "function withdrawalDelay() view returns (uint64)", "function totalBalance(address token) view returns (uint256)", "function earned(address token) view returns (uint256)", "function balanceOf(address user, address token) view returns (uint256)", "function available(address user, address token) view returns (uint256)", "function chargeable(address user, address token) view returns (uint256)", "function pendingWithdrawal(address user, address token) view returns (uint256 amount, uint64 unlockAt)", "function deposit(address token, uint256 amount)", "function requestWithdrawal(address token, uint256 amount)", "function cancelWithdrawal(address token)", "function withdraw(address token)", ]; const credits = new Interface(CREDITS_ABI); const erc20 = new Interface([ "function balanceOf(address account) view returns (uint256)", "function allowance(address owner, address spender) view returns (uint256)", ]); /** One view call. `chain` is a JSON-RPC caller to Elysium: (method, params) => result. */ async function view(chain, to, iface, fn, args = []) { const out = await chain("eth_call", [{ to, data: iface.encodeFunctionData(fn, args) }, "latest"]); if (typeof out !== "string" || out === "0x") throw new Error(fn + "() did not answer at " + to); return iface.decodeFunctionResult(fn, out); } const amount = (units, decimals) => ({ units: units.toString(), amount: formatUnits(units, decimals) }); /** * What the contract says of itself, read now. * @param {(method:string, params:any[])=>Promise} chain * @param {{address:string, tokens:{symbol:string, address:string, decimals:number}[]}} cfg */ export async function readCredits(chain, cfg) { const at = getAddress(cfg.address); const code = await chain("eth_getCode", [at, "latest"]); if (typeof code !== "string" || code === "0x") return { deployed: false, address: at }; const [[usdc], [eqnvda], [operator], [pendingOperator], [delay], ...figures] = await Promise.all([ view(chain, at, credits, "usdc"), view(chain, at, credits, "eqnvda"), view(chain, at, credits, "operator"), view(chain, at, credits, "pendingOperator"), view(chain, at, credits, "withdrawalDelay"), ...cfg.tokens.flatMap((t) => [view(chain, at, credits, "totalBalance", [t.address]), view(chain, at, credits, "earned", [t.address]), view(chain, t.address, erc20, "balanceOf", [at])]), ]); // The contract must be the one this deployment takes it for: same two tokens, in the same roles. const want = Object.fromEntries(cfg.tokens.map((t) => [t.symbol, getAddress(t.address)])); const matches = getAddress(usdc) === want.USDC && getAddress(eqnvda) === want.EQNVDA; return { deployed: true, address: at, codeBytes: (code.length - 2) / 2, matches, operator: getAddress(operator), pendingOperator: /^0x0{40}$/i.test(pendingOperator) ? null : getAddress(pendingOperator), withdrawalDelaySeconds: Number(delay), tokens: cfg.tokens.map((t, i) => { const [[held], [earned], [inContract]] = figures.slice(i * 3, i * 3 + 3); return { symbol: t.symbol, address: getAddress(t.address), decimals: t.decimals, // What users hold here and have not been charged, what was charged and not collected, and the // token balance of the contract itself. The third is never less than the sum of the first two. heldForUsers: amount(held, t.decimals), earnedByOperator: amount(earned, t.decimals), tokenBalance: amount(inContract, t.decimals), covered: inContract >= held + earned, }; }), }; } /** * What one address has in the contract and around it, per token. * @param {(method:string, params:any[])=>Promise} chain * @param {{address:string, tokens:{symbol:string, address:string, decimals:number}[]}} cfg * @param {string} user */ export async function readCreditsAccount(chain, cfg, user) { const at = getAddress(cfg.address); const who = getAddress(user); const rows = await Promise.all( cfg.tokens.map(async (t) => { const [[wallet], [allowance], [balance], [available], [chargeable], pending] = await Promise.all([ view(chain, t.address, erc20, "balanceOf", [who]), view(chain, t.address, erc20, "allowance", [who, at]), view(chain, at, credits, "balanceOf", [who, t.address]), view(chain, at, credits, "available", [who, t.address]), view(chain, at, credits, "chargeable", [who, t.address]), view(chain, at, credits, "pendingWithdrawal", [who, t.address]), ]); const unlockAt = Number(pending.unlockAt); return { symbol: t.symbol, address: getAddress(t.address), decimals: t.decimals, inWallet: amount(wallet, t.decimals), allowance: amount(allowance, t.decimals), inContract: amount(balance, t.decimals), // The balance less a requested withdrawal: what could be spent on rentals once the engine reads it. available: amount(available, t.decimals), chargeable: amount(chargeable, t.decimals), pendingWithdrawal: pending.amount > 0n ? { ...amount(pending.amount, t.decimals), unlockAt: new Date(unlockAt * 1000).toISOString(), unlockAtUnix: unlockAt } : null, }; }), ); return { address: who, contract: at, tokens: rows }; }