Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions projects/defa/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
const axios = require("axios");
const { callSoroban } = require("../helper/chain/stellar");
const { queryContract } = require("../helper/chain/cosmos");

const RPC_URL = "https://soroban-rpc.mainnet.stellar.gateway.fm";
// ============================================================
// Stellar — Soroban TVL Logger (get_active_tvl)
// Queries the on-chain Logger contract via Soroban RPC simulation.
// The contract returns active vault liquidity denominated in USD
// with 7 decimal places.
// ============================================================

const SIM_TX =
"AAAAAgAAAABInMr//HjTQ5mfaqRBHzYCJKEHrM2r8m9/4lm9PBoMVgABhqAAAAAAAAAAAgAAAAEAAAAAAAAAAAAAAABpkzaBAAAAAAAAAAEAAAAAAAAAGAAAAAAAAAAB61PtVr2XWjuuWsIcyFlAq888asA0bR8BnY/AeIm0wxQAAAAOZ2V0X2FjdGl2ZV90dmwAAAAAAAAAAAAAAAAAAAAAAAA=";
const STELLAR_CONTRACT = "CDVVH3KWXWLVUO5OLLBBZSCZICV46PDKYA2G2HYBTWH4A6EJWTBRIXRK";

async function getActiveTvl() {
const body = {
jsonrpc: "2.0",
id: 1,
method: "simulateTransaction",
params: {
transaction: SIM_TX,
},
};
async function stellarTvl(api) {
const activeTvl = await callSoroban(STELLAR_CONTRACT, "get_active_tvl");

const { data: json } = await axios.post(RPC_URL, body, {
headers: { "Content-Type": "application/json" },
});
if (!activeTvl) throw new Error("Stellar TVL is zero");

api.addCGToken("usd-coin", Number(activeTvl) / 1e7);
return api.getBalances();
}

const xdr = json?.result?.results?.[0]?.xdr;
if (!xdr) throw new Error("TVL value not returned from Soroban simulation");
// ============================================================
// ZigChain — CosmWasm TVL Logger (active_tvl)
// Queries the on-chain Logger contract via LCD REST endpoint.
// The contract returns active liquidity in DeFa zigchian contracts denominated in USD
// with 6 decimal places.
// ============================================================

const ZIGCHAIN_TVL_CONTRACT = "zig19pp9rxhqktgpf2yqkwwx6yekmgvnu3hvj0c4e5rlpw88l0shh3qs9qcdyg";

async function zigchainTvl(api) {
const activeTvl = await queryContract({
contract: ZIGCHAIN_TVL_CONTRACT,
chain: "zigchain",
data: { active_tvl: {} },
});

// Decode u128 from last 16 bytes (matches your SIM_TX output)
const buffer = Buffer.from(xdr, "base64");
const value = BigInt("0x" + buffer.slice(-16).toString("hex"));
if (!activeTvl || activeTvl === "0") throw new Error("ZigChain TVL is zero");

return value;
api.addCGToken("usd-coin", Number(activeTvl) / 1e6);
return api.getBalances();
}

async function tvl(api) {
const activeTvl = await getActiveTvl();

if (activeTvl === 0n) throw new Error("TVL is zero");

const normalized = Number(activeTvl) / 1e7;
api.addCGToken("usd-coin", normalized);

return api.getBalances();
}

// ============================================================
// Export
// ============================================================

module.exports = {
timetravel: false,
misrepresentedTokens: true,
methodology:
"TVL represents the total active liquidity across invoice-backed pools as reported directly by the on-chain Logger contract via Soroban simulation (get_active_tvl).",
stellar: {
tvl,
},
"TVL is the total active liquidity across invoice-backed pools, reported by on-chain Logger contracts on Stellar (Soroban) and ZigChain (CosmWasm).",
stellar: { tvl: stellarTvl },
zigchain: { tvl: zigchainTvl },
};
Loading