# 数据 | 数据分析之Dune Analytics（一）


By [一点儿知识](https://paragraph.com/@0x0f3193886ae7ecf1bb1d335c375a0526d4522b34) · 2022-05-02

---

写下日常的Dune 学习笔记，方便回看,图片不清晰可以去原地址查看:

> [https://docs.google.com/presentation/d/1I6vDOS52uMCmWg3KIbuKYe8lK-8\_ol9bEksIUEHNNLI/edit#slide=id.p](https://docs.google.com/presentation/d/1I6vDOS52uMCmWg3KIbuKYe8lK-8_ol9bEksIUEHNNLI/edit#slide=id.p)
> 
> [![]({{DOMAIN}}/editor/youtube/play.png)](https://www.youtube.com/watch?v=EnUUeM7FPac)

💡文章中出现的代码块可以向右滑动查看。

![](https://storage.googleapis.com/papyrus_images/7b8127a2b156c3abbe663ac23d51f4bcea79276828b365e2d5c976130ea66209.png)

![](https://storage.googleapis.com/papyrus_images/17bc8963856f9f25f9a2e96e738f478a29707b9bdb3483019ab26dd4ed5df651.png)

上图对应外部合约与内部合约，下图对应日志内容，调用时看是transtions、trace、logs中的哪一个，就对应哪一种数据调用。

![](https://storage.googleapis.com/papyrus_images/b015993ddc60be10d84a0885a3d97cdfd6f51d12ef0771c882e8dd331af092c7.png)

我们在Dune中主要是去调用区块浏览器的交易数据和日志信息，一般钱包地址签名发起的均为外部合约，而合约与合约之间的交互为内部合约（如下图）

![](https://storage.googleapis.com/papyrus_images/f3b8bd8069a29e04926b47afee216de7b2f04a733381798d9d639d871f5d6d7d.png)

    SELECT * FROM ethereum."transactions”
    LIMIT 10
    

![](https://storage.googleapis.com/papyrus_images/23b70b4ac2ef83ff13cfa300cbc4f9fe6645db94200eebc1c16b1038b3093a50.png)

SELECT——指想要目标的哪一栏(column)

\*——指所有，这里是想要每一个单一的以太坊交易

FROM——指要查询的表，这里是以太坊所有的交易

Run快捷键——Command+Enter

LIMIT——执行我所想要执行的

10——最早的10笔交易

    SELECT "hash" FROM ethereum."transactions"
    LIMIT 10
    

![](https://storage.googleapis.com/papyrus_images/8bac10a446b4d3ff8ce1f503c8adf4dddf8d6f5bd1dddf181186a93ce85c7a05.png)

选择列(column)或者表的时候必须要有双引号，如这里的”hash”，英文引号。

    SELECT * FROM ethereum."transactions"
    WHERE "hash" = '\x66d2be3bf2865711e769b64b55a26fe7720e93111506f75f42587fc65a2e7e4e'
    

![](https://storage.googleapis.com/papyrus_images/643165fed7db331bd5fb472a8ed50257ad29af3f084355c7674ff4cdc358760e.png)

WHERE——在结果里面筛选自己需要的内容

Hash——0x在sql语句中，0必须换成 \\

内部调用的tx\_hash与外部调用的hash（上面均是外部调用）是同一个值即：Transaction Hash

    SELECT * FROM ethereum."traces" as tr --mention not all transfers have an internal tx, USDC is special due to their proxy pattern
    WHERE tr."tx_hash"='\x979b8fb58254f28f5f659df16d81c6ac2103d8b135ba809474bb06e75c8a0ec2'
    

![](https://storage.googleapis.com/papyrus_images/d2609a0563595340442899b15b9532d9617a29ef727584f29f3eb87d450c0b23.png)

    SELECT * FROM ethereum."logs" el
    WHERE el."tx_hash"='\x979b8fb58254f28f5f659df16d81c6ac2103d8b135ba809474bb06e75c8a0ec2'
    

![](https://storage.googleapis.com/papyrus_images/3f94951fef5bcebb3d56d2808f9b179710ed85e76a483efc42ebcb20d65f2b21.png)

上面分别是调用的内部合约和日志数据

![](https://storage.googleapis.com/papyrus_images/5c0d7149670f24fda0499543513a6c04a70148fa34866e84fbc004bc71c1c94b.png)

💡 上方我们通过日志查询得出的data数据是解码之前的Hex数据，我们想要知道究竟Transfer了多少的usdt，我们需要对数据解码，需要用到函数bytea2numeric（字节转数字）函数

因为这笔交易的U值是1000，但这里的Value是1000000000，是因为数据在并不是以10进制的形式存储在链上，我们需要移除6个0，因此还需要除以10^6(1e6)

![](https://storage.googleapis.com/papyrus_images/acce8cc28b895fc0ec907112ea58e250ee5229095650ed7ce99211e59be1ccb9.png)

右下角数据名为?column?，我们可以为其重命名为data\_translated，这里as同样可以省略

    SELECT "data", bytea2numeric("data")/1e6 as "data_translated" FROM ethereum."logs" el
    WHERE el."tx_hash"='\x979b8fb58254f28f5f659df16d81c6ac2103d8b135ba809474bb06e75c8a0ec2'
    

![](https://storage.googleapis.com/papyrus_images/bc12e62e12eb029d52da20d4cca710e5e5a2bda5f23bf6fa23a3b1bf2ead4cea.png)

* * *

当我们在polygon网络时，方式是差不多的,假设我们想要查询Aave质押状况，我们直接找到官方整理好的Aave表即可，然后去polygonscan找到对应的交易

![](https://storage.googleapis.com/papyrus_images/fce3b8c368a7fdd9af8483ebd8355bfc0d6c7577586d5df549ff375949ae1658.png)

![](https://storage.googleapis.com/papyrus_images/4c57c262dcd6005c6494b83aa632a86eca9788f1deb7e649871e0979c1c1197c.png)

这里的Address和Number是可以和Dune的User和Amount对上号的，点击上方104数字旁的Address，我们可以进入合约页面

![](https://storage.googleapis.com/papyrus_images/cc665dd04dd5d26e564ccc7d7796a7eac19ca88fef73dc241ab5d88c4365c0f3.png)

然后我们搜索合约中的deposit函数，函数包含reserve(Token合约地址，如USDT)、user（钱包地址）、onBehalfof、amount（质押数量）、referral（提交的代码）等参数，这里的参数都可以和Dune结果栏的参数对上号。

* * *

下方SQL语句是筛选出给定地址有质押的情况，并对其排序,Descending(DESC)是递减，没有的话是按照递增排序。

    SELECT * FROM aave_v2."LendingPool_evt_Deposit"
    WHERE "user" IN ('\x0f8361ef329b43fa48ac66a7cd8f619c517274f1','\xa409cceeb14f0228b7951b8d08d119131d85b855','\xa783839c4fa4e7030866ddb3869db5355a820679','\x6f3a6d9867d6213056b55ce52b55514215deb343','\xd43de4138422ad09890203c2e82624715350b784','\xaf5f36cbdc99e25ccd8ab1a4ec23f15a90598645','\xea0f1f68d7c3f580a80e170a21ee0ce34cdcdd17','\x85f3f846a2e13970adea6c213a90c8f61c6e86ef')
    ORDER BY "amount" DESC
    

![](https://storage.googleapis.com/papyrus_images/c29968746ad655b79176fa31cb057961641f0672bc39f65b264b5b1a1a9cfb0f.png)

该结果运行后会筛选出给定地址中，于一年内发生的区块事件，最多输出为10条。

    WHERE "user" IN ('\x0f8361ef329b43fa48ac66a7cd8f619c517274f1','\xa409cceeb14f0228b7951b8d08d119131d85b855','\xa783839c4fa4e7030866ddb3869db5355a820679','\x6f3a6d9867d6213056b55ce52b55514215deb343','\xd43de4138422ad09890203c2e82624715350b784','\xaf5f36cbdc99e25ccd8ab1a4ec23f15a90598645','\xea0f1f68d7c3f580a80e170a21ee0ce34cdcdd17','\x85f3f846a2e13970adea6c213a90c8f61c6e86ef')
    AND "evt_block_time" > now() - interval '1 year'
    LIMIT 10
    

* * *

下方将展示如何将Ethereum.logs的Topic2的内容在Dune中输出为钱包地址。

![](https://storage.googleapis.com/papyrus_images/75d20c4a481cfb252afc90e7e694f6436f6af1abf20f8c5530753da551ac7c86.png)

    SELECT CONCAT( '\x', RIGHT(encode("topic2", 'hex'),40))::bytea as from_address FROM ethereum."logs" el
    WHERE el."tx_hash"='\x979b8fb58254f28f5f659df16d81c6ac2103d8b135ba809474bb06e75c8a0ec2'
    

![](https://storage.googleapis.com/papyrus_images/57bc0773f33144067b7d41f029bae90c3af2e92e12f981311641ea384921909d.png)

先用encode对topic2的hex内容进行解码，然后取右侧40位字符，去掉了0x，后与\\x进行组合，此时组合的数据为字符串，我们需要将其转换为字节，故加入::bytea

---

*Originally published on [一点儿知识](https://paragraph.com/@0x0f3193886ae7ecf1bb1d335c375a0526d4522b34/dune-analytics)*
