@agentbadge/database 0.5.4 → 0.6.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.
@@ -0,0 +1,105 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ import type { Models } from "../prisma/contract.d.js";
3
+ import type { PrismaDb } from "./event-repository.js";
4
+ type CardRow = Models.public_BoardCard;
5
+ type EntryRow = Models.public_BoardEntry;
6
+ type ReputationRow = Models.public_ReputationWrite;
7
+ type OutboxRow = Models.public_ChainTxOutbox;
8
+ export interface BoardCardInsert {
9
+ bountyId: string;
10
+ column: string;
11
+ title: string;
12
+ budgetUsdc?: string | null;
13
+ specHash: string;
14
+ createdBy?: string | null;
15
+ currentPassportId?: string | null;
16
+ fixDeadlineMs?: bigint | null;
17
+ payload: JsonValue;
18
+ }
19
+ export interface BoardCardPatch {
20
+ column?: string;
21
+ currentPassportId?: string | null;
22
+ fixDeadlineMs?: bigint | null;
23
+ payload?: JsonValue;
24
+ }
25
+ export interface BoardEntryInsert {
26
+ cardId: string;
27
+ seq: number;
28
+ attempt?: number;
29
+ kind: string;
30
+ actor: string;
31
+ txHash?: string | null;
32
+ payload: JsonValue;
33
+ atMs: bigint;
34
+ }
35
+ export interface ReputationWriteInsert {
36
+ sourceType: string;
37
+ sourceId: string;
38
+ jobAttempt?: number;
39
+ passportId: string;
40
+ agentId?: string | null;
41
+ score: number;
42
+ status?: string;
43
+ feedbackUri?: string | null;
44
+ feedbackHash?: string | null;
45
+ txHash?: string | null;
46
+ attempts?: number;
47
+ payload: JsonValue;
48
+ }
49
+ export interface ReputationWritePatch {
50
+ status?: string;
51
+ agentId?: string | null;
52
+ txHash?: string | null;
53
+ feedbackUri?: string | null;
54
+ feedbackHash?: string | null;
55
+ attempts?: number;
56
+ payload?: JsonValue;
57
+ }
58
+ export interface ChainTxOutboxInsert {
59
+ kind: string;
60
+ cardId?: string | null;
61
+ jobAttempt?: number | null;
62
+ idempotencyKey: string;
63
+ status?: string;
64
+ txHash?: string | null;
65
+ attempts?: number;
66
+ lastError?: string | null;
67
+ payload: JsonValue;
68
+ }
69
+ export interface ChainTxOutboxPatch {
70
+ status?: string;
71
+ txHash?: string | null;
72
+ attempts?: number;
73
+ lastError?: string | null;
74
+ payload?: JsonValue;
75
+ }
76
+ export declare class BoardRepository {
77
+ private readonly db;
78
+ constructor(db: PrismaDb);
79
+ insertCard(cardId: string, data: BoardCardInsert): Promise<CardRow>;
80
+ getCard(cardId: string): Promise<CardRow | null>;
81
+ getCardByBounty(bountyId: string): Promise<CardRow | null>;
82
+ listCards(opts?: {
83
+ column?: string;
84
+ currentPassportId?: string;
85
+ limit?: number;
86
+ }): Promise<CardRow[]>;
87
+ listAllCards(): Promise<CardRow[]>;
88
+ listExpiredFix(nowMs: bigint): Promise<CardRow[]>;
89
+ patchCard(cardId: string, patch: BoardCardPatch): Promise<CardRow | null>;
90
+ insertEntry(id: string, data: BoardEntryInsert): Promise<EntryRow>;
91
+ listEntries(cardId: string): Promise<EntryRow[]>;
92
+ listAllEntries(): Promise<EntryRow[]>;
93
+ insertReputation(data: ReputationWriteInsert): Promise<ReputationRow>;
94
+ getReputation(sourceType: string, sourceId: string, jobAttempt: number): Promise<ReputationRow | null>;
95
+ listReputation(opts?: {
96
+ status?: string;
97
+ passportId?: string;
98
+ }): Promise<ReputationRow[]>;
99
+ patchReputation(id: string, patch: ReputationWritePatch): Promise<ReputationRow | null>;
100
+ enqueueOutbox(data: ChainTxOutboxInsert): Promise<OutboxRow>;
101
+ getOutboxByKey(idempotencyKey: string): Promise<OutboxRow | null>;
102
+ listOutboxByStatus(status: string): Promise<OutboxRow[]>;
103
+ patchOutbox(id: string, patch: ChainTxOutboxPatch): Promise<OutboxRow | null>;
104
+ }
105
+ export {};
@@ -0,0 +1,142 @@
1
+ export class BoardRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async insertCard(cardId, data) {
7
+ return this.db.orm.public.BoardCard.create({ cardId, ...data });
8
+ }
9
+ async getCard(cardId) {
10
+ return this.db.orm.public.BoardCard.where({ cardId }).first();
11
+ }
12
+ async getCardByBounty(bountyId) {
13
+ return this.db.orm.public.BoardCard.where({ bountyId }).first();
14
+ }
15
+ async listCards(opts) {
16
+ let q = this.db.orm.public.BoardCard.orderBy((c) => c.createdAt.desc());
17
+ if (opts?.column !== undefined)
18
+ q = q.where({ column: opts.column });
19
+ if (opts?.currentPassportId !== undefined) {
20
+ q = q.where({ currentPassportId: opts.currentPassportId });
21
+ }
22
+ if (opts?.limit !== undefined)
23
+ q = q.limit(opts.limit);
24
+ return q.all();
25
+ }
26
+ async listAllCards() {
27
+ return this.db.orm.public.BoardCard.all();
28
+ }
29
+ async listExpiredFix(nowMs) {
30
+ const buggy = await this.db.orm.public.BoardCard.where({
31
+ column: "bug",
32
+ }).all();
33
+ return buggy.filter((c) => c.fixDeadlineMs !== null && BigInt(c.fixDeadlineMs) < nowMs);
34
+ }
35
+ async patchCard(cardId, patch) {
36
+ const existing = await this.getCard(cardId);
37
+ if (!existing)
38
+ return null;
39
+ return this.db.orm.public.BoardCard.where({ cardId }).update({
40
+ column: patch.column ?? existing.column,
41
+ currentPassportId: patch.currentPassportId !== undefined
42
+ ? patch.currentPassportId
43
+ : existing.currentPassportId,
44
+ fixDeadlineMs: patch.fixDeadlineMs !== undefined
45
+ ? patch.fixDeadlineMs
46
+ : existing.fixDeadlineMs,
47
+ payload: patch.payload ?? existing.payload,
48
+ });
49
+ }
50
+ async insertEntry(id, data) {
51
+ return this.db.orm.public.BoardEntry.create({ id, ...data });
52
+ }
53
+ async listEntries(cardId) {
54
+ return this.db.orm.public.BoardEntry.where({ cardId })
55
+ .orderBy((e) => e.seq.asc())
56
+ .all();
57
+ }
58
+ async listAllEntries() {
59
+ return this.db.orm.public.BoardEntry.all();
60
+ }
61
+ async insertReputation(data) {
62
+ const existing = await this.getReputation(data.sourceType, data.sourceId, data.jobAttempt ?? 1);
63
+ if (existing)
64
+ return existing;
65
+ return this.db.orm.public.ReputationWrite.create({
66
+ status: "pending",
67
+ jobAttempt: 1,
68
+ ...data,
69
+ });
70
+ }
71
+ async getReputation(sourceType, sourceId, jobAttempt) {
72
+ return this.db.orm.public.ReputationWrite.where({
73
+ sourceType,
74
+ sourceId,
75
+ jobAttempt,
76
+ }).first();
77
+ }
78
+ async listReputation(opts) {
79
+ let q = this.db.orm.public.ReputationWrite.orderBy((r) => r.createdAt.desc());
80
+ if (opts?.status !== undefined)
81
+ q = q.where({ status: opts.status });
82
+ if (opts?.passportId !== undefined) {
83
+ q = q.where({ passportId: opts.passportId });
84
+ }
85
+ return q.all();
86
+ }
87
+ async patchReputation(id, patch) {
88
+ const existing = await this.db.orm.public.ReputationWrite.where({
89
+ id,
90
+ }).first();
91
+ if (!existing)
92
+ return null;
93
+ return this.db.orm.public.ReputationWrite.where({ id }).update({
94
+ status: patch.status ?? existing.status,
95
+ agentId: patch.agentId !== undefined ? patch.agentId : existing.agentId,
96
+ txHash: patch.txHash !== undefined ? patch.txHash : existing.txHash,
97
+ feedbackUri: patch.feedbackUri !== undefined
98
+ ? patch.feedbackUri
99
+ : existing.feedbackUri,
100
+ feedbackHash: patch.feedbackHash !== undefined
101
+ ? patch.feedbackHash
102
+ : existing.feedbackHash,
103
+ attempts: patch.attempts ?? existing.attempts,
104
+ payload: patch.payload ?? existing.payload,
105
+ });
106
+ }
107
+ async enqueueOutbox(data) {
108
+ const existing = await this.db.orm.public.ChainTxOutbox.where({
109
+ idempotencyKey: data.idempotencyKey,
110
+ }).first();
111
+ if (existing)
112
+ return existing;
113
+ return this.db.orm.public.ChainTxOutbox.create({
114
+ status: "pending",
115
+ ...data,
116
+ });
117
+ }
118
+ async getOutboxByKey(idempotencyKey) {
119
+ return this.db.orm.public.ChainTxOutbox.where({
120
+ idempotencyKey,
121
+ }).first();
122
+ }
123
+ async listOutboxByStatus(status) {
124
+ return this.db.orm.public.ChainTxOutbox.where({ status })
125
+ .orderBy((o) => o.createdAt.asc())
126
+ .all();
127
+ }
128
+ async patchOutbox(id, patch) {
129
+ const existing = await this.db.orm.public.ChainTxOutbox.where({
130
+ id,
131
+ }).first();
132
+ if (!existing)
133
+ return null;
134
+ return this.db.orm.public.ChainTxOutbox.where({ id }).update({
135
+ status: patch.status ?? existing.status,
136
+ txHash: patch.txHash !== undefined ? patch.txHash : existing.txHash,
137
+ attempts: patch.attempts ?? existing.attempts,
138
+ lastError: patch.lastError !== undefined ? patch.lastError : existing.lastError,
139
+ payload: patch.payload ?? existing.payload,
140
+ });
141
+ }
142
+ }
@@ -0,0 +1,81 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ import type { Models } from "../prisma/contract.d.js";
3
+ import type { PrismaDb } from "./event-repository.js";
4
+ type BountyRow = Models.public_Bounty;
5
+ type ClaimRow = Models.public_BountyClaim;
6
+ type TransitionRow = Models.public_BountyTransition;
7
+ type OutcomeRow = Models.public_BountyOutcome;
8
+ type PassportOwnerRow = Models.public_PassportOwner;
9
+ export interface BountyUpsert {
10
+ status: string;
11
+ onchainJobId?: string | null;
12
+ jobAttempt?: number;
13
+ budgetUsdc?: string | null;
14
+ createdBy?: string | null;
15
+ payload: JsonValue;
16
+ }
17
+ export interface BountyClaimInsert {
18
+ bountyId: string;
19
+ jobAttempt: number;
20
+ passportId: string;
21
+ payoutWallet: string;
22
+ claimedAtMs: bigint;
23
+ leaseUntilMs: bigint;
24
+ state?: string;
25
+ payload: JsonValue;
26
+ }
27
+ export interface BountyTransitionInsert {
28
+ bountyId: string;
29
+ actor: string;
30
+ from: string;
31
+ to: string;
32
+ reason?: string | null;
33
+ atMs: bigint;
34
+ payload: JsonValue;
35
+ }
36
+ export interface BountyOutcomeInsert {
37
+ bountyId: string;
38
+ jobAttempt: number;
39
+ passportId?: string | null;
40
+ verdict: string;
41
+ reasonCodes?: JsonValue | null;
42
+ verdictMemoTx?: string | null;
43
+ verdictRejectTx?: string | null;
44
+ commitSha?: string | null;
45
+ payload: JsonValue;
46
+ }
47
+ export interface PassportOwnerUpsert {
48
+ owner: string;
49
+ blockNumber: bigint;
50
+ logIndex?: number | null;
51
+ payload: JsonValue;
52
+ }
53
+ export declare class BountyRepository {
54
+ private readonly db;
55
+ constructor(db: PrismaDb);
56
+ upsertBounty(bountyId: string, data: BountyUpsert): Promise<BountyRow>;
57
+ getBounty(bountyId: string): Promise<BountyRow | null>;
58
+ listBounties(opts?: {
59
+ status?: string;
60
+ limit?: number;
61
+ offset?: number;
62
+ }): Promise<BountyRow[]>;
63
+ listAllBounties(): Promise<BountyRow[]>;
64
+ insertClaim(data: BountyClaimInsert): Promise<ClaimRow>;
65
+ getClaim(bountyId: string, jobAttempt: number): Promise<ClaimRow | null>;
66
+ listExpiredClaims(nowMs: bigint): Promise<ClaimRow[]>;
67
+ listClaimsByPassport(passportId: string): Promise<ClaimRow[]>;
68
+ listAllClaims(): Promise<ClaimRow[]>;
69
+ patchClaimState(id: string, state: string): Promise<ClaimRow | null>;
70
+ insertTransition(data: BountyTransitionInsert): Promise<TransitionRow>;
71
+ listTransitions(bountyId: string): Promise<TransitionRow[]>;
72
+ listAllTransitions(): Promise<TransitionRow[]>;
73
+ insertOutcome(data: BountyOutcomeInsert): Promise<OutcomeRow>;
74
+ listOutcomesByPassport(passportId: string): Promise<OutcomeRow[]>;
75
+ listAllOutcomes(): Promise<OutcomeRow[]>;
76
+ upsertPassportOwner(passportId: string, data: PassportOwnerUpsert): Promise<PassportOwnerRow>;
77
+ getPassportOwner(passportId: string): Promise<PassportOwnerRow | null>;
78
+ listPassportsByOwner(owner: string): Promise<PassportOwnerRow[]>;
79
+ listAllPassportOwners(): Promise<PassportOwnerRow[]>;
80
+ }
81
+ export {};
@@ -0,0 +1,111 @@
1
+ export class BountyRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async upsertBounty(bountyId, data) {
7
+ return this.db.orm.public.Bounty.upsert({
8
+ create: { bountyId, ...data },
9
+ update: { ...data },
10
+ conflictOn: { bountyId },
11
+ });
12
+ }
13
+ async getBounty(bountyId) {
14
+ return this.db.orm.public.Bounty.where({ bountyId }).first();
15
+ }
16
+ async listBounties(opts) {
17
+ let q = this.db.orm.public.Bounty.orderBy((b) => b.createdAt.desc());
18
+ if (opts?.status !== undefined)
19
+ q = q.where({ status: opts.status });
20
+ if (opts?.offset !== undefined)
21
+ q = q.offset(opts.offset);
22
+ if (opts?.limit !== undefined)
23
+ q = q.limit(opts.limit);
24
+ return q.all();
25
+ }
26
+ async listAllBounties() {
27
+ return this.db.orm.public.Bounty.all();
28
+ }
29
+ async insertClaim(data) {
30
+ return this.db.orm.public.BountyClaim.upsert({
31
+ create: { state: "open", ...data },
32
+ update: { state: "open", ...data },
33
+ conflictOn: { bountyId: data.bountyId, jobAttempt: data.jobAttempt },
34
+ });
35
+ }
36
+ async getClaim(bountyId, jobAttempt) {
37
+ return this.db.orm.public.BountyClaim.where({
38
+ bountyId,
39
+ jobAttempt,
40
+ }).first();
41
+ }
42
+ async listExpiredClaims(nowMs) {
43
+ const open = await this.db.orm.public.BountyClaim.where({
44
+ state: "open",
45
+ }).all();
46
+ return open.filter((c) => BigInt(c.leaseUntilMs) < nowMs);
47
+ }
48
+ async listClaimsByPassport(passportId) {
49
+ return this.db.orm.public.BountyClaim.where({ passportId })
50
+ .orderBy((c) => c.createdAt.desc())
51
+ .all();
52
+ }
53
+ async listAllClaims() {
54
+ return this.db.orm.public.BountyClaim.all();
55
+ }
56
+ async patchClaimState(id, state) {
57
+ return this.db.orm.public.BountyClaim.where({ id }).update({
58
+ state,
59
+ });
60
+ }
61
+ async insertTransition(data) {
62
+ return this.db.orm.public.BountyTransition.create(data);
63
+ }
64
+ async listTransitions(bountyId) {
65
+ return this.db.orm.public.BountyTransition.where({ bountyId })
66
+ .orderBy((t) => t.atMs.asc())
67
+ .all();
68
+ }
69
+ async listAllTransitions() {
70
+ return this.db.orm.public.BountyTransition.all();
71
+ }
72
+ async insertOutcome(data) {
73
+ return this.db.orm.public.BountyOutcome.upsert({
74
+ create: { ...data },
75
+ update: { ...data },
76
+ conflictOn: { bountyId: data.bountyId, jobAttempt: data.jobAttempt },
77
+ });
78
+ }
79
+ async listOutcomesByPassport(passportId) {
80
+ return this.db.orm.public.BountyOutcome.where({ passportId })
81
+ .orderBy((o) => o.createdAt.desc())
82
+ .all();
83
+ }
84
+ async listAllOutcomes() {
85
+ return this.db.orm.public.BountyOutcome.all();
86
+ }
87
+ async upsertPassportOwner(passportId, data) {
88
+ const existing = await this.db.orm.public.PassportOwner.where({
89
+ passportId,
90
+ }).first();
91
+ if (existing && BigInt(existing.blockNumber) >= BigInt(data.blockNumber)) {
92
+ return existing;
93
+ }
94
+ return this.db.orm.public.PassportOwner.upsert({
95
+ create: { passportId, ...data },
96
+ update: { ...data },
97
+ conflictOn: { passportId },
98
+ });
99
+ }
100
+ async getPassportOwner(passportId) {
101
+ return this.db.orm.public.PassportOwner.where({
102
+ passportId,
103
+ }).first();
104
+ }
105
+ async listPassportsByOwner(owner) {
106
+ return this.db.orm.public.PassportOwner.where({ owner }).all();
107
+ }
108
+ async listAllPassportOwners() {
109
+ return this.db.orm.public.PassportOwner.all();
110
+ }
111
+ }
@@ -0,0 +1,8 @@
1
+ import type { PrismaDb } from "./event-repository.js";
2
+ import type { ForensicsCursorStore } from "./forensics-store.js";
3
+ export declare class ForensicsCursorRepository implements ForensicsCursorStore {
4
+ private readonly db;
5
+ constructor(db: PrismaDb);
6
+ get(chain: string): Promise<bigint | null>;
7
+ set(chain: string, lastBlock: bigint): Promise<void>;
8
+ }
@@ -0,0 +1,25 @@
1
+ export class ForensicsCursorRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async get(chain) {
7
+ const r = await this.db.orm.public.ForensicsCursor.where({ chain }).first();
8
+ return r ? BigInt(r.lastBlock) : null;
9
+ }
10
+ async set(chain, lastBlock) {
11
+ const existing = await this.db.orm.public.ForensicsCursor.where({
12
+ chain,
13
+ }).first();
14
+ if (existing) {
15
+ await this.db.orm.public.ForensicsCursor.where({ chain }).update({
16
+ lastBlock: lastBlock,
17
+ });
18
+ return;
19
+ }
20
+ await this.db.orm.public.ForensicsCursor.create({
21
+ chain,
22
+ lastBlock: lastBlock,
23
+ });
24
+ }
25
+ }
@@ -0,0 +1,15 @@
1
+ import type { PrismaDb } from "./event-repository.js";
2
+ import type { ForensicsEdgeInsert, ForensicsEdgeRow, ForensicsEdgeStore } from "./forensics-store.js";
3
+ export declare class ForensicsEdgeRepository implements ForensicsEdgeStore {
4
+ private readonly db;
5
+ constructor(db: PrismaDb);
6
+ append(e: ForensicsEdgeInsert): Promise<ForensicsEdgeRow | null>;
7
+ appendBatch(es: ForensicsEdgeInsert[]): Promise<number>;
8
+ listBySeller(chain: string, seller: string, opts?: {
9
+ limit?: number;
10
+ fromBlock?: bigint;
11
+ }): Promise<ForensicsEdgeRow[]>;
12
+ listByPayer(chain: string, payer: string): Promise<ForensicsEdgeRow[]>;
13
+ countByChain(chain: string): Promise<number>;
14
+ private toRow;
15
+ }
@@ -0,0 +1,68 @@
1
+ export class ForensicsEdgeRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async append(e) {
7
+ const existing = await this.db.orm.public.ForensicsEdge.where({
8
+ chain: e.chain,
9
+ txHash: e.txHash,
10
+ payer: e.payer.toLowerCase(),
11
+ seller: e.seller.toLowerCase(),
12
+ }).first();
13
+ if (existing)
14
+ return null;
15
+ return this.toRow(await this.db.orm.public.ForensicsEdge.create({
16
+ chain: e.chain,
17
+ payer: e.payer.toLowerCase(),
18
+ seller: e.seller.toLowerCase(),
19
+ amountAtomic: e.amountAtomic,
20
+ txHash: e.txHash,
21
+ blockNumber: e.blockNumber,
22
+ source: e.source,
23
+ ts: e.ts,
24
+ }));
25
+ }
26
+ async appendBatch(es) {
27
+ let n = 0;
28
+ for (const e of es)
29
+ if ((await this.append(e)) !== null)
30
+ n++;
31
+ return n;
32
+ }
33
+ async listBySeller(chain, seller, opts) {
34
+ let q = this.db.orm.public.ForensicsEdge.orderBy((r) => r.blockNumber.asc()).where({ chain, seller: seller.toLowerCase() });
35
+ if (opts?.limit !== undefined)
36
+ q = q.limit(opts.limit);
37
+ const rows = await q.all();
38
+ const mapped = rows.map((r) => this.toRow(r));
39
+ return opts?.fromBlock !== undefined
40
+ ? mapped.filter((r) => r.blockNumber >= opts.fromBlock)
41
+ : mapped;
42
+ }
43
+ async listByPayer(chain, payer) {
44
+ const rows = await this.db.orm.public.ForensicsEdge.where({
45
+ chain,
46
+ payer: payer.toLowerCase(),
47
+ }).all();
48
+ return rows.map((r) => this.toRow(r));
49
+ }
50
+ async countByChain(chain) {
51
+ const rows = await this.db.orm.public.ForensicsEdge.where({ chain }).all();
52
+ return rows.length;
53
+ }
54
+ toRow(r) {
55
+ return {
56
+ id: r.id,
57
+ chain: r.chain,
58
+ payer: r.payer,
59
+ seller: r.seller,
60
+ amountAtomic: BigInt(r.amountAtomic),
61
+ txHash: r.txHash,
62
+ blockNumber: BigInt(r.blockNumber),
63
+ source: r.source,
64
+ ts: BigInt(r.ts),
65
+ createdAt: String(r.createdAt),
66
+ };
67
+ }
68
+ }
@@ -0,0 +1,11 @@
1
+ import type { PrismaDb } from "./event-repository.js";
2
+ import type { ForensicsFundingInsert, ForensicsFundingRow, ForensicsFundingStore } from "./forensics-store.js";
3
+ export declare class ForensicsFundingRepository implements ForensicsFundingStore {
4
+ private readonly db;
5
+ constructor(db: PrismaDb);
6
+ insertFirst(f: ForensicsFundingInsert): Promise<ForensicsFundingRow | null>;
7
+ getFirst(chain: string, funded: string): Promise<ForensicsFundingRow | null>;
8
+ listFundedBy(chain: string, funder: string): Promise<ForensicsFundingRow[]>;
9
+ countByChain(chain: string): Promise<number>;
10
+ private toRow;
11
+ }
@@ -0,0 +1,54 @@
1
+ export class ForensicsFundingRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async insertFirst(f) {
7
+ const existing = await this.db.orm.public.ForensicsFunding.where({
8
+ chain: f.chain,
9
+ funded: f.funded.toLowerCase(),
10
+ }).first();
11
+ if (existing)
12
+ return null;
13
+ return this.toRow(await this.db.orm.public.ForensicsFunding.create({
14
+ chain: f.chain,
15
+ funded: f.funded.toLowerCase(),
16
+ funder: f.funder.toLowerCase(),
17
+ txHash: f.txHash,
18
+ blockNumber: f.blockNumber,
19
+ isPublicSource: f.isPublicSource ?? false,
20
+ sourceKind: f.sourceKind ?? null,
21
+ }));
22
+ }
23
+ async getFirst(chain, funded) {
24
+ const r = await this.db.orm.public.ForensicsFunding.where({
25
+ chain,
26
+ funded: funded.toLowerCase(),
27
+ }).first();
28
+ return r ? this.toRow(r) : null;
29
+ }
30
+ async listFundedBy(chain, funder) {
31
+ const rows = await this.db.orm.public.ForensicsFunding.where({
32
+ chain,
33
+ funder: funder.toLowerCase(),
34
+ }).all();
35
+ return rows.map((r) => this.toRow(r));
36
+ }
37
+ async countByChain(chain) {
38
+ const rows = await this.db.orm.public.ForensicsFunding.where({ chain }).all();
39
+ return rows.length;
40
+ }
41
+ toRow(r) {
42
+ return {
43
+ id: r.id,
44
+ chain: r.chain,
45
+ funded: r.funded,
46
+ funder: r.funder,
47
+ txHash: r.txHash,
48
+ blockNumber: BigInt(r.blockNumber),
49
+ isPublicSource: r.isPublicSource,
50
+ sourceKind: r.sourceKind,
51
+ createdAt: String(r.createdAt),
52
+ };
53
+ }
54
+ }