forked from re-doubt/ton-etl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dedust_swap.py
63 lines (56 loc) · 2.35 KB
/
dedust_swap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from model.parser import Parser, TOPIC_MESSAGES
from loguru import logger
from db import DB
from pytoniq_core import Cell, Address
from model.dexswap import DEX_DEDUST, DexSwapParsed
from model.dedust import read_dedust_asset
from parsers.message.swap_volume import estimate_volume
# twin non-stable pools to avoid wrong prices estimation
# TODO - move to a special table?
BLACKLIST = set([
Parser.uf2raw('EQA0a6c40n_Kejx_Wj0vowdeYCFYG9XnLdLMRHihXc27cng5'),
Parser.uf2raw('EQDpuDAY31FH2jM9PysFsmJ3aXMMReGYb_P65aDOXVYDcCJX')
])
class DedustSwap(Parser):
def topics(self):
return [TOPIC_MESSAGES]
def predicate(self, obj) -> bool:
# only external messages not blacklist
return obj.get("opcode", None) == Parser.opcode_signed(0x9c610de3) and \
obj.get("direction", None) == "out" and \
obj.get("destination", 'None') is None and \
not obj.get("source", None) in BLACKLIST
def handle_internal(self, obj, db: DB):
cell = Parser.message_body(obj, db).begin_parse()
op_id = cell.load_uint(32) # swap#9c610de3
asset_in = read_dedust_asset(cell)
asset_out = read_dedust_asset(cell)
amount_in = cell.load_coins()
amount_out = cell.load_coins()
if amount_in == 0 or amount_out == 0:
logger.info(f"Skipping zero amount swap for {obj}")
return
payload = cell.load_ref().begin_parse()
sender_addr = payload.load_address()
referral_addr = payload.load_address()
reserve0 = payload.load_coins()
reserve1 = payload.load_coins()
swap = DexSwapParsed(
tx_hash=Parser.require(obj.get('tx_hash', None)),
msg_hash=Parser.require(obj.get('msg_hash', None)),
trace_id=Parser.require(obj.get('trace_id', None)),
platform=DEX_DEDUST,
swap_utime=Parser.require(obj.get('created_at', None)),
swap_user=sender_addr,
swap_pool=Parser.require(obj.get('source', None)),
swap_src_token=asset_in,
swap_dst_token=asset_out,
swap_src_amount=amount_in,
swap_dst_amount=amount_out,
referral_address=referral_addr,
reserve0=reserve0,
reserve1=reserve1
)
estimate_volume(swap, db)
db.serialize(swap)
db.discover_dex_pool(swap)