1- import { gql , request } from "graphql-request" ;
1+ import { gql , GraphQLClient } from "graphql-request" ;
22import { createPublicClient , http , PublicClient } from "viem" ;
33import { EVMNetworkValidator } from "./evm.js" ;
44
55const ANY_EVM = "ANY_EVM" ;
6- const DEFAULT_API_URL = "https://www.opensource.observer /api/v1/graphql" ;
6+ const DEFAULT_API_URL = "https://www.oso.xyz /api/v1/graphql" ;
77
88export interface ContractsV0ValidatorOptions {
99 apiUrl ?: string ;
1010 contractNamespace ?: string ;
1111 rpcUrl : string ;
12+ apiKey ?: string ;
1213}
1314
1415interface ContractsV0Response {
@@ -24,16 +25,22 @@ interface ContractsV0Response {
2425}
2526
2627export class ContractsV0Validator implements EVMNetworkValidator {
27- private readonly apiUrl : string ;
28+ private readonly graphqlClient : GraphQLClient ;
2829 private readonly contractNamespace : string ;
2930 private readonly client : PublicClient ;
3031
3132 constructor ( options : ContractsV0ValidatorOptions ) {
32- this . apiUrl = options . apiUrl ?? DEFAULT_API_URL ;
33+ const apiUrl = options . apiUrl ?? DEFAULT_API_URL ;
3334 this . contractNamespace = options . contractNamespace ?? ANY_EVM ;
3435 this . client = createPublicClient ( {
3536 transport : http ( options . rpcUrl ) ,
3637 } ) ;
38+
39+ const headers : Record < string , string > = { } ;
40+ if ( options . apiKey ) {
41+ headers . Authorization = `Bearer ${ options . apiKey } ` ;
42+ }
43+ this . graphqlClient = new GraphQLClient ( apiUrl , { headers } ) ;
3744 }
3845
3946 private normalizeAddress ( address : string ) : `0x${string } ` {
@@ -106,8 +113,7 @@ export class ContractsV0Validator implements EVMNetworkValidator {
106113 variables : Record < string , string > ,
107114 ) : Promise < boolean > {
108115 try {
109- const response = await request < ContractsV0Response > (
110- this . apiUrl ,
116+ const response = await this . graphqlClient . request < ContractsV0Response > (
111117 query ,
112118 variables ,
113119 ) ;
@@ -129,28 +135,37 @@ export class ContractsV0Validator implements EVMNetworkValidator {
129135export const createContractsV0Validator = (
130136 rpcUrl : string ,
131137 network ?: string ,
138+ apiKey ?: string ,
132139) : ContractsV0Validator => {
133140 const contractNamespace = network ?? ANY_EVM ;
134141
135- return new ContractsV0Validator ( { contractNamespace, rpcUrl } ) ;
142+ return new ContractsV0Validator ( { contractNamespace, rpcUrl, apiKey } ) ;
136143} ;
137144
138145export const EthereumContractsV0Validator = (
139146 rpcUrl : string ,
140- ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , "ETHEREUM" ) ;
147+ apiKey ?: string ,
148+ ) : ContractsV0Validator =>
149+ createContractsV0Validator ( rpcUrl , "ETHEREUM" , apiKey ) ;
141150
142151export const ArbitrumContractsV0Validator = (
143152 rpcUrl : string ,
144- ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , "ARBITRUM" ) ;
153+ apiKey ?: string ,
154+ ) : ContractsV0Validator =>
155+ createContractsV0Validator ( rpcUrl , "ARBITRUM" , apiKey ) ;
145156
146157export const BaseContractsV0Validator = (
147158 rpcUrl : string ,
148- ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , "BASE" ) ;
159+ apiKey ?: string ,
160+ ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , "BASE" , apiKey ) ;
149161
150162export const OptimismContractsV0Validator = (
151163 rpcUrl : string ,
152- ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , "OPTIMISM" ) ;
164+ apiKey ?: string ,
165+ ) : ContractsV0Validator =>
166+ createContractsV0Validator ( rpcUrl , "OPTIMISM" , apiKey ) ;
153167
154168export const AnyEVMContractsV0Validator = (
155169 rpcUrl : string ,
156- ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , ANY_EVM ) ;
170+ apiKey ?: string ,
171+ ) : ContractsV0Validator => createContractsV0Validator ( rpcUrl , ANY_EVM , apiKey ) ;
0 commit comments