|
| 1 | +{{ |
| 2 | + config( |
| 3 | + schema = 'tokens_v1' |
| 4 | + ,alias = 'erc20' |
| 5 | + ,materialized = 'table' |
| 6 | + ,partition_by = ['blockchain'] |
| 7 | + ,post_hook='{{ expose_spells(\'[ |
| 8 | + "arbitrum" |
| 9 | + ,"avalanche_c" |
| 10 | + ,"base" |
| 11 | + ,"blast" |
| 12 | + ,"bnb" |
| 13 | + ,"boba" |
| 14 | + ,"celo" |
| 15 | + ,"corn" |
| 16 | + ,"ethereum" |
| 17 | + ,"fantom" |
| 18 | + ,"fuse" |
| 19 | + ,"gnosis" |
| 20 | + ,"goerli" |
| 21 | + ,"ink" |
| 22 | + ,"abstract" |
| 23 | + ,"kaia" |
| 24 | + ,"lens" |
| 25 | + ,"linea" |
| 26 | + ,"mantle" |
| 27 | + ,"nova" |
| 28 | + ,"optimism" |
| 29 | + ,"ronin" |
| 30 | + ,"polygon" |
| 31 | + ,"scroll" |
| 32 | + ,"sei" |
| 33 | + ,"sepolia" |
| 34 | + ,"shape" |
| 35 | + ,"worldchain" |
| 36 | + ,"zkevm" |
| 37 | + ,"zksync" |
| 38 | + ,"zora" |
| 39 | + ,"bob" |
| 40 | + ,"sonic" |
| 41 | + ,"sophon" |
| 42 | + ,"berachain" |
| 43 | + ,"apechain" |
| 44 | + ,"opbnb" |
| 45 | + ,"unichain" |
| 46 | + ]\', |
| 47 | + "sector", |
| 48 | + "tokens", |
| 49 | + \'["bh2smith","0xManny","hildobby","soispoke","dot2dotseurat","mtitus6","wuligy","lgingerich","0xRob","jeff-dude","viniabussafi","IrishLatte19","angus_1","Henrystats","rantum", "IrishLatte19"]\') }}' |
| 50 | + ) |
| 51 | +}} |
| 52 | + |
| 53 | +/* |
| 54 | + the main source for erc20 tokens is dune.definedfi.dataset_tokens -- an automated source pulled into Dune |
| 55 | + in order to maintain the same amount of coverage of tokens as before, and provide future addition framework, each chain still contains a static file to add for any missing in the automated source |
| 56 | +*/ |
| 57 | + |
| 58 | +{% set static_models = { |
| 59 | + 'tokens_arbitrum': {'blockchain': 'arbitrum', 'model': ref('tokens_arbitrum_erc20')} |
| 60 | + ,'tokens_avalanche_c': {'blockchain': 'avalanche_c', 'model': ref('tokens_avalanche_c_erc20')} |
| 61 | + ,'tokens_base': {'blockchain': 'base', 'model': ref('tokens_base_erc20')} |
| 62 | + ,'tokens_bnb': {'blockchain': 'bnb', 'model': ref('tokens_bnb_bep20')} |
| 63 | + ,'tokens_celo': {'blockchain': 'celo', 'model': ref('tokens_celo_erc20')} |
| 64 | + ,'tokens_ethereum': {'blockchain': 'ethereum', 'model': ref('tokens_ethereum_erc20')} |
| 65 | + ,'tokens_fantom': {'blockchain': 'fantom', 'model': ref('tokens_fantom_erc20')} |
| 66 | + ,'tokens_fuse': {'blockchain': 'fuse', 'model': ref('tokens_fuse_erc20')} |
| 67 | + ,'tokens_gnosis': {'blockchain': 'gnosis', 'model': ref('tokens_gnosis_erc20')} |
| 68 | + ,'tokens_goerli': {'blockchain': 'goerli', 'model': ref('tokens_goerli_erc20')} |
| 69 | + ,'tokens_mantle': {'blockchain': 'mantle', 'model': ref('tokens_mantle_erc20')} |
| 70 | + ,'tokens_optimism': {'blockchain': 'optimism', 'model': ref('tokens_optimism_erc20')} |
| 71 | + ,'tokens_polygon': {'blockchain': 'polygon', 'model': ref('tokens_polygon_erc20')} |
| 72 | + ,'tokens_scroll': {'blockchain': 'scroll', 'model': ref('tokens_scroll_erc20')} |
| 73 | + ,'tokens_shape': {'blockchain': 'shape', 'model': ref('tokens_shape_erc20')} |
| 74 | + ,'tokens_zkevm': {'blockchain': 'zkevm', 'model': ref('tokens_zkevm_erc20')} |
| 75 | + ,'tokens_zksync': {'blockchain': 'zksync', 'model': ref('tokens_zksync_erc20')} |
| 76 | + ,'tokens_zora': {'blockchain': 'zora', 'model': ref('tokens_zora_erc20')} |
| 77 | + ,'tokens_blast': {'blockchain': 'blast', 'model': ref('tokens_blast_erc20')} |
| 78 | + ,'tokens_sepolia': {'blockchain': 'sepolia', 'model': ref('tokens_sepolia_erc20')} |
| 79 | + ,'tokens_sei': {'blockchain': 'sei', 'model': ref('tokens_sei_erc20')} |
| 80 | + ,'tokens_nova': {'blockchain': 'nova', 'model': ref('tokens_nova_erc20')} |
| 81 | + ,'tokens_linea': {'blockchain': 'linea', 'model': ref('tokens_linea_erc20')} |
| 82 | + ,'tokens_worldchain': {'blockchain': 'worldchain', 'model': ref('tokens_worldchain_erc20')} |
| 83 | + ,'tokens_kaia': {'blockchain': 'kaia', 'model': ref('tokens_kaia_erc20')} |
| 84 | + ,'tokens_tron': {'blockchain': 'tron', 'model': ref('tokens_tron_erc20')} |
| 85 | + ,'tokens_ronin': {'blockchain': 'ronin', 'model': ref('tokens_ronin_erc20')} |
| 86 | + ,'tokens_bob': {'blockchain': 'bob', 'model': ref('tokens_bob_erc20')} |
| 87 | + ,'tokens_flare': {'blockchain': 'flare', 'model': ref('tokens_flare_erc20')} |
| 88 | + ,'tokens_boba': {'blockchain': 'boba', 'model': ref('tokens_boba_erc20')} |
| 89 | + ,'tokens_viction': {'blockchain': 'viction', 'model': ref('tokens_viction_erc20')} |
| 90 | + ,'tokens_sonic': {'blockchain': 'sonic', 'model': ref('tokens_sonic_erc20')} |
| 91 | + ,'tokens_corn': {'blockchain': 'corn', 'model': ref('tokens_corn_erc20')} |
| 92 | + ,'tokens_ink': {'blockchain': 'ink', 'model': ref('tokens_ink_erc20')} |
| 93 | + ,'tokens_sophon': {'blockchain': 'sophon', 'model': ref('tokens_sophon_erc20')} |
| 94 | + ,'tokens_abstract': {'blockchain': 'abstract', 'model': ref('tokens_abstract_erc20')} |
| 95 | + ,'tokens_berachain': {'blockchain': 'berachain', 'model': ref('tokens_berachain_erc20')} |
| 96 | + ,'tokens_apechain': {'blockchain': 'apechain', 'model': ref('tokens_apechain_erc20')} |
| 97 | + ,'tokens_opbnb': {'blockchain': 'opbnb', 'model': ref('tokens_opbnb_erc20')} |
| 98 | + ,'tokens_unichain': {'blockchain': 'unichain', 'model': ref('tokens_unichain_erc20')} |
| 99 | + ,'tokens_lens': {'blockchain': 'lens', 'model': ref('tokens_lens_erc20')} |
| 100 | +} %} |
| 101 | + |
| 102 | +with automated_source as ( |
| 103 | + with evms_info as ( |
| 104 | + select |
| 105 | + blockchain |
| 106 | + , case |
| 107 | + when blockchain = 'sei' then 531 -- the automated erc20 source below (definedfi) has a bug on networkid value for SEI EVM chain |
| 108 | + else chain_id |
| 109 | + end as chain_id |
| 110 | + from |
| 111 | + {{ source('evms', 'info') }} |
| 112 | + ), raw_source as ( |
| 113 | + select |
| 114 | + i.blockchain |
| 115 | + , t.address as contract_address |
| 116 | + , case |
| 117 | + when ( |
| 118 | + t.address = 0x0000000000000000000000000000000000001010 |
| 119 | + and i.blockchain = 'polygon' |
| 120 | + ) then 'POL' -- source has incorrect symbol for POL on polygon post-migration |
| 121 | + else t.symbol |
| 122 | + end as symbol |
| 123 | + , t.decimals |
| 124 | + , row_number() over ( |
| 125 | + partition by |
| 126 | + i.blockchain, |
| 127 | + t.address |
| 128 | + order by |
| 129 | + t.createdat desc |
| 130 | + ) as rn |
| 131 | + from |
| 132 | + {{ source("definedfi", "dataset_tokens", database="dune") }} as t |
| 133 | + join evms_info as i on t.networkid = i.chain_id |
| 134 | + ) |
| 135 | + select |
| 136 | + blockchain |
| 137 | + , contract_address |
| 138 | + , symbol |
| 139 | + , decimals |
| 140 | + from |
| 141 | + raw_source |
| 142 | + where |
| 143 | + rn = 1 |
| 144 | +), clean_automated_source as ( |
| 145 | + select |
| 146 | + * |
| 147 | + from |
| 148 | + automated_source |
| 149 | + where |
| 150 | + contract_address not in ( |
| 151 | + -- incorrect decimal assignment in raw source |
| 152 | + 0xeb9951021698b42e4399f9cbb6267aa35f82d59d |
| 153 | + , 0x0ba45a8b5d5575935b8158a88c631e9f9c95a2e5 |
| 154 | + -- incorrect naming of raw source |
| 155 | + , 0x136471a34f6ef19fe571effc1ca711fdb8e49f2b -- USYC |
| 156 | + ) |
| 157 | +), static_source as ( |
| 158 | + {% for key, value in static_models.items() %} |
| 159 | + select |
| 160 | + '{{ value.blockchain }}' as blockchain |
| 161 | + , contract_address |
| 162 | + , symbol |
| 163 | + , decimals |
| 164 | + from |
| 165 | + {{ value.model }} |
| 166 | + {% if value.blockchain == 'optimism' %} |
| 167 | + where |
| 168 | + symbol is not null --This can be removed if/when all other chains show all ERC20 tokens, rather than only mapped ones |
| 169 | + {% endif %} |
| 170 | + {% if not loop.last %} |
| 171 | + union all |
| 172 | + {% endif %} |
| 173 | + {% endfor %} |
| 174 | +), clean_static_source as ( |
| 175 | + select |
| 176 | + s.blockchain, |
| 177 | + s.contract_address, |
| 178 | + s.symbol, |
| 179 | + s.decimals |
| 180 | + from |
| 181 | + static_source as s |
| 182 | + left join automated_source as a on s.blockchain = a.blockchain |
| 183 | + and s.contract_address = a.contract_address |
| 184 | + where |
| 185 | + a.contract_address is null |
| 186 | +) |
| 187 | +select |
| 188 | + * |
| 189 | +from |
| 190 | + clean_automated_source |
| 191 | +union all |
| 192 | +select |
| 193 | + * |
| 194 | +from |
| 195 | + clean_static_source |
0 commit comments