Skip to content

Commit

Permalink
Merge pull request #230 from aleksandrkaekhtin/feature/s6_projects
Browse files Browse the repository at this point in the history
Refactor metrics
  • Loading branch information
aleksandrkaekhtin authored Oct 23, 2024
2 parents e2c7853 + e1fe080 commit 282799a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
17 changes: 9 additions & 8 deletions models/metrics/contracts_by_code_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def calculate(self, context: CalculationContext, metric):
class ProxyContractInteractionToncenterCppImpl(ToncenterCppMetricImpl):
def calculate(self, context: CalculationContext, metric):
if len(metric.op_codes) > 0:
op_codes_filter = " OR ".join(map(lambda op: f"m.opcode = {op}", metric.op_codes))
op_codes_filter = " or ".join(map(lambda op: f"m.opcode = {op}", metric.op_codes))
else:
op_codes_filter = "TRUE"
return f"""
Expand All @@ -31,13 +31,14 @@ def calculate(self, context: CalculationContext, metric):
select distinct(account) from latest_account_states
where code_hash = '{metric.code_hash}' and timestamp > {context.season.start_time}
)
select t.hash as id, '{context.project.name}' as project, m.source as user_address, t.now as ts
from transactions t
join proxy_contracts pc on pc.account = t.account
join messages m on m.tx_hash = t.hash and m.direction = 'in'
where compute_exit_code = 0 and action_result_code = 0
and now >= {context.season.start_time}::integer
and now < {context.season.end_time}::integer
select m.tx_hash as id, '{context.project.name}' as project, m.source as user_address, m.created_at as ts
from messages m
join proxy_contracts pc on pc.account = m.destination
join transactions t on m.tx_hash = t.hash
where t.compute_exit_code = 0 and t.action_result_code = 0
and m.direction = 'in'
and m.created_at >= {context.season.start_time}::integer
and m.created_at < {context.season.end_time}::integer
and {op_codes_filter}
)
"""
Expand Down
17 changes: 9 additions & 8 deletions models/metrics/gaspump_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ def calculate(self, context: CalculationContext, metric):
class GasPumpJettonsBuysToncenterCppImpl(ToncenterCppMetricImpl):
def calculate(self, context: CalculationContext, metric):
BUY_OP_CODE = 1825825968
admin_addresses_filter = " OR ".join(map(lambda addr: f"jm.admin_address = '{self.to_raw(addr)}'", metric.admin_addresses))
admin_addresses_filter = " or ".join(map(lambda addr: f"jm.admin_address = '{self.to_raw(addr)}'", metric.admin_addresses))

return f"""
(
with j_masters as (
select jm.address as jetton_master_address from jetton_masters jm
where {admin_addresses_filter}
)
select t.hash as id, '{context.project.name}' as project, m.source as user_address, t.now as ts
from transactions t
join j_masters jm on t.account = jm.jetton_master_address
join messages m on m.tx_hash = t.hash and m.direction = 'in'
where compute_exit_code = 0 and action_result_code = 0
and t.now >= {context.season.start_time}::integer
and t.now < {context.season.end_time}::integer
select m.tx_hash as id, '{context.project.name}' as project, m.source as user_address, m.created_at as ts
from messages m
join j_masters jm on m.destination = jm.jetton_master_address
join transactions t on m.tx_hash = t.hash
where t.compute_exit_code = 0 and t.action_result_code = 0
and m.direction = 'in'
and m.created_at >= {context.season.start_time}::integer
and m.created_at < {context.season.end_time}::integer
and m.opcode = {BUY_OP_CODE}
)
"""
Expand Down
15 changes: 8 additions & 7 deletions models/metrics/jetton_master_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ def calculate(self, context: CalculationContext, metric):
select jm.address as jetton_master_address from jetton_masters jm
where {admin_addresses_filter}
)
select t.hash as id, '{context.project.name}' as project, m.source as user_address, t.now as ts
from transactions t
join j_masters jm on t.account = jm.jetton_master_address
join messages m on m.tx_hash = t.hash and m.direction = 'in'
where compute_exit_code = 0 and action_result_code = 0
and t.now >= {context.season.start_time}::integer
and t.now < {context.season.end_time}::integer
select m.tx_hash as id, '{context.project.name}' as project, m.source as user_address, m.created_at as ts
from messages m
join j_masters jm on m.destination = jm.jetton_master_address
join transactions t on m.tx_hash = t.hash
where t.compute_exit_code = 0 and t.action_result_code = 0
and m.direction = 'in'
and m.created_at >= {context.season.start_time}::integer
and m.created_at < {context.season.end_time}::integer
and {op_codes_filter}
)
"""
Expand Down
32 changes: 16 additions & 16 deletions models/metrics/smc_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,40 @@ class SmartContractInteractionToncenterCppImpl(ToncenterCppMetricImpl):

def calculate(self, context: CalculationContext, metric):
if len(metric.op_codes) > 0:
op_codes_filter = " OR ".join(map(lambda op: f"opcode = {op}", metric.op_codes))
op_codes_filter = " or ".join(map(lambda op: f"opcode = {op}", metric.op_codes))
else:
op_codes_filter = "TRUE"
if metric.comment_regexp:
comment_regexp_filter = f"and comment like '{metric.comment_regexp}'"
else:
comment_regexp_filter = ""
if metric.address:
address_filter = f"t.account ='{self.to_raw(metric.address)}'"
address_filter = f"destination ='{self.to_raw(metric.address)}'"
else:
assert len(metric.addresses) > 0, f"You should provide either address or addresses non empty list " \
f"({context.project.name}: {metric.description})"
address_filter = " OR ".join(map(lambda a: f"t.account ='{self.to_raw(a)}'", metric.addresses))
address_filter = " OR ".join(map(lambda a: f"destination ='{self.to_raw(a)}'", metric.addresses))
if len(metric.comment_not_equals) > 0:
comment_not_equals_filter = "and " + " and ".join(map(lambda v: f"comment != '{v}'", metric.comment_not_equals))
else:
comment_not_equals_filter = ""

return f"""
select
t.hash as id, '{context.project.name}' as project,
source as user_address, t.now as ts from transactions t
join messages m on m.tx_hash = t.hash and direction = 'in'
-- TODO replace left join to inner join for comment_required case
left join parsed.message_comments mc on mc.hash = m.body_hash
where compute_exit_code = 0 and action_result_code = 0 and
now >= {context.season.start_time}::integer and
now < {context.season.end_time}::integer and
({address_filter}) {'and length("comment") > 0' if metric.comment_required else ''}
{comment_regexp_filter} {comment_not_equals_filter}
AND (
m.tx_hash as id, '{context.project.name}' as project,
source as user_address, m.created_at as ts from messages m
join transactions t on m.tx_hash = t.hash
-- TODO replace left join to inner join for comment_required case
left join parsed.message_comments mc on mc.hash = m.body_hash
where compute_exit_code = 0 and action_result_code = 0 and
direction = 'in' and
created_at >= {context.season.start_time}::integer and
created_at < {context.season.end_time}::integer and
({address_filter}) {'and length("comment") > 0' if metric.comment_required else ''}
{comment_regexp_filter} {comment_not_equals_filter}
and (
{op_codes_filter}
)
)
"""


Expand Down

0 comments on commit 282799a

Please sign in to comment.