Let's take these two examples here, they are swaps done on VVS SINGLE/USDC Liquidity Pool:
Example 1: Buy USDC with SINGLE (i.e. SELL SINGLE) https://cronoscan.com/tx/0xbd958b205291355d681cf01fa01f2064078d7be3b1ba63be8a9d2dff72bcce48
Example 2: Buy SINGLE with USDC (i.e. BUY SINGLE) https://cronoscan.com/tx/0x4f8898c244bd7bc7f77d6d8aec7af670ff6d86f252d9d231860dc1528dacf91c
If you follow steps to decode the transaction using contract.events.Transfer().processReceipt,
from typing import Dict
from web3 import Web3
from web3.exceptions import TransactionNotFound
cronos_mainnet_rpc = "https://evm.cronos.org"
w3 = Web3(Web3.HTTPProvider(cronos_mainnet_rpc))
pool_address = Web3.toChecksumAddress("0x0fBAB8A90CAC61b481530AAd3a64fE17B322C25d")
pool_abi = '... very long ...'
pool_contract = w3.eth.contract(address=pool_address, abi=pool_abi)
trx_hash = '0xbd958b205291355d681cf01fa01f2064078d7be3b1ba63be8a9d2dff72bcce48'
trx = w3.eth.getTransaction(trx_hash) # This only means you sent the transaction. It does not mean it succeeded.
trx_block_number = trx.blockNumber
trx_unix_ts = w3.eth.getBlock(trx_block_number).timestamp
status : int = -1
while True:
try:
trx_receipt = w3.eth.getTransactionReceipt(trx_hash)
status = trx_receipt['status']
if status == 0 or status == 1:
logs_summary = []
from web3._utils.events import EventLogErrorFlags
logs = pool_contract.events.Transfer().processReceipt(trx_receipt, EventLogErrorFlags.Warn)
for log in logs:
transaction_type = log.event
log_index = log.logIndex # Each 'log' has different log_index, you can use it to determine which is first and which comes after.
transaction_index = log.transactionIndex # But they have same transaction_index!
sender_address = log.args['from']
destination_address = log.args['to']
amount = log.args['value']
logs_summary.append( {
'log_index' : log_index,
'transaction_index' : transaction_index,
'transaction_type' : transaction_type,
'sender_address' : sender_address,
'destination_address' : destination_address,
'amount' : amount
})
break
except TransactionNotFound:
# Transaction not found!
pass
trx_status : str = "Pending"
if status==1:
trx_status = "Confirmmed"
elif status==0:
trx_status = "Reverted"
transaction_report : Dict = {
'trx_hash' : trx_hash,
'trx_block_number' : trx.blockNumber,
'status' : trx_status,
'trx_unix_ts' : trx_unix_ts,
'logs' : logs_summary
}
There's nothing from "logs" which tells you whether you're buying? Or selling the SINGLE coin?
Note
- 0x0fBAB8A90CAC61b481530AAd3a64fE17B322C25d is SINGLE/USDC liquidity pool on VVS/Cronos chain.
- 0x88D669588b50F3cAa21507Ee5A867f132F9db97C is the wallet address
Question is, from "logs" (Nothing under log.args definitely), I find nothing which can tell me whether the transaction is a BUY trade? or a SELL trade.
logs = pool_contract.events.Transfer().processReceipt(trx_receipt, EventLogErrorFlags.Warn)
This is important as in additional to the first objective, which is to be able to tell whether it's BUY vs SELL, this information will also help me to adjust "amount" using either USDC's decimal, or SINGLE's decimal. That'd be the second objective.