Query editor
-- Moolah Contract on BSC-- Market ID: 0xa6a01504ccb6a0e3832e1fae31cc4f606a7c38cd76071f27befd013b8e46e78eWITH borrow_events AS (SELECTtopic3 AS user_address,-- Borrow(id, caller, onBehalf, receiver, assets, shares)-- shares is the 2nd 32-byte word in data (bytes 32-64)bytearray_to_uint256(bytearray_substring(data, 65, 32)) AS shares_changeFROM bnb.logsWHERE contract_address = 0x8f73b65b4caaf64fba2af91cc5d4a2a1318e5d8cAND topic1 = 0xa6a01504ccb6a0e3832e1fae31cc4f606a7c38cd76071f27befd013b8e46e78eAND topic0 = 0x570954540bed6b1304a87dfe815a5eda4a648f7097a16240dcd85c9b5fd42a43),repay_events AS (SELECTtopic3 AS user_address,-- Repay(id, caller, onBehalf, assets, shares)-- caller and onBehalf are indexed, so data only contains: assets, shares-- shares is the 2nd 32-byte word in data (bytes 32-63)-1 * cast(bytearray_to_uint256(bytearray_substring(data, 33, 32)) as double) AS shares_changeFROM bnb.logsWHERE contract_address = 0x8f73b65b4caaf64fba2af91cc5d4a2a1318e5d8cAND topic1 = 0xa6a01504ccb6a0e3832e1fae31cc4f606a7c38cd76071f27befd013b8e46e78eAND topic0 = 0x52acb05cebbd3cd39715469f22afbf5a17496295ef3bc9bb5944056c63ccaa09),liquidate_events AS (SELECTtopic3 AS user_address,-- Liquidate(id, caller, borrower, repaidAssets, repaidShares, seizedAssets, badDebtAssets, badDebtShares)-- repaidShares is the 2nd 32-byte word (bytes 32-64)-- badDebtShares is the 5th 32-byte word (bytes 128-160)-1 * (cast(bytearray_to_uint256(bytearray_substring(data, 33, 32)) as double) +cast(bytearray_to_uint256(bytearray_substring(data, 129, 32)) as double)) AS shares_changeFROM bnb.logsWHERE contract_address = 0x8f73b65b4caaf64fba2af91cc5d4a2a1318e5d8cAND topic1 = 0xa6a01504ccb6a0e3832e1fae31cc4f606a7c38cd76071f27befd013b8e46e78eAND topic0 = 0xa4946ede45d0c6f06a0f5ce92c9ad3b4751452d2fe0e25010783bcab57a67e41),all_changes AS (SELECT user_address, cast(shares_change as double) as shares_change FROM borrow_eventsUNION ALLSELECT user_address, shares_change FROM repay_eventsUNION ALLSELECT user_address, shares_change FROM liquidate_events)SELECTuser_address,SUM(shares_change) / 1e24 AS current_borrow_shares,(SUM(shares_change) / SUM(SUM(shares_change)) OVER ()) * 100 AS percentage_of_totalFROM all_changesGROUP BY 1HAVING SUM(shares_change) > 0 -- 过滤掉已经还清的用户ORDER BY 2 DESC