R: retrieving etherscan data

39 views Asked by At

I am trying to retrieve the blockchain data at

https://etherscan.io/token/0x62d0a8458ed7719fdaf978fe5929c6d342b0bfce?a=0x6bdabb7c6ac53552224d5b8a1d57b9d83c41cb03

and I modified a bit the approach here

https://www.r-bloggers.com/2021/06/data-science-on-blockchain-with-r/

However, something clearly goes wrong and I retrieve different transaction data. I am not at all an api expert, but I believe the solution to this should be a one-liner. Please have a look at the reprex below.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(httr)
library(jsonlite)



## See https://www.r-bloggers.com/2021/06/data-science-on-blockchain-with-r/


## and see https://etherscan.io/token/0x62d0a8458ed7719fdaf978fe5929c6d342b0bfce?a=0x6bdabb7c6ac53552224d5b8a1d57b9d83c41cb03


api_key <- "UJP16VCE9D29XFAA86RWADATJ5K4PBSYD9" 

address <- "0x6bdabB7c6ac53552224d5B8a1d57B9d83c41cB03"


query_etherscan <- function(api_key, address){
res <- GET("https://api.etherscan.io/api",
          query = list(module="account", 
                       action="txlist", 
                       address=address,
                       sort="desc",
                       apikey=api_key## ,
                       ## page=1,
                       ## offset=100,
                       ## startblock=0,
                       ## endblock=27025780
                       ))

return(res)

}


etherscan_data <- function(query_result){

    res <- fromJSON(rawToChar(query_result$content),
        flatten=TRUE)$result |>
        as_tibble() |>
        mutate(value=as.numeric(value)) |>
        mutate(across(contains("gas"), ~as.numeric(.x))) |>
        mutate(timeStamp=as.POSIXct(as.numeric(timeStamp), origin="1970-01-01"))
        
                                

    return(res)
    
}






my_query <- query_etherscan(api_key, address)

my_data <- etherscan_data(my_query)

my_data|>glimpse()
#> Rows: 6
#> Columns: 20
#> $ blockNumber       <chr> "18828914", "18807413", "18807412", "18794538", "187…
#> $ timeStamp         <dttm> 2023-12-20 19:31:23, 2023-12-17 19:06:35, 2023-12-1…
#> $ hash              <chr> "0x8864421b428c9ed85e961af9bf29f45837c25919b2b2f6263…
#> $ nonce             <chr> "4", "3", "2", "1", "0", "935536"
#> $ blockHash         <chr> "0x431812425e020fc0785c2971d710750120f046f1abd770a49…
#> $ transactionIndex  <chr> "142", "338", "70", "53", "98", "230"
#> $ from              <chr> "0x6bdabb7c6ac53552224d5b8a1d57b9d83c41cb03", "0x6bd…
#> $ to                <chr> "0x62d0a8458ed7719fdaf978fe5929c6d342b0bfce", "0x62d…
#> $ value             <dbl> 0e+00, 0e+00, 0e+00, 0e+00, 0e+00, 4e+17
#> $ gas               <dbl> 67420, 67420, 67420, 67420, 67420, 21000
#> $ gasPrice          <dbl> 60948134795, 42028172804, 43628373749, 44874128902, …
#> $ isError           <chr> "0", "0", "0", "0", "0", "0"
#> $ txreceipt_status  <chr> "1", "1", "1", "1", "1", "1"
#> $ input             <chr> "0xa9059cbb000000000000000000000000e69022e830428f5a5…
#> $ contractAddress   <chr> "", "", "", "", "", ""
#> $ cumulativeGasUsed <dbl> 7543612, 12587986, 4561046, 14525464, 8487230, 20333…
#> $ gasUsed           <dbl> 56184, 39084, 56184, 56184, 56184, 21000
#> $ confirmations     <chr> "17", "21518", "21519", "34393", "34953", "36538"
#> $ methodId          <chr> "0xa9059cbb", "0xa9059cbb", "0xa9059cbb", "0xa9059cb…
#> $ functionName      <chr> "transfer(address _to, uint256 _value)", "transfer(a…

## this does not look like the data at
## https://etherscan.io/token/0x62d0a8458ed7719fdaf978fe5929c6d342b0bfce?a=0x6bdabb7c6ac53552224d5b8a1d57b9d83c41cb03
## probably because the url I generate is different

my_query$url
#> [1] "https://api.etherscan.io/api?module=account&action=txlist&address=0x6bdabB7c6ac53552224d5B8a1d57B9d83c41cB03&sort=desc&apikey=UJP16VCE9D29XFAA86RWADATJ5K4PBSYD9"

## how to fix this ?

sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 12 (bookworm)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/Brussels
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] jsonlite_1.8.7 httr_1.4.7     dplyr_1.1.3   
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.4       cli_3.6.1         knitr_1.45        rlang_1.1.2      
#>  [5] xfun_0.41         purrr_1.0.2       styler_1.10.2     generics_0.1.3   
#>  [9] glue_1.6.2        htmltools_0.5.7   fansi_1.0.5       rmarkdown_2.25   
#> [13] R.cache_0.16.0    tibble_3.2.1      evaluate_0.23     fastmap_1.1.1    
#> [17] yaml_2.3.7        lifecycle_1.0.4   compiler_4.3.2    fs_1.6.3         
#> [21] pkgconfig_2.0.3   R.oo_1.25.0       R.utils_2.12.2    digest_0.6.33    
#> [25] R6_2.5.1          tidyselect_1.2.0  reprex_2.0.2      utf8_1.2.4       
#> [29] curl_5.1.0        pillar_1.9.0      magrittr_2.0.3    R.methodsS3_1.8.2
#> [33] tools_4.3.2       withr_2.5.2

Created on 2023-12-20 with reprex v2.0.2

0

There are 0 answers