@priors/mcp 0.19.1 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/README.md +57 -6
- package/package.json +1 -1
- package/src/board-trade-core.cjs +234 -0
- package/src/line-options.mjs +21 -6
- package/src/offer-becomes.mjs +71 -0
- package/src/server.mjs +499 -1
- package/src/strategies.mjs +162 -0
- package/src/strategy-tools.mjs +107 -0
- package/src/trade-core.cjs +691 -0
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
/* Trade's core: everything Go mode's Trade tab knows (web/account-trade.js), with no DOM, so Node loads it too
|
|
2
|
+
(scripts/test-trade-core.mjs, and the executor's side: autopilot/strategy.mjs compiles the same graphs and compares).
|
|
3
|
+
It ships with the tab, behind its own switch (SITE_TRADE=on, and only with Automations: scripts/build-site.mjs).
|
|
4
|
+
|
|
5
|
+
A strategy is a graph an account draws for its agent, n8n's idea: nodes joined by links, each node a card on a canvas.
|
|
6
|
+
- Triggers start a path: on a timer, a price rises to a level, a price falls to a level, a loan of the agent comes due
|
|
7
|
+
soon, every pass of the executor (every 10 minutes).
|
|
8
|
+
- If checks one thing the executor reads on chain (a Chainlink price against a level, a token held, the USDG kept, a
|
|
9
|
+
loan of the agent open or due within some hours, the time of day in UTC) and follows Yes or No: both need a next node.
|
|
10
|
+
- Actions: borrow on the agent's line (through its line key, LineKey, paid to the account, $5 to $10: it opens a loan of
|
|
11
|
+
the agent that must be repaid), buy a stock token, sell some of it, repay the agent's loan, stop.
|
|
12
|
+
- Limits: the most per trade, the most the key spends in all, the USDG always kept, how long the key lasts, never past
|
|
13
|
+
the stage's caps (STAGE: autopilot/rules.mjs STAGE and automation-core.js STAGE, held equal by the test).
|
|
14
|
+
problems() refuses a graph that cannot run, each refusal one sentence that names the fix: a loop, a buy with no
|
|
15
|
+
funding, a borrow over the caps, a sell of a token the strategy never buys before it, an If with a branch missing, a
|
|
16
|
+
node left unconnected, more buys or sells than one key takes, a field's value. Every refusal line below carries its
|
|
17
|
+
code (// check:<code>), so the test can take each one out and see its own case fail.
|
|
18
|
+
|
|
19
|
+
The graph (kept in the browser, localStorage priors.trade.<account>, account-trade.js):
|
|
20
|
+
{ v: 1, id, name, seq, nodes: [{ id: "n1", type, values, x, y }], edges: [{ from, to, branch? }],
|
|
21
|
+
limits: { perTrade, total, keep, days }, template?, from?, applied? }
|
|
22
|
+
node types: schedule, price_up, price_down, loan_due, tick (triggers); if; borrow, buy, sell, repay, stop (actions).
|
|
23
|
+
An edge leaves a node's output and enters another's input. Triggers have no input; If has two outputs, its edges
|
|
24
|
+
carry branch "yes" or "no"; Stop has none; every other node has one output ("out", no branch on its edge). One edge
|
|
25
|
+
per output: linking an output again replaces its edge. Several edges may enter one input.
|
|
26
|
+
|
|
27
|
+
compile(graph) -> the strategy rules, version 1 (what Tasks 2 and 3's executor runs, autopilot/strategy.mjs; amounts
|
|
28
|
+
are decimal strings of dollars, prices decimal strings of dollars by Chainlink, tokens the stock tokens' addresses
|
|
29
|
+
as the graph holds them, agents decimal strings of their ids, hours and days integers):
|
|
30
|
+
{ "kind": "priors.strategy", "v": 1,
|
|
31
|
+
"caps": { "perTradeUsd": "5", "totalUsd": "20", "keepUsd": "5" }, "days": 14,
|
|
32
|
+
"rules": [ { "id": "<trigger node id>", "when": <when>, "do": [<step>, ...] }, ... ] } (by trigger id: n1, n2, ..)
|
|
33
|
+
<when>: { "on": "schedule", "everyHours": 168 } starts at the first pass, then every everyHours
|
|
34
|
+
{ "on": "price_up", "token": "0x…", "above": "450" } Chainlink's price >= above (fresh), at most hourly
|
|
35
|
+
{ "on": "price_down", "token": "0x…", "below": "380" } Chainlink's price <= below (fresh), at most hourly
|
|
36
|
+
{ "on": "loan_due", "agent": "7310", "withinHours": 24 } a loan of the agent the account took is due within
|
|
37
|
+
{ "on": "tick" } every pass
|
|
38
|
+
<step>: { "op": "if", "node": "n2", "test": <test>, "yes": [<step>…], "no": [<step>…] } (always a list's last)
|
|
39
|
+
{ "op": "borrow", "node": "n3", "agent": "7310", "usd": "5", "days": 7 } LineKey's spend to the account,
|
|
40
|
+
Repay on time's coverLast in the same operation
|
|
41
|
+
{ "op": "buy", "node": "n4", "token": "0x…", "usd": "5", "from": "loan" | "usdg" }
|
|
42
|
+
{ "op": "sell", "node": "n5", "token": "0x…", "pct": 100 }
|
|
43
|
+
{ "op": "repay", "node": "n6", "agent": "7310", "capUsd": "6" }
|
|
44
|
+
{ "op": "stop", "node": "n7" } (always a list's last)
|
|
45
|
+
<test>: { "is": "price_above", "token": "0x…", "price": "450" } Chainlink's price >= price
|
|
46
|
+
{ "is": "price_below", "token": "0x…", "price": "380" } Chainlink's price <= price
|
|
47
|
+
{ "is": "holds", "token": "0x…" } the account's balance of it > 0
|
|
48
|
+
{ "is": "usdg_at_least", "usd": "10" } the account's USDG >= usd
|
|
49
|
+
{ "is": "loan_open", "agent": "7310" } a loan of the agent the account took is open
|
|
50
|
+
{ "is": "loan_due_within", "agent": "7310", "hours": 24 } one of them is due within hours
|
|
51
|
+
{ "is": "time_between", "fromHour": 14, "toHour": 20, "weekdays": true } UTC, from included, to not
|
|
52
|
+
(from > to wraps midnight), Monday to Friday
|
|
53
|
+
Several links into one node compile to the same steps under each path (the node id says it is one node).
|
|
54
|
+
toAutomationRules(graph): for a graph with no If, no borrow and nothing today's executors don't run, what they run
|
|
55
|
+
unchanged: { rules: Trade by rules' rules ({ buys, sells, caps, days }, automation-core.js compile's form) or null,
|
|
56
|
+
plans: Repay on time's plans ([{ agent_id, cap_usd, use_savings, late }], desk-core's autopay_on) }; else null.
|
|
57
|
+
fromPublished(strategy) -> a draft graph (Copy into Go mode): a published record { id?, name?, strategy, graph? } or
|
|
58
|
+
the strategy rules alone; the rules are the source, the published graph only gives the cards their places.
|
|
59
|
+
|
|
60
|
+
window.PriorsTradeCore:
|
|
61
|
+
STAGE, BORROW, LIQUID, SCHEMA, VERSION, NODES, TRIGGERS, ACTIONS, TESTS, TEMPLATES, CODES
|
|
62
|
+
node(type, values) a node's words: { type, kind, kindLabel, title, help, example, note?, fields }
|
|
63
|
+
outputs(type), hasInput(type)
|
|
64
|
+
newGraph(name), addNode(g, type, at, ctx), connect(g, from, to, branch), canConnect(…), disconnect(g, from, branch),
|
|
65
|
+
removeNode(g, id), moveNode(g, id, x, y), setValue(g, id, key, value), next(g, id, port), insertAfter(g, from,
|
|
66
|
+
branch, type, ctx), layout(g), template(key), fromTemplate(key, ctx), tradable(ctx)
|
|
67
|
+
fieldError(type, key, value, ctx, values), problems(g, ctx): [{ code, node, field, text }]
|
|
68
|
+
compile(g), toAutomationRules(g), needsExecutor(g), fromPublished(x), summary(node, ctx), limitsSentence(L)
|
|
69
|
+
ctx: { assets: [{ symbol, name, token }], prices: { SYMBOL: dollars }, agents: [{ id, label, cap }], caps (the gas
|
|
70
|
+
gate's, when it names lower ones), repayCap }. */
|
|
71
|
+
(function (root) {
|
|
72
|
+
"use strict";
|
|
73
|
+
const fill = (s, v) => String(s).replace(/\{(\w+)\}/g, (m, k) => (v && v[k] != null ? String(v[k]) : m));
|
|
74
|
+
const _t = (s, v) => (root.PriorsI18n ? root.PriorsI18n.t(s, v) : fill(s, v));
|
|
75
|
+
const own = (o, k) => Object.prototype.hasOwnProperty.call(o || {}, k);
|
|
76
|
+
const clone = (x) => JSON.parse(JSON.stringify(x));
|
|
77
|
+
|
|
78
|
+
// the stage's caps (autopilot/rules.mjs STAGE, automation-core.js STAGE: the test holds the three equal)
|
|
79
|
+
const STAGE = Object.freeze({ perTradeUsd: 10, totalUsd: 50, days: 31, buys: 4, sells: 4, minTradeUsd: 1, minEveryHours: 1 });
|
|
80
|
+
// a borrow: the pool's smallest loan, and the most per trade
|
|
81
|
+
const BORROW = Object.freeze({ minUsd: 5, maxUsd: 10 });
|
|
82
|
+
// the stock tokens a strategy may trade (automation-core.js LIQUID, the Desk's own list)
|
|
83
|
+
const LIQUID = Object.freeze(["TSLA", "NVDA", "SPY", "QQQ", "AAPL", "MSFT", "GOOGL", "AMZN", "META", "PLTR"]);
|
|
84
|
+
// Repay on time's most per loan for now (automation-core.js REPAY_CAP), unless the gate names less
|
|
85
|
+
const REPAY_CAP = 25;
|
|
86
|
+
const SCHEMA = "priors.strategy", VERSION = 1, MAX_NODES = 40;
|
|
87
|
+
const LIMITS = Object.freeze({ perTrade: "5", total: "20", keep: "5", days: 14 });
|
|
88
|
+
|
|
89
|
+
// ---- amounts: dollars as a person types them, kept as a plain decimal string (automation-core.js's own rules) ----
|
|
90
|
+
function dec(v) {
|
|
91
|
+
if (typeof v === "number") v = Number.isFinite(v) ? String(v) : "";
|
|
92
|
+
if (typeof v !== "string") return null;
|
|
93
|
+
let s = v.replace(/[\s$ ]/g, "");
|
|
94
|
+
if (/^\d{1,3}(,\d{3})+(\.\d+)?$/.test(s)) s = s.replace(/,/g, "");
|
|
95
|
+
else if (/^\d+,\d+$/.test(s)) s = s.replace(",", ".");
|
|
96
|
+
if (!/^\d+(\.\d+)?$/.test(s)) return null;
|
|
97
|
+
let [w, f = ""] = s.split(".");
|
|
98
|
+
w = w.replace(/^0+(?=\d)/, ""); f = f.replace(/0+$/, "");
|
|
99
|
+
return f ? w + "." + f : w;
|
|
100
|
+
}
|
|
101
|
+
function units(d, places) {
|
|
102
|
+
if (d == null) return null;
|
|
103
|
+
const [w, f = ""] = d.split(".");
|
|
104
|
+
if (f.length > places) return null;
|
|
105
|
+
return BigInt(w) * 10n ** BigInt(places) + BigInt((f + "0".repeat(places)).slice(0, places) || "0");
|
|
106
|
+
}
|
|
107
|
+
const usdOk = (d) => d != null && /^\d{1,7}(\.\d{1,6})?$/.test(d);
|
|
108
|
+
const priceOk = (d) => d != null && /^\d{1,9}(\.\d{1,8})?$/.test(d);
|
|
109
|
+
const usd6 = (v) => { const d = dec(v == null ? "" : String(v)); return usdOk(d) ? units(d, 6) : null; };
|
|
110
|
+
function money(v) {
|
|
111
|
+
const d = dec(String(v)); if (d == null) return String(v);
|
|
112
|
+
const [w, f = ""] = d.split(".");
|
|
113
|
+
return "$" + Number(w).toLocaleString("en-US") + (f ? "." + (f.length === 1 ? f + "0" : f) : "");
|
|
114
|
+
}
|
|
115
|
+
const money6 = (u) => money((Number(u) / 1e6).toFixed(6));
|
|
116
|
+
const quoted = (v) => "“" + String(v == null ? "" : v).trim().slice(0, 24) + "”";
|
|
117
|
+
|
|
118
|
+
// ---- the catalog ----
|
|
119
|
+
const EVERY = [6, 12, 24, 72, 168];
|
|
120
|
+
const everyWords = (h) => (h > 24 && h % 24 === 0 ? _t("Every {n} days", { n: h / 24 }) : _t("Every {n} hours", { n: h }));
|
|
121
|
+
const PCT = [100, 50, 25];
|
|
122
|
+
const pctWords = (p) => _t("{pct}% of what it holds", { pct: p });
|
|
123
|
+
const TERMS = [7, 14, 30];
|
|
124
|
+
const KEY_DAYS = [7, 14, 31];
|
|
125
|
+
const daysWords = (n) => _t("{n} days", { n });
|
|
126
|
+
const DUE = [6, 24, 48];
|
|
127
|
+
const WITHIN = [6, 24, 48, 72];
|
|
128
|
+
const HOURS = Array.from({ length: 24 }, (_, h) => h);
|
|
129
|
+
const hourWords = (h) => _t("{h}:00 UTC", { h: String(h).padStart(2, "0") });
|
|
130
|
+
const TESTS = Object.freeze(["price_above", "price_below", "holds", "usdg_min", "loan_open", "loan_due", "time"]);
|
|
131
|
+
const TEST_WORDS = {
|
|
132
|
+
price_above: () => _t("A price is at or above a level"), price_below: () => _t("A price is at or below a level"),
|
|
133
|
+
holds: () => _t("Your account holds a token"), usdg_min: () => _t("Your account keeps at least some USDG"),
|
|
134
|
+
loan_open: () => _t("A loan of your agent is open"), loan_due: () => _t("A loan of your agent is due soon"),
|
|
135
|
+
time: () => _t("The time is in a window (UTC)"),
|
|
136
|
+
};
|
|
137
|
+
const FROM = { usdg: () => _t("Your account's USDG"), loan: () => _t("The USDG borrowed before it") };
|
|
138
|
+
|
|
139
|
+
const fToken = () => ({ key: "token", type: "token", label: _t("Which token"), help: _t("A stock token. Only the tokens with the most trading on Uniswap are offered."), example: _t("Example: TSLA, Tesla's stock token.") });
|
|
140
|
+
const fPrice = (up) => ({ key: "price", type: "price", label: _t("Level, in dollars"), help: _t("For one token, by Chainlink's price."), example: up ? _t("Example: 450, a little over today's price.") : _t("Example: 380, a little under today's price.") });
|
|
141
|
+
const fAgent = (help) => ({ key: "agent", type: "agent", label: _t("Agent"), help, example: _t("Example: your agent research-desk #7310.") });
|
|
142
|
+
const sel = (key, label, help, example, options) => ({ key, type: "select", label, help, example, options });
|
|
143
|
+
const NODES = {
|
|
144
|
+
schedule: { kind: "trigger", title: () => _t("On a timer"), help: () => _t("Starts its path again and again: first at the executor's next pass after you turn it on (it passes every 10 minutes), then once each period."), example: () => _t("Example: once a week."),
|
|
145
|
+
fields: () => [sel("every", _t("How often it starts"), _t("The time between two starts."), _t("Example: every 7 days."), EVERY.map((h) => [h, everyWords(h)]))] },
|
|
146
|
+
price_up: { kind: "trigger", title: () => _t("Price rises to"), help: () => _t("Starts its path when a stock token's price is at or above a level you choose, by Chainlink's price, which is fresh only while the markets are open. At most once an hour."), example: () => _t("Example: TSLA at $450 or more."),
|
|
147
|
+
fields: () => [fToken(), fPrice(true)] },
|
|
148
|
+
price_down: { kind: "trigger", title: () => _t("Price falls to"), help: () => _t("Starts its path when a stock token's price is at or below a level you choose, by Chainlink's price, which is fresh only while the markets are open. At most once an hour."), example: () => _t("Example: TSLA at $380 or less."),
|
|
149
|
+
fields: () => [fToken(), fPrice(false)] },
|
|
150
|
+
loan_due: { kind: "trigger", title: () => _t("Loan due soon"), help: () => _t("Starts its path when a loan your account took for your agent is due within the hours you choose."), example: () => _t("Example: 24 hours before the due date."),
|
|
151
|
+
fields: () => [fAgent(_t("The agent whose loans it watches: the loans your account takes for it.")), sel("hours", _t("How long before it is due"), _t("With 6 hours it is what Repay on time does today, so it can run now."), _t("Example: 24 hours before it is due."), DUE.map((h) => [h, _t("{n} hours before it is due", { n: h })]))] },
|
|
152
|
+
tick: { kind: "trigger", title: () => _t("Every pass"), help: () => _t("Starts its path at each of the executor's passes, every 10 minutes. Put an If after it, so it acts only when that holds."), example: () => _t("Example: check a price at every pass."),
|
|
153
|
+
fields: () => [] },
|
|
154
|
+
if: { kind: "if", title: () => _t("If"), help: () => _t("Checks one thing the executor reads on chain, then follows Yes or No. Both need a next node: use Stop to end one."), example: () => _t("Example: if TSLA is at $400 or more."),
|
|
155
|
+
fields: (v) => {
|
|
156
|
+
const test = sel("test", _t("What it checks"), _t("Read on chain at each pass, nothing else."), _t("Example: a price is at or above a level."), TESTS.map((t) => [t, TEST_WORDS[t]()]));
|
|
157
|
+
const t = v && TESTS.includes(v.test) ? v.test : "price_above";
|
|
158
|
+
const more = {
|
|
159
|
+
price_above: () => [fToken(), fPrice(true)], price_below: () => [fToken(), fPrice(false)], holds: () => [fToken()],
|
|
160
|
+
usdg_min: () => [{ key: "usd", type: "usd", label: _t("USDG at least"), help: _t("In dollars: the USDG your account holds when it checks."), example: _t("Example: 10.") }],
|
|
161
|
+
loan_open: () => [fAgent(_t("The agent whose loans it reads: the loans your account takes for it."))],
|
|
162
|
+
loan_due: () => [fAgent(_t("The agent whose loans it reads: the loans your account takes for it.")), sel("hours", _t("Due within"), _t("Counted from the moment it checks."), _t("Example: within 24 hours."), WITHIN.map((h) => [h, _t("Within {n} hours", { n: h })]))],
|
|
163
|
+
time: () => [sel("from", _t("From (UTC)"), _t("The executor's clock, in UTC."), _t("Example: 14:00 UTC."), HOURS.map((h) => [h, hourWords(h)])), sel("to", _t("Until (UTC)"), _t("Up to this hour, not included. Before the start, it runs past midnight."), _t("Example: 20:00 UTC."), HOURS.map((h) => [h, hourWords(h)])),
|
|
164
|
+
{ key: "weekdays", type: "check", label: _t("Weekdays only"), help: _t("Monday to Friday, the days the stock markets open."), example: _t("Example: on.") }],
|
|
165
|
+
}[t]();
|
|
166
|
+
return [test, ...more];
|
|
167
|
+
} },
|
|
168
|
+
borrow: { kind: "action", title: () => _t("Borrow on your agent's line"), help: () => _t("Opens a loan of your agent, through its line key (LineKey), paid to your account. It is a loan that must be repaid: Repay on time marks it in the same operation, and it counts in the key's limits."), example: () => _t("Example: borrow $5 for 7 days."),
|
|
169
|
+
note: () => _t("Opens a loan of your agent that must be repaid."),
|
|
170
|
+
fields: () => [fAgent(_t("The agent whose line it borrows on. Its line key must be set up for your account.")), { key: "usd", type: "usd", label: _t("Amount to borrow"), help: _t("In dollars of USDG: at least $5, the pool's smallest loan, and at most $10, the most per trade."), example: _t("Example: $5.") },
|
|
171
|
+
sel("days", _t("Loan term"), _t("When the loan is due. A loan repaid late counts against your agent's record."), _t("Example: 7 days."), TERMS.map((d) => [d, daysWords(d)]))] },
|
|
172
|
+
buy: { kind: "action", title: () => _t("Buy a token"), help: () => _t("Buys a stock token on Uniswap, paid to your account. It pays with your account's USDG, or with the USDG a Borrow before it opened."), example: () => _t("Example: buy $5 of TSLA with the loan."),
|
|
173
|
+
fields: () => [fToken(), { key: "usd", type: "usd", label: _t("Amount to spend"), help: _t("In dollars of USDG: $1 or more, and no more than the most per trade in Limits."), example: _t("Example: $5.") },
|
|
174
|
+
sel("from", _t("Pays with"), _t("With the borrowed USDG, a Borrow must come before this buy on its path."), _t("Example: the USDG borrowed before it."), Object.keys(FROM).map((k) => [k, FROM[k]()]))] },
|
|
175
|
+
sell: { kind: "action", title: () => _t("Sell a share of a token"), help: () => _t("Sells some of a stock token your account holds, for USDG paid to your account. This strategy must buy that token before it."), example: () => _t("Example: sell all the TSLA it holds."),
|
|
176
|
+
fields: () => [fToken(), sel("pct", _t("Share to sell"), _t("A share of what your account holds of it when this runs."), _t("Example: 100% of what it holds."), PCT.map((p) => [p, pctWords(p)]))] },
|
|
177
|
+
repay: { kind: "action", title: () => _t("Repay your agent's loan"), help: () => _t("Your account repays the open loan of your agent from its USDG: what was borrowed and its fee."), example: () => _t("Example: up to $6 for a $5 loan."),
|
|
178
|
+
fields: () => [fAgent(_t("The agent whose loan it repays.")), { key: "cap", type: "usd", label: _t("Most it repays for one loan"), help: _t("What was borrowed and its fee: at least the amount its Borrow opens."), example: _t("Example: $6, for a $5 loan and its fee.") }] },
|
|
179
|
+
stop: { kind: "action", title: () => _t("Stop here"), help: () => _t("Ends this path: nothing more happens on it until its trigger starts it again."), example: () => _t("Example: on an If's No, do nothing."),
|
|
180
|
+
fields: () => [] },
|
|
181
|
+
};
|
|
182
|
+
const LIMIT_FIELDS = () => [
|
|
183
|
+
{ key: "perTrade", type: "usd", label: _t("Most a trade spends"), help: _t("No buy, sell or borrow spends more than this. At most $10 for now."), example: _t("Example: $5.") },
|
|
184
|
+
{ key: "total", type: "usd", label: _t("Most the key spends"), help: _t("Everything the key may spend, borrowed USDG included. At most $50 for now."), example: _t("Example: $20.") },
|
|
185
|
+
{ key: "keep", type: "usd", label: _t("USDG always kept"), help: _t("Buys stop before your account's USDG falls under this, so loans can still be repaid."), example: _t("Example: $5.") },
|
|
186
|
+
sel("days", _t("How long the key lasts"), _t("After this the key stops by itself."), _t("Example: for 14 days."), KEY_DAYS.map((d) => [d, daysWords(d)])),
|
|
187
|
+
];
|
|
188
|
+
const TRIGGERS = Object.freeze(["schedule", "price_up", "price_down", "loan_due", "tick"]);
|
|
189
|
+
const ACTIONS = Object.freeze(["borrow", "buy", "sell", "repay", "stop"]);
|
|
190
|
+
const KIND = { trigger: () => _t("Trigger"), if: () => _t("If"), action: () => _t("Action") };
|
|
191
|
+
const kindOf = (type) => (own(NODES, type) ? NODES[type].kind : null);
|
|
192
|
+
/** A node's words in the reader's language, its fields as they are for `values` (an If's depend on what it checks). */
|
|
193
|
+
function node(type, values) {
|
|
194
|
+
const b = NODES[type]; if (!b) throw new Error("no such node: " + type);
|
|
195
|
+
const out = { type, kind: b.kind, kindLabel: KIND[b.kind](), title: b.title(), help: b.help(), example: b.example(), fields: b.fields(values || {}) };
|
|
196
|
+
if (b.note) out.note = b.note();
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
const outputs = (type) => (kindOf(type) === "if" ? ["yes", "no"] : type === "stop" || !own(NODES, type) ? [] : ["out"]);
|
|
200
|
+
const hasInput = (type) => own(NODES, type) && kindOf(type) !== "trigger";
|
|
201
|
+
|
|
202
|
+
// ---- the graph ----
|
|
203
|
+
let gseq = 0;
|
|
204
|
+
const newId = () => "s" + Date.now().toString(36) + (++gseq).toString(36) + Math.floor(Math.random() * 1e6).toString(36);
|
|
205
|
+
const newGraph = (name) => ({ v: 1, id: newId(), name: String(name || ""), seq: 0, nodes: [], edges: [], limits: { ...LIMITS } });
|
|
206
|
+
const byId = (g, id) => g.nodes.find((n) => n.id === id) || null;
|
|
207
|
+
const nid = (id) => { const m = /^n(\d+)$/.exec(id); return m ? Number(m[1]) : Infinity; };
|
|
208
|
+
const order = (a, b) => nid(a.id) - nid(b.id) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
209
|
+
const triggers = (g) => g.nodes.filter((n) => kindOf(n.type) === "trigger").sort(order);
|
|
210
|
+
const portOf = (e) => e.branch || "out";
|
|
211
|
+
const next = (g, id, port = "out") => { const e = g.edges.find((x) => x.from === id && portOf(x) === port); return e ? e.to : null; };
|
|
212
|
+
const tradable = (ctx) => ((ctx && ctx.assets) || []).filter((a) => LIQUID.includes(a.symbol));
|
|
213
|
+
const tokenOf = (ctx, sym) => { const a = tradable(ctx).find((x) => x.symbol === sym); return a ? a.token : ""; };
|
|
214
|
+
const symOf = (ctx, token) => { const a = ((ctx && ctx.assets) || []).find((x) => String(x.token).toLowerCase() === String(token || "").toLowerCase()); return a ? a.symbol : null; };
|
|
215
|
+
const same = (a, b) => !!a && String(a).toLowerCase() === String(b || "").toLowerCase();
|
|
216
|
+
function near(ctx, token, pct) {
|
|
217
|
+
const s = symOf(ctx, token), p = s && ctx && ctx.prices ? Number(ctx.prices[s]) : NaN;
|
|
218
|
+
if (!(p > 0)) return "";
|
|
219
|
+
const cents = Math.round(p * 100) * (100 + pct), out = pct > 0 ? Math.ceil(cents / 100) : Math.floor(cents / 100);
|
|
220
|
+
return dec((out / 100).toFixed(2));
|
|
221
|
+
}
|
|
222
|
+
/** A new node's values: the token the graph last used (else TSLA), a level 10% from the price now, the first agent. */
|
|
223
|
+
function defaults(g, type, ctx) {
|
|
224
|
+
const last = g.nodes.slice().reverse().find((n) => n.values && n.values.token);
|
|
225
|
+
const token = last ? last.values.token : tokenOf(ctx, "TSLA");
|
|
226
|
+
const ag = ((ctx && ctx.agents) || [])[0], agent = ag ? String(ag.id) : "";
|
|
227
|
+
switch (type) {
|
|
228
|
+
case "schedule": return { every: 168 };
|
|
229
|
+
case "price_up": return { token, price: near(ctx, token, 10) };
|
|
230
|
+
case "price_down": return { token, price: near(ctx, token, -10) };
|
|
231
|
+
case "loan_due": return { agent, hours: 24 };
|
|
232
|
+
case "if": return { test: "price_above", token, price: near(ctx, token, 5), usd: "10", agent, hours: 24, from: 14, to: 20, weekdays: true };
|
|
233
|
+
case "borrow": return { agent, usd: "5", days: 7 };
|
|
234
|
+
case "buy": return { token, usd: "3", from: "usdg" };
|
|
235
|
+
case "sell": return { token, pct: 100 };
|
|
236
|
+
case "repay": return { agent, cap: ag && ag.cap ? dec(String(ag.cap)) || "6" : "6" };
|
|
237
|
+
default: return {};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function addNode(g, type, at, ctx) {
|
|
241
|
+
if (!own(NODES, type)) throw new Error("no such node: " + type);
|
|
242
|
+
if (g.nodes.length >= MAX_NODES) throw new Error(_t("A strategy holds at most {n} nodes.", { n: MAX_NODES }));
|
|
243
|
+
let n = (g.seq || 0) + 1;
|
|
244
|
+
while (byId(g, "n" + n)) n++;
|
|
245
|
+
g.seq = n;
|
|
246
|
+
const x = { id: "n" + n, type, values: defaults(g, type, ctx), x: Math.max(0, Math.round((at && at.x) || 0)), y: Math.max(0, Math.round((at && at.y) || 0)) };
|
|
247
|
+
g.nodes.push(x);
|
|
248
|
+
return x;
|
|
249
|
+
}
|
|
250
|
+
/** null, or why `from`'s output `branch` can't link to `to`. */
|
|
251
|
+
function canConnect(g, from, to, branch) {
|
|
252
|
+
const a = byId(g, from), b = byId(g, to);
|
|
253
|
+
if (!a || !b) return _t("That node isn't there any more.");
|
|
254
|
+
if (from === to) return _t("A node can't link to itself.");
|
|
255
|
+
const port = branch || "out";
|
|
256
|
+
if (!outputs(a.type).includes(port)) return a.type === "stop" ? _t("Stop ends its path: it has no output.") : _t("Pick Yes or No: an If has two outputs.");
|
|
257
|
+
if (!hasInput(b.type)) return _t("A trigger starts a path: nothing links into it.");
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
/** Links `from`'s output (an If's: branch "yes" or "no") to `to`, in place of the link that output had. */
|
|
261
|
+
function connect(g, from, to, branch) {
|
|
262
|
+
const why = canConnect(g, from, to, branch); if (why) return why;
|
|
263
|
+
const port = branch || "out";
|
|
264
|
+
g.edges = g.edges.filter((e) => !(e.from === from && portOf(e) === port));
|
|
265
|
+
g.edges.push(kindOf(byId(g, from).type) === "if" ? { from, to, branch: port } : { from, to });
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
function disconnect(g, from, branch) { const port = branch || "out"; g.edges = g.edges.filter((e) => !(e.from === from && portOf(e) === port)); return g; }
|
|
269
|
+
function removeNode(g, id) { g.nodes = g.nodes.filter((n) => n.id !== id); g.edges = g.edges.filter((e) => e.from !== id && e.to !== id); return g; }
|
|
270
|
+
function moveNode(g, id, x, y) { const n = byId(g, id); if (n) { n.x = Math.max(0, Math.round(x)); n.y = Math.max(0, Math.round(y)); } return g; }
|
|
271
|
+
function setValue(g, id, key, value) { const n = byId(g, id); if (n) n.values[key] = value; return g; }
|
|
272
|
+
// every node `id` leads to (not itself, unless a loop comes back to it)
|
|
273
|
+
function reach(g, id) {
|
|
274
|
+
const seen = new Set(), todo = [id];
|
|
275
|
+
while (todo.length) { const x = todo.pop(); for (const e of g.edges) if (e.from === x && !seen.has(e.to)) { seen.add(e.to); todo.push(e.to); } }
|
|
276
|
+
return seen;
|
|
277
|
+
}
|
|
278
|
+
/** What "add after" offers on a phone: a trigger to start (from = null), else an If or an action. */
|
|
279
|
+
const insertable = (g, from) => (from == null ? TRIGGERS.slice() : ["if", ...ACTIONS]);
|
|
280
|
+
/** Adds a `type` node after `from`'s output (null: a new trigger), between it and what that output led to. */
|
|
281
|
+
function insertAfter(g, from, branch, type, ctx) {
|
|
282
|
+
const a = from == null ? null : byId(g, from);
|
|
283
|
+
if (from != null && !a) throw new Error("no such node: " + from);
|
|
284
|
+
if (!insertable(g, from).includes(type)) throw new Error(`a ${type} node can't go there`);
|
|
285
|
+
const port = branch || "out", was = a ? next(g, from, port) : null;
|
|
286
|
+
const at = a ? { x: a.x + 260, y: a.y } : { x: 40, y: g.nodes.length ? Math.max(...g.nodes.map((n) => n.y)) + 160 : 40 };
|
|
287
|
+
if (was) for (const id of new Set([was, ...reach(g, was)])) { const m = byId(g, id); if (m && id !== from) m.x += 260; }
|
|
288
|
+
const n = addNode(g, type, at, ctx);
|
|
289
|
+
if (a) connect(g, from, n.id, kindOf(a.type) === "if" ? port : undefined);
|
|
290
|
+
if (was && outputs(type).length) connect(g, n.id, was, kindOf(type) === "if" ? "yes" : undefined);
|
|
291
|
+
return n;
|
|
292
|
+
}
|
|
293
|
+
/** Places the cards in columns: each trigger's paths left to right, an If's Yes above its No. */
|
|
294
|
+
function layout(g) {
|
|
295
|
+
const COL = 260, ROW = 128, X0 = 40, Y0 = 40, placed = new Set();
|
|
296
|
+
let row = 0;
|
|
297
|
+
const place = (id, depth) => {
|
|
298
|
+
const n = byId(g, id); if (!n || placed.has(id)) return; placed.add(id);
|
|
299
|
+
n.x = X0 + depth * COL; n.y = Y0 + row * ROW;
|
|
300
|
+
const kids = outputs(n.type).map((p) => next(g, id, p)).filter((k) => k && !placed.has(k));
|
|
301
|
+
if (!kids.length) { row++; return; }
|
|
302
|
+
kids.forEach((k) => place(k, depth + 1));
|
|
303
|
+
};
|
|
304
|
+
for (const t of triggers(g)) { place(t.id, 0); row += 0.25; }
|
|
305
|
+
for (const n of g.nodes.slice().sort(order)) if (!placed.has(n.id)) place(n.id, 0);
|
|
306
|
+
for (const n of g.nodes) n.y = Math.round(n.y);
|
|
307
|
+
return g;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ---- the templates (the Buildout's three) ----
|
|
311
|
+
const TEMPLATES = Object.freeze([
|
|
312
|
+
{ key: "buy_schedule", name: () => _t("Buy on a schedule"), line: () => _t("Buys $3 of a stock token every week with your account's USDG.") },
|
|
313
|
+
{ key: "borrow_dip", name: () => _t("Borrow to buy when the price falls, sell when it rises, repay"), line: () => _t("When the price falls, borrows $5 on your agent's line and buys with it; when it rises, sells and repays the loan; a loan due within a day is repaid first. A borrow is a loan of your agent that must be repaid.") },
|
|
314
|
+
{ key: "repay_due", name: () => _t("Repay before the due date"), line: () => _t("Repays your agent's loans from your account's USDG in the 6 hours before each is due.") },
|
|
315
|
+
]);
|
|
316
|
+
const template = (key) => { const t = TEMPLATES.find((x) => x.key === key); if (!t) throw new Error("no such template: " + key); return { key, name: t.name(), line: t.line() }; };
|
|
317
|
+
function fromTemplate(key, ctx) {
|
|
318
|
+
const T = template(key), g = newGraph(T.name); g.template = key;
|
|
319
|
+
const add = (type, values) => { const n = addNode(g, type, {}, ctx); Object.assign(n.values, values || {}); return n.id; };
|
|
320
|
+
const link = (a, b, branch) => { const why = connect(g, a, b, branch); if (why) throw new Error(why); };
|
|
321
|
+
const tsla = tokenOf(ctx, "TSLA");
|
|
322
|
+
if (key === "buy_schedule") link(add("schedule", { every: 168 }), add("buy", { token: tsla, usd: "3", from: "usdg" }));
|
|
323
|
+
else if (key === "repay_due") link(add("loan_due", { hours: 6 }), add("repay", {}));
|
|
324
|
+
else {
|
|
325
|
+
const down = add("price_down", { token: tsla, price: near(ctx, tsla, -10) }), open = add("if", { test: "loan_open" });
|
|
326
|
+
const borrow = add("borrow", { usd: "5", days: 7 }), buy = add("buy", { token: tsla, usd: "5", from: "loan" }), wait = add("stop");
|
|
327
|
+
link(down, open); link(open, wait, "yes"); link(open, borrow, "no"); link(borrow, buy);
|
|
328
|
+
const up = add("price_up", { token: tsla, price: near(ctx, tsla, 10) }), held = add("if", { test: "holds", token: tsla });
|
|
329
|
+
const sell = add("sell", { token: tsla, pct: 100 }), repay = add("repay", { cap: "6" }), none = add("stop");
|
|
330
|
+
link(up, held); link(held, sell, "yes"); link(held, none, "no"); link(sell, repay);
|
|
331
|
+
const due = add("loan_due", { hours: 24 }), pay = add("repay", { cap: "6" });
|
|
332
|
+
link(due, pay);
|
|
333
|
+
}
|
|
334
|
+
return layout(g);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ---- refusals: one sentence that names the fix ----
|
|
338
|
+
const capsOf = (ctx) => {
|
|
339
|
+
const c = (ctx && ctx.caps) || {};
|
|
340
|
+
const lower = (a, b) => (b != null && Number(b) > 0 && Number(b) < a ? Number(b) : a);
|
|
341
|
+
return { perTrade: lower(STAGE.perTradeUsd, c.perTradeUsd), total: lower(STAGE.totalUsd, c.totalUsd), days: lower(STAGE.days, c.days) };
|
|
342
|
+
};
|
|
343
|
+
const limitsOf = (g) => ({ ...LIMITS, ...((g && g.limits) || {}) });
|
|
344
|
+
const titleOf = (n) => (n && own(NODES, n.type) ? NODES[n.type].title() : "");
|
|
345
|
+
const choice = (list, v) => list.some((x) => x === Number(v));
|
|
346
|
+
/** null, or the sentence that refuses `value` for field `key` of a `type` node (`values`: the node's others; `ctx.limits`:
|
|
347
|
+
* the strategy's limits). */
|
|
348
|
+
function fieldError(type, key, value, ctx, values) {
|
|
349
|
+
const caps = capsOf(ctx), L = { ...LIMITS, ...((ctx && ctx.limits) || {}) };
|
|
350
|
+
const str = value == null ? "" : String(value).trim(), d = dec(str);
|
|
351
|
+
if (type === "limits") {
|
|
352
|
+
if (key === "perTrade") {
|
|
353
|
+
if (!str) return _t("Type the most a trade spends, like 5.");
|
|
354
|
+
if (!usdOk(d)) return _t("{value} isn't an amount in dollars: type one like {like}.", { value: quoted(value), like: "5" });
|
|
355
|
+
if (units(d, 6) < 1_000_000n) return _t("{value} is under $1: a trade spends at least $1.", { value: money(d) });
|
|
356
|
+
if (units(d, 6) > BigInt(caps.perTrade) * 1_000_000n) return _t("{value} is over {max}, the most a trade spends for now: type {max} or less.", { value: money(d), max: money(caps.perTrade) });
|
|
357
|
+
return null;
|
|
358
|
+
}
|
|
359
|
+
if (key === "total") {
|
|
360
|
+
if (!str) return _t("Type the most the key spends in all, like 20.");
|
|
361
|
+
if (!usdOk(d)) return _t("{value} isn't an amount in dollars: type one like {like}.", { value: quoted(value), like: "20" });
|
|
362
|
+
const per = usd6(L.perTrade);
|
|
363
|
+
if (per != null && units(d, 6) < per) return _t("{value} is less than the most per trade: the key spends at least one trade.", { value: money(d) });
|
|
364
|
+
if (units(d, 6) > BigInt(caps.total) * 1_000_000n) return _t("{value} is more than {max}, the most a key spends for now: type {max} or less.", { value: money(d), max: money(caps.total) });
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
if (key === "keep") return !str ? _t("Type the USDG to keep at all times, like 5 (0 keeps none).") : usdOk(d) ? null : _t("{value} isn't an amount in dollars: type one like {like}.", { value: quoted(value), like: "5" });
|
|
368
|
+
if (key === "days") return KEY_DAYS.some((x) => x === Number(value) && x <= caps.days) ? null : _t("Pick the key's length from the list.");
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
if (key === "token") return str && tradable(ctx).some((a) => same(a.token, str)) ? null : _t("Pick a token from the list.");
|
|
372
|
+
if (key === "agent") {
|
|
373
|
+
const agents = (ctx && ctx.agents) || [];
|
|
374
|
+
if (!agents.length) return _t("Your account has no agent yet: create one in Agents, then come back.");
|
|
375
|
+
return agents.some((a) => String(a.id) === str) ? null : _t("Pick an agent of yours from the list.");
|
|
376
|
+
}
|
|
377
|
+
if (key === "price") {
|
|
378
|
+
if (!str) return _t("Type the level in dollars, like 450.");
|
|
379
|
+
if (!priceOk(d)) return _t("{value} isn't a level: type dollars for one token, like 450 or 412.50.", { value: quoted(value) });
|
|
380
|
+
return units(d, 8) === 0n ? _t("$0 is not a level a price reaches: type the price for one token, like 450.") : null;
|
|
381
|
+
}
|
|
382
|
+
if (key === "usd") {
|
|
383
|
+
if (!str) return _t("Type an amount in dollars, like 5.");
|
|
384
|
+
if (!usdOk(d)) return _t("{value} isn't an amount in dollars: type one like {like}.", { value: quoted(value), like: "5" });
|
|
385
|
+
if (type === "buy") {
|
|
386
|
+
if (units(d, 6) < BigInt(STAGE.minTradeUsd) * 1_000_000n) return _t("{value} is under $1, the smallest buy: type $1 or more.", { value: money(d) });
|
|
387
|
+
const per = usd6(L.perTrade) != null ? dec(String(L.perTrade)) : String(caps.perTrade);
|
|
388
|
+
if (units(d, 6) > units(per, 6)) return _t("{value} is over {max}, this strategy's most per trade: type {max} or less, or raise it in Limits.", { value: money(d), max: money(per) });
|
|
389
|
+
}
|
|
390
|
+
if (type === "borrow") {
|
|
391
|
+
const per = usd6(L.perTrade) != null ? dec(String(L.perTrade)) : String(caps.perTrade), max = units(per, 6) < BigInt(BORROW.maxUsd) * 1_000_000n ? per : String(BORROW.maxUsd);
|
|
392
|
+
if (units(d, 6) < BigInt(BORROW.minUsd) * 1_000_000n || units(d, 6) > units(max, 6)) return _t("{value} is outside what a Borrow takes: from $5, the pool's smallest loan, to {max}, the most per trade.", { value: money(d), max: money(max) });
|
|
393
|
+
}
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
if (key === "cap") {
|
|
397
|
+
if (!str) return _t("Type the most it repays on one loan, like 6.");
|
|
398
|
+
if (!usdOk(d)) return _t("{value} isn't an amount in dollars: type one like {like}.", { value: quoted(value), like: "6" });
|
|
399
|
+
if (units(d, 6) === 0n) return _t("$0 repays nothing: type the most it repays on one loan, like 6.");
|
|
400
|
+
const max = ctx && Number(ctx.repayCap) > 0 ? Number(ctx.repayCap) : REPAY_CAP;
|
|
401
|
+
return units(d, 6) > units(dec(String(max)), 6) ? _t("{value} is more than {max}, the most repaid for one loan for now: type {max} or less.", { value: money(d), max: money(max) }) : null;
|
|
402
|
+
}
|
|
403
|
+
if (key === "every") return choice(EVERY, value) ? null : _t("Pick how often it starts from the list.");
|
|
404
|
+
if (key === "pct") return choice(PCT, value) ? null : _t("Pick the share to sell from the list.");
|
|
405
|
+
if (key === "days") return choice(TERMS, value) ? null : _t("Pick the loan's term from the list.");
|
|
406
|
+
if (key === "hours") return choice(type === "loan_due" ? DUE : WITHIN, value) ? null : _t("Pick the hours from the list.");
|
|
407
|
+
if (key === "from" && type === "if") return choice(HOURS, value) ? null : _t("Pick an hour from the list.");
|
|
408
|
+
if (key === "to") {
|
|
409
|
+
if (!choice(HOURS, value)) return _t("Pick an hour from the list.");
|
|
410
|
+
return values && Number(values.from) === Number(value) ? _t("The window starts and ends at the same hour: pick another end.") : null;
|
|
411
|
+
}
|
|
412
|
+
if (key === "from") return own(FROM, str) ? null : _t("Pick what it pays with from the list.");
|
|
413
|
+
if (key === "test") return TESTS.includes(str) ? null : _t("Pick what it checks from the list.");
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const CODES = Object.freeze(["empty", "no_trigger", "bad_edge", "unconnected", "cycle", "if_branch", "field", "limits", "borrow_caps", "no_funding", "sell_unbought", "too_many_buys", "too_many_sells", "repay_cap", "borrow_buy", "borrow_agents"]);
|
|
418
|
+
/** What keeps a graph from running, in order: each { code, node (an id, "limits", or null), field (or null), text }. */
|
|
419
|
+
function problems(g, ctx) {
|
|
420
|
+
const out = [], push = (code, node, field, text) => out.push({ code, node, field, text });
|
|
421
|
+
const nodes = g.nodes.slice().sort(order), ids = new Set(nodes.map((n) => n.id));
|
|
422
|
+
const cx = { ...(ctx || {}), limits: limitsOf(g) };
|
|
423
|
+
if (!nodes.length) { push("empty", null, null, _t("Add a trigger to start: a timer, a price, a loan due soon or every pass.")); return out; } // check:empty
|
|
424
|
+
if (!nodes.some((n) => kindOf(n.type) === "trigger")) push("no_trigger", null, null, _t("Nothing starts this strategy: add a trigger.")); // check:no_trigger
|
|
425
|
+
for (const e of g.edges) {
|
|
426
|
+
const a = byId(g, e.from), b = byId(g, e.to);
|
|
427
|
+
if (!a || !b || !outputs(a.type).includes(portOf(e)) || !hasInput(b.type) || (kindOf(a.type) === "if") !== !!e.branch) push("bad_edge", a ? a.id : null, null, _t("A link goes to or from a node that can't take it: remove it.")); // check:bad_edge
|
|
428
|
+
}
|
|
429
|
+
for (const n of nodes) {
|
|
430
|
+
if (!own(NODES, n.type)) { push("bad_edge", n.id, null, _t("This node isn't one this page knows: remove it.")); continue; } // check:bad_edge
|
|
431
|
+
const ins = g.edges.filter((e) => e.to === n.id && ids.has(e.from)), outs = g.edges.filter((e) => e.from === n.id && ids.has(e.to));
|
|
432
|
+
if (!ins.length && !outs.length) push("unconnected", n.id, null, _t("{title} is not connected: link it into a path, or remove it.", { title: titleOf(n) })); // check:unconnected
|
|
433
|
+
else if (kindOf(n.type) === "trigger" && !outs.length) push("unconnected", n.id, null, _t("{title} starts nothing: link its output to the next node.", { title: titleOf(n) })); // check:unconnected
|
|
434
|
+
else if (kindOf(n.type) !== "trigger" && !ins.length) push("unconnected", n.id, null, _t("Nothing leads to {title}: link another node's output to it.", { title: titleOf(n) })); // check:unconnected
|
|
435
|
+
}
|
|
436
|
+
// loops: a path that comes back to a node it went through
|
|
437
|
+
const state = new Map(), seenLoops = new Set();
|
|
438
|
+
const visit = (id, path) => {
|
|
439
|
+
state.set(id, 1); path.push(id);
|
|
440
|
+
for (const e of g.edges.filter((x) => x.from === id).sort((a, b) => (portOf(a) < portOf(b) ? 1 : -1))) {
|
|
441
|
+
if (!ids.has(e.to)) continue;
|
|
442
|
+
if (state.get(e.to) === 1) {
|
|
443
|
+
const loop = path.slice(path.indexOf(e.to)), key = loop.slice().sort().join(",");
|
|
444
|
+
if (!seenLoops.has(key)) { seenLoops.add(key); push("cycle", e.to, null, _t("These nodes go round in a loop ({path}): a path must end. Remove one of its links.", { path: loop.map((x) => titleOf(byId(g, x))).join(" → ") })); } // check:cycle
|
|
445
|
+
} else if (!state.get(e.to)) visit(e.to, path);
|
|
446
|
+
}
|
|
447
|
+
path.pop(); state.set(id, 2);
|
|
448
|
+
};
|
|
449
|
+
for (const n of nodes) if (!state.get(n.id)) visit(n.id, []);
|
|
450
|
+
for (const n of nodes) if (kindOf(n.type) === "if") for (const [p, w] of [["yes", _t("Yes")], ["no", _t("No")]]) if (!next(g, n.id, p) || !ids.has(next(g, n.id, p))) push("if_branch", n.id, null, _t("{title} has nothing after {branch}: link it to a next node, or add a Stop.", { title: titleOf(n), branch: w })); // check:if_branch
|
|
451
|
+
// each field's value, then the limits
|
|
452
|
+
for (const n of nodes) {
|
|
453
|
+
if (!own(NODES, n.type)) continue;
|
|
454
|
+
for (const f of NODES[n.type].fields(n.values)) {
|
|
455
|
+
const e = fieldError(n.type, f.key, n.values[f.key], cx, n.values);
|
|
456
|
+
if (!e) continue;
|
|
457
|
+
const over = n.type === "borrow" && f.key === "usd" && usdOk(dec(String(n.values.usd == null ? "" : n.values.usd)));
|
|
458
|
+
if (over) push("borrow_caps", n.id, "usd", e); // check:borrow_caps
|
|
459
|
+
if (!over) push("field", n.id, f.key, e); // check:field
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
for (const f of LIMIT_FIELDS()) { const e = fieldError("limits", f.key, cx.limits[f.key], cx); if (e) push("limits", "limits", f.key, e); } // check:limits
|
|
463
|
+
// funding: a buy that pays with borrowed USDG needs that much borrowed before it on every path to it
|
|
464
|
+
// (paths are walked one by one: a budget keeps a graph of many Ifs that meet again from taking long)
|
|
465
|
+
const unfunded = new Map();
|
|
466
|
+
let budget = 20000;
|
|
467
|
+
const walk = (id, left, borrowed, onPath) => {
|
|
468
|
+
const n = byId(g, id); if (!n || onPath.has(id) || --budget < 0) return;
|
|
469
|
+
onPath.add(id);
|
|
470
|
+
if (n.type === "borrow") { const u = usd6(n.values.usd); if (u != null) { left += u; borrowed = true; } }
|
|
471
|
+
if (n.type === "buy" && n.values.from === "loan" && !unfunded.has(id)) {
|
|
472
|
+
const u = usd6(n.values.usd);
|
|
473
|
+
if (!borrowed) unfunded.set(id, _t("This buy pays with borrowed USDG, but no Borrow comes before it on a path to it: add a Borrow before it, or pay with your account's USDG."));
|
|
474
|
+
else if (u != null && u > left) unfunded.set(id, _t("This buy spends {usd} of borrowed USDG, but the Borrow before it opens only {left}: borrow more, or buy less.", { usd: money6(u), left: money6(left) }));
|
|
475
|
+
else if (u != null) left -= u;
|
|
476
|
+
}
|
|
477
|
+
for (const p of outputs(n.type)) { const k = next(g, id, p); if (k) walk(k, left, borrowed, onPath); }
|
|
478
|
+
onPath.delete(id);
|
|
479
|
+
};
|
|
480
|
+
for (const t of triggers(g)) walk(t.id, 0n, false, new Set());
|
|
481
|
+
for (const n of nodes) if (n.type === "buy" && n.values.from === "loan" && g.edges.some((e) => e.to === n.id) && unfunded.has(n.id)) push("no_funding", n.id, "from", unfunded.get(n.id)); // check:no_funding
|
|
482
|
+
// a sell of a token the strategy buys before it (on its path, or on another trigger's: never only after it)
|
|
483
|
+
for (const n of nodes) {
|
|
484
|
+
if (n.type !== "sell" || !n.values.token) continue;
|
|
485
|
+
const after = reach(g, n.id), sym = symOf(ctx, n.values.token) || _t("that token");
|
|
486
|
+
if (!nodes.some((b) => b.type === "buy" && same(b.values.token, n.values.token) && !after.has(b.id))) push("sell_unbought", n.id, "token", _t("This sells {sym}, which this strategy never buys before it: add a Buy of {sym} first, or sell a token it buys.", { sym })); // check:sell_unbought
|
|
487
|
+
}
|
|
488
|
+
const count = (t) => nodes.filter((n) => n.type === t);
|
|
489
|
+
if (count("buy").length > STAGE.buys) push("too_many_buys", count("buy").at(-1).id, null, _t("This strategy has {n} buys: one key takes {max} at most. Remove a buy.", { n: count("buy").length, max: STAGE.buys })); // check:too_many_buys
|
|
490
|
+
if (count("sell").length > STAGE.sells) push("too_many_sells", count("sell").at(-1).id, null, _t("This strategy has {n} sells: one key takes {max} at most. Remove a sell.", { n: count("sell").length, max: STAGE.sells })); // check:too_many_sells
|
|
491
|
+
// a repayment that can't cover what its agent's Borrow opens
|
|
492
|
+
for (const n of count("repay")) {
|
|
493
|
+
const cap = usd6(n.values.cap), most = count("borrow").filter((b) => String(b.values.agent) === String(n.values.agent)).map((b) => usd6(b.values.usd)).filter((u) => u != null).reduce((a, b) => (b > a ? b : a), 0n);
|
|
494
|
+
if (cap != null && most > 0n && cap < most) push("repay_cap", n.id, "cap", _t("This repays at most {cap}, less than the {usd} its agent's Borrow opens: make it at least {usd} and its fee.", { cap: money6(cap), usd: money6(most) })); // check:repay_cap
|
|
495
|
+
}
|
|
496
|
+
// a Borrow and the Buy it pays for run as one operation (the executor's borrow and buy: it buys exactly what it
|
|
497
|
+
// borrows): right after each Borrow whose amount is right, one Buy paying with the borrowed USDG, for the same amount
|
|
498
|
+
for (const n of count("borrow")) {
|
|
499
|
+
if (fieldError("borrow", "usd", n.values.usd, cx, n.values)) continue;
|
|
500
|
+
const b = byId(g, next(g, n.id, "out")), d = dec(String(n.values.usd));
|
|
501
|
+
if (!b || b.type !== "buy" || b.values.from !== "loan" || dec(String(b.values.usd == null ? "" : b.values.usd)) !== d) push("borrow_buy", n.id, "usd", _t("This Borrow runs together with the Buy it pays for: link it straight to one Buy that pays with the borrowed USDG, for the same {usd}.", { usd: money(d) })); // check:borrow_buy
|
|
502
|
+
}
|
|
503
|
+
// one key borrows for one agent: every Borrow of a strategy on the same agent's line
|
|
504
|
+
const lines = [...new Set(count("borrow").map((b) => String(b.values.agent || "")).filter(Boolean))];
|
|
505
|
+
if (lines.length > 1) push("borrow_agents", count("borrow").find((b) => String(b.values.agent) === lines[1]).id, "agent", _t("This strategy borrows on {n} agents' lines, and its key borrows for one agent: keep every Borrow on the same agent.", { n: lines.length })); // check:borrow_agents
|
|
506
|
+
return out;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ---- compiling ----
|
|
510
|
+
const whenOf = (n) => {
|
|
511
|
+
const v = n.values || {};
|
|
512
|
+
switch (n.type) {
|
|
513
|
+
case "schedule": return { on: "schedule", everyHours: Number(v.every) };
|
|
514
|
+
case "price_up": return { on: "price_up", token: String(v.token || ""), above: dec(String(v.price == null ? "" : v.price)) };
|
|
515
|
+
case "price_down": return { on: "price_down", token: String(v.token || ""), below: dec(String(v.price == null ? "" : v.price)) };
|
|
516
|
+
case "loan_due": return { on: "loan_due", agent: String(v.agent || ""), withinHours: Number(v.hours) };
|
|
517
|
+
default: return { on: "tick" };
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
const testOf = (v) => {
|
|
521
|
+
const t = v.test, price = () => dec(String(v.price == null ? "" : v.price));
|
|
522
|
+
if (t === "price_above") return { is: "price_above", token: String(v.token || ""), price: price() };
|
|
523
|
+
if (t === "price_below") return { is: "price_below", token: String(v.token || ""), price: price() };
|
|
524
|
+
if (t === "holds") return { is: "holds", token: String(v.token || "") };
|
|
525
|
+
if (t === "usdg_min") return { is: "usdg_at_least", usd: dec(String(v.usd == null ? "" : v.usd)) };
|
|
526
|
+
if (t === "loan_open") return { is: "loan_open", agent: String(v.agent || "") };
|
|
527
|
+
if (t === "loan_due") return { is: "loan_due_within", agent: String(v.agent || ""), hours: Number(v.hours) };
|
|
528
|
+
return { is: "time_between", fromHour: Number(v.from), toHour: Number(v.to), weekdays: !!v.weekdays };
|
|
529
|
+
};
|
|
530
|
+
function stepsFrom(g, id, onPath) {
|
|
531
|
+
const out = [];
|
|
532
|
+
for (let cur = id; cur;) {
|
|
533
|
+
const n = byId(g, cur); if (!n || onPath.has(cur)) break;
|
|
534
|
+
const v = n.values || {};
|
|
535
|
+
if (n.type === "if") {
|
|
536
|
+
const inner = new Set([...onPath, cur]);
|
|
537
|
+
out.push({ op: "if", node: n.id, test: testOf(v), yes: stepsFrom(g, next(g, cur, "yes"), inner), no: stepsFrom(g, next(g, cur, "no"), inner) });
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
if (n.type === "stop") { out.push({ op: "stop", node: n.id }); break; }
|
|
541
|
+
if (n.type === "borrow") out.push({ op: "borrow", node: n.id, agent: String(v.agent || ""), usd: dec(String(v.usd == null ? "" : v.usd)), days: Number(v.days) });
|
|
542
|
+
else if (n.type === "buy") out.push({ op: "buy", node: n.id, token: String(v.token || ""), usd: dec(String(v.usd == null ? "" : v.usd)), from: v.from === "loan" ? "loan" : "usdg" });
|
|
543
|
+
else if (n.type === "sell") out.push({ op: "sell", node: n.id, token: String(v.token || ""), pct: Number(v.pct) });
|
|
544
|
+
else if (n.type === "repay") out.push({ op: "repay", node: n.id, agent: String(v.agent || ""), capUsd: dec(String(v.cap == null ? "" : v.cap)) });
|
|
545
|
+
else break;
|
|
546
|
+
onPath = new Set([...onPath, cur]);
|
|
547
|
+
cur = next(g, cur, "out");
|
|
548
|
+
}
|
|
549
|
+
return out;
|
|
550
|
+
}
|
|
551
|
+
const capsOfLimits = (L) => ({ perTradeUsd: dec(String(L.perTrade)) || String(L.perTrade), totalUsd: dec(String(L.total)) || String(L.total), keepUsd: dec(String(L.keep)) || String(L.keep) });
|
|
552
|
+
/** The strategy rules (the schema above): every trigger by its id, and the steps its links lead to. */
|
|
553
|
+
function compile(g) {
|
|
554
|
+
const L = limitsOf(g);
|
|
555
|
+
return { kind: SCHEMA, v: VERSION, caps: capsOfLimits(L), days: Number(L.days), rules: triggers(g).map((t) => ({ id: t.id, when: whenOf(t), do: stepsFrom(g, next(g, t.id, "out"), new Set([t.id])) })) };
|
|
556
|
+
}
|
|
557
|
+
/** Today's executors' form, for a graph they run unchanged (each trigger with one action: a timer's buy with the
|
|
558
|
+
* account's USDG, a price's sell of that token, a loan due within 6 hours' repayment of that agent's loan; a Stop after
|
|
559
|
+
* it changes nothing), or null. */
|
|
560
|
+
function toAutomationRules(g) {
|
|
561
|
+
const buys = [], sells = [], plans = [], ts = triggers(g);
|
|
562
|
+
if (!ts.length) return null;
|
|
563
|
+
for (const t of ts) {
|
|
564
|
+
const chain = [];
|
|
565
|
+
for (let cur = next(g, t.id, "out"); cur && chain.length < 3; cur = next(g, cur, "out")) { const n = byId(g, cur); if (!n) return null; chain.push(n); if (n.type === "stop") break; }
|
|
566
|
+
if (chain.length === 2 && chain[1].type !== "stop") return null;
|
|
567
|
+
if (chain.length > 2 || !chain.length) return null;
|
|
568
|
+
const a = chain[0], v = t.values || {}, w = a.values || {};
|
|
569
|
+
if (t.type === "schedule" && a.type === "buy" && w.from !== "loan") buys.push({ token: w.token, usd: dec(String(w.usd)), everyHours: Number(v.every) });
|
|
570
|
+
else if ((t.type === "price_up" || t.type === "price_down") && a.type === "sell" && same(w.token, v.token)) sells.push({ token: v.token, [t.type === "price_up" ? "above" : "below"]: dec(String(v.price)), sellPct: Number(w.pct) });
|
|
571
|
+
else if (t.type === "loan_due" && Number(v.hours) === 6 && a.type === "repay" && String(w.agent) === String(v.agent)) plans.push({ agent_id: Number(v.agent), cap_usd: Number(dec(String(w.cap))), use_savings: false, late: true });
|
|
572
|
+
else return null;
|
|
573
|
+
}
|
|
574
|
+
if (g.nodes.some((n) => n.type === "if" || n.type === "borrow" || n.type === "tick")) return null;
|
|
575
|
+
const L = limitsOf(g);
|
|
576
|
+
return { rules: buys.length || sells.length ? { buys, sells, caps: capsOfLimits(L), days: Number(L.days) } : null, plans };
|
|
577
|
+
}
|
|
578
|
+
const needsExecutor = (g) => g.nodes.length > 0 && toAutomationRules(g) === null;
|
|
579
|
+
|
|
580
|
+
// ---- Copy into Go mode: a published strategy back as a draft graph ----
|
|
581
|
+
const isAddr = (s) => typeof s === "string" && /^0x[0-9a-fA-F]{40}$/.test(s);
|
|
582
|
+
const isAgent = (s) => typeof s === "string" && /^\d{1,12}$/.test(s);
|
|
583
|
+
const amount = (s) => (typeof s === "string" && usdOk(dec(s)) ? dec(s) : null);
|
|
584
|
+
function fromPublished(x) {
|
|
585
|
+
const rec = x && x.kind === SCHEMA ? { strategy: x } : x || {}, s = rec.strategy;
|
|
586
|
+
const bad = () => new Error(_t("This isn't a strategy this page can read."));
|
|
587
|
+
if (!s || s.kind !== SCHEMA || s.v !== VERSION || !Array.isArray(s.rules) || s.rules.length > MAX_NODES) throw bad();
|
|
588
|
+
const g = newGraph(typeof rec.name === "string" && rec.name.trim() ? rec.name.trim().slice(0, 60) : _t("Copied strategy"));
|
|
589
|
+
g.from = rec.id != null && /^[\w:-]{1,80}$/.test(String(rec.id)) ? "published:" + String(rec.id) : "published";
|
|
590
|
+
const c = s.caps || {};
|
|
591
|
+
g.limits = { perTrade: amount(c.perTradeUsd) || LIMITS.perTrade, total: amount(c.totalUsd) || LIMITS.total, keep: amount(c.keepUsd) || LIMITS.keep, days: KEY_DAYS.includes(Number(s.days)) ? Number(s.days) : LIMITS.days };
|
|
592
|
+
// a node by the id the rules give it (the same id again: the same node, where two paths meet)
|
|
593
|
+
const put = (id, type, values) => {
|
|
594
|
+
const have = typeof id === "string" && /^n\d{1,4}$/.test(id) ? byId(g, id) : null;
|
|
595
|
+
if (have) { if (have.type !== type) throw bad(); return have; }
|
|
596
|
+
if (g.nodes.length >= MAX_NODES) throw bad();
|
|
597
|
+
const n = { id: typeof id === "string" && /^n\d{1,4}$/.test(id) ? id : null, type, values: { ...defaults(g, type, null), ...values }, x: 0, y: 0 };
|
|
598
|
+
if (!n.id) { let k = (g.seq || 0) + 1; while (byId(g, "n" + k)) k++; n.id = "n" + k; }
|
|
599
|
+
g.seq = Math.max(g.seq || 0, nid(n.id) === Infinity ? 0 : nid(n.id));
|
|
600
|
+
g.nodes.push(n);
|
|
601
|
+
return n;
|
|
602
|
+
};
|
|
603
|
+
const tok = (t) => { if (!isAddr(t)) throw bad(); return t; };
|
|
604
|
+
const ag = (a) => { if (!isAgent(a)) throw bad(); return a; };
|
|
605
|
+
const lvl = (p) => { const d = typeof p === "string" ? dec(p) : null; if (!priceOk(d)) throw bad(); return d; };
|
|
606
|
+
const trig = (w) => {
|
|
607
|
+
if (!w || typeof w !== "object") throw bad();
|
|
608
|
+
if (w.on === "schedule" && EVERY.includes(Number(w.everyHours))) return ["schedule", { every: Number(w.everyHours) }];
|
|
609
|
+
if (w.on === "price_up") return ["price_up", { token: tok(w.token), price: lvl(w.above) }];
|
|
610
|
+
if (w.on === "price_down") return ["price_down", { token: tok(w.token), price: lvl(w.below) }];
|
|
611
|
+
if (w.on === "loan_due" && DUE.includes(Number(w.withinHours))) return ["loan_due", { agent: ag(w.agent), hours: Number(w.withinHours) }];
|
|
612
|
+
if (w.on === "tick") return ["tick", {}];
|
|
613
|
+
throw bad();
|
|
614
|
+
};
|
|
615
|
+
const test = (t) => {
|
|
616
|
+
if (!t || typeof t !== "object") throw bad();
|
|
617
|
+
if (t.is === "price_above" || t.is === "price_below") return { test: t.is, token: tok(t.token), price: lvl(t.price) };
|
|
618
|
+
if (t.is === "holds") return { test: "holds", token: tok(t.token) };
|
|
619
|
+
if (t.is === "usdg_at_least") { const u = amount(t.usd); if (u == null) throw bad(); return { test: "usdg_min", usd: u }; }
|
|
620
|
+
if (t.is === "loan_open") return { test: "loan_open", agent: ag(t.agent) };
|
|
621
|
+
if (t.is === "loan_due_within" && WITHIN.includes(Number(t.hours))) return { test: "loan_due", agent: ag(t.agent), hours: Number(t.hours) };
|
|
622
|
+
if (t.is === "time_between" && HOURS.includes(Number(t.fromHour)) && HOURS.includes(Number(t.toHour))) return { test: "time", from: Number(t.fromHour), to: Number(t.toHour), weekdays: !!t.weekdays };
|
|
623
|
+
throw bad();
|
|
624
|
+
};
|
|
625
|
+
const step = (s1) => {
|
|
626
|
+
if (!s1 || typeof s1 !== "object") throw bad();
|
|
627
|
+
const u = (v) => { const a = amount(v); if (a == null) throw bad(); return a; };
|
|
628
|
+
if (s1.op === "if") return ["if", test(s1.test)];
|
|
629
|
+
if (s1.op === "borrow" && TERMS.includes(Number(s1.days))) return ["borrow", { agent: ag(s1.agent), usd: u(s1.usd), days: Number(s1.days) }];
|
|
630
|
+
if (s1.op === "buy" && (s1.from === "loan" || s1.from === "usdg")) return ["buy", { token: tok(s1.token), usd: u(s1.usd), from: s1.from }];
|
|
631
|
+
if (s1.op === "sell" && PCT.includes(Number(s1.pct))) return ["sell", { token: tok(s1.token), pct: Number(s1.pct) }];
|
|
632
|
+
if (s1.op === "repay") return ["repay", { agent: ag(s1.agent), cap: u(s1.capUsd) }];
|
|
633
|
+
if (s1.op === "stop") return ["stop", {}];
|
|
634
|
+
throw bad();
|
|
635
|
+
};
|
|
636
|
+
const chain = (from, port, steps, depth) => {
|
|
637
|
+
if (!Array.isArray(steps) || depth > MAX_NODES) throw bad();
|
|
638
|
+
let prev = from, p = port;
|
|
639
|
+
for (const s1 of steps) {
|
|
640
|
+
const [type, values] = step(s1), n = put(s1.node, type, values);
|
|
641
|
+
if (connect(g, prev, n.id, kindOf(byId(g, prev).type) === "if" ? p : undefined)) throw bad();
|
|
642
|
+
if (type === "if") { chain(n.id, "yes", s1.yes, depth + 1); chain(n.id, "no", s1.no, depth + 1); return; }
|
|
643
|
+
if (type === "stop") return;
|
|
644
|
+
prev = n.id; p = "out";
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
for (const r of s.rules) { if (!r || typeof r !== "object") throw bad(); const [type, values] = trig(r.when), t = put(r.id, type, values); chain(t.id, "out", r.do, 0); }
|
|
648
|
+
layout(g);
|
|
649
|
+
// the cards where the published graph had them, when it holds the same nodes
|
|
650
|
+
const pub = rec.graph && Array.isArray(rec.graph.nodes) ? rec.graph.nodes : [];
|
|
651
|
+
for (const n of pub) { const m = n && byId(g, n.id); if (m && m.type === n.type && Number.isFinite(n.x) && Number.isFinite(n.y)) { m.x = Math.max(0, Math.min(4000, Math.round(n.x))); m.y = Math.max(0, Math.min(4000, Math.round(n.y))); } }
|
|
652
|
+
return g;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// ---- in a few words, for a card's face ----
|
|
656
|
+
function summary(n, ctx) {
|
|
657
|
+
const v = n.values || {}, sym = (t) => symOf(ctx, t) || "—", lvl = (p) => (dec(String(p == null ? "" : p)) ? money(dec(String(p))) : "—");
|
|
658
|
+
const agent = () => { const a = ((ctx && ctx.agents) || []).find((x) => String(x.id) === String(v.agent)); return a ? a.label : v.agent ? "#" + v.agent : "—"; };
|
|
659
|
+
switch (n.type) {
|
|
660
|
+
case "schedule": return choice(EVERY, v.every) ? everyWords(Number(v.every)) : "—";
|
|
661
|
+
case "price_up": return _t("{sym} at {price} or more", { sym: sym(v.token), price: lvl(v.price) });
|
|
662
|
+
case "price_down": return _t("{sym} at {price} or less", { sym: sym(v.token), price: lvl(v.price) });
|
|
663
|
+
case "loan_due": return _t("{agent} · {n} h before due", { agent: agent(), n: Number(v.hours) || "—" });
|
|
664
|
+
case "tick": return _t("Every 10 minutes");
|
|
665
|
+
case "if": {
|
|
666
|
+
const t = v.test;
|
|
667
|
+
if (t === "price_above") return _t("{sym} at {price} or more", { sym: sym(v.token), price: lvl(v.price) });
|
|
668
|
+
if (t === "price_below") return _t("{sym} at {price} or less", { sym: sym(v.token), price: lvl(v.price) });
|
|
669
|
+
if (t === "holds") return _t("Holds {sym}", { sym: sym(v.token) });
|
|
670
|
+
if (t === "usdg_min") return _t("USDG at {usd} or more", { usd: lvl(v.usd) });
|
|
671
|
+
if (t === "loan_open") return _t("A loan of {agent} is open", { agent: agent() });
|
|
672
|
+
if (t === "loan_due") return _t("A loan of {agent} due within {n} h", { agent: agent(), n: Number(v.hours) || "—" });
|
|
673
|
+
return _t("{from} to {to}", { from: hourWords(Number(v.from) || 0), to: hourWords(Number(v.to) || 0) }) + (v.weekdays ? " · " + _t("weekdays") : "");
|
|
674
|
+
}
|
|
675
|
+
case "borrow": return _t("{usd} for {days} days · {agent}", { usd: lvl(v.usd), days: Number(v.days) || "—", agent: agent() });
|
|
676
|
+
case "buy": return _t("{usd} of {sym}", { usd: lvl(v.usd), sym: sym(v.token) }) + " · " + (v.from === "loan" ? _t("borrowed USDG") : _t("your USDG"));
|
|
677
|
+
case "sell": return _t("{pct}% of {sym}", { pct: Number(v.pct) || "—", sym: sym(v.token) });
|
|
678
|
+
case "repay": return _t("{agent} · up to {cap}", { agent: agent(), cap: lvl(v.cap) });
|
|
679
|
+
case "stop": return _t("Ends this path");
|
|
680
|
+
default: return "";
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
const limitsSentence = (L) => _t("At most {per} a trade and {total} in all, borrowed USDG included, always keeping {keep} in USDG, for {days} days.", { per: money(L.perTrade), total: money(L.total), keep: money(L.keep), days: Number(L.days) });
|
|
684
|
+
|
|
685
|
+
root.PriorsTradeCore = {
|
|
686
|
+
STAGE, BORROW, LIQUID, SCHEMA, VERSION, LIMITS, MAX_NODES, NODES, TRIGGERS, ACTIONS, TESTS, TEMPLATES, CODES, LIMIT_FIELDS,
|
|
687
|
+
node, kindOf, outputs, hasInput, newGraph, addNode, canConnect, connect, disconnect, removeNode, moveNode, setValue, next, reach,
|
|
688
|
+
insertable, insertAfter, layout, template, fromTemplate, tradable, fieldError, problems, compile, toAutomationRules, needsExecutor,
|
|
689
|
+
fromPublished, summary, limitsSentence, limitsOf, money, dec, clone,
|
|
690
|
+
};
|
|
691
|
+
})(typeof window !== "undefined" ? window : globalThis);
|