dune_get_relation_sol_transfer.sql 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. -- 思路 获取一段时间的 transfer 数据 根据dextrade 或者 其他筛选 先排除一部分 tx_id 在groupby tx_id 进一步筛选 最后 成功
  2. -- 到了 先排除一部分 就时间太长了
  3. with
  4. input_owner (address) as (
  5. values
  6. ('DZuiugsZ8bJNLYnkLE3aK6Fyj3PdGEeev2kUQ9htk1No'),
  7. ('FoRrce2AqaZAhPZozrH49835VueVj31Vq4hRQNtt1ao7'),
  8. ('7PXAh12kVHji6FRdtpnZprZog9zC5aboVZRt5UnP961n'),
  9. ('4ZStjfNTvTDHGsBGqQLTfXvtALMHwnfpUEdbnRmgbPVm'),
  10. ('62GvRgevD9g93xoyV8SjFL5VKF2h7YhCvJWiLzfNm8uC'),
  11. ('AgPsPtpAiQqMtcmSsGEGvje1dXTco3NGi1gAF7gvYC7L'),
  12. ('BXvEmdFHrSCDE1DYg47U1SgW6juGtSHfx32ijpM5oq23')
  13. ),
  14. exclude_address (address) as (
  15. values
  16. ('AVzP2GeRmqGphJsMxWoqjpUifPpCret7LqWhD8NWQK49'), -- Jupiter Labs Perpetuals Vault Authority
  17. ('A2rCQdCqQB4wu72x9Q7iq1QugtdZmcyWsiSPfgfZzu2u'), -- Jupiter Perpetuals Keeper 1
  18. ('DFZcDnmEYNUK1khquZzx5dQYiEyjJ3N5STqaDVLZ88ZU'), -- Jupiter Perpetuals Keeper 2
  19. ('PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu'), -- Jupiter Labs Perpetuals
  20. ('BUvduFTd2sWFagCunBPLupG8fBTJqweLw9DuhruNFSCm'), -- Jupiter Labs Perpetuals (SOL) Vault
  21. ('WzWUoCmtVv7eqAbU3BfKPU3fhLP6CXR8NCJH78UK9VS'), -- Jupiter Labs Perpetuals (USDC) Vault
  22. ('Gex24YznvguMad1mBzTQ7a64U1CJy59gvsStQmNnnwAd'), -- Jupiter Labs Perpetuals (USDT) Vault
  23. ('Bgarxg65CEjN3kosjCW5Du3wEqvV3dpCGDR3a2HRQsYJ'), -- Jupiter Labs Perpetuals (ETH) Vault
  24. ('FgpXg2J3TzSs7w3WGYYE7aWePdrxBVLCXSxmAKnCZNtZ'), -- Jupiter Labs Perpetuals (BTC) Vault
  25. ('BQ72nSv9f3PRyRKCBnHLVrerrv37CYTHm5h3s9VSGQDV'), -- Jupiter Aggregator Authority 1
  26. ('2MFoS3MPtvyQ4Wh4M9pdfPjz6UhVoNbFbGJAskCPCj3h'), -- Jupiter Aggregator Authority 2
  27. ('HU23r7UoZbqTUuh3vA7emAGztFtqwTeVips789vqxxBw'), -- Jupiter Aggregator Authority 3
  28. ('3CgvbiM3op4vjrrjH2zcrQUwsqh5veNVRjFCB9N6sRoD'), -- Jupiter Aggregator Authority 4
  29. ('6LXutJvKUw8Q5ue2gCgKHQdAN4suWW8awzFVC6XCguFx'), -- Jupiter Aggregator Authority 5
  30. ('CapuXNQoDviLvU1PxFiizLgPNQCxrsag1uMeyk6zLVps'), -- Jupiter Aggregator Authority 6
  31. ('GGztQqQ6pCPaJQnNpXBgELr5cs3WwDakRbh1iEMzjgSJ'), -- Jupiter Aggregator Authority 7
  32. ('9nnLbotNTcUhvbrsA6Mdkx45Sm82G35zo28AqUvjExn8'), -- Jupiter Aggregator Authority 8
  33. ('3LoAYHuSd7Gh8d7RTFnhvYtiTiefdZ5ByamU42vkzd76'), -- Jupiter Aggregator Authority 9
  34. ('DSN3j1ykL3obAVNv7ZX49VsFCPe4LqzxHnmtLiPwY6xg'), -- Jupiter Aggregator Authority 10
  35. ('69yhtoJR4JYPPABZcSNkzuqbaFbwHsCkja1sP1Q2aVT5'), -- Jupiter Aggregator Authority 11
  36. ('6U91aKa8pmMxkJwBCfPTmUEfZi6dHe7DcFq2ALvB2tbB'), -- Jupiter Aggregator Authority 12
  37. ('7iWnBRRhBCiNXXPhqiGzvvBkKrvFSWqqmxRyu9VyYBxE'), -- Jupiter Aggregator Authority 13
  38. ('4xDsmeTWPNjgSVSS1VTfzFq3iHZhp77ffPkAmkZkdu71'), -- Jupiter Aggregator Authority 14
  39. ('GP8StUXNYSZjPikyRsvkTbvRV1GBxMErb59cpeCJnDf1'), -- Jupiter Aggregator Authority 15
  40. ('HFqp6ErWHY6Uzhj8rFyjYuDya2mXUpYEk8VW75K9PSiY'), -- Jupiter Aggregator Authority 16
  41. ('45ruCyfdRkWpRNGEqWzjCiXRHkZs8WXCLQ67Pnpye7Hp'), -- Jupiter Partner Referral Fee Vault
  42. ('jupoNjAxXgZ4rjzxzPMP4oxduvQsQtZzyknqvzYNrNu'), -- Jupiter Limit Order
  43. ('Cw8CFyM9FkoMi7K7Crf6HNQqf4uEMzpKw6QNghXLvLkY'), -- Jitotip 3
  44. ('j1o2qRpjcyUwEvwtcfhEQefh773ZgjxcVRry7LDqg5X'), -- Jupiter Limit Order V2
  45. ('DD3AeAssFvjqTvRTrRAtpfjkBF8FpVKnFuwnMLN9haXD') -- Marginfi Bank Liquidity (SOL) Vault Authority
  46. -- ["DSwpgjMvXhtGn6BsbqmacdBZyfLj6jSWf3HJpdJtmg6N", "Dexlab"],
  47. -- ["BSwp6bEBihVLdqJRKGgzjcGLHkcTuzmSo1TQkHepzH8p", "Bonkswap"],
  48. -- ["Eo7WjKq67rjJQSZxS6z3YkapzY3eMj6Xy8X5EQVn5UaB", "Meteora"],
  49. -- ["FLUXubRmkEi2q6K3Y9kBPg9248ggaZVsoSFhtJHSrm1X", "FluxBeam"],
  50. -- ["HyaB3W9q6XdA5xwpU4XnSZV94htfmbmqJXZcEbRaJutt", "Invariant"],
  51. -- ["PERPHjGBqRHArX4DySjwM6UJHiR3sWAatqfdBS2qQJu", "Perps"],
  52. -- ["treaf4wWBBty3fHdyBpo35Mz84M8k3heKXmjmi9vFt5", "Helium Network"],
  53. -- ["DjVE6JNiYqPL2QXyCUUh8rNjHrbz9hXHNYt99MQ59qw1", "Orca V1"],
  54. -- ["CURVGoZn8zycx6FXwwevgBTB2gVvdbGTEpvMJDbgs2t4", "Aldrin V2"],
  55. -- ["LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo", "Meteora DLMM"],
  56. -- ["PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY", "Phoenix"],
  57. -- ["SSwapUtytfBdBn1b9NUGG6foMVPtcWgpRU32HToDUZr", "Saros"],
  58. -- ["AMM55ShdkoGRB5jVYPjWziwk8m5MpwyDgsMWHaMSQWH6", "Aldrin"],
  59. -- ["9tKE7Mbmj4mxDjWatikzGAtkoWosiiZX9y6J4Hfm2R8H", "Oasis"],
  60. -- ["MERLuDFBMmsHnsBPZw2sDQZHvXFMwp8EdjudcU2HKky", "Mercurial"],
  61. -- ["SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8", "Token Swap"],
  62. -- ["srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX", "Openbook"],
  63. -- ["CLMM9tUoggJu2wagPkkqs9eFG4BWhVBZWkP1qv3Sp7tR", "Crema"],
  64. -- ["DecZY86MU5Gj7kppfUCEmd4LbXXuyZH1yHaP2NTqdiZB", "Saber (Decimals)"],
  65. -- ["CTMAxxk34HjKWxQ3QLZK1HpaLXmBveao3ESePXbiyfzh", "Cropper"],
  66. -- ["CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK", "Raydium CLMM"],
  67. -- ["whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc", "Whirlpool"],
  68. -- ["PSwapMdSai8tjrEXcxFeQth87xC4rRsa4VA5mhGhXkP", "Penguin"],
  69. -- ["EewxydAPCCVuNEyrVN68PuSYdQ7wKn27V9Gjeoi8dy3S", "Lifinity V1"],
  70. -- ["C1onEW2kPetmHmwe74YC1ESx3LnFEpVau6g2pg4fHycr", "Clone Protocol"],
  71. -- ["2wT8Yq49kHgDzXuPxZSaeLaH1qbmGXtEyPy64bL7aD3c", "Lifinity V2"],
  72. -- ["Dooar9JkhdZ7J3LHN3A7YCuoGRUggXhQaG4kijfLGU2j", "StepN"],
  73. -- ["9W959DqEETiGZocYWCQPaJ6sBmUzgfxXfqGeTEdp3aQP", "Orca V2"],
  74. -- ["opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb", "OpenBook V2"],
  75. -- ["MarBmsSgKXdrN1egZf5sqe1TMai9K1rChYNDJgjq7aD", "Marinade"],
  76. -- ["GFXsSL5sSaDfNFQUYsHekbWBW1TsFdjDYzACh62tEHxn", "GooseFX"],
  77. -- ["SSwpkEEcbUqx4vtoEByFjSkhKdCT862DNVb52nZg1UZ", "Saber"],
  78. -- ["stkitrT1Uoy18Dk1fTrgPw8W6MVzoCfYoAFT4MLsmhq", "Sanctum"],
  79. -- ["675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8", "Raydium"],
  80. ),
  81. trade as (
  82. SELECT
  83. tx_id
  84. from
  85. dex_solana.trades
  86. where
  87. block_time > CAST('{{begin_date_01}}' AS TIMESTAMP)
  88. and block_time <= CAST('{{end_date_01}}' AS TIMESTAMP)
  89. ),
  90. -- temp_01 可以获得非swap 的 input_owner 参与的transfer 交易 (不是一笔交易的全部 一笔交易可能实际非swap 但会有多个transfer mint 等)
  91. temp_01 as (
  92. SELECT
  93. transfer.*
  94. -- tx_id,
  95. -- block_date,
  96. -- amount,
  97. -- action,
  98. -- token_mint_address,
  99. -- from_owner,
  100. -- to_owner,
  101. -- from_token_account,
  102. -- to_token_account
  103. FROM
  104. (
  105. SELECT
  106. tx_id,
  107. block_date,
  108. amount,
  109. action,
  110. token_mint_address,
  111. from_owner,
  112. to_owner,
  113. from_token_account,
  114. to_token_account
  115. FROM
  116. tokens_solana.transfers
  117. where
  118. block_date > CAST('{{begin_date_02}}' AS TIMESTAMP)
  119. and block_date <= CAST('{{end_date_02}}' AS TIMESTAMP)
  120. ) transfer
  121. inner join input_owner on (
  122. input_owner.address = transfer.from_owner
  123. or input_owner.address = transfer.to_owner
  124. )
  125. -- 非swap 的交易
  126. left join trade on trade.tx_id = transfer.tx_id
  127. WHERE
  128. trade.tx_id is null
  129. ),
  130. -- 下一步 temp_02 有连个方案
  131. -- 1. groupby tx_id 去除 一些是 none action 含有mint等非 ,mint_token_address为非 sol等的tx_id 获取到真正的tranfer 交易 然后获取他们的 relation owner
  132. -- 1. 缺点 明显太复杂 不知道时间能否允许不
  133. -- 2. 不用groupby 用union all 获取 exclude_ids 然后获取真正的 relation owner
  134. -- exclude_tx_ids as (
  135. -- SELECT
  136. -- tx_id
  137. -- from
  138. -- temp_01
  139. -- where
  140. -- action != 'transfer'
  141. -- UNION all
  142. -- SELECT
  143. -- tx_id
  144. -- from
  145. -- temp_01
  146. -- where
  147. -- token_mint_address != 'So11111111111111111111111111111111111111112'
  148. -- UNION all
  149. -- SELECT
  150. -- tx_id
  151. -- from
  152. -- temp_01
  153. -- where
  154. -- from_owner is null
  155. -- or to_owner is null
  156. -- UNION all
  157. -- SELECT
  158. -- temp_01.tx_id as tx_id
  159. -- from
  160. -- temp_01
  161. -- left join exclude_address on temp_01.from_owner = exclude_address.address
  162. -- or temp_01.to_owner = exclude_address.address
  163. -- where
  164. -- exclude_address.address is null
  165. -- )
  166. -- temp_02 as (
  167. -- select
  168. -- temp_01.*
  169. -- from
  170. -- temp_01
  171. -- left join exclude_tx_ids on temp_01.tx_id = exclude_tx_ids.tx_id
  172. -- where
  173. -- exclude_tx_ids.tx_id is null
  174. -- )
  175. temp_02 as (
  176. SELECT
  177. tx_id
  178. from
  179. temp_01
  180. GROUP BY
  181. tx_id
  182. HAVING
  183. SUM(
  184. CASE
  185. WHEN action = 'transfer' THEN 1
  186. ELSE 0
  187. END
  188. ) > 0
  189. and COUNT(DISTINCT COALESCE(token_mint_address, 'NULL')) = 1
  190. and COUNT(DISTINCT COALESCE(action, 'NULL')) = 1
  191. and (
  192. SUM(
  193. CASE
  194. WHEN token_mint_address = 'So11111111111111111111111111111111111111112' THEN 1
  195. ELSE 0
  196. END
  197. ) > 0
  198. and sum(amount) >= 0.5 * 1e9
  199. )
  200. and SUM(
  201. CASE
  202. WHEN from_owner is null THEN 1
  203. ELSE 0
  204. END
  205. ) = 0
  206. and SUM(
  207. CASE
  208. WHEN to_owner is null THEN 1
  209. ELSE 0
  210. END
  211. ) = 0
  212. )
  213. SELECT
  214. -- DISTINCT
  215. temp_02.tx_id as tx_id,
  216. action,
  217. from_owner,
  218. to_owner,
  219. from_token_account,
  220. to_token_account,
  221. amount
  222. from
  223. temp_02
  224. inner join temp_01 on temp_01.tx_id = temp_02.tx_id