1+ import { createPublicClient , http , parseAbiItem } from "viem" ;
2+ import { arbitrumGoerli } from "viem/chains" ;
3+
4+ const publicClient = createPublicClient ( {
5+ chain : arbitrumGoerli ,
6+ transport : http ( ) ,
7+ } ) ;
8+
19export const mappings = [
210 {
311 type : "fetch" ,
@@ -33,6 +41,20 @@ export const mappings = [
3341 seek : [ "photo" , "video" ] ,
3442 populate : [ "photoUrl" , "videoUrl" ] ,
3543 } ,
44+ {
45+ type : "abi/call" ,
46+ source : "contractCall" ,
47+ inputs : [ ] ,
48+ seek : [ ] ,
49+ populate : [ ] ,
50+ } ,
51+ {
52+ type : "abi/event" ,
53+ source : "contractEvent" ,
54+ inputs : [ ] ,
55+ seek : [ ] ,
56+ populate : [ ] ,
57+ } ,
3658] ;
3759
3860const initialState = {
@@ -75,6 +97,45 @@ const jsonAction = (currentAcc, source, seek, populate) => {
7597 return jsonData ;
7698} ;
7799
100+ const callAction = async ( source , inputs , seek , populate ) => {
101+ const data = await publicClient . readContract ( {
102+ address : inputs [ 1 ] ,
103+ abi : parseAbiItem ( source ) ,
104+ functionName : "" ,
105+ args : inputs ,
106+ } ) ;
107+
108+ let populatedData = { } ;
109+
110+ seek . map ( ( item , index ) => {
111+ populatedData [ populate [ index ] ] = data [ item ] ;
112+ } ) ;
113+
114+ return populatedData ;
115+ } ;
116+
117+ const eventAction = async ( source , inputs , seek , populate ) => {
118+ const filter = await publicClient . createEventFilter ( {
119+ address : inputs [ 1 ] ,
120+ event : parseAbiItem ( source ) ,
121+ args : inputs ,
122+ } ) ;
123+
124+ const contractEvent = await publicClient . getFilterLogs ( {
125+ filter : filter ,
126+ } ) ;
127+
128+ const eventData = contractEvent [ 0 ] . args ;
129+
130+ let populatedData = { } ;
131+
132+ seek . map ( ( item , index ) => {
133+ populatedData [ populate [ index ] ] = eventData [ item ] ;
134+ } ) ;
135+
136+ return populatedData ;
137+ } ;
138+
78139const accumulatedData = mappings . reduce ( async ( acc , { type, source, inputs, seek, populate } ) => {
79140 const currentAcc = await acc ;
80141
@@ -96,6 +157,16 @@ const accumulatedData = mappings.reduce(async (acc, { type, source, inputs, seek
96157 ...currentAcc ,
97158 ...jsonAction ( currentAcc , source , seek , populate ) ,
98159 } ;
160+ case "abi/call" :
161+ return {
162+ ...currentAcc ,
163+ ...( await callAction ( source , inputs , seek , populate ) ) ,
164+ } ;
165+ case "abi/event" :
166+ return {
167+ ...currentAcc ,
168+ ...( await eventAction ( source , inputs , seek , populate ) ) ,
169+ } ;
99170
100171 default :
101172 return currentAcc ;
0 commit comments