-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGENERATE_ABI_FUNCTIONS.sql
37 lines (37 loc) · 1.63 KB
/
GENERATE_ABI_FUNCTIONS.sql
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
CREATE OR REPLACE FUNCTION `abi_functions`.GENERATE_ABI_FUNCTIONS(prefix STRING, abi STRING) RETURNS STRUCT<functions ARRAY<STRING>, events ARRAY<STRING>> LANGUAGE js AS """
abi = JSON.parse(abi);
rr = [];
res_functions = [];
res_events = [];
abi.forEach(function(el) {
q3 = '"'+'"'+'"';
if (el.type == "function") {
inputs = [];
name = el.name;
el.inputs.forEach(function(ii) {
inputs.push(ii.name);
});
res_functions.push(
"CREATE TEMP FUNCTION "+prefix+"_call_"+name+"(kv ARRAY<STRUCT<arg STRING,val STRING>>) RETURNS STRUCT<"+inputs.map(x=>x+" STRING").join(",")+"> LANGUAGE js AS "+q3+"\\n"+
"want=new Set(["+inputs.map(x=>"'"+x+"'").join(",")+"]);res=[];kv.forEach(function(el){if(want.has(el.arg)){res[el.arg]=el.val}});return res;\\n"+q3+"\\n"+
'OPTIONS(library="gs://blockchain-etl-bigquery/ethers.js");'+"\\n"
);
}
else if (el.type == "event") {
inputs = [];
name = el.name;
el.inputs.forEach(function(ii) {
inputs.push(ii.name);
});
res_events.push(
"CREATE TEMP FUNCTION "+prefix+"_evt_"+name+"(kv ARRAY<STRUCT<arg STRING,val STRING>>) RETURNS STRUCT<"+inputs.map(x=>x+" STRING").join(",")+"> LANGUAGE js AS "+q3+"\\n"+
"want=new Set(["+inputs.map(x=>"'"+x+"'").join(",")+"]);res=[];kv.forEach(function(el){if(want.has(el.arg)){res[el.arg]=el.val}});return res;\\n"+q3+"\\n"+
'OPTIONS(library="gs://blockchain-etl-bigquery/ethers.js");'+"\\n"
);
}
});
rr.functions = res_functions;
rr.events = res_events;
return rr;
"""
OPTIONS(library="gs://blockchain-etl-bigquery/ethers.js");