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
+
1
9
export const mappings = [
2
10
{
3
11
type : "fetch" ,
@@ -33,6 +41,20 @@ export const mappings = [
33
41
seek : [ "photo" , "video" ] ,
34
42
populate : [ "photoUrl" , "videoUrl" ] ,
35
43
} ,
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
+ } ,
36
58
] ;
37
59
38
60
const initialState = {
@@ -75,6 +97,45 @@ const jsonAction = (currentAcc, source, seek, populate) => {
75
97
return jsonData ;
76
98
} ;
77
99
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
+
78
139
const accumulatedData = mappings . reduce ( async ( acc , { type, source, inputs, seek, populate } ) => {
79
140
const currentAcc = await acc ;
80
141
@@ -96,6 +157,16 @@ const accumulatedData = mappings.reduce(async (acc, { type, source, inputs, seek
96
157
...currentAcc ,
97
158
...jsonAction ( currentAcc , source , seek , populate ) ,
98
159
} ;
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
+ } ;
99
170
100
171
default :
101
172
return currentAcc ;
0 commit comments