Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added modified PARSE_ABI_FUNCTIONS and PARSE_ABI_EVENTS udf functions #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions sql/PARSE_ABI_EVENTS_UDF.sql
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge this file with the base file from which it is cloned

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
CREATE OR REPLACE FUNCTION `blocktrekker.udfs.PARSE_ABI_EVENTS`(abi STRING, dune_name STRING) RETURNS ARRAY<STRUCT<name STRING, anonymous BOOL, hash_id STRING, inputs STRING, types STRING>> LANGUAGE js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factor out blocktrekker.udfs

to $UDF_PROJECT.$UDF_DATASET

using DECLARE.

Rename dune_name to contract_prefix.

OPTIONS (library=["gs://blockchain-etl-bigquery/ethers.js"]) AS R"""
abi = JSON.parse(abi);
res = [];
const typeMap = {
"uint32[]": "INT64",
"uint16[]": "INT64",
"uint8[]": "INT64",
"uint64[]": "INT64",
"uint128[]": "INT64",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above int64 please use string to avoid precision loss

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above int64 please use string to avoid precision loss

"uint256[]": "BIGNUMERIC",
"bool[]": "BOOL",
"address[]": "STRING",
"string[]": "STRING",
"bytes[]": "BYTES",
"bytes4": "BYTES",
"bytes32": "BYTES",
"uint32": "INT64",
"uint16": "INT64",
"uint8": "INT64",
"uint64": "INT64",
"unit80": "INT64",
"uint112": "INT64",
"uint128": "INT64",
"uint168": "BIGNUMERIC",
"uint256": "BIGNUMERIC",
"BIGNUMERIC": "BIGNUMERIC",
"bool": "BOOL",
"address": "STRING",
"STRING": "STRING",
"string": "STRING",
"bytes": "BYTES"
};

const nameMap = {
"from": "from_address",
"to": "to_address",
"limit": "_limit",
"all": "_all"
};

abi.forEach(function(x){
tuple = [];
tuple['name'] = dune_name + "_evt_" + x.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guard against dune_name is null with a default value

tuple['anonymous'] = x.anonymous;

if (x.type != 'event') {
return;
}

argtypes = [];
argpairs = [];
let count = 1;
x.inputs.forEach(function(y){
pair = {};
argtypes.push(y.type);
if (y.name in nameMap) {
pair.name = nameMap[y.name];
} else if (y.name == "") {
pair.name = `input_${count}`;
} else {
pair.name = y.name ? y.name : `input_${count}`;
}
if (y.type in typeMap) {
pair.type = typeMap[y.type];
} else {
if (y.type.slice(0, 4).toLowerCase() === "uint") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to throw an error on unknown types

pair.type = "BIGNUMERIC";
} else {
pair.type = "STRING";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise. Throw.

}
}
argpairs.push(JSON.stringify(pair));
count = count + 1;
});
tuple['inputs'] = argpairs.join(",");
tuple['hash_id'] = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(tuple['name'] + '(' + argtypes.join(',') + ')'));
res.push(tuple);
});
return res;
""";
106 changes: 106 additions & 0 deletions sql/PARSE_ABI_FUNCTIONS_UDF.sql
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge into base file

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
CREATE OR REPLACE FUNCTION `blocktrekker.udfs.PARSE_ABI_FUNCTIONS`(abi STRING, dune_name STRING) RETURNS ARRAY<STRUCT<name STRING, hash_id STRING, constant BOOL, payable BOOL, inputs STRING, outputs STRING>> LANGUAGE js
OPTIONS (library=["gs://blockchain-etl-bigquery/ethers.js"]) AS R"""
abi = JSON.parse(abi);
res = [];
const typeMap = {
"uint32[]": "INT64",
"uint16[]": "INT64",
"uint8[]": "INT64",
"uint64[]": "INT64",
"uint128[]": "INT64",
"uint256[]": "BIGNUMERIC",
"bool[]": "BOOL",
"address[]": "STRING",
"string[]": "STRING",
"bytes[]": "BYTES",
"bytes4": "BYTES",
"bytes32": "BYTES",
"uint32": "INT64",
"uint16": "INT64",
"uint8": "INT64",
"uint64": "INT64",
"unit80": "INT64",
"uint112": "INT64",
"uint128": "INT64",
"uint168": "BIGNUMERIC",
"uint256": "BIGNUMERIC",
"BIGNUMERIC": "BIGNUMERIC",
"bool": "BOOL",
"address": "STRING",
"STRING": "STRING",
"string": "STRING",
"bytes": "BYTES"
};


const nameMap = {
"from": "from_address",
"to": "to_address",
"limit": "_limit",
"all": "_all"
};

abi.forEach(function(x){
tuple = [];
tuple['constant'] = x.constant;
tuple['payable'] = x.payable;
if (x.type != 'function') {
return;
}

argtypes = [];
argpairs = [];
let count = 1;
x.inputs.forEach(function(y){
pair = {};
argtypes.push(y.type);
if (y.name in nameMap) {
pair.name = nameMap[y.name];
} else if (y.name == "") {
pair.name = `input_${count}`;
} else {
pair.name = y.name ? y.name : `input_${count}`;
}
if (y.type in typeMap) {
pair.type = typeMap[y.type];
} else {
if (y.type.slice(0, 4).toLowerCase() === "uint") {
pair.type = "BIGNUMERIC";
} else {
pair.type = "STRING";
}
}
argpairs.push(JSON.stringify(pair));
count = count + 1;
});

outpairs = [];
if (x.outputs) {
let count = 1;
x.outputs.forEach(function(y){
pair = {};
if (y.name in nameMap) {
pair.name = nameMap[y.name];
} else if (y.name == "") {
pair.name = `output_${count}`
} else {
pair.name = y.name ? y.name : `output_${count}`;
}
if (y.type in typeMap) {
pair.type = typeMap[y.type];
} else if (y.type.slice(0, 4).toLowerCase() === "uint") {
pair.type = "BIGNUMERIC";
} else {
pair.type = "STRING";
}
outpairs.push(JSON.stringify(pair));
});
}
tuple['inputs'] = argpairs.join(",");
tuple['outputs'] = outpairs.join(",");
tuple['hash_id'] = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(x.name + '(' + argtypes.join(',') + ')')).substr(0,10);
tuple['name'] = dune_name + "_call_" + x.name;
res.push(tuple);
});
return res;
""";