@raintree-technology/perps 0.1.0
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 +33 -0
- package/LICENSE +21 -0
- package/README.md +175 -0
- package/dist/adapters/aevo.d.ts +64 -0
- package/dist/adapters/aevo.js +899 -0
- package/dist/adapters/certification.d.ts +33 -0
- package/dist/adapters/certification.js +99 -0
- package/dist/adapters/decibel/order-manager.d.ts +45 -0
- package/dist/adapters/decibel/order-manager.js +140 -0
- package/dist/adapters/decibel/rest-client.d.ts +176 -0
- package/dist/adapters/decibel/rest-client.js +155 -0
- package/dist/adapters/decibel/ws-feed.d.ts +28 -0
- package/dist/adapters/decibel/ws-feed.js +166 -0
- package/dist/adapters/decibel.d.ts +108 -0
- package/dist/adapters/decibel.js +1377 -0
- package/dist/adapters/hyperliquid.d.ts +63 -0
- package/dist/adapters/hyperliquid.js +797 -0
- package/dist/adapters/index.d.ts +11 -0
- package/dist/adapters/index.js +12 -0
- package/dist/adapters/interface.d.ts +310 -0
- package/dist/adapters/interface.js +15 -0
- package/dist/adapters/orderly.d.ts +70 -0
- package/dist/adapters/orderly.js +936 -0
- package/dist/adapters/paradex.d.ts +69 -0
- package/dist/adapters/paradex.js +862 -0
- package/dist/adapters/utils.d.ts +17 -0
- package/dist/adapters/utils.js +122 -0
- package/dist/cli/command-metadata.d.ts +2 -0
- package/dist/cli/command-metadata.js +44 -0
- package/dist/cli/context.d.ts +14 -0
- package/dist/cli/context.js +59 -0
- package/dist/cli/experience.d.ts +48 -0
- package/dist/cli/experience.js +243 -0
- package/dist/cli/ink/app/AppShell.d.ts +12 -0
- package/dist/cli/ink/app/AppShell.js +32 -0
- package/dist/cli/ink/app/MetricStrip.d.ts +6 -0
- package/dist/cli/ink/app/MetricStrip.js +14 -0
- package/dist/cli/ink/app/Panel.d.ts +9 -0
- package/dist/cli/ink/app/Panel.js +7 -0
- package/dist/cli/ink/app/ascii.d.ts +2 -0
- package/dist/cli/ink/app/ascii.js +46 -0
- package/dist/cli/ink/app/index.d.ts +5 -0
- package/dist/cli/ink/app/index.js +4 -0
- package/dist/cli/ink/app/types.d.ts +15 -0
- package/dist/cli/ink/app/types.js +1 -0
- package/dist/cli/ink/components/PnL.d.ts +12 -0
- package/dist/cli/ink/components/PnL.js +23 -0
- package/dist/cli/ink/components/Spinner.d.ts +13 -0
- package/dist/cli/ink/components/Spinner.js +13 -0
- package/dist/cli/ink/components/Table.d.ts +14 -0
- package/dist/cli/ink/components/Table.js +42 -0
- package/dist/cli/ink/components/WatchHeader.d.ts +10 -0
- package/dist/cli/ink/components/WatchHeader.js +18 -0
- package/dist/cli/ink/components/index.d.ts +4 -0
- package/dist/cli/ink/components/index.js +4 -0
- package/dist/cli/ink/index.d.ts +4 -0
- package/dist/cli/ink/index.js +4 -0
- package/dist/cli/ink/render.d.ts +12 -0
- package/dist/cli/ink/render.js +21 -0
- package/dist/cli/ink/theme.d.ts +29 -0
- package/dist/cli/ink/theme.js +40 -0
- package/dist/cli/network-defaults.d.ts +10 -0
- package/dist/cli/network-defaults.js +35 -0
- package/dist/cli/output.d.ts +11 -0
- package/dist/cli/output.js +115 -0
- package/dist/cli/program.d.ts +18 -0
- package/dist/cli/program.js +164 -0
- package/dist/cli/watch.d.ts +19 -0
- package/dist/cli/watch.js +35 -0
- package/dist/client/index.d.ts +55 -0
- package/dist/client/index.js +157 -0
- package/dist/commands/account/add.d.ts +2 -0
- package/dist/commands/account/add.js +510 -0
- package/dist/commands/account/balances-simple.d.ts +5 -0
- package/dist/commands/account/balances-simple.js +63 -0
- package/dist/commands/account/index.d.ts +2 -0
- package/dist/commands/account/index.js +17 -0
- package/dist/commands/account/ls.d.ts +2 -0
- package/dist/commands/account/ls.js +95 -0
- package/dist/commands/account/positions-simple.d.ts +5 -0
- package/dist/commands/account/positions-simple.js +77 -0
- package/dist/commands/account/remove.d.ts +2 -0
- package/dist/commands/account/remove.js +47 -0
- package/dist/commands/account/set-default.d.ts +2 -0
- package/dist/commands/account/set-default.js +47 -0
- package/dist/commands/agent/index.d.ts +2 -0
- package/dist/commands/agent/index.js +126 -0
- package/dist/commands/arb/alert.d.ts +6 -0
- package/dist/commands/arb/alert.js +88 -0
- package/dist/commands/arb/basis-execute.d.ts +6 -0
- package/dist/commands/arb/basis-execute.js +332 -0
- package/dist/commands/arb/basis.d.ts +6 -0
- package/dist/commands/arb/basis.js +181 -0
- package/dist/commands/arb/compare.d.ts +6 -0
- package/dist/commands/arb/compare.js +216 -0
- package/dist/commands/arb/execute.d.ts +6 -0
- package/dist/commands/arb/execute.js +467 -0
- package/dist/commands/arb/funding.d.ts +6 -0
- package/dist/commands/arb/funding.js +201 -0
- package/dist/commands/arb/history.d.ts +6 -0
- package/dist/commands/arb/history.js +153 -0
- package/dist/commands/arb/index.d.ts +6 -0
- package/dist/commands/arb/index.js +29 -0
- package/dist/commands/arb/positions.d.ts +6 -0
- package/dist/commands/arb/positions.js +158 -0
- package/dist/commands/arb/spread.d.ts +6 -0
- package/dist/commands/arb/spread.js +253 -0
- package/dist/commands/arb/track.d.ts +6 -0
- package/dist/commands/arb/track.js +259 -0
- package/dist/commands/asset/book-simple.d.ts +5 -0
- package/dist/commands/asset/book-simple.js +77 -0
- package/dist/commands/asset/index.d.ts +2 -0
- package/dist/commands/asset/index.js +5 -0
- package/dist/commands/completion.d.ts +2 -0
- package/dist/commands/completion.js +161 -0
- package/dist/commands/config/index.d.ts +5 -0
- package/dist/commands/config/index.js +109 -0
- package/dist/commands/data/index.d.ts +31 -0
- package/dist/commands/data/index.js +1466 -0
- package/dist/commands/doctor.d.ts +2 -0
- package/dist/commands/doctor.js +201 -0
- package/dist/commands/exchange/index.d.ts +2 -0
- package/dist/commands/exchange/index.js +107 -0
- package/dist/commands/index.d.ts +2 -0
- package/dist/commands/index.js +48 -0
- package/dist/commands/markets/index.d.ts +2 -0
- package/dist/commands/markets/index.js +5 -0
- package/dist/commands/markets/ls-simple.d.ts +7 -0
- package/dist/commands/markets/ls-simple.js +277 -0
- package/dist/commands/operator/index.d.ts +2 -0
- package/dist/commands/operator/index.js +146 -0
- package/dist/commands/order/cancel-simple.d.ts +5 -0
- package/dist/commands/order/cancel-simple.js +104 -0
- package/dist/commands/order/index.d.ts +2 -0
- package/dist/commands/order/index.js +13 -0
- package/dist/commands/order/limit-simple.d.ts +5 -0
- package/dist/commands/order/limit-simple.js +195 -0
- package/dist/commands/order/market-simple.d.ts +5 -0
- package/dist/commands/order/market-simple.js +190 -0
- package/dist/commands/order/shared.d.ts +17 -0
- package/dist/commands/order/shared.js +51 -0
- package/dist/commands/order/trigger-simple.d.ts +5 -0
- package/dist/commands/order/trigger-simple.js +246 -0
- package/dist/commands/referral/index.d.ts +2 -0
- package/dist/commands/referral/index.js +7 -0
- package/dist/commands/referral/set.d.ts +2 -0
- package/dist/commands/referral/set.js +26 -0
- package/dist/commands/referral/status.d.ts +2 -0
- package/dist/commands/referral/status.js +31 -0
- package/dist/commands/replay/index.d.ts +2 -0
- package/dist/commands/replay/index.js +152 -0
- package/dist/commands/risk/analytics.d.ts +2 -0
- package/dist/commands/risk/analytics.js +64 -0
- package/dist/commands/risk/audit.d.ts +2 -0
- package/dist/commands/risk/audit.js +52 -0
- package/dist/commands/risk/index.d.ts +2 -0
- package/dist/commands/risk/index.js +9 -0
- package/dist/commands/risk/rules.d.ts +2 -0
- package/dist/commands/risk/rules.js +102 -0
- package/dist/commands/server.d.ts +2 -0
- package/dist/commands/server.js +208 -0
- package/dist/commands/setup/index.d.ts +2 -0
- package/dist/commands/setup/index.js +478 -0
- package/dist/commands/signal/index.d.ts +2 -0
- package/dist/commands/signal/index.js +129 -0
- package/dist/commands/state/index.d.ts +2 -0
- package/dist/commands/state/index.js +5 -0
- package/dist/commands/state/show.d.ts +2 -0
- package/dist/commands/state/show.js +105 -0
- package/dist/commands/strategy/index.d.ts +4 -0
- package/dist/commands/strategy/index.js +73 -0
- package/dist/commands/traces/index.d.ts +2 -0
- package/dist/commands/traces/index.js +76 -0
- package/dist/commands/ui/demo.d.ts +9 -0
- package/dist/commands/ui/demo.js +195 -0
- package/dist/commands/ui/index.d.ts +2 -0
- package/dist/commands/ui/index.js +7 -0
- package/dist/commands/ui/terminal.d.ts +2 -0
- package/dist/commands/ui/terminal.js +255 -0
- package/dist/commands/upgrade.d.ts +2 -0
- package/dist/commands/upgrade.js +98 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -0
- package/dist/lib/agent/audit.d.ts +12 -0
- package/dist/lib/agent/audit.js +13 -0
- package/dist/lib/agent/gateway.d.ts +13 -0
- package/dist/lib/agent/gateway.js +598 -0
- package/dist/lib/agent/metrics.d.ts +33 -0
- package/dist/lib/agent/metrics.js +175 -0
- package/dist/lib/agent/signature.d.ts +8 -0
- package/dist/lib/agent/signature.js +28 -0
- package/dist/lib/agent/tools.d.ts +28 -0
- package/dist/lib/agent/tools.js +453 -0
- package/dist/lib/agent/x402.d.ts +23 -0
- package/dist/lib/agent/x402.js +62 -0
- package/dist/lib/api-wallet.d.ts +69 -0
- package/dist/lib/api-wallet.js +101 -0
- package/dist/lib/balance-watcher.d.ts +25 -0
- package/dist/lib/balance-watcher.js +83 -0
- package/dist/lib/book-watcher.d.ts +25 -0
- package/dist/lib/book-watcher.js +48 -0
- package/dist/lib/config.d.ts +88 -0
- package/dist/lib/config.js +427 -0
- package/dist/lib/constants.d.ts +50 -0
- package/dist/lib/constants.js +84 -0
- package/dist/lib/contracts.d.ts +7 -0
- package/dist/lib/contracts.js +8 -0
- package/dist/lib/credential-vault.d.ts +22 -0
- package/dist/lib/credential-vault.js +109 -0
- package/dist/lib/db/accounts.d.ts +83 -0
- package/dist/lib/db/accounts.js +203 -0
- package/dist/lib/db/funding-history.d.ts +69 -0
- package/dist/lib/db/funding-history.js +183 -0
- package/dist/lib/db/index.d.ts +11 -0
- package/dist/lib/db/index.js +272 -0
- package/dist/lib/events/bus.d.ts +10 -0
- package/dist/lib/events/bus.js +17 -0
- package/dist/lib/events/types.d.ts +51 -0
- package/dist/lib/events/types.js +1 -0
- package/dist/lib/exchange.d.ts +30 -0
- package/dist/lib/exchange.js +84 -0
- package/dist/lib/execution/journal.d.ts +25 -0
- package/dist/lib/execution/journal.js +158 -0
- package/dist/lib/execution/safety.d.ts +34 -0
- package/dist/lib/execution/safety.js +197 -0
- package/dist/lib/exit-codes.d.ts +18 -0
- package/dist/lib/exit-codes.js +60 -0
- package/dist/lib/fetch.d.ts +18 -0
- package/dist/lib/fetch.js +66 -0
- package/dist/lib/fs-security.d.ts +10 -0
- package/dist/lib/fs-security.js +26 -0
- package/dist/lib/funding-tracker.d.ts +40 -0
- package/dist/lib/funding-tracker.js +118 -0
- package/dist/lib/logger.d.ts +27 -0
- package/dist/lib/logger.js +82 -0
- package/dist/lib/network-model.d.ts +13 -0
- package/dist/lib/network-model.js +30 -0
- package/dist/lib/onboarding.d.ts +133 -0
- package/dist/lib/onboarding.js +1459 -0
- package/dist/lib/operator-state.d.ts +25 -0
- package/dist/lib/operator-state.js +82 -0
- package/dist/lib/orders-watcher.d.ts +24 -0
- package/dist/lib/orders-watcher.js +74 -0
- package/dist/lib/paths.d.ts +20 -0
- package/dist/lib/paths.js +23 -0
- package/dist/lib/portfolio-watcher.d.ts +33 -0
- package/dist/lib/portfolio-watcher.js +95 -0
- package/dist/lib/position-watcher.d.ts +16 -0
- package/dist/lib/position-watcher.js +44 -0
- package/dist/lib/price-watcher.d.ts +15 -0
- package/dist/lib/price-watcher.js +84 -0
- package/dist/lib/prompts.d.ts +32 -0
- package/dist/lib/prompts.js +105 -0
- package/dist/lib/rate-limit.d.ts +32 -0
- package/dist/lib/rate-limit.js +88 -0
- package/dist/lib/risk/analytics.d.ts +39 -0
- package/dist/lib/risk/analytics.js +98 -0
- package/dist/lib/risk/drawdown.d.ts +18 -0
- package/dist/lib/risk/drawdown.js +49 -0
- package/dist/lib/risk/evaluation-log.d.ts +29 -0
- package/dist/lib/risk/evaluation-log.js +61 -0
- package/dist/lib/risk/index.d.ts +4 -0
- package/dist/lib/risk/index.js +4 -0
- package/dist/lib/risk/limits.d.ts +23 -0
- package/dist/lib/risk/limits.js +27 -0
- package/dist/lib/risk/manager.d.ts +32 -0
- package/dist/lib/risk/manager.js +85 -0
- package/dist/lib/risk/policy-middleware.d.ts +33 -0
- package/dist/lib/risk/policy-middleware.js +267 -0
- package/dist/lib/risk/position-sizer.d.ts +9 -0
- package/dist/lib/risk/position-sizer.js +14 -0
- package/dist/lib/risk/rules-store.d.ts +16 -0
- package/dist/lib/risk/rules-store.js +47 -0
- package/dist/lib/schema.d.ts +254 -0
- package/dist/lib/schema.js +199 -0
- package/dist/lib/secrets.d.ts +3 -0
- package/dist/lib/secrets.js +62 -0
- package/dist/lib/settings.d.ts +24 -0
- package/dist/lib/settings.js +86 -0
- package/dist/lib/signals.d.ts +73 -0
- package/dist/lib/signals.js +136 -0
- package/dist/lib/stable-stringify.d.ts +6 -0
- package/dist/lib/stable-stringify.js +17 -0
- package/dist/lib/state-context.d.ts +44 -0
- package/dist/lib/state-context.js +133 -0
- package/dist/lib/strategy/basis-trade.d.ts +2 -0
- package/dist/lib/strategy/basis-trade.js +24 -0
- package/dist/lib/strategy/funding-arb.d.ts +2 -0
- package/dist/lib/strategy/funding-arb.js +23 -0
- package/dist/lib/strategy/interface.d.ts +23 -0
- package/dist/lib/strategy/interface.js +1 -0
- package/dist/lib/strategy/registry.d.ts +4 -0
- package/dist/lib/strategy/registry.js +10 -0
- package/dist/lib/telemetry.d.ts +25 -0
- package/dist/lib/telemetry.js +101 -0
- package/dist/lib/trace-queries.d.ts +20 -0
- package/dist/lib/trace-queries.js +133 -0
- package/dist/lib/trace.d.ts +1 -0
- package/dist/lib/trace.js +4 -0
- package/dist/lib/trade-reputation.d.ts +6 -0
- package/dist/lib/trade-reputation.js +99 -0
- package/dist/lib/ui-tokens.d.ts +21 -0
- package/dist/lib/ui-tokens.js +26 -0
- package/dist/lib/validate.d.ts +39 -0
- package/dist/lib/validate.js +108 -0
- package/dist/lib/validation.d.ts +9 -0
- package/dist/lib/validation.js +64 -0
- package/dist/server/cache.d.ts +38 -0
- package/dist/server/cache.js +56 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +89 -0
- package/dist/server/ipc.d.ts +18 -0
- package/dist/server/ipc.js +159 -0
- package/dist/server/subscriptions.d.ts +18 -0
- package/dist/server/subscriptions.js +114 -0
- package/package.json +124 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arb Compare Command
|
|
3
|
+
* Compare arbitrage opportunities across all assets
|
|
4
|
+
*/
|
|
5
|
+
import { getContext, getOutputOptions } from "../../cli/program.js";
|
|
6
|
+
import { output, outputError } from "../../cli/output.js";
|
|
7
|
+
import { getExchangeAdapterById, getAvailableExchanges } from "../../lib/exchange.js";
|
|
8
|
+
import { getSpreadStats, storeFundingRates } from "../../lib/db/funding-history.js";
|
|
9
|
+
import { COMMON_ASSETS, DEFAULT_WATCH_INTERVAL_SEC } from "../../lib/constants.js";
|
|
10
|
+
import { pLimit } from "../../lib/rate-limit.js";
|
|
11
|
+
import { parseAssetList, parsePositiveInt } from "../../lib/validate.js";
|
|
12
|
+
export function registerArbCompareCommand(arb) {
|
|
13
|
+
arb
|
|
14
|
+
.command("compare")
|
|
15
|
+
.description("Compare arbitrage opportunities across all assets")
|
|
16
|
+
.option("-a, --assets <assets>", "Comma-separated assets", COMMON_ASSETS.join(","))
|
|
17
|
+
.option("--min-spread <percent>", "Minimum annualized spread to show", "0")
|
|
18
|
+
.option("--sort <field>", "Sort by: spread, consistency, daily", "spread")
|
|
19
|
+
.option("-w, --watch", "Continuous monitoring mode")
|
|
20
|
+
.option("-i, --interval <seconds>", "Refresh interval for watch mode", String(DEFAULT_WATCH_INTERVAL_SEC))
|
|
21
|
+
.option("--store", "Store funding data to database while watching")
|
|
22
|
+
.action(async function () {
|
|
23
|
+
const ctx = getContext(this);
|
|
24
|
+
const outputOpts = getOutputOptions(this);
|
|
25
|
+
const opts = this.opts();
|
|
26
|
+
const assets = parseAssetList(opts.assets, COMMON_ASSETS);
|
|
27
|
+
const minSpread = parseFloat(opts.minSpread) || 0;
|
|
28
|
+
const exchanges = getAvailableExchanges();
|
|
29
|
+
const intervalSec = parsePositiveInt(opts.interval, "interval", DEFAULT_WATCH_INTERVAL_SEC);
|
|
30
|
+
const isTestnet = ctx.config.testnet;
|
|
31
|
+
if (opts.watch) {
|
|
32
|
+
// Watch mode - continuous monitoring
|
|
33
|
+
console.clear();
|
|
34
|
+
console.log(" \x1b[1mFunding Rate Monitor\x1b[0m - Refreshing every " + intervalSec + "s (Ctrl+C to stop)\n");
|
|
35
|
+
const runOnce = async () => {
|
|
36
|
+
try {
|
|
37
|
+
const startTime = Date.now();
|
|
38
|
+
const opportunities = await fetchAllOpportunities(assets, exchanges, minSpread, isTestnet, opts.store ?? false);
|
|
39
|
+
sortOpportunities(opportunities, opts.sort);
|
|
40
|
+
// Clear and redraw
|
|
41
|
+
console.clear();
|
|
42
|
+
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
43
|
+
console.log(` \x1b[1mFunding Rate Monitor\x1b[0m - Updated ${new Date().toLocaleTimeString()} (${elapsed}s) | Refresh: ${intervalSec}s\n`);
|
|
44
|
+
displayOpportunities(opportunities, opts.store ?? false);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
console.error(` \x1b[31mError:\x1b[0m ${err instanceof Error ? err.message : err}`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// Run immediately
|
|
51
|
+
await runOnce();
|
|
52
|
+
// Then on interval
|
|
53
|
+
const interval = setInterval(runOnce, intervalSec * 1000);
|
|
54
|
+
// Handle Ctrl+C
|
|
55
|
+
process.once("SIGINT", () => {
|
|
56
|
+
clearInterval(interval);
|
|
57
|
+
console.log("\n\n Monitor stopped.\n");
|
|
58
|
+
process.exit(0);
|
|
59
|
+
});
|
|
60
|
+
// Keep running
|
|
61
|
+
await new Promise(() => { });
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// One-shot mode
|
|
65
|
+
try {
|
|
66
|
+
if (!outputOpts.json) {
|
|
67
|
+
console.log("\n Fetching funding rates across all exchanges...\n");
|
|
68
|
+
}
|
|
69
|
+
const opportunities = await fetchAllOpportunities(assets, exchanges, minSpread, isTestnet, false);
|
|
70
|
+
sortOpportunities(opportunities, opts.sort);
|
|
71
|
+
if (outputOpts.json) {
|
|
72
|
+
output({ opportunities }, { json: true });
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
displayOpportunities(opportunities, false);
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
outputError(err instanceof Error ? err.message : String(err));
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
async function fetchAllOpportunities(assets, exchanges, minSpread, isTestnet, storeData = false) {
|
|
85
|
+
const opportunities = [];
|
|
86
|
+
const now = Date.now();
|
|
87
|
+
for (const asset of assets) {
|
|
88
|
+
try {
|
|
89
|
+
const rates = await fetchFundingForAsset(asset, exchanges, isTestnet);
|
|
90
|
+
if (rates.length < 2)
|
|
91
|
+
continue;
|
|
92
|
+
// Store to database if requested
|
|
93
|
+
if (storeData) {
|
|
94
|
+
const records = rates.map(r => ({
|
|
95
|
+
exchange: r.exchange,
|
|
96
|
+
market: `${asset}-PERP`,
|
|
97
|
+
rate: r.rate,
|
|
98
|
+
annualized: r.rate * 24 * 365 * 100,
|
|
99
|
+
recordedAt: now,
|
|
100
|
+
}));
|
|
101
|
+
storeFundingRates(records);
|
|
102
|
+
}
|
|
103
|
+
const sorted = [...rates].sort((a, b) => b.rate - a.rate);
|
|
104
|
+
const highest = sorted[0];
|
|
105
|
+
const lowest = sorted[sorted.length - 1];
|
|
106
|
+
const spread = (highest.rate - lowest.rate) * 100; // Convert to percentage
|
|
107
|
+
const annualized = spread * 24 * 365;
|
|
108
|
+
if (annualized < minSpread)
|
|
109
|
+
continue;
|
|
110
|
+
// Get historical stats if available
|
|
111
|
+
const stats = getSpreadStats(`${asset}-PERP`, 24);
|
|
112
|
+
opportunities.push({
|
|
113
|
+
asset,
|
|
114
|
+
spread,
|
|
115
|
+
annualized,
|
|
116
|
+
shortExchange: highest.exchange,
|
|
117
|
+
shortRate: highest.rate * 100,
|
|
118
|
+
longExchange: lowest.exchange,
|
|
119
|
+
longRate: lowest.rate * 100,
|
|
120
|
+
dailyOn10k: (spread / 100) * 24 * 10000,
|
|
121
|
+
consistency: stats.samples > 0 ? stats.consistency : undefined,
|
|
122
|
+
samples: stats.samples > 0 ? stats.samples : undefined,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
// Skip assets that fail
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return opportunities;
|
|
130
|
+
}
|
|
131
|
+
function sortOpportunities(opportunities, sortField) {
|
|
132
|
+
if (sortField === "consistency") {
|
|
133
|
+
opportunities.sort((a, b) => (b.consistency || 0) - (a.consistency || 0));
|
|
134
|
+
}
|
|
135
|
+
else if (sortField === "daily") {
|
|
136
|
+
opportunities.sort((a, b) => b.dailyOn10k - a.dailyOn10k);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
opportunities.sort((a, b) => b.annualized - a.annualized);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function displayOpportunities(opportunities, showStored) {
|
|
143
|
+
if (opportunities.length === 0) {
|
|
144
|
+
console.log(" No opportunities found above threshold.\n");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// Display table
|
|
148
|
+
console.log(" " +
|
|
149
|
+
"Asset".padEnd(8) +
|
|
150
|
+
"Spread".padEnd(10) +
|
|
151
|
+
"Annual".padEnd(10) +
|
|
152
|
+
"$/day".padEnd(10) +
|
|
153
|
+
"Short".padEnd(16) +
|
|
154
|
+
"Long".padEnd(16) +
|
|
155
|
+
"Consistency");
|
|
156
|
+
console.log(" " + "─".repeat(85));
|
|
157
|
+
for (const opp of opportunities) {
|
|
158
|
+
const spreadColor = opp.annualized >= 50 ? "\x1b[32m" : opp.annualized >= 20 ? "\x1b[33m" : "\x1b[90m";
|
|
159
|
+
const consistColor = opp.consistency !== undefined
|
|
160
|
+
? opp.consistency >= 80 ? "\x1b[32m" : opp.consistency >= 50 ? "\x1b[33m" : "\x1b[31m"
|
|
161
|
+
: "\x1b[90m";
|
|
162
|
+
const consistStr = opp.consistency !== undefined
|
|
163
|
+
? `${opp.consistency.toFixed(0)}% (${opp.samples})`
|
|
164
|
+
: "no data";
|
|
165
|
+
console.log(` ${opp.asset.padEnd(8)}` +
|
|
166
|
+
`${spreadColor}${opp.spread.toFixed(4)}%\x1b[0m`.padEnd(19) +
|
|
167
|
+
`${spreadColor}${opp.annualized.toFixed(1)}%\x1b[0m`.padEnd(19) +
|
|
168
|
+
`${spreadColor}$${opp.dailyOn10k.toFixed(2)}\x1b[0m`.padEnd(19) +
|
|
169
|
+
`${opp.shortExchange.substring(0, 12).padEnd(16)}` +
|
|
170
|
+
`${opp.longExchange.substring(0, 12).padEnd(16)}` +
|
|
171
|
+
`${consistColor}${consistStr}\x1b[0m`);
|
|
172
|
+
}
|
|
173
|
+
console.log(" " + "─".repeat(85));
|
|
174
|
+
// Summary
|
|
175
|
+
const bestOpp = opportunities[0];
|
|
176
|
+
if (bestOpp && bestOpp.annualized >= 20) {
|
|
177
|
+
console.log(`\n \x1b[32mBest:\x1b[0m ${bestOpp.asset} ${bestOpp.annualized.toFixed(1)}% | Short ${bestOpp.shortExchange} / Long ${bestOpp.longExchange}`);
|
|
178
|
+
}
|
|
179
|
+
if (showStored) {
|
|
180
|
+
console.log(` \x1b[90mData stored to database\x1b[0m`);
|
|
181
|
+
}
|
|
182
|
+
console.log("");
|
|
183
|
+
}
|
|
184
|
+
async function fetchFundingForAsset(asset, exchanges, isTestnet) {
|
|
185
|
+
const results = [];
|
|
186
|
+
const market = `${asset}-PERP`;
|
|
187
|
+
// Use rate-limited parallel execution
|
|
188
|
+
const tasks = exchanges.map((exchangeId) => async () => {
|
|
189
|
+
const adapter = getExchangeAdapterById(exchangeId);
|
|
190
|
+
let connected = false;
|
|
191
|
+
try {
|
|
192
|
+
await adapter.connect({ testnet: isTestnet });
|
|
193
|
+
connected = true;
|
|
194
|
+
const funding = await adapter.getFundingRate(market);
|
|
195
|
+
return {
|
|
196
|
+
exchange: adapter.info.name,
|
|
197
|
+
rate: parseFloat(funding.rate),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
if (connected) {
|
|
205
|
+
await adapter.disconnect().catch(() => undefined);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
const settled = await pLimit(tasks, 3); // Max 3 concurrent requests
|
|
210
|
+
for (const result of settled) {
|
|
211
|
+
if (result.status === "fulfilled" && result.value) {
|
|
212
|
+
results.push(result.value);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return results;
|
|
216
|
+
}
|