// ts/ledger.ts var apiKey = document.querySelector("#api-key"); { const storedApiKey = localStorage.getItem("fio-api-key"); if (storedApiKey) apiKey.value = storedApiKey; } document.querySelector("#fetch").addEventListener("click", async () => { const loader = document.querySelector("#loader"); loader.style.display = "block"; try { await renderLedger(apiKey.value); localStorage.setItem("fio-api-key", apiKey.value); } catch (e) { console.error(e); } loader.style.display = "none"; }); async function getPrices() { const allPrices = await fetchJSON("https://refined-prun.github.io/refined-prices/all.json"); const priceMap = {}; for (const price of allPrices) if (price.ExchangeCode === "IC1") priceMap[price.MaterialTicker] = price.VWAP30D; return priceMap; } var pricePromise = getPrices(); var ledger = document.querySelector("textarea#ledger"); async function renderLedger(apiKey2) { ledger.style.display = "none"; ledger.value = `Time,Mat,Quantity,Actual Unit Price,Discounted Unit Price,Gateway,Debit,Credit,Contract ID `; const prices = await pricePromise; const contracts = await fetchJSON("https://rest.fnar.net/contract/allcontracts", { headers: { Authorization: apiKey2 } }); contracts.sort((a, b) => a.DateEpochMs - b.DateEpochMs); for (const contract of contracts) { if (contract.PartnerCompanyCode === null || contract.PartnerName.endsWith(" Commodity Exchange")) continue; contract.Conditions.sort((a, b) => a.ConditionIndex - b.ConditionIndex); for (let i = 0;i < contract.Conditions.length; i++) { const condition = contract.Conditions[i]; if (contract.Party !== condition.Party && condition.Type === "DELIVERY" || contract.Party === condition.Party && condition.Type === "COMEX_PURCHASE_PICKUP") { const time = new Date(contract.DateEpochMs).toISOString(); const mat = condition.MaterialTicker; const quantity = condition.MaterialAmount; const totalPrice = contract.Conditions[i - 1].Amount; ledger.value += `${time},${mat},${quantity},${prices[mat]},${totalPrice / quantity},,${totalPrice},,${contract.ContractLocalId} `; } else continue; } } ledger.style.display = "block"; } document.querySelector("#copy").addEventListener("click", () => { navigator.clipboard.writeText(ledger.value); }); async function fetchJSON(url, options = {}) { const controller = new AbortController; const timeoutId = setTimeout(() => controller.abort(), 5000); const doc = await fetch(url, { ...options, signal: controller.signal }).then((r) => r.json()); clearTimeout(timeoutId); return doc; } //# debugId=1ECCA7DD80B750F864756E2164756E21