stream PancakeSwapTradesForTWTBNB
# We are interested in the BEP20 Token Transfers for the PancakeSwap case.
# We have a table with (interesting to us) PancakeSwap Pairs. We want to monitor both BUYs and SELLs for them.
where @to in PancakeSwapPairs || @from in PancakeSwapPairs
# Filter out everything except TWT/WBNB
where @to in PancakeSwapPairs && PancakeSwapPairs[@to].Pair == "TWT/WBNB" || @from in PancakeSwapPairs && PancakeSwapPairs[@from].Pair == "TWT/WBNB"
state buyVolumeInUSD = {value: 0, decimals: 0}
state sellVolumeInUSD = {value: 0, decimals: 0}
# Basic initialization variable for later usage
let TWTinUSD = {value: 0, decimals: 0}
if startingBlock == 0 then
startingBlock = @block.number
let cryptorankPriceInUSDforTWT = {value: 0, decimals: 0}
if @token.symbol == "TWT" then
cryptorankPriceInUSDforTWT = getRate(@token.symbol)
TWTinUSD = {value: @value * cryptorankPriceInUSDforTWT.value, decimals: @token.decimals + cryptorankPriceInUSDforTWT.decimals}
# If our Trading Asset (TWT) is used in BUY operation
if @from in PancakeSwapPairs && @token.contract == PancakeSwapPairs[@from].PairFirst then
# The address that initiated the TWT BUY operation
# The according to operation TWT volume update
# The according to operation USD volume update
buyVolumeInUSD = add(buyVolumeInUSD, TWTinUSD)
# If our Trading Asset (TWT) is used in SELL operation
elseif @to in PancakeSwapPairs && @token.contract == PancakeSwapPairs[@to].PairFirst then
sellVolumeInUSD = add(sellVolumeInUSD, TWTinUSD)
let convertedBuyVolumeInUSD = {value: buyVolumeTWT * cryptorankPriceInUSDforTWT.value, decimals: @token.decimals + cryptorankPriceInUSDforTWT.decimals}
let convertedSellVolumeInUSD = {value: sellVolumeTWT * cryptorankPriceInUSDforTWT.value, decimals: @token.decimals + cryptorankPriceInUSDforTWT.decimals}
# Check that BUY or SELL happened
if action == "BUY" || action == "SELL" then
decimals: @token.decimals,
pair: PancakeSwapPairs[pair].Pair,
blockNumber: @block.number,
convertedSellVolumeInUSD,
cryptorankPriceInUSDforTWT