Created
August 21, 2020 12:21
-
-
Save medvedev1088/65b0f7148250fd74bdde766eb793cbe9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TEMP FUNCTION PARSE_LOG(data STRING, topics ARRAY<STRING>) RETURNS STRUCT<`from` STRING, `to` STRING, `value` STRING, `data` STRING> LANGUAGE js | |
OPTIONS (library=["gs://blockchain-etl-bigquery/ethers.js"]) AS """ | |
var abi = {"anonymous": false, "inputs": [{"indexed": true, "name": "from", "type": "address"}, {"indexed": true, "name": "to", "type": "address"}, {"indexed": false, "name": "value", "type": "uint256"}, {"indexed": true, "name": "data", "type": "bytes"}], "name": "Transfer", "type": "event"} | |
var interface_instance = new ethers.utils.Interface([abi]); | |
var parsedLog = interface_instance.parseLog({topics: topics, data: data}); | |
var parsedValues = parsedLog.values; | |
var transformParams = function(params, abiInputs) { | |
var result = {}; | |
if (params && params.length >= abiInputs.length) { | |
for (var i = 0; i < abiInputs.length; i++) { | |
var paramName = abiInputs[i].name; | |
var paramValue = params[i]; | |
if (abiInputs[i].type === 'address' && typeof paramValue === 'string') { | |
// For consistency all addresses are lower-cased. | |
paramValue = paramValue.toLowerCase(); | |
} | |
if (ethers.utils.Interface.isIndexed(paramValue)) { | |
paramValue = paramValue.hash; | |
} | |
if (abiInputs[i].type === 'tuple' && 'components' in abiInputs[i]) { | |
paramValue = transformParams(paramValue, abiInputs[i].components) | |
} | |
result[paramName] = paramValue; | |
} | |
} | |
return result; | |
}; | |
var result = transformParams(parsedValues, abi.inputs); | |
return result; | |
"""; | |
WITH parsed_logs AS | |
(SELECT | |
logs.block_timestamp AS block_timestamp | |
,logs.block_number AS block_number | |
,logs.transaction_hash AS transaction_hash | |
,logs.log_index AS log_index | |
,PARSE_LOG(logs.data, logs.topics) AS parsed | |
FROM `bigquery-public-data.crypto_ethereum.logs` AS logs | |
WHERE address = '0xc12d099be31567add4e4e4d0d45691c3f58f5663' | |
AND topics[SAFE_OFFSET(0)] = '0xe19260aff97b920c7df27010903aeb9c8d2be5d310a2c67824cf3f15396e4c16' | |
) | |
SELECT | |
block_timestamp | |
,block_number | |
,transaction_hash | |
,log_index | |
,parsed.from AS `from` | |
,parsed.to AS `to` | |
,parsed.value AS `value` | |
,parsed.data AS `data` | |
FROM parsed_logs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment