-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAgentTransactionHistory.ts
More file actions
212 lines (194 loc) · 5.11 KB
/
Copy pathAgentTransactionHistory.ts
File metadata and controls
212 lines (194 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { gql, request } from 'graphql-request';
import { EvmChainId } from '@/constants/chains';
import {
getTransactionHistorySchemaRevision,
TRANSACTION_HISTORY_SUBGRAPH_URLS_BY_EVM_CHAIN,
} from '@/constants/urls';
import { Address } from '@/types/Address';
import {
AgentTransactionHistoryResponse,
AgentTransactionHistoryResponseSchema,
AgentTransactionHistoryResponseV2Schema,
} from '@/types/TransactionHistory';
// Deep import (not the '@/utils' barrel): config/chains pulls the barrel at
// module init, so barrel-importing here forms a cycle that breaks test loads.
import { normalizeAgentTransactionHistoryResponseV2 } from '@/utils/transactionHistory';
const FETCH_AGENT_TRANSACTION_HISTORY_QUERY = gql`
query GetAgentTransactionHistory(
$agentSafe: Bytes!
$first: Int!
$skip: Int!
) {
fundsMovements(
where: {
agentSafe: $agentSafe
category_in: [MASTER_TO_AGENT, AGENT_TO_MASTER]
}
orderBy: blockTimestamp
orderDirection: desc
first: $first
skip: $skip
) {
id
category
source
bondType
token
amount
from
to
blockTimestamp
transactionHash
agentSafe {
id
service {
id
agentIds
}
}
service {
id
agentIds
}
}
_meta {
block {
number
timestamp
}
hasIndexingErrors
}
}
`;
// v2 (subgraph v0.0.7) variant: no `bondType` on FundsMovement, service refs
// carry the numeric id in `serviceId`. The category filter is unchanged — on
// v2 OLAS reward sweeps are categorized AGENT_OLAS_TO_MASTER, so the same
// filter now excludes them server-side (v1 hides them client-side).
const FETCH_AGENT_TRANSACTION_HISTORY_QUERY_V2 = gql`
query GetAgentTransactionHistoryV2(
$agentSafe: Bytes!
$first: Int!
$skip: Int!
) {
fundsMovements(
where: {
agentSafe: $agentSafe
category_in: [MASTER_TO_AGENT, AGENT_TO_MASTER]
}
orderBy: blockTimestamp
orderDirection: desc
first: $first
skip: $skip
) {
id
category
source
token
amount
from
to
blockTimestamp
transactionHash
agentSafe {
id
service {
id
serviceId
agentIds
}
}
service {
id
serviceId
agentIds
}
}
_meta {
block {
number
timestamp
}
hasIndexingErrors
}
}
`;
type GetAgentTransactionHistoryParams = {
chainId: EvmChainId;
agentSafe: Address;
first?: number;
skip?: number;
};
const DEFAULT_PAGE_SIZE = 100;
const get = async ({
chainId,
agentSafe,
first = DEFAULT_PAGE_SIZE,
skip = 0,
}: GetAgentTransactionHistoryParams): Promise<AgentTransactionHistoryResponse> => {
const url = TRANSACTION_HISTORY_SUBGRAPH_URLS_BY_EVM_CHAIN[chainId];
if (!url) {
throw new Error(
`No transaction-history subgraph configured for chain ${chainId}`,
);
}
const variables = { agentSafe: agentSafe.toLowerCase(), first, skip };
if (getTransactionHistorySchemaRevision(chainId) === 'v2') {
const raw = await request(
url,
FETCH_AGENT_TRANSACTION_HISTORY_QUERY_V2,
variables,
);
return normalizeAgentTransactionHistoryResponseV2(
AgentTransactionHistoryResponseV2Schema.parse(raw),
);
}
const raw = await request(
url,
FETCH_AGENT_TRANSACTION_HISTORY_QUERY,
variables,
);
return AgentTransactionHistoryResponseSchema.parse(raw);
};
// The Graph caps `first` at 1000. We fetch a single 1000-row page for now —
// plenty for the 10-at-a-time view and bounds gateway cost. Older history
// beyond 1000 raw movements is dropped (an error fires); the real fix
// (server-side filtering + pagination) is a subgraph follow-up. getAll keeps
// the page loop so the cap is a one-line bump (MAX_PAGES) once that lands.
const PAGE_SIZE = 1000;
const MAX_PAGES = 1;
const getAll = async ({
chainId,
agentSafe,
}: Pick<
GetAgentTransactionHistoryParams,
'chainId' | 'agentSafe'
>): Promise<AgentTransactionHistoryResponse> => {
if (!TRANSACTION_HISTORY_SUBGRAPH_URLS_BY_EVM_CHAIN[chainId]) {
throw new Error(
`No transaction-history subgraph configured for chain ${chainId}`,
);
}
let base: AgentTransactionHistoryResponse | null = null;
const fundsMovements: AgentTransactionHistoryResponse['fundsMovements'] = [];
for (let page = 0; page < MAX_PAGES; page += 1) {
const res = await get({
chainId,
agentSafe,
first: PAGE_SIZE,
skip: page * PAGE_SIZE,
});
if (!base) base = res; // _meta comes from the first page.
fundsMovements.push(...res.fundsMovements);
if (res.fundsMovements.length < PAGE_SIZE) break;
if (page === MAX_PAGES - 1) {
console.error(
`[AgentTransactionHistory] hit the ${MAX_PAGES * PAGE_SIZE}-row cap for ${agentSafe}; older history may be truncated.`,
);
}
}
return {
...(base as AgentTransactionHistoryResponse),
fundsMovements,
};
};
export const AgentTransactionHistoryService = { get, getAll };