ledger.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // ts/ledger.ts
  2. var apiKey = document.querySelector("#api-key");
  3. {
  4. const storedApiKey = localStorage.getItem("fio-api-key");
  5. if (storedApiKey)
  6. apiKey.value = storedApiKey;
  7. }
  8. document.querySelector("#fetch").addEventListener("click", async () => {
  9. const loader = document.querySelector("#loader");
  10. loader.style.display = "block";
  11. try {
  12. await renderLedger(apiKey.value);
  13. localStorage.setItem("fio-api-key", apiKey.value);
  14. } catch (e) {
  15. console.error(e);
  16. }
  17. loader.style.display = "none";
  18. });
  19. async function getPrices() {
  20. const allPrices = await fetchJSON("https://refined-prun.github.io/refined-prices/all.json");
  21. const priceMap = {};
  22. for (const price of allPrices)
  23. if (price.ExchangeCode === "IC1")
  24. priceMap[price.MaterialTicker] = price.VWAP30D;
  25. return priceMap;
  26. }
  27. var pricePromise = getPrices();
  28. var ledger = document.querySelector("textarea#ledger");
  29. async function renderLedger(apiKey2) {
  30. ledger.style.display = "none";
  31. ledger.value = `Time,Mat,Quantity,Actual Unit Price,Discounted Unit Price,Gateway,Debit,Credit,Contract ID
  32. `;
  33. const prices = await pricePromise;
  34. const contracts = await fetchJSON("https://rest.fnar.net/contract/allcontracts", { headers: { Authorization: apiKey2 } });
  35. contracts.sort((a, b) => a.DateEpochMs - b.DateEpochMs);
  36. for (const contract of contracts) {
  37. if (contract.PartnerCompanyCode === null || contract.PartnerName.endsWith(" Commodity Exchange"))
  38. continue;
  39. contract.Conditions.sort((a, b) => a.ConditionIndex - b.ConditionIndex);
  40. for (let i = 0;i < contract.Conditions.length; i++) {
  41. const condition = contract.Conditions[i];
  42. if (contract.Party !== condition.Party && condition.Type === "DELIVERY" || contract.Party === condition.Party && condition.Type === "COMEX_PURCHASE_PICKUP") {
  43. const time = new Date(contract.DateEpochMs).toISOString();
  44. const mat = condition.MaterialTicker;
  45. const quantity = condition.MaterialAmount;
  46. const totalPrice = contract.Conditions[i - 1].Amount;
  47. ledger.value += `${time},${mat},${quantity},${prices[mat]},${totalPrice / quantity},,${totalPrice},,${contract.ContractLocalId}
  48. `;
  49. } else
  50. continue;
  51. }
  52. }
  53. ledger.style.display = "block";
  54. }
  55. document.querySelector("#copy").addEventListener("click", () => {
  56. navigator.clipboard.writeText(ledger.value);
  57. });
  58. async function fetchJSON(url, options = {}) {
  59. const controller = new AbortController;
  60. const timeoutId = setTimeout(() => controller.abort(), 5000);
  61. const doc = await fetch(url, { ...options, signal: controller.signal }).then((r) => r.json());
  62. clearTimeout(timeoutId);
  63. return doc;
  64. }
  65. //# debugId=1ECCA7DD80B750F864756E2164756E21