STREAM-113/evm-rpc-primitives (#21)

This commit is contained in:
Julius Remigio 2023-06-13 16:36:38 -07:00 committed by GitHub
parent 4f234e8b7e
commit 8add7be491
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 144 additions and 22 deletions

View File

@ -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) -}}

View File

@ -0,0 +1 @@
{{- crud_udfs_in_schema(config_evm_rpc_primitives, "ethereum", None, drop_) -}}

View File

@ -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 %}

View File

@ -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 -%}

View File

@ -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 -%}

View File

@ -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 -%}