Skip to content

Instantly share code, notes, and snippets.

@ries9112
Created September 5, 2021 20:39
Show Gist options
  • Save ries9112/d7b387a78cc5b23dbfb1abf4a8d32add to your computer and use it in GitHub Desktop.
Save ries9112/d7b387a78cc5b23dbfb1abf4a8d32add to your computer and use it in GitHub Desktop.
A dashboard to plot cumulative sales over time for either an artist or a given tag: https://predictcrypto.shinyapps.io/HEN_lookup/
---
title: "HEN Data Lookup"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
theme: spacelab
source_code: embed
social: ['twitter','facebook','linkedin']
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(ghql)
library(jsonlite)
library(tidyverse)
library(ggdark)
# library(ggfx) # causes install issues for shinyapp
library(scales)
library(shiny)
library(DT)
```
## Column {.sidebar}
### User Inputs
```{r}
# Do you want to filter by artist or by tag?
selectInput(inputId = 'choice', label = 'Do you want to filter by artist or by tag?', choices = c('Artist','Tag'), selected = 'Artist')
shiny::renderUI({
## INDEXERS - TOTAL
if(input$choice == 'Artist'){
textInput(inputId = 'artist_address', 'Enter artist wallet address:', value = "tz2Pkj2xWJovKKCsABjnr3NbyMVJTMBkpTvb", width = NULL, placeholder = NULL)
} else{
textInput(inputId = 'tag_selection', 'Enter tag to look-up:', value = "GAN", width = NULL, placeholder = NULL)
}
})
```
This is a simple interface that queries the hicdex by [\@marchingsquare](https://twitter.com/marchingsquare). Visit the official page, where you can donate to the project: <https://hicdex.com/>
## Column {data-width="350"}
### Plot Cumulative Sales Over Time
```{r}
renderPlot({
if(input$choice == 'Artist'){
# Tag to lookup:
artist = input$artist_address
# connect to the endpoint
con = GraphqlClient$new(
url = "https://api.hicdex.com/v1/graphql"
)
# initialize a new query
graphql_request = Query$new()
# Define query
graphql_request$query('mydata', paste0('query artistSales {
hic_et_nunc_trade(where: {token: {creator: {address: {_eq: ',artist,'}}}}, order_by: {swap: {price: desc}}) {
token {
title
mime
description
id
artifact_uri
display_uri
}
timestamp
amount
token_id
swap {
price
}
}
}'))
# Run query (pull data)
hen_data = con$exec(graphql_request$queries$mydata)
# convert results to JSON
hen_data = fromJSON(hen_data)
# extract dataframe
hen_data = hen_data$data$hic_et_nunc_trade
# Add Tezos price
hen_data$price_tezos = hen_data$swap$price/1000000
# Add date
hen_data$date = as.Date(hen_data$timestamp)
# Add datetime
hen_data$datetime = as.POSIXct(hen_data$timestamp)
# Order by datetime
hen_data = arrange(hen_data, datetime)
# Plot cumulative sum over time
ggplot(hen_data, aes(x=date, y=cumsum(price_tezos))) +
# with_outer_glow(geom_point(color="white",size=1.1),colour="deeppink",sigma=10,expand=1) +
geom_point(color="white",size=1.1) +
geom_smooth(color='dark orange') +
#geom_point() +
scale_y_continuous(name='Cumulative Trades in Tezos', labels = comma) +
ggtitle(paste('Cumulative Tezos Traded'), paste(artist)) +
theme(axis.title.x=element_blank()) +
dark_theme_gray() +
theme(plot.background = element_rect(fill = "grey10"),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_line(color = "grey30", size = 0.2),
legend.background = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank())
} else{
# Tag to lookup:
tag = input$tag_selection
# connect to the endpoint
con = GraphqlClient$new(
url = "https://api.hicdex.com/v1/graphql"
)
# initialize a new query
graphql_request = Query$new()
# Define query
graphql_request$query('mydata', paste0('query mySecondaryMarketSales {
hic_et_nunc_trade(where: {token: {token_tags: {tag: {tag: {_ilike: ',tag,'}}}}}, order_by: {swap: {price: desc}}) {
timestamp
swap {
price
}
}
}'))
# Run query (pull data)
hen_data = con$exec(graphql_request$queries$mydata)
# convert results to JSON
hen_data = fromJSON(hen_data)
# extract dataframe
hen_data = hen_data$data$hic_et_nunc_trade
# Add Tezos price
hen_data$price_tezos = hen_data$swap$price/1000000
# Add date
hen_data$date = as.Date(hen_data$timestamp)
# Add datetime
hen_data$datetime = as.POSIXct(hen_data$timestamp)
# Order by datetime
hen_data = arrange(hen_data, datetime)
# Plot cumulative sum over time
ggplot(hen_data, aes(x=date, y=cumsum(price_tezos))) +
# with_outer_glow(geom_point(color="white",size=1.1),colour="deeppink",sigma=10,expand=1) +
geom_point(color="white",size=1.1) +
geom_smooth(color='dark orange') +
scale_y_continuous(name='Cumulative Trades in Tezos', labels = comma) +
ggtitle(paste('Cumulative Tezos Traded -', tag,'in tag'), paste('Through:', max(hen_data$date))) +
theme(axis.title.x=element_blank()) +
dark_theme_gray() +
theme(plot.background = element_rect(fill = "grey10"),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_line(color = "grey30", size = 0.2),
legend.background = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank())
}
})
```
Row {data-height=250}
-----------------------------------------------------------------------
### Data
```{r}
renderDT(server=FALSE,{
if(input$choice == 'Artist'){
# Tag to lookup:
artist = input$artist_address
# connect to the endpoint
con = GraphqlClient$new(
url = "https://api.hicdex.com/v1/graphql"
)
# initialize a new query
graphql_request = Query$new()
# Define query
graphql_request$query('mydata', paste0('query artistSales {
hic_et_nunc_trade(where: {token: {creator: {address: {_eq: ',artist,'}}}}, order_by: {swap: {price: desc}}) {
timestamp
token_id
swap {
price
}
}
}'))
# Run query (pull data)
hen_data = con$exec(graphql_request$queries$mydata)
# convert results to JSON
hen_data = fromJSON(hen_data)
# extract dataframe
hen_data = hen_data$data$hic_et_nunc_trade
# Add Tezos price
hen_data$price_tezos = hen_data$swap$price/1000000
# Add date
hen_data$date = as.Date(hen_data$timestamp)
# Add datetime
hen_data$datetime = as.POSIXct(hen_data$timestamp)
# Order by datetime
hen_data = arrange(hen_data, datetime)
} else{
# Tag to lookup:
tag = input$tag_selection
# connect to the endpoint
con = GraphqlClient$new(
url = "https://api.hicdex.com/v1/graphql"
)
# initialize a new query
graphql_request = Query$new()
# Define query
graphql_request$query('mydata', paste0('query mySecondaryMarketSales {
hic_et_nunc_trade(where: {token: {token_tags: {tag: {tag: {_ilike: ',tag,'}}}}}, order_by: {swap: {price: desc}}) {
timestamp
swap {
price
}
}
}'))
# Run query (pull data)
hen_data = con$exec(graphql_request$queries$mydata)
# convert results to JSON
hen_data = fromJSON(hen_data)
# extract dataframe
hen_data = hen_data$data$hic_et_nunc_trade
# Add Tezos price
hen_data$price_tezos = hen_data$swap$price/1000000
# Add date
hen_data$date = as.Date(hen_data$timestamp)
# Add datetime
hen_data$datetime = as.POSIXct(hen_data$timestamp)
# Order by datetime
hen_data = arrange(hen_data, datetime)
}
# Drop swap nested column
hen_data = select(hen_data, -swap, -datetime)
# Show data table
datatable(hen_data, extensions = "Buttons",
options = list(paging = TRUE,
scrollX=TRUE,
searching = TRUE,
ordering = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf'),
pageLength=3,
lengthMenu=c(3,5,10) ))
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment