From 8add7be491117f3026d5be65be785d34ef0076ca Mon Sep 17 00:00:00 2001 From: Julius Remigio <14811322+juls858@users.noreply.github.com> Date: Tue, 13 Jun 2023 16:36:38 -0700 Subject: [PATCH] STREAM-113/evm-rpc-primitives (#21) --- ...st_create_or_drop_function_from_config.sql | 5 +- analysis/test_crud_udfs_in_schema.sql | 1 + macros/create_udfs.sql | 25 +++--- .../api_integrations.sql | 0 .../configs.yaml.sql | 0 macros/livequery/evm_primitives.yaml.sql | 85 +++++++++++++++++++ .../functions.py.sql | 0 .../{streamline => livequery}/functions.sql | 0 .../utils.sql => livequery/manage_udfs.sql} | 26 ++++-- .../streamline_udfs.sql | 0 macros/livequery/utils.sql | 24 ++++++ 11 files changed, 144 insertions(+), 22 deletions(-) create mode 100644 analysis/test_crud_udfs_in_schema.sql rename macros/{streamline => livequery}/api_integrations.sql (100%) rename macros/{streamline => livequery}/configs.yaml.sql (100%) create mode 100644 macros/livequery/evm_primitives.yaml.sql rename macros/{streamline => livequery}/functions.py.sql (100%) rename macros/{streamline => livequery}/functions.sql (100%) rename macros/{streamline/utils.sql => livequery/manage_udfs.sql} (70%) rename macros/{streamline => livequery}/streamline_udfs.sql (100%) create mode 100644 macros/livequery/utils.sql diff --git a/analysis/test_create_or_drop_function_from_config.sql b/analysis/test_create_or_drop_function_from_config.sql index 32168bf..639f3b5 100644 --- a/analysis/test_create_or_drop_function_from_config.sql +++ b/analysis/test_create_or_drop_function_from_config.sql @@ -1,7 +1,4 @@ -{%- set name -%} - {{- udf_configs() -}} -{% endset %} -{% set udfs = fromyaml(name) %} +{%- set udfs = fromyaml(udf_configs()) -%} {%- for udf in udfs -%} {{- create_or_drop_function_from_config(udf, drop_=True) -}} {{- create_or_drop_function_from_config(udf, drop_=False) -}} diff --git a/analysis/test_crud_udfs_in_schema.sql b/analysis/test_crud_udfs_in_schema.sql new file mode 100644 index 0000000..69b0c23 --- /dev/null +++ b/analysis/test_crud_udfs_in_schema.sql @@ -0,0 +1 @@ +{{- crud_udfs_in_schema(config_evm_rpc_primitives, "ethereum", None, drop_) -}} \ No newline at end of file diff --git a/macros/create_udfs.sql b/macros/create_udfs.sql index bdfa5da..418e790 100644 --- a/macros/create_udfs.sql +++ b/macros/create_udfs.sql @@ -1,19 +1,18 @@ {% macro create_udfs(drop_=False) %} {% if var("UPDATE_UDFS_AND_SPS") %} - {% set name %} - {{- udf_configs() -}} - {% endset %} - {% set udfs = fromyaml(name) %} {% set sql %} - CREATE schema if NOT EXISTS silver; - CREATE schema if NOT EXISTS beta; - CREATE schema if NOT EXISTS utils; - CREATE schema if NOT EXISTS _utils; - CREATE schema if NOT EXISTS _live; - CREATE schema if NOT EXISTS live; - {%- for udf in udfs -%} - {{- create_or_drop_function_from_config(udf, drop_=drop_) -}} - {% endfor %} + CREATE SCHEMA IF NOT EXISTS silver; + CREATE SCHEMA IF NOT EXISTS beta; + CREATE SCHEMA IF NOT EXISTS utils; + CREATE SCHEMA IF NOT EXISTS _utils; + CREATE SCHEMA IF NOT EXISTS _live; + CREATE SCHEMA IF NOT EXISTS live; + {%- set udfs = fromyaml(udf_configs()) -%} + {%- for udf in udfs -%} + {{- create_or_drop_function_from_config(udf, drop_=drop_) -}} + {% endfor %} + + {{- crud_udfs_in_schema(config_evm_rpc_primitives, "ethereum", None, drop_) -}} {% endset %} {% do run_query(sql) %} {% endif %} diff --git a/macros/streamline/api_integrations.sql b/macros/livequery/api_integrations.sql similarity index 100% rename from macros/streamline/api_integrations.sql rename to macros/livequery/api_integrations.sql diff --git a/macros/streamline/configs.yaml.sql b/macros/livequery/configs.yaml.sql similarity index 100% rename from macros/streamline/configs.yaml.sql rename to macros/livequery/configs.yaml.sql diff --git a/macros/livequery/evm_primitives.yaml.sql b/macros/livequery/evm_primitives.yaml.sql new file mode 100644 index 0000000..c0421f3 --- /dev/null +++ b/macros/livequery/evm_primitives.yaml.sql @@ -0,0 +1,85 @@ +{%- macro config_evm_rpc_primitives(schema, blockchain) -%} +{#- + Generates a set of UDFs that call the Ethereum JSON RPC API + + - eth_call + - eth_getLogs + - eth_getBalance + + -#} +- name: {{ schema -}}.rpc_eth_call + signature: + - [transaction, OBJECT, The transaction object] + - [block_or_tag, STRING, The block number or tag to execute the call on] + return_type: [VARIANT, The return value of the executed contract code] + options: | + NOT NULL + RETURNS NULL ON NULL INPUT + VOLATILE + COMMENT = $$Executes a new message call immediately without creating a transaction on the block chain.$$ + sql: | + {{ sql_live_rpc_call('eth_call', "[transaction, block_or_tag]", blockchain, "'mainnet'") | indent(4) -}} +- name: {{ schema -}}.rpc_eth_call + signature: + - [transaction, OBJECT, The transaction object] + - [block_or_tag, STRING, The block number or tag to execute the call on] + - [network, STRING, The network to execute the call on] + return_type: [VARIANT, The return value of the executed contract code] + options: | + NOT NULL + RETURNS NULL ON NULL INPUT + VOLATILE + COMMENT = $$Executes a new message call immediately without creating a transaction on the block chain.$$ + sql: | + {{ sql_live_rpc_call('eth_call', '[transaction, block_or_tag]', blockchain, 'network') | indent(4) -}} + +- name: {{ schema -}}.rpc_eth_get_logs + signature: + - [filter, OBJECT, The filter object] + return_type: [VARIANT, An array of all logs matching filter with given address] + options: | + NOT NULL + RETURNS NULL ON NULL INPUT + VOLATILE + COMMENT = $$Returns an array of all logs matching filter with given address.$$ + sql: | + {{ sql_live_rpc_call('eth_getLogs', '[filter]', blockchain, "'mainnet'") | indent(4) -}} +- name: {{ schema -}}.rpc_eth_get_logs + signature: + - [filter, OBJECT, The filter object] + - [network, STRING, The network to execute the call on] + return_type: [VARIANT, An array of all logs matching filter with given address] + options: | + NOT NULL + RETURNS NULL ON NULL INPUT + VOLATILE + COMMENT = $$Returns an array of all logs matching filter with given address.$$ + sql: | + {{ sql_live_rpc_call('eth_getLogs', '[filter]', blockchain, 'network') | indent(4) -}} + +- name: {{ schema -}}.rpc_eth_get_balance + signature: + - [address, STRING, The address to get the balance of] + - [block_or_tag, STRING, The block number or tag to execute the call on] + return_type: [VARIANT, The balance of the account of given address] + options: | + NOT NULL + RETURNS NULL ON NULL INPUT + VOLATILE + COMMENT = $$Returns the balance of the account of given address.$$ + sql: | + {{ sql_live_rpc_call('eth_getBalance', '[address, block_or_tag]', blockchain, "'mainnet'") | indent(4) -}} +- name: {{ schema -}}.rpc_eth_get_balance + signature: + - [address, STRING, The address to get the balance of] + - [block_or_tag, STRING, The block number or tag to execute the call on] + - [network, STRING, The network to execute the call on] + return_type: [VARIANT, The balance of the account of given address] + options: | + NOT NULL + RETURNS NULL ON NULL INPUT + VOLATILE + COMMENT = $$Returns the balance of the account of given address.$$ + sql: | + {{ sql_live_rpc_call('eth_getBalance', '[address, block_or_tag]', blockchain, 'network') | indent(4) -}} +{% endmacro -%} diff --git a/macros/streamline/functions.py.sql b/macros/livequery/functions.py.sql similarity index 100% rename from macros/streamline/functions.py.sql rename to macros/livequery/functions.py.sql diff --git a/macros/streamline/functions.sql b/macros/livequery/functions.sql similarity index 100% rename from macros/streamline/functions.sql rename to macros/livequery/functions.sql diff --git a/macros/streamline/utils.sql b/macros/livequery/manage_udfs.sql similarity index 70% rename from macros/streamline/utils.sql rename to macros/livequery/manage_udfs.sql index 04e28e5..c2656b0 100644 --- a/macros/streamline/utils.sql +++ b/macros/livequery/manage_udfs.sql @@ -13,17 +13,18 @@ params, drop_ = False ) -%} - {% for name, - data_type in params -%} + {% for p in params -%} + {%- set name = p.0 -%} + {%- set data_type = p.1 -%} {% if drop_ %} {{ data_type -}} {% else %} {{ name ~ " " ~ data_type -}} - {% endif -%} + {%- endif -%} {%-if not loop.last -%}, {%- endif -%} {% endfor -%} -{% endmacro %} +{%- endmacro -%} {% macro create_sql_function( name_, @@ -59,7 +60,7 @@ ) -%} {% set name_ = config ["name"] %} {% set signature = config ["signature"] %} - {% set return_type = config ["return_type"] %} + {% set return_type = config ["return_type"] if config ["return_type"] is string else config ["return_type"][0] %} {% set sql_ = config ["sql"] %} {% set options = config ["options"] %} {% set api_integration = config ["api_integration"] %} @@ -82,3 +83,18 @@ ) }} {%- endif %} {% endmacro %} + +{% macro crud_udfs_in_schema(config_func, blockchain, network, drop_) %} +{# + config_func: function that returns a list of udf configs + blockchain: blockchain name + network: network name + drop_: whether to drop or create the udfs + #} + {% set schema = blockchain if not network else blockchain ~ "_" ~ network %} + CREATE SCHEMA IF NOT EXISTS {{ schema }}; + {%- set ethereum_rpc_udfs = fromyaml(config_func(blockchain, network)) if network else fromyaml(config_func(schema, blockchain)) -%} + {%- for udf in ethereum_rpc_udfs -%} + {{- create_or_drop_function_from_config(udf, drop_=drop_) -}} + {%- endfor -%} +{%- endmacro -%} \ No newline at end of file diff --git a/macros/streamline/streamline_udfs.sql b/macros/livequery/streamline_udfs.sql similarity index 100% rename from macros/streamline/streamline_udfs.sql rename to macros/livequery/streamline_udfs.sql diff --git a/macros/livequery/utils.sql b/macros/livequery/utils.sql new file mode 100644 index 0000000..5ef9e88 --- /dev/null +++ b/macros/livequery/utils.sql @@ -0,0 +1,24 @@ +{% macro sql_live_rpc_call(method, params, blockchain, network) %} +{# + Helper macro to call a JSON RPC method on a live node. + + Parameters: + method (string): The JSON RPC method to call. + params (string): The JSON RPC parameters to pass to the method. + blockchain (string): The blockchain to call the method on. + network (string): The network to call the method on. + Returns: + string: The SQL to call the method. + #} + WITH result as ( + SELECT + live.udf_api( + '{endpoint}' + ,utils.udf_json_rpc_call('{{ method }}', {{ params }}) + ,concat_ws('/', 'integration', _utils.udf_provider(), '{{ blockchain }}', {{ network }}) + )::VARIANT:data AS data + ) + SELECT + COALESCE(data:result, {'error':data:error}) + FROM result +{%- endmacro -%} \ No newline at end of file