"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProvider = getProvider;
exports.getBridgeContract = getBridgeContract;
exports.getTruuTokenContract = getTruuTokenContract;
exports.getTruuBalance = getTruuBalance;
exports.getTruuAllowance = getTruuAllowance;
exports.encodeRecipientBytes = encodeRecipientBytes;
exports.lift = lift;
exports.onLift = onLift;
/**
 * Ethereum adapter for Truth Bridge and TRUU token.
 * Lift (lock on ETH → mint on TN), read state; lower requires proof (see proof-builder).
 * See: docs/07-ccip/TRUTH_NETWORK_BRIDGE_SPEC.md
 */
require("dotenv/config");
const ethers_1 = require("ethers");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const abiDir = path.join(__dirname, '../../../abis');
const BRIDGE_ABI = JSON.parse(fs.readFileSync(path.join(abiDir, 'truth-bridge.json'), 'utf8'));
const TOKEN_ABI = JSON.parse(fs.readFileSync(path.join(abiDir, 'truth-token.json'), 'utf8'));
const RPC = process.env.ETHEREUM_MAINNET_RPC || process.env.CHAIN_1_RPC_URL || 'https://eth.llamarpc.com';
const BRIDGE_ADDRESS = process.env.TRUTH_BRIDGE_ETH_MAINNET || '0x50c02710b06d6AdDb864D6b038010eF6fA1BCd92';
const TOKEN_ADDRESS = process.env.TRUU_TOKEN_ETH_MAINNET || '0xDAe0faFD65385E7775Cf75b1398735155EF6aCD2';
function getProvider() {
    return new ethers_1.JsonRpcProvider(RPC);
}
function getBridgeContract(signer) {
    const provider = getProvider();
    return new ethers_1.Contract(BRIDGE_ADDRESS, BRIDGE_ABI, signer || provider);
}
function getTruuTokenContract(signer) {
    const provider = getProvider();
    return new ethers_1.Contract(TOKEN_ADDRESS, TOKEN_ABI, signer || provider);
}
async function getTruuBalance(address) {
    const token = getTruuTokenContract();
    const bal = await token.balanceOf(address);
    return (0, ethers_1.formatEther)(bal);
}
async function getTruuAllowance(owner, spender) {
    const token = getTruuTokenContract();
    return await token.allowance(owner, spender);
}
/** Encode recipient for lift(bytes): SS58 address as hex bytes. */
function encodeRecipientBytes(ss58AddressHex) {
    if (ss58AddressHex.startsWith('0x'))
        ss58AddressHex = ss58AddressHex.slice(2);
    return '0x' + ss58AddressHex;
}
/**
 * Call lift(amount, recipientBytes). Requires prior TRUU approve(bridge, amount).
 */
async function lift(amountWei, recipientBytes, privateKey) {
    const wallet = new ethers_1.Wallet(privateKey, getProvider());
    const bridge = getBridgeContract(wallet);
    const tx = await bridge.lift(amountWei, recipientBytes);
    const rec = await tx.wait();
    return rec.hash;
}
/** Subscribe to Lift events (for indexer). */
function onLift(callback) {
    const bridge = getBridgeContract();
    bridge.on('Lift', (sender, amount, recipient) => {
        callback(sender, amount, recipient);
    });
}
// CLI
async function main() {
    const cmd = process.argv[2];
    const arg = process.argv[3];
    if (cmd === 'balance' && arg) {
        const bal = await getTruuBalance(arg);
        console.log('TRUU balance:', bal);
    }
    else {
        console.log('Usage: node ethereum-adapter.js balance <address>');
        process.exit(1);
    }
}
if (require.main === module) {
    main().catch((e) => {
        console.error(e);
        process.exit(1);
    });
}
