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

feat: add banana gun evm bot trades spellbook #6290

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0f80cee
feat: add banana gun evm trades spellbook skeletton
whalehunting Jun 30, 2024
f93c92d
chore: add and use variables in ethereum query
whalehunting Jun 30, 2024
aea502f
chore: add correct schema
whalehunting Jun 30, 2024
8b5d093
Merge branch 'main' into feat-add-banana-gun-evm-spellbooks
whalehunting Jul 3, 2024
b574887
fix: only include ethereum for now
whalehunting Jul 3, 2024
af8961f
fix: move comment
whalehunting Jul 3, 2024
7dc0670
fix: use correct source tags for referenced tables
whalehunting Jul 3, 2024
a247d8a
chore: add sample seed
whalehunting Jul 3, 2024
842e36e
fix: add missing source tag
whalehunting Jul 3, 2024
949f20e
fix: use variable instead of hardcoded string
whalehunting Jul 3, 2024
4f3d3e9
fix: add block_date and block_month columns
whalehunting Jul 3, 2024
78ef06b
fix: add block_date and block_month columns
whalehunting Jul 3, 2024
f4a9e44
fix: use correct table name
whalehunting Jul 3, 2024
7ea95af
fix: select correct columns
whalehunting Jul 3, 2024
c2d3cb9
fix: add block_date and block_month columns
whalehunting Jul 3, 2024
5923a32
fix: fix naming
whalehunting Jul 3, 2024
070ced7
fix: remove nonexisting columns
whalehunting Jul 3, 2024
b8b7179
fix: use correct seed file
whalehunting Jul 3, 2024
a24361f
fix: fix column name in uniqueness test
whalehunting Jul 3, 2024
3078049
fix: force version column to string in seed
whalehunting Jul 3, 2024
be99df5
feat: add other evm chains
whalehunting Jul 3, 2024
97a276d
chore: add base seed file
whalehunting Jul 3, 2024
56cb4ed
fix: force version column in seed to get parsed as a varchar
whalehunting Jul 4, 2024
95f8349
Merge branch 'main' into feat-add-banana-gun-evm-spellbooks
whalehunting Jul 4, 2024
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
123 changes: 123 additions & 0 deletions solana/models/_sector/dex/bot_trades/evm/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
version: 2

models:
- name: dex_evm_bot_trades
meta:
blockchain: ethereum # TODO: It's actually multichain?
sector: dex
contributors: ["whale_hunter"]
config:
tags: ["evm", "dex", "bot", "trades"]
description: >
Trades by trading bots on EVM chains
columns:
- &blockchain
name: blockchain
description: "Blockchain which the DEX is deployed"
- &block_number
name: block_number
description: "Block number that includes the DEX trade"
- &block_time
name: block_time
description: "UTC event block time of each DEX trade"
- &block_date
name: block_date
description: "UTC event block date of each DEX trade"
- &block_month
name: block_month
description: "UTC event block month of each DEX trade"
- &amount_usd
name: amount_usd
description: "USD value of the trade at time of execution"
- &type
name: type
description: "Wether the trade is a buy or sell"
- &token_bought_amount
name: token_bought_amount
description: "Value of the token bought at time of execution in the original currency"
- &token_bought_symbol
name: token_bought_symbol
description: "Token symbol for token bought in the trade"
- &token_bought_address
name: token_bought_address
description: "Contract address of the token bought"
- &token_sold_amount
name: token_sold_amount
description: "Value of the token sold at time of execution in the original currency"
- &token_sold_symbol
name: token_sold_symbol
description: "Token symbol for token sold in the trade"
- &token_sold_address
name: token_sold_address
description: "Contract address of the token sold"
- &fee_percentage_fraction
name: fee_percentage_fraction
description: "The trading fee in percent, as a fraction of 100"
- &fee_usd
name: fee_usd
description: "USD value of the fee at time of execution"
- &fee_token_amount
name: fee_token_amount
description: "Value of the fee paid at time of execution in the original currency"
- &fee_token_symbol
name: fee_token_symbol
description: "Token symbol for fee token"
- &fee_token_address
name: fee_token_address
description: "Contract address of the fee token"
- &bribe_usd
name: bribe_usd
description: "USD value of the bribe at time of execution"
- &bribe_token_amount
name: bribe_token_amount
description: "Value of the bribe paid at time of execution in the original currency"
- &bribe_token_symbol
name: bribe_token_symbol
description: "Token symbol for bribe token"
- &bribe_token_address
name: bribe_token_address
description: "Contract address of the bribe token"
- &project
name: project
description: "Project name of the DEX"
- &version
name: version
description: "Version of the contract built and deployed by the DEX project"
- &token_pair
name: token_pair
description: "Token symbol pair for each token involved in the trade"
- &project_contract_address
name: project_contract_address
description: "Project contract address which executed the trade on the blockchain"
- &user
name: user
description: "Address which initiated the trade"
- &tx_hash
name: tx_hash
description: "Unique transaction hash value tied to each transaction on the DEX"
- &evt_index
name: evt_index
description: "Index of the corresponding trade event"
- &is_last_trade_in_transaction
name: is_last_trade_in_transaction
description: "Wether the trade is the last hop of the trade transaction, in case of a multi-hop trade"

- name: banana_gun_evm_bot_trades
meta:
blockchain: ethereum # TODO
sector: dex
project: banana_gun
contributors: whale_hunter
config:
tags: ["evm", "dex", "banana_gun", "trades"]
description: >
Banan Gun trades on EVM
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- blockchain
- tx_hash
- evt_index
- check_bot_trades_seed:
seed_file: ref('banana_gun_evm_trades_seed')
blockchain: ethereum
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{{
config(
schema = 'bot_trades',
alias = 'banana_gun_evm',
materialized = 'view',
post_hook = '{{ expose_spells(
blockchains = \'["ethereum", "base", "avalanche_c", "blast", "bnb"]\',
spell_type = "sector",
spell_name = "banana_gun",
contributors = \'["whale_hunter"]\') }}'
)
}}



{% set banana_gun_blockchains = [
ref('banana_gun_avalanche_c_bot_trades')
, ref('banana_gun_ethereum_bot_trades')
, ref('banana_gun_base_bot_trades')
, ref('banana_gun_blast_bot_trades')
, ref('banana_gun_bnb_bot_trades')
] %}

{% for blockchain in banana_gun_blockchains %}
SELECT block_time,
block_date,
block_month,
block_number,
blockchain,
-- Trade
amount_usd,
type,
token_bought_amount,
token_bought_symbol,
token_bought_address,
token_sold_amount,
token_sold_symbol,
token_sold_address,
-- Fees
fee_percentage_fraction,
fee_usd,
fee_token_amount,
fee_token_symbol,
fee_token_address,
-- Bribes
bribe_usd,
bribe_token_amount,
bribe_token_symbol,
bribe_token_address,
-- Dex
project,
version,
token_pair,
project_contract_address,
-- User
user,
tx_hash,
evt_index,
is_last_trade_in_transaction
FROM {{ blockchain }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
{{ config(
alias = 'bot_trades',
schema = 'banana_gun_avalanche_c',
partition_by = ['block_month'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'tx_hash', 'evt_index']
)
}}

{% set project_start_date = '2024-03-15' %}
{% set blockchain = 'avalanche_c' %}
{% set bot_deployer_1 = '0x37aAb97476bA8dC785476611006fD5dDA4eed66B' %}
{% set wavax = '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7' %}
{% set fee_token_symbol = 'AVAX' %}

WITH
botContracts AS (
SELECT
address
FROM
{{ source('avalanche_c','creation_traces') }}
WHERE
(
"from" = {{bot_deployer_1}}
)
AND block_time >= TIMESTAMP '{{project_start_date}}'
),
botTrades AS (
SELECT
trades.block_time,
amount_usd,
IF(
token_sold_address = {{wavax}},
'Buy',
'Sell'
) AS type,
token_bought_amount,
token_bought_symbol,
token_bought_address,
token_sold_amount,
token_sold_symbol,
token_sold_address,
project,
version,
token_pair,
project_contract_address,
tx_from AS user,
tx_to AS bot,
trades.tx_hash,
evt_index
FROM
{{ source('dex', 'trades') }} as trades
JOIN botContracts ON trades.tx_to = botContracts.address
WHERE
trades.blockchain = '{{blockchain}}'
AND trades.block_time >= TIMESTAMP '{{project_start_date}}'
),
highestEventIndexForEachTrade AS (
SELECT
tx_hash,
MAX(evt_index) AS highestEventIndex
FROM
botTrades
GROUP BY
tx_hash
),
botETHDeposits AS (
SELECT
tx_hash,
block_number,
CAST(value AS DECIMAL (38, 0)) AS deltaGwei,
CAST(value AS DECIMAL (38, 0)) AS depositGwei
FROM
{{ source('avalanche_c','traces') }}
JOIN botContracts ON to = botContracts.address
WHERE
block_time >= TIMESTAMP '{{project_start_date}}'
AND value > CAST(0 AS UINT256)
),
botETHWithdrawals AS (
SELECT
tx_hash,
block_number,
CAST(value AS DECIMAL (38, 0)) * -1 AS deltaGwei,
0 AS depositGwei,
block_hash,
to
FROM
{{ source('avalanche_c','traces') }}
JOIN botContracts ON "from" = botContracts.address
WHERE
block_time >= TIMESTAMP '{{project_start_date}}'
AND value > CAST(0 AS UINT256)
),
botEthTransfers AS (
/* Deposits */
(
SELECT
tx_hash,
block_number,
deltaGwei,
depositGwei
FROM
botETHDeposits
)
UNION ALL
/* Withdrawals */
(
SELECT
tx_hash,
block_number,
deltaGwei,
depositGwei
FROM
botETHWithdrawals
)
),
botEthDeltas AS (
SELECT
tx_hash,
block_number,
SUM(deltaGwei) AS feeGwei,
SUM(depositGwei) AS depositGwei
FROM
botEthTransfers
GROUP BY
tx_hash,
block_number
),
minerBribes AS (
SELECT
tx_hash,
SUM(deltaGwei * -1) / 1e18 AS bribeETH
FROM
botETHWithdrawals
JOIN {{ source('avalanche_c','blocks') }} AS blocks ON (
botETHWithdrawals.block_hash = blocks.hash
AND botETHWithdrawals.to = blocks.miner
)
GROUP BY
tx_hash
)
SELECT
block_time,
date_trunc('day', botTrades.block_time) as block_date,
date_trunc('month', botTrades.block_time) as block_month,
block_number,
'{{blockchain}}' AS blockchain,
-- Trade
amount_usd,
type,
token_bought_amount,
token_bought_symbol,
CAST(token_bought_address AS VARCHAR) AS token_bought_address,
token_sold_amount,
token_sold_symbol,
CAST(token_sold_address AS VARCHAR) AS token_sold_address,
-- Fees
ROUND(
CAST(feeGwei AS DOUBLE) / CAST(depositGwei AS DOUBLE),
/* Round feePercentage to 0.01% steps */
4
) AS fee_percentage_fraction,
(feeGwei / 1e18) * price AS fee_usd,
feeGwei / 1e18 fee_token_amount,
'{{fee_token_symbol}}' AS fee_token_symbol,
'{{wavax}}' AS fee_token_address,
-- Bribes
bribeETH * price AS bribe_usd,
bribeETH AS bribe_token_amount,
'{{fee_token_symbol}}' AS bribe_token_symbol,
'{{wavax}}' AS bribe_token_address,
-- Dex
project,
version,
token_pair,
CAST(project_contract_address AS VARCHAR) AS project_contract_address,
-- User
CAST(user AS VARCHAR) AS user,
CAST(botTrades.tx_hash AS VARCHAR) AS tx_hash,
evt_index,
IF(evt_index = highestEventIndex, true, false) AS is_last_trade_in_transaction
FROM
botTrades
JOIN highestEventIndexForEachTrade ON botTrades.tx_hash = highestEventIndexForEachTrade.tx_hash
LEFT JOIN botETHDeltas ON botTrades.tx_hash = botETHDeltas.tx_hash
LEFT JOIN minerBribes ON botTrades.tx_hash = minerBribes.tx_hash
LEFT JOIN {{ source('prices', 'usd') }} ON (
blockchain = '{{blockchain}}'
AND contract_address = {{wavax}}
AND minute = DATE_TRUNC('minute', block_time)
)
ORDER BY
block_time DESC,
evt_index DESC
Loading
Loading