Files
smom-dbis-138/scripts/deployment/post-btc-m2-headroom-adjustment.mjs
zaragoza444 54ca9bea3b
Some checks failed
CI/CD Pipeline / Solidity Contracts (push) Failing after 52s
CI/CD Pipeline / Security Scanning (push) Successful in 2m40s
CI/CD Pipeline / Lint and Format (push) Failing after 49s
CI/CD Pipeline / Terraform Validation (push) Failing after 24s
CI/CD Pipeline / Kubernetes Validation (push) Successful in 27s
Deploy ChainID 138 / Deploy ChainID 138 (push) Has been cancelled
Validation / validate-genesis (push) Has been cancelled
Validation / validate-terraform (push) Has been cancelled
Validation / validate-kubernetes (push) Has been cancelled
Verify Deployment / Verify Deployment (push) Has been cancelled
Validation / validate-smart-contracts (push) Failing after 14s
Validation / validate-security (push) Failing after 1m20s
Validation / validate-documentation (push) Failing after 18s
feat(settlement): wire real Chain 138 contracts for 1,000 BTC ledger
Add canonical btc-l1-ledger config, /btc/contracts API, Python chain138-oracle
service, and production deploy scripts so secure.omdnl.org can serve live cBTC,
DODO, and PMM pool addresses with headroom-aware mint gating.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-21 02:26:20 -07:00

115 lines
3.8 KiB
JavaScript

#!/usr/bin/env node
/**
* Post three-leg BTC M2 headroom policy adjustment to Fineract:
* Leg 1 (Office 1): Dr 2100 / Cr 2410
* Leg 2 (Office 24): Dr 1410 / Cr 2100
* Leg 3 (Office 24): Dr 2100 / Cr 2200
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, '../..');
const configPath = path.join(repoRoot, 'config/offices/btc-m2-headroom-adjustment-20260720.v1.json');
const dryRun = process.argv.includes('--dry-run');
const cfg = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const base = (process.env.OMNL_FINERACT_BASE_URL || process.env.FINERACT_BASE_URL || '').replace(/\/$/, '');
const tenant = process.env.OMNL_FINERACT_TENANT || process.env.FINERACT_TENANT || 'omnl';
const user =
process.env.OMNL_FINERACT_USER?.trim() ||
process.env.OMNL_FINERACT_USERNAME?.trim() ||
'ali_hospitallers_tenant';
const pass = process.env.OMNL_FINERACT_PASSWORD || '';
const amountRaw = process.env[cfg.amountEnv] || String(cfg.amountUsd || '');
const amount = parseFloat(amountRaw);
if (!base || !pass) {
console.error('Set OMNL_FINERACT_BASE_URL and OMNL_FINERACT_PASSWORD');
process.exit(1);
}
if (!Number.isFinite(amount) || amount <= 0) {
console.error(`Set ${cfg.amountEnv} or amountUsd in config to a positive USD amount`);
process.exit(1);
}
const auth = Buffer.from(`${user}:${pass}`).toString('base64');
const headers = {
Authorization: `Basic ${auth}`,
'Fineract-Platform-TenantId': tenant,
'Content-Type': 'application/json',
Accept: 'application/json',
};
const glCache = new Map();
async function glIdForCode(glCode) {
const code = String(glCode).trim();
if (glCache.has(code)) return glCache.get(code);
const res = await fetch(`${base}/glaccounts?glCode=${encodeURIComponent(code)}`, { headers });
if (!res.ok) throw new Error(`GL lookup failed for ${code} (${res.status})`);
const data = await res.json();
const list = Array.isArray(data) ? data : data?.pageItems ?? [];
const match = list.find((row) => String(row.glCode) === code);
if (!match?.id) throw new Error(`GL code ${code} not found in Fineract tenant ${tenant}`);
glCache.set(code, Number(match.id));
return Number(match.id);
}
const txDate = cfg.transactionDate || new Date().toISOString().slice(0, 10);
const results = [];
for (const leg of cfg.entries) {
const debitGlAccountId = await glIdForCode(leg.debitGlCode);
const creditGlAccountId = await glIdForCode(leg.creditGlCode);
const entry = {
officeId: leg.officeId,
transactionDate: txDate,
referenceNumber: leg.memo,
comments: leg.comments,
currencyCode: cfg.currencyCode || 'USD',
dateFormat: 'yyyy-MM-dd',
locale: 'en',
debits: [{ glAccountId: debitGlAccountId, amount }],
credits: [{ glAccountId: creditGlAccountId, amount }],
};
const summary = {
memo: leg.memo,
officeId: leg.officeId,
amount,
debitGlCode: leg.debitGlCode,
creditGlCode: leg.creditGlCode,
debitGlAccountId,
creditGlAccountId,
};
console.log(JSON.stringify({ dryRun, ...summary }, null, 2));
if (dryRun) {
results.push({ ...summary, status: 'dry-run' });
continue;
}
const res = await fetch(`${base}/journalentries`, {
method: 'POST',
headers,
body: JSON.stringify(entry),
});
const body = await res.text();
if (!res.ok) {
console.error(`Fineract POST failed for ${leg.memo} (${res.status}): ${body}`);
process.exit(1);
}
let parsed;
try {
parsed = JSON.parse(body);
} catch {
parsed = { raw: body };
}
results.push({ ...summary, status: 'posted', response: parsed });
console.log(`Posted ${leg.memo}:`, body);
}
console.log(JSON.stringify({ dryRun, amount, legs: results.length, results }, null, 2));