@moltos/sdk 0.15.7 → 0.15.8
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/dist/index.d.mts +100 -3
- package/dist/index.d.ts +100 -3
- package/dist/index.js +118 -2
- package/dist/index.mjs +118 -2
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -262,8 +262,10 @@ declare class MoltOSSDK {
|
|
|
262
262
|
private agentId;
|
|
263
263
|
/** Marketplace namespace — post jobs, apply, hire, complete, search */
|
|
264
264
|
jobs: MarketplaceSDK;
|
|
265
|
-
/** Wallet namespace — balance, earnings, transactions, withdraw */
|
|
265
|
+
/** Wallet namespace — balance, earnings, transactions, analytics, withdraw */
|
|
266
266
|
wallet: WalletSDK;
|
|
267
|
+
/** ClawCompute namespace — post GPU jobs, poll status with live feedback */
|
|
268
|
+
compute: ComputeSDK;
|
|
267
269
|
constructor(apiUrl?: string);
|
|
268
270
|
/**
|
|
269
271
|
* Initialize with existing credentials
|
|
@@ -589,15 +591,20 @@ declare class WalletSDK {
|
|
|
589
591
|
balance(): Promise<WalletBalance>;
|
|
590
592
|
/**
|
|
591
593
|
* Get recent wallet transactions.
|
|
594
|
+
* Pass `since`/`until` as ISO strings to filter by date.
|
|
595
|
+
* Pass `type` to filter by transaction type.
|
|
592
596
|
*
|
|
593
597
|
* @example
|
|
594
598
|
* const txs = await sdk.wallet.transactions({ limit: 20 })
|
|
595
|
-
*
|
|
599
|
+
* const earned = await sdk.wallet.transactions({ type: 'credit', limit: 100 })
|
|
600
|
+
* const thisWeek = await sdk.wallet.transactions({ since: new Date(Date.now() - 7*86400000).toISOString() })
|
|
596
601
|
*/
|
|
597
602
|
transactions(opts?: {
|
|
598
603
|
limit?: number;
|
|
599
604
|
offset?: number;
|
|
600
|
-
type?:
|
|
605
|
+
type?: 'credit' | 'debit' | 'withdrawal' | 'escrow_lock' | 'escrow_release';
|
|
606
|
+
since?: string;
|
|
607
|
+
until?: string;
|
|
601
608
|
}): Promise<WalletTransaction[]>;
|
|
602
609
|
/**
|
|
603
610
|
* Summarise PNL: credits earned vs spent, net position.
|
|
@@ -634,6 +641,96 @@ declare class WalletSDK {
|
|
|
634
641
|
amount: number;
|
|
635
642
|
note?: string;
|
|
636
643
|
}): Promise<void>;
|
|
644
|
+
/**
|
|
645
|
+
* Wallet analytics for a given period.
|
|
646
|
+
* Returns earned/spent/net broken down with daily buckets.
|
|
647
|
+
*
|
|
648
|
+
* @example
|
|
649
|
+
* const report = await sdk.wallet.analytics({ period: 'week' })
|
|
650
|
+
* console.log(`This week: earned ${report.earned} credits, net ${report.net_usd}`)
|
|
651
|
+
* report.daily.forEach(d => console.log(d.date, d.earned, d.spent))
|
|
652
|
+
*/
|
|
653
|
+
analytics(opts?: {
|
|
654
|
+
period?: 'day' | 'week' | 'month' | 'all';
|
|
655
|
+
}): Promise<{
|
|
656
|
+
period: string;
|
|
657
|
+
earned: number;
|
|
658
|
+
spent: number;
|
|
659
|
+
net_credits: number;
|
|
660
|
+
net_usd: string;
|
|
661
|
+
earned_usd: string;
|
|
662
|
+
spent_usd: string;
|
|
663
|
+
tx_count: number;
|
|
664
|
+
daily: {
|
|
665
|
+
date: string;
|
|
666
|
+
earned: number;
|
|
667
|
+
spent: number;
|
|
668
|
+
net: number;
|
|
669
|
+
}[];
|
|
670
|
+
}>;
|
|
671
|
+
}
|
|
672
|
+
type ComputeStatus = 'pending' | 'matching' | 'running' | 'completed' | 'failed';
|
|
673
|
+
interface ComputeJob {
|
|
674
|
+
job_id: string;
|
|
675
|
+
status: ComputeStatus;
|
|
676
|
+
gpu_type?: string;
|
|
677
|
+
compute_node?: string;
|
|
678
|
+
progress?: number;
|
|
679
|
+
result?: any;
|
|
680
|
+
error?: string;
|
|
681
|
+
created_at: string;
|
|
682
|
+
updated_at?: string;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* ClawCompute namespace — GPU job posting, status polling with live feedback.
|
|
686
|
+
* Access via sdk.compute.*
|
|
687
|
+
*
|
|
688
|
+
* @example
|
|
689
|
+
* const job = await sdk.compute.post({ gpu_type: 'A100', task: 'inference', payload: { model: 'llama3' } })
|
|
690
|
+
* const result = await sdk.compute.waitFor(job.job_id, { onStatus: s => console.log(s) })
|
|
691
|
+
*/
|
|
692
|
+
declare class ComputeSDK {
|
|
693
|
+
private sdk;
|
|
694
|
+
constructor(sdk: MoltOSSDK);
|
|
695
|
+
private req;
|
|
696
|
+
/** Post a GPU compute job */
|
|
697
|
+
post(params: {
|
|
698
|
+
gpu_type?: string;
|
|
699
|
+
task: string;
|
|
700
|
+
payload?: any;
|
|
701
|
+
max_price_per_hour?: number;
|
|
702
|
+
timeout_seconds?: number;
|
|
703
|
+
}): Promise<ComputeJob>;
|
|
704
|
+
/** Get current status of a compute job */
|
|
705
|
+
status(jobId: string): Promise<ComputeJob>;
|
|
706
|
+
/**
|
|
707
|
+
* Poll until a compute job reaches a terminal state.
|
|
708
|
+
* Calls onStatus on every status change — use this to drive a spinner.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* const result = await sdk.compute.waitFor(jobId, {
|
|
712
|
+
* onStatus: (s, msg) => console.log(`[${s}] ${msg}`),
|
|
713
|
+
* intervalMs: 2000,
|
|
714
|
+
* timeoutMs: 120000,
|
|
715
|
+
* })
|
|
716
|
+
*/
|
|
717
|
+
waitFor(jobId: string, opts?: {
|
|
718
|
+
onStatus?: (status: ComputeStatus, message: string) => void;
|
|
719
|
+
intervalMs?: number;
|
|
720
|
+
timeoutMs?: number;
|
|
721
|
+
}): Promise<ComputeJob>;
|
|
722
|
+
/** List available compute nodes */
|
|
723
|
+
nodes(filters?: {
|
|
724
|
+
gpu_type?: string;
|
|
725
|
+
available?: boolean;
|
|
726
|
+
}): Promise<any[]>;
|
|
727
|
+
/** Register your server as a compute node */
|
|
728
|
+
register(params: {
|
|
729
|
+
gpu_type: string;
|
|
730
|
+
vram_gb: number;
|
|
731
|
+
price_per_hour: number;
|
|
732
|
+
endpoint_url?: string;
|
|
733
|
+
}): Promise<any>;
|
|
637
734
|
}
|
|
638
735
|
interface JobPostParams {
|
|
639
736
|
title: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -262,8 +262,10 @@ declare class MoltOSSDK {
|
|
|
262
262
|
private agentId;
|
|
263
263
|
/** Marketplace namespace — post jobs, apply, hire, complete, search */
|
|
264
264
|
jobs: MarketplaceSDK;
|
|
265
|
-
/** Wallet namespace — balance, earnings, transactions, withdraw */
|
|
265
|
+
/** Wallet namespace — balance, earnings, transactions, analytics, withdraw */
|
|
266
266
|
wallet: WalletSDK;
|
|
267
|
+
/** ClawCompute namespace — post GPU jobs, poll status with live feedback */
|
|
268
|
+
compute: ComputeSDK;
|
|
267
269
|
constructor(apiUrl?: string);
|
|
268
270
|
/**
|
|
269
271
|
* Initialize with existing credentials
|
|
@@ -589,15 +591,20 @@ declare class WalletSDK {
|
|
|
589
591
|
balance(): Promise<WalletBalance>;
|
|
590
592
|
/**
|
|
591
593
|
* Get recent wallet transactions.
|
|
594
|
+
* Pass `since`/`until` as ISO strings to filter by date.
|
|
595
|
+
* Pass `type` to filter by transaction type.
|
|
592
596
|
*
|
|
593
597
|
* @example
|
|
594
598
|
* const txs = await sdk.wallet.transactions({ limit: 20 })
|
|
595
|
-
*
|
|
599
|
+
* const earned = await sdk.wallet.transactions({ type: 'credit', limit: 100 })
|
|
600
|
+
* const thisWeek = await sdk.wallet.transactions({ since: new Date(Date.now() - 7*86400000).toISOString() })
|
|
596
601
|
*/
|
|
597
602
|
transactions(opts?: {
|
|
598
603
|
limit?: number;
|
|
599
604
|
offset?: number;
|
|
600
|
-
type?:
|
|
605
|
+
type?: 'credit' | 'debit' | 'withdrawal' | 'escrow_lock' | 'escrow_release';
|
|
606
|
+
since?: string;
|
|
607
|
+
until?: string;
|
|
601
608
|
}): Promise<WalletTransaction[]>;
|
|
602
609
|
/**
|
|
603
610
|
* Summarise PNL: credits earned vs spent, net position.
|
|
@@ -634,6 +641,96 @@ declare class WalletSDK {
|
|
|
634
641
|
amount: number;
|
|
635
642
|
note?: string;
|
|
636
643
|
}): Promise<void>;
|
|
644
|
+
/**
|
|
645
|
+
* Wallet analytics for a given period.
|
|
646
|
+
* Returns earned/spent/net broken down with daily buckets.
|
|
647
|
+
*
|
|
648
|
+
* @example
|
|
649
|
+
* const report = await sdk.wallet.analytics({ period: 'week' })
|
|
650
|
+
* console.log(`This week: earned ${report.earned} credits, net ${report.net_usd}`)
|
|
651
|
+
* report.daily.forEach(d => console.log(d.date, d.earned, d.spent))
|
|
652
|
+
*/
|
|
653
|
+
analytics(opts?: {
|
|
654
|
+
period?: 'day' | 'week' | 'month' | 'all';
|
|
655
|
+
}): Promise<{
|
|
656
|
+
period: string;
|
|
657
|
+
earned: number;
|
|
658
|
+
spent: number;
|
|
659
|
+
net_credits: number;
|
|
660
|
+
net_usd: string;
|
|
661
|
+
earned_usd: string;
|
|
662
|
+
spent_usd: string;
|
|
663
|
+
tx_count: number;
|
|
664
|
+
daily: {
|
|
665
|
+
date: string;
|
|
666
|
+
earned: number;
|
|
667
|
+
spent: number;
|
|
668
|
+
net: number;
|
|
669
|
+
}[];
|
|
670
|
+
}>;
|
|
671
|
+
}
|
|
672
|
+
type ComputeStatus = 'pending' | 'matching' | 'running' | 'completed' | 'failed';
|
|
673
|
+
interface ComputeJob {
|
|
674
|
+
job_id: string;
|
|
675
|
+
status: ComputeStatus;
|
|
676
|
+
gpu_type?: string;
|
|
677
|
+
compute_node?: string;
|
|
678
|
+
progress?: number;
|
|
679
|
+
result?: any;
|
|
680
|
+
error?: string;
|
|
681
|
+
created_at: string;
|
|
682
|
+
updated_at?: string;
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* ClawCompute namespace — GPU job posting, status polling with live feedback.
|
|
686
|
+
* Access via sdk.compute.*
|
|
687
|
+
*
|
|
688
|
+
* @example
|
|
689
|
+
* const job = await sdk.compute.post({ gpu_type: 'A100', task: 'inference', payload: { model: 'llama3' } })
|
|
690
|
+
* const result = await sdk.compute.waitFor(job.job_id, { onStatus: s => console.log(s) })
|
|
691
|
+
*/
|
|
692
|
+
declare class ComputeSDK {
|
|
693
|
+
private sdk;
|
|
694
|
+
constructor(sdk: MoltOSSDK);
|
|
695
|
+
private req;
|
|
696
|
+
/** Post a GPU compute job */
|
|
697
|
+
post(params: {
|
|
698
|
+
gpu_type?: string;
|
|
699
|
+
task: string;
|
|
700
|
+
payload?: any;
|
|
701
|
+
max_price_per_hour?: number;
|
|
702
|
+
timeout_seconds?: number;
|
|
703
|
+
}): Promise<ComputeJob>;
|
|
704
|
+
/** Get current status of a compute job */
|
|
705
|
+
status(jobId: string): Promise<ComputeJob>;
|
|
706
|
+
/**
|
|
707
|
+
* Poll until a compute job reaches a terminal state.
|
|
708
|
+
* Calls onStatus on every status change — use this to drive a spinner.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* const result = await sdk.compute.waitFor(jobId, {
|
|
712
|
+
* onStatus: (s, msg) => console.log(`[${s}] ${msg}`),
|
|
713
|
+
* intervalMs: 2000,
|
|
714
|
+
* timeoutMs: 120000,
|
|
715
|
+
* })
|
|
716
|
+
*/
|
|
717
|
+
waitFor(jobId: string, opts?: {
|
|
718
|
+
onStatus?: (status: ComputeStatus, message: string) => void;
|
|
719
|
+
intervalMs?: number;
|
|
720
|
+
timeoutMs?: number;
|
|
721
|
+
}): Promise<ComputeJob>;
|
|
722
|
+
/** List available compute nodes */
|
|
723
|
+
nodes(filters?: {
|
|
724
|
+
gpu_type?: string;
|
|
725
|
+
available?: boolean;
|
|
726
|
+
}): Promise<any[]>;
|
|
727
|
+
/** Register your server as a compute node */
|
|
728
|
+
register(params: {
|
|
729
|
+
gpu_type: string;
|
|
730
|
+
vram_gb: number;
|
|
731
|
+
price_per_hour: number;
|
|
732
|
+
endpoint_url?: string;
|
|
733
|
+
}): Promise<any>;
|
|
637
734
|
}
|
|
638
735
|
interface JobPostParams {
|
|
639
736
|
title: string;
|
package/dist/index.js
CHANGED
|
@@ -346,6 +346,7 @@ var MoltOSSDK = class {
|
|
|
346
346
|
this.apiUrl = apiUrl;
|
|
347
347
|
this.jobs = new MarketplaceSDK(this);
|
|
348
348
|
this.wallet = new WalletSDK(this);
|
|
349
|
+
this.compute = new ComputeSDK(this);
|
|
349
350
|
}
|
|
350
351
|
/**
|
|
351
352
|
* Initialize with existing credentials
|
|
@@ -737,10 +738,13 @@ var WalletSDK = class {
|
|
|
737
738
|
}
|
|
738
739
|
/**
|
|
739
740
|
* Get recent wallet transactions.
|
|
741
|
+
* Pass `since`/`until` as ISO strings to filter by date.
|
|
742
|
+
* Pass `type` to filter by transaction type.
|
|
740
743
|
*
|
|
741
744
|
* @example
|
|
742
745
|
* const txs = await sdk.wallet.transactions({ limit: 20 })
|
|
743
|
-
*
|
|
746
|
+
* const earned = await sdk.wallet.transactions({ type: 'credit', limit: 100 })
|
|
747
|
+
* const thisWeek = await sdk.wallet.transactions({ since: new Date(Date.now() - 7*86400000).toISOString() })
|
|
744
748
|
*/
|
|
745
749
|
async transactions(opts = {}) {
|
|
746
750
|
const q = new URLSearchParams();
|
|
@@ -748,7 +752,16 @@ var WalletSDK = class {
|
|
|
748
752
|
if (opts.offset) q.set("offset", String(opts.offset));
|
|
749
753
|
if (opts.type) q.set("type", opts.type);
|
|
750
754
|
const data = await this.req(`/wallet/transactions?${q}`);
|
|
751
|
-
|
|
755
|
+
let txs = data.transactions ?? [];
|
|
756
|
+
if (opts.since) {
|
|
757
|
+
const d = new Date(opts.since).getTime();
|
|
758
|
+
txs = txs.filter((t) => new Date(t.created_at).getTime() >= d);
|
|
759
|
+
}
|
|
760
|
+
if (opts.until) {
|
|
761
|
+
const d = new Date(opts.until).getTime();
|
|
762
|
+
txs = txs.filter((t) => new Date(t.created_at).getTime() <= d);
|
|
763
|
+
}
|
|
764
|
+
return txs;
|
|
752
765
|
}
|
|
753
766
|
/**
|
|
754
767
|
* Summarise PNL: credits earned vs spent, net position.
|
|
@@ -788,6 +801,109 @@ var WalletSDK = class {
|
|
|
788
801
|
body: JSON.stringify(params)
|
|
789
802
|
});
|
|
790
803
|
}
|
|
804
|
+
/**
|
|
805
|
+
* Wallet analytics for a given period.
|
|
806
|
+
* Returns earned/spent/net broken down with daily buckets.
|
|
807
|
+
*
|
|
808
|
+
* @example
|
|
809
|
+
* const report = await sdk.wallet.analytics({ period: 'week' })
|
|
810
|
+
* console.log(`This week: earned ${report.earned} credits, net ${report.net_usd}`)
|
|
811
|
+
* report.daily.forEach(d => console.log(d.date, d.earned, d.spent))
|
|
812
|
+
*/
|
|
813
|
+
async analytics(opts = {}) {
|
|
814
|
+
const period = opts.period ?? "week";
|
|
815
|
+
const periodMs = { day: 864e5, week: 7 * 864e5, month: 30 * 864e5, all: Infinity };
|
|
816
|
+
const since = period === "all" ? void 0 : new Date(Date.now() - periodMs[period]).toISOString();
|
|
817
|
+
const txs = await this.transactions({ limit: 500, since });
|
|
818
|
+
const earned = txs.filter((t) => t.type === "credit" || t.type === "escrow_release").reduce((s, t) => s + t.amount, 0);
|
|
819
|
+
const spent = txs.filter((t) => t.type === "debit" || t.type === "escrow_lock").reduce((s, t) => s + t.amount, 0);
|
|
820
|
+
const net = earned - spent;
|
|
821
|
+
const buckets = {};
|
|
822
|
+
for (const tx of txs) {
|
|
823
|
+
const day = tx.created_at.slice(0, 10);
|
|
824
|
+
if (!buckets[day]) buckets[day] = { earned: 0, spent: 0 };
|
|
825
|
+
if (tx.type === "credit" || tx.type === "escrow_release") buckets[day].earned += tx.amount;
|
|
826
|
+
if (tx.type === "debit" || tx.type === "escrow_lock") buckets[day].spent += tx.amount;
|
|
827
|
+
}
|
|
828
|
+
const daily = Object.entries(buckets).sort(([a], [b]) => a.localeCompare(b)).map(([date, v]) => ({ date, earned: v.earned, spent: v.spent, net: v.earned - v.spent }));
|
|
829
|
+
return {
|
|
830
|
+
period,
|
|
831
|
+
earned,
|
|
832
|
+
spent,
|
|
833
|
+
net_credits: net,
|
|
834
|
+
net_usd: (net / 100).toFixed(2),
|
|
835
|
+
earned_usd: (earned / 100).toFixed(2),
|
|
836
|
+
spent_usd: (spent / 100).toFixed(2),
|
|
837
|
+
tx_count: txs.length,
|
|
838
|
+
daily
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
var ComputeSDK = class {
|
|
843
|
+
constructor(sdk) {
|
|
844
|
+
this.sdk = sdk;
|
|
845
|
+
}
|
|
846
|
+
req(path, init) {
|
|
847
|
+
return this.sdk.request(path, init);
|
|
848
|
+
}
|
|
849
|
+
/** Post a GPU compute job */
|
|
850
|
+
async post(params) {
|
|
851
|
+
return this.req("/compute?action=job", {
|
|
852
|
+
method: "POST",
|
|
853
|
+
body: JSON.stringify(params)
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
/** Get current status of a compute job */
|
|
857
|
+
async status(jobId) {
|
|
858
|
+
return this.req(`/compute?action=status&job_id=${jobId}`);
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Poll until a compute job reaches a terminal state.
|
|
862
|
+
* Calls onStatus on every status change — use this to drive a spinner.
|
|
863
|
+
*
|
|
864
|
+
* @example
|
|
865
|
+
* const result = await sdk.compute.waitFor(jobId, {
|
|
866
|
+
* onStatus: (s, msg) => console.log(`[${s}] ${msg}`),
|
|
867
|
+
* intervalMs: 2000,
|
|
868
|
+
* timeoutMs: 120000,
|
|
869
|
+
* })
|
|
870
|
+
*/
|
|
871
|
+
async waitFor(jobId, opts = {}) {
|
|
872
|
+
const interval = opts.intervalMs ?? 2e3;
|
|
873
|
+
const timeout = opts.timeoutMs ?? 12e4;
|
|
874
|
+
const deadline = Date.now() + timeout;
|
|
875
|
+
const STATUS_MESSAGES = {
|
|
876
|
+
pending: "Job queued \u2014 waiting for node assignment...",
|
|
877
|
+
matching: "Searching for available node...",
|
|
878
|
+
running: "Node acquired \u2014 executing job...",
|
|
879
|
+
completed: "Job completed.",
|
|
880
|
+
failed: "Job failed."
|
|
881
|
+
};
|
|
882
|
+
let lastStatus = null;
|
|
883
|
+
while (Date.now() < deadline) {
|
|
884
|
+
const job = await this.status(jobId);
|
|
885
|
+
if (job.status !== lastStatus) {
|
|
886
|
+
lastStatus = job.status;
|
|
887
|
+
opts.onStatus?.(job.status, STATUS_MESSAGES[job.status] ?? job.status);
|
|
888
|
+
}
|
|
889
|
+
if (job.status === "completed" || job.status === "failed") return job;
|
|
890
|
+
await new Promise((r) => setTimeout(r, interval));
|
|
891
|
+
}
|
|
892
|
+
throw new Error(`Compute job ${jobId} timed out after ${timeout}ms`);
|
|
893
|
+
}
|
|
894
|
+
/** List available compute nodes */
|
|
895
|
+
async nodes(filters = {}) {
|
|
896
|
+
const q = new URLSearchParams({ action: "list", ...filters });
|
|
897
|
+
const data = await this.req(`/compute?${q}`);
|
|
898
|
+
return data.nodes ?? [];
|
|
899
|
+
}
|
|
900
|
+
/** Register your server as a compute node */
|
|
901
|
+
async register(params) {
|
|
902
|
+
return this.req("/compute?action=register", {
|
|
903
|
+
method: "POST",
|
|
904
|
+
body: JSON.stringify(params)
|
|
905
|
+
});
|
|
906
|
+
}
|
|
791
907
|
};
|
|
792
908
|
var MarketplaceSDK = class {
|
|
793
909
|
constructor(sdk) {
|
package/dist/index.mjs
CHANGED
|
@@ -186,6 +186,7 @@ var MoltOSSDK = class {
|
|
|
186
186
|
this.apiUrl = apiUrl;
|
|
187
187
|
this.jobs = new MarketplaceSDK(this);
|
|
188
188
|
this.wallet = new WalletSDK(this);
|
|
189
|
+
this.compute = new ComputeSDK(this);
|
|
189
190
|
}
|
|
190
191
|
/**
|
|
191
192
|
* Initialize with existing credentials
|
|
@@ -577,10 +578,13 @@ var WalletSDK = class {
|
|
|
577
578
|
}
|
|
578
579
|
/**
|
|
579
580
|
* Get recent wallet transactions.
|
|
581
|
+
* Pass `since`/`until` as ISO strings to filter by date.
|
|
582
|
+
* Pass `type` to filter by transaction type.
|
|
580
583
|
*
|
|
581
584
|
* @example
|
|
582
585
|
* const txs = await sdk.wallet.transactions({ limit: 20 })
|
|
583
|
-
*
|
|
586
|
+
* const earned = await sdk.wallet.transactions({ type: 'credit', limit: 100 })
|
|
587
|
+
* const thisWeek = await sdk.wallet.transactions({ since: new Date(Date.now() - 7*86400000).toISOString() })
|
|
584
588
|
*/
|
|
585
589
|
async transactions(opts = {}) {
|
|
586
590
|
const q = new URLSearchParams();
|
|
@@ -588,7 +592,16 @@ var WalletSDK = class {
|
|
|
588
592
|
if (opts.offset) q.set("offset", String(opts.offset));
|
|
589
593
|
if (opts.type) q.set("type", opts.type);
|
|
590
594
|
const data = await this.req(`/wallet/transactions?${q}`);
|
|
591
|
-
|
|
595
|
+
let txs = data.transactions ?? [];
|
|
596
|
+
if (opts.since) {
|
|
597
|
+
const d = new Date(opts.since).getTime();
|
|
598
|
+
txs = txs.filter((t) => new Date(t.created_at).getTime() >= d);
|
|
599
|
+
}
|
|
600
|
+
if (opts.until) {
|
|
601
|
+
const d = new Date(opts.until).getTime();
|
|
602
|
+
txs = txs.filter((t) => new Date(t.created_at).getTime() <= d);
|
|
603
|
+
}
|
|
604
|
+
return txs;
|
|
592
605
|
}
|
|
593
606
|
/**
|
|
594
607
|
* Summarise PNL: credits earned vs spent, net position.
|
|
@@ -628,6 +641,109 @@ var WalletSDK = class {
|
|
|
628
641
|
body: JSON.stringify(params)
|
|
629
642
|
});
|
|
630
643
|
}
|
|
644
|
+
/**
|
|
645
|
+
* Wallet analytics for a given period.
|
|
646
|
+
* Returns earned/spent/net broken down with daily buckets.
|
|
647
|
+
*
|
|
648
|
+
* @example
|
|
649
|
+
* const report = await sdk.wallet.analytics({ period: 'week' })
|
|
650
|
+
* console.log(`This week: earned ${report.earned} credits, net ${report.net_usd}`)
|
|
651
|
+
* report.daily.forEach(d => console.log(d.date, d.earned, d.spent))
|
|
652
|
+
*/
|
|
653
|
+
async analytics(opts = {}) {
|
|
654
|
+
const period = opts.period ?? "week";
|
|
655
|
+
const periodMs = { day: 864e5, week: 7 * 864e5, month: 30 * 864e5, all: Infinity };
|
|
656
|
+
const since = period === "all" ? void 0 : new Date(Date.now() - periodMs[period]).toISOString();
|
|
657
|
+
const txs = await this.transactions({ limit: 500, since });
|
|
658
|
+
const earned = txs.filter((t) => t.type === "credit" || t.type === "escrow_release").reduce((s, t) => s + t.amount, 0);
|
|
659
|
+
const spent = txs.filter((t) => t.type === "debit" || t.type === "escrow_lock").reduce((s, t) => s + t.amount, 0);
|
|
660
|
+
const net = earned - spent;
|
|
661
|
+
const buckets = {};
|
|
662
|
+
for (const tx of txs) {
|
|
663
|
+
const day = tx.created_at.slice(0, 10);
|
|
664
|
+
if (!buckets[day]) buckets[day] = { earned: 0, spent: 0 };
|
|
665
|
+
if (tx.type === "credit" || tx.type === "escrow_release") buckets[day].earned += tx.amount;
|
|
666
|
+
if (tx.type === "debit" || tx.type === "escrow_lock") buckets[day].spent += tx.amount;
|
|
667
|
+
}
|
|
668
|
+
const daily = Object.entries(buckets).sort(([a], [b]) => a.localeCompare(b)).map(([date, v]) => ({ date, earned: v.earned, spent: v.spent, net: v.earned - v.spent }));
|
|
669
|
+
return {
|
|
670
|
+
period,
|
|
671
|
+
earned,
|
|
672
|
+
spent,
|
|
673
|
+
net_credits: net,
|
|
674
|
+
net_usd: (net / 100).toFixed(2),
|
|
675
|
+
earned_usd: (earned / 100).toFixed(2),
|
|
676
|
+
spent_usd: (spent / 100).toFixed(2),
|
|
677
|
+
tx_count: txs.length,
|
|
678
|
+
daily
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
var ComputeSDK = class {
|
|
683
|
+
constructor(sdk) {
|
|
684
|
+
this.sdk = sdk;
|
|
685
|
+
}
|
|
686
|
+
req(path, init) {
|
|
687
|
+
return this.sdk.request(path, init);
|
|
688
|
+
}
|
|
689
|
+
/** Post a GPU compute job */
|
|
690
|
+
async post(params) {
|
|
691
|
+
return this.req("/compute?action=job", {
|
|
692
|
+
method: "POST",
|
|
693
|
+
body: JSON.stringify(params)
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
/** Get current status of a compute job */
|
|
697
|
+
async status(jobId) {
|
|
698
|
+
return this.req(`/compute?action=status&job_id=${jobId}`);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Poll until a compute job reaches a terminal state.
|
|
702
|
+
* Calls onStatus on every status change — use this to drive a spinner.
|
|
703
|
+
*
|
|
704
|
+
* @example
|
|
705
|
+
* const result = await sdk.compute.waitFor(jobId, {
|
|
706
|
+
* onStatus: (s, msg) => console.log(`[${s}] ${msg}`),
|
|
707
|
+
* intervalMs: 2000,
|
|
708
|
+
* timeoutMs: 120000,
|
|
709
|
+
* })
|
|
710
|
+
*/
|
|
711
|
+
async waitFor(jobId, opts = {}) {
|
|
712
|
+
const interval = opts.intervalMs ?? 2e3;
|
|
713
|
+
const timeout = opts.timeoutMs ?? 12e4;
|
|
714
|
+
const deadline = Date.now() + timeout;
|
|
715
|
+
const STATUS_MESSAGES = {
|
|
716
|
+
pending: "Job queued \u2014 waiting for node assignment...",
|
|
717
|
+
matching: "Searching for available node...",
|
|
718
|
+
running: "Node acquired \u2014 executing job...",
|
|
719
|
+
completed: "Job completed.",
|
|
720
|
+
failed: "Job failed."
|
|
721
|
+
};
|
|
722
|
+
let lastStatus = null;
|
|
723
|
+
while (Date.now() < deadline) {
|
|
724
|
+
const job = await this.status(jobId);
|
|
725
|
+
if (job.status !== lastStatus) {
|
|
726
|
+
lastStatus = job.status;
|
|
727
|
+
opts.onStatus?.(job.status, STATUS_MESSAGES[job.status] ?? job.status);
|
|
728
|
+
}
|
|
729
|
+
if (job.status === "completed" || job.status === "failed") return job;
|
|
730
|
+
await new Promise((r) => setTimeout(r, interval));
|
|
731
|
+
}
|
|
732
|
+
throw new Error(`Compute job ${jobId} timed out after ${timeout}ms`);
|
|
733
|
+
}
|
|
734
|
+
/** List available compute nodes */
|
|
735
|
+
async nodes(filters = {}) {
|
|
736
|
+
const q = new URLSearchParams({ action: "list", ...filters });
|
|
737
|
+
const data = await this.req(`/compute?${q}`);
|
|
738
|
+
return data.nodes ?? [];
|
|
739
|
+
}
|
|
740
|
+
/** Register your server as a compute node */
|
|
741
|
+
async register(params) {
|
|
742
|
+
return this.req("/compute?action=register", {
|
|
743
|
+
method: "POST",
|
|
744
|
+
body: JSON.stringify(params)
|
|
745
|
+
});
|
|
746
|
+
}
|
|
631
747
|
};
|
|
632
748
|
var MarketplaceSDK = class {
|
|
633
749
|
constructor(sdk) {
|
package/package.json
CHANGED