Files
smom-dbis-138/services/truth-adapter/dist/index.js
2026-03-02 12:14:09 -08:00

85 lines
2.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApi = getApi;
exports.getBalance = getBalance;
exports.getChainConstants = getChainConstants;
exports.getFinalizedHead = getFinalizedHead;
exports.disconnect = disconnect;
/**
* Truth Network (Substrate) chain adapter.
* Connects via WSS, exposes balance (native TRUU), chain constants, and bridge-related helpers.
* See: docs/07-ccip/TRUTH_NETWORK_BRIDGE_SPEC.md
*/
const api_1 = require("@polkadot/api");
const TRUTH_WSS = process.env.TRUTH_WSS_URL || 'wss://chain-external.truth-network.io';
const SS58_FORMAT = parseInt(process.env.TRUTH_SS58_FORMAT || '42', 10);
let api = null;
async function getApi(wssUrl = TRUTH_WSS) {
if (api)
return api;
const provider = new api_1.WsProvider(wssUrl);
api = await api_1.ApiPromise.create({ provider });
return api;
}
async function getBalance(ss58Address, wssUrl) {
const a = await getApi(wssUrl || TRUTH_WSS);
const account = await a.query.system.account(ss58Address);
const data = account.toJSON();
return data?.data?.free ?? '0';
}
async function getChainConstants(wssUrl) {
const a = await getApi(wssUrl || TRUTH_WSS);
const [genesisHash, props, runtime] = await Promise.all([
a.rpc.chain.getBlockHash(0),
a.rpc.system.properties(),
a.rpc.state.getRuntimeVersion(),
]);
const tokenDecimals = props.tokenDecimals ?? 10;
const tokenSymbol = props.tokenSymbol ?? 'TRUU';
return {
genesisHash: genesisHash.toHex(),
ss58Format: props.ss58Format ?? SS58_FORMAT,
tokenSymbol,
tokenDecimals,
specName: runtime.specName.toString(),
specVersion: runtime.specVersion.toNumber(),
minFinalityBlocks: 1,
};
}
async function getFinalizedHead(wssUrl) {
const a = await getApi(wssUrl || TRUTH_WSS);
const hash = await a.rpc.chain.getFinalizedHead();
return hash.toHex();
}
/** Disconnect and free the API. */
async function disconnect() {
if (api) {
await api.disconnect();
api = null;
}
}
// CLI: node dist/index.js balance <ss58Address> | constants
async function main() {
const cmd = process.argv[2];
const arg = process.argv[3];
if (cmd === 'balance' && arg) {
const bal = await getBalance(arg);
console.log(bal);
}
else if (cmd === 'constants') {
const c = await getChainConstants();
console.log(JSON.stringify(c, null, 2));
}
else {
console.log('Usage: balance <ss58Address> | constants');
process.exit(1);
}
await disconnect();
}
if (require.main === module) {
main().catch((e) => {
console.error(e);
process.exit(1);
});
}