-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
98 lines (87 loc) · 3.15 KB
/
Copy pathmain.ts
File metadata and controls
98 lines (87 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Deno entry: demo intersected valuation without requiring RPC unless env set.
*
* Usage:
* deno task intersect
* RPC_URL=... LEDGER=0x... deno run --allow-env --allow-net main.ts
*/
import { intersectValuations, relaxCoupledValuation } from "./src/intersect/engine.ts";
import { readLedgerValuation } from "./src/intersect/onchain.ts";
import { WAD } from "./src/intersect/math.ts";
import type { LedgerValuation } from "./src/intersect/types.ts";
import type { Address } from "viem";
import { parseArgs } from "jsr:@std/cli/parse-args";
function envAddr(name: string): Address | undefined {
const v = Deno.env.get(name);
if (!v?.startsWith("0x")) return undefined;
return v as Address;
}
async function main() {
const args = parseArgs(Deno.args, {
string: ["rpc", "ledger", "token"],
boolean: ["help"],
default: {},
});
if (args.help) {
console.log(`MuniCoins intersected valuation (Deno)
Local demo (no chain):
deno run --allow-env main.ts
On-chain snapshot:
RPC_URL=https://... LEDGER=0x... TOKEN_ID=1 deno run --allow-env --allow-net main.ts
`);
return;
}
const rpcUrl = args.rpc ?? Deno.env.get("RPC_URL");
const ledgerAddr = (args.ledger as Address | undefined) ?? envAddr("LEDGER");
const tokenId = BigInt(args.token ?? Deno.env.get("TOKEN_ID") ?? "1");
if (rpcUrl && ledgerAddr) {
const snap = await readLedgerValuation(
{ rpcUrl, ledgerAddress: ledgerAddr },
tokenId,
);
const ledgerVal = {
slotValueMinor: snap.slotValueMinor,
cumulativeFactorWad: snap.cumulativeFactorWad,
operationalFundMinor: snap.operationalFundMinor,
premiumReserveMinor: snap.premiumReserveMinor,
};
const anchor = {
mirrorValueMinor: snap.slotValueMinor,
legMultiplierWad: WAD,
};
const g = intersectValuations(ledgerVal, anchor, { kind: "geometric" });
const w = intersectValuations(ledgerVal, anchor, {
kind: "weighted",
ledgerWeightBps: 7000n,
});
console.log(JSON.stringify({ chain: { ledger: ledgerAddr, tokenId }, g, w }, (_, v) =>
typeof v === "bigint" ? v.toString() : v, 2));
return;
}
// Offline illustration: two legs feeding each other
const ledgerMinor = 1_247_400n; // $1.2474 in 1e6 units
const nftMirror = 1_200_000n; // slightly different anchor
const offlineLedger: LedgerValuation = {
slotValueMinor: ledgerMinor,
cumulativeFactorWad: 1_247_400_000_000_000_000n, // illustrative WAD-scale factor
operationalFundMinor: 350_000_000_000n,
premiumReserveMinor: 315_000_000_000n,
};
const anchor = { mirrorValueMinor: nftMirror, legMultiplierWad: WAD };
const blended = intersectValuations(offlineLedger, anchor, {
kind: "weighted",
ledgerWeightBps: 5000n,
});
const geo = intersectValuations(offlineLedger, anchor, { kind: "geometric" });
const relaxed = relaxCoupledValuation(ledgerMinor, nftMirror, 1n);
console.log(JSON.stringify({
note: "offline demo; set RPC_URL + LEDGER for live reads",
weighted: blended,
geometric: geo,
relaxedCoupling: relaxed,
}, (_, v) => typeof v === "bigint" ? v.toString() : v, 2));
}
main().catch((e) => {
console.error(e);
Deno.exit(1);
});