@@ -5,16 +5,8 @@ import { VirtualAddress } from 'ox/tempo'
55import { getCode } from 'viem/actions'
66import { type AccountType , getAccountType } from '#lib/account'
77import { isTip20Address } from '#lib/domain/tip20'
8- import {
9- type ContractCreationData ,
10- fetchContractCreationData ,
11- } from '#lib/server/contract-creation'
128import { api } from '#lib/server/tempo-api'
139import {
14- type ContractCreationReceiptRow ,
15- fetchAddressOldestTx ,
16- fetchAddressTxStats ,
17- fetchContractCreationReceipt ,
1810 fetchTokenTransferBoundaries ,
1911 fetchVirtualAddressTransferStats ,
2012} from '#lib/server/tempo-queries'
@@ -59,60 +51,69 @@ type AddressTxAggregate = {
5951export function pickTip20CreatedTimestamp ( params : {
6052 tokenCreatedTimestamp : unknown
6153 firstTransferTimestamp : unknown
62- contractCreationTimestamp ?: unknown
6354} ) : number | undefined {
6455 const tokenCreatedTimestamp = parseTimestamp ( params . tokenCreatedTimestamp )
6556 const firstTransferTimestamp = parseTimestamp ( params . firstTransferTimestamp )
66- const contractCreationTimestamp = parseTimestamp (
67- params . contractCreationTimestamp ,
68- )
6957
7058 if ( tokenCreatedTimestamp != null ) return tokenCreatedTimestamp
71-
72- return contractCreationTimestamp != null &&
73- ( firstTransferTimestamp == null ||
74- contractCreationTimestamp < firstTransferTimestamp )
75- ? contractCreationTimestamp
76- : firstTransferTimestamp
59+ return firstTransferTimestamp
7760}
7861
79- export function buildAddressTxMetadata (
80- aggregate : AddressTxAggregate ,
81- creation : ContractCreationReceiptRow | ContractCreationData | undefined ,
82- ) : {
62+ export function buildAddressTxMetadata ( aggregate : AddressTxAggregate ) : {
8363 txCount : number
8464 lastActivityTimestamp ?: number
8565 createdTimestamp ?: number
8666 createdTxHash ?: string
8767 createdBy ?: string
8868} {
8969 const oldestTimestamp = parseTimestamp ( aggregate . oldestTxsBlockTimestamp )
90- const creationTimestamp = parseTimestamp (
91- creation && 'block_timestamp' in creation
92- ? creation . block_timestamp
93- : creation ?. timestamp ,
94- )
95- const useCreation =
96- creationTimestamp != null &&
97- ( oldestTimestamp == null || creationTimestamp <= oldestTimestamp )
9870
9971 return {
100- txCount : ( aggregate . count ?? 0 ) + ( creation ? 1 : 0 ) ,
72+ txCount : aggregate . count ?? 0 ,
10173 lastActivityTimestamp : parseTimestamp ( aggregate . latestTxsBlockTimestamp ) ,
102- createdTimestamp :
103- useCreation && creationTimestamp != null
104- ? creationTimestamp
105- : oldestTimestamp ,
106- createdTxHash :
107- useCreation && creation
108- ? 'tx_hash' in creation
109- ? creation . tx_hash
110- : ( creation . hash ?? undefined )
111- : aggregate . oldestTxHash ,
112- createdBy :
113- useCreation && creation
114- ? ( creation . from ?? undefined )
115- : aggregate . oldestTxFrom ,
74+ createdTimestamp : oldestTimestamp ,
75+ createdTxHash : aggregate . oldestTxHash ,
76+ createdBy : aggregate . oldestTxFrom ,
77+ }
78+ }
79+
80+ /** Address activity boundaries and count from structured Tempo API pages. */
81+ export async function fetchAddressTxMetadata (
82+ chainId : number ,
83+ address : Address . Address ,
84+ ) : Promise < AddressTxAggregate > {
85+ const [ oldestPage , latestPage ] = await Promise . all ( [
86+ parseResponse (
87+ api . v1 . transactions . $get ( {
88+ query : {
89+ address,
90+ chainId : String ( chainId ) ,
91+ include : 'totalCount' ,
92+ limit : '5' ,
93+ order : 'asc' ,
94+ } ,
95+ } ) ,
96+ ) ,
97+ parseResponse (
98+ api . v1 . transactions . $get ( {
99+ query : {
100+ address,
101+ chainId : String ( chainId ) ,
102+ limit : '5' ,
103+ order : 'desc' ,
104+ } ,
105+ } ) ,
106+ ) ,
107+ ] )
108+ const oldest = oldestPage . data [ 0 ]
109+ const latest = latestPage . data [ 0 ]
110+
111+ return {
112+ count : oldestPage . meta ?. totalCount ,
113+ latestTxsBlockTimestamp : latest ?. timestamp ,
114+ oldestTxsBlockTimestamp : oldest ?. timestamp ,
115+ oldestTxHash : oldest ?. hash ,
116+ oldestTxFrom : oldest ?. sender ,
116117 }
117118}
118119
@@ -211,11 +212,6 @@ async function loadAddressMetadata(
211212 latestTimestamp : undefined ,
212213 } ) ) ,
213214 ] )
214- const contractCreation =
215- stats ?. createdAt == null
216- ? await fetchContractCreationData ( address ) . catch ( ( ) => null )
217- : null
218-
219215 response = {
220216 address,
221217 chainId,
@@ -225,36 +221,17 @@ async function loadAddressMetadata(
225221 createdTimestamp : pickTip20CreatedTimestamp ( {
226222 tokenCreatedTimestamp : stats ?. createdAt ,
227223 firstTransferTimestamp : boundaries . oldestTimestamp ,
228- contractCreationTimestamp : contractCreation ?. timestamp ,
229224 } ) ,
230225 }
231226 } else {
232- // One aggregate (exact distinct count + boundaries) + the oldest
233- // tx row for the "created by" stat. Creation receipt stays on
234- // the SQL lane (D4.1) with the existing RPC bisection fallback.
235- const [ bytecode , stats , oldestTx , indexedCreation ] = await Promise . all ( [
227+ // Structured Tempo API pages provide the first and latest indexed activity
228+ // without the historical RPC binary search previously used for contracts.
229+ const [ bytecode , stats ] = await Promise . all ( [
236230 bytecodePromise ,
237- fetchAddressTxStats ( address , chainId ) ,
238- fetchAddressOldestTx ( address , chainId ) . catch ( ( ) => undefined ) ,
239- fetchContractCreationReceipt ( address , chainId ) . catch ( ( ) => undefined ) ,
231+ fetchAddressTxMetadata ( chainId , address ) ,
240232 ] )
241233 const accountType = getAccountType ( bytecode )
242- const creation =
243- indexedCreation ??
244- ( accountType === 'contract'
245- ? await fetchContractCreationData ( address ) . catch ( ( ) => null )
246- : undefined ) ??
247- undefined
248- const metadata = buildAddressTxMetadata (
249- {
250- count : stats . count ,
251- latestTxsBlockTimestamp : stats . latestTimestamp ,
252- oldestTxsBlockTimestamp : stats . oldestTimestamp ,
253- oldestTxHash : oldestTx ?. hash ,
254- oldestTxFrom : oldestTx ?. from ,
255- } ,
256- creation ,
257- )
234+ const metadata = buildAddressTxMetadata ( stats )
258235
259236 response = {
260237 address,
0 commit comments