Skip to content

Commit

Permalink
update r code engine samples
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsk4 committed Dec 14, 2022
1 parent 3ea01d1 commit f09e27d
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions R-Code-Samples/aave-protocol-dated-func.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,46 @@ library("httr")
library("jsonlite")
library("lubridate")
```

This function sets up an Engine socket and makes the get request. The engine feeds the socket while
the function listens and parses the data, and then returns it as a data frame.
```{R}
getDF <- function(startdate, enddate) {
engine_key <- "b6c810a7f35f4fa0d28258278325b4b5ab82ba79868ab33d01d5c878e13872ec129a91a3fbf702e59c2404f0fb4a53420a3ffb50130c35b4d06b32d81e56c1f4"
getJson <- function(startdate, enddate) {
socket <- make.socket("defi-de.idea.rpi.edu", 61200)
ss <- read.socket(socket)
ss <- gsub("[\r\n]", "", ss)
httr::GET(url = "http://defi-de.idea.rpi.edu:8080/defi/v1/rest/initialize?source=amber_data&auth_data=key,UAK7ed69235426c360be22bfc2bde1809b6")
engine_key <- "b6c810a7f35f4fa0d28258278325b4b5ab82ba79868ab33d01d5c878e13872ec129a91a3fbf702e59c2404f0fb4a53420a3ffb50130c35b4d06b32d81e56c1f4"
socket <- socketConnection("defi-de.idea.rpi.edu", 61200, blocking=TRUE)
ss <- readLines(socket, 1)
ss
httr::GET(
url = "http://defi-de.idea.rpi.edu:8080/defi/v1/rest/request_dated",
query = list(
destination = ss,
key = engine_key,
request = "aave-protocol-dated",
query = "get_all,aave-protocol_dated",
start_date = startdate,
end_date = enddate
)
)
reqString <- paste(
"SRC&&&RQST&&&destination&&&", ss,
"&&&key&&&", engine_key, "&&&",
"start_date&&&", startdate, "&&&",
"end_date&&&", enddate, "&&&",
"query&&&aave-protocol-dated&&&",
"request&&&aave-protocol-dated\n", sep="")
data <- read.socket(socket)
writeLines(reqString, socket)
data <- readLines(socket, 1)
while (TRUE) {
temp <- read.socket(socket)
temp <- readLines(socket, 1)
data <- paste(data, temp, "")
if (grepl("<<<end>>>", temp, fixed=TRUE)) {break}
}
data
data <- str_replace(data, "\n<<<end>>>\n ", "")
data <- str_replace_all(data, "\n", ",")
data <- sub(',', '', data)
data <- str_replace(data, " <<<end>>> ", "")
data <- str_replace_all(data, " ", "")
data <- str_replace_all(data, "\\}\\{", "\\},\\{")
data <- paste("[", data, "]", sep="")
output <- fromJSON(data)
output <- output[,-(colnames(output) == "_id")]
colnames(output)[colnames(output) == "action"] = "type"
output$type <- as.factor(output$type)
output <- output %>% mutate(datetime = as_datetime(as.numeric(timestamp)))
output <- output %>% mutate(datetime = as_datetime(as.numeric(substr(timestamp, 1, nchar(timestamp)-3))))
colnames(output)[colnames(output) == "assetId"] = "reserveId"
colnames(output)[colnames(output) == "assetSymbol"] = "reserve"
Expand All @@ -62,28 +53,40 @@ getDF <- function(startdate, enddate) {
colnames(output)[colnames(output) == "user"] = "userId"
output <- output %>% unite(col = "onBehalfOfId", onBehalfOf,repayer,initiator, na.rm = TRUE, sep = "")
colnames(output)[colnames(output) == "collateralAssetId"] = "collateralReserveId"
colnames(output)[colnames(output) == "collateralAssetSymbol"] = "collateralReserve"
colnames(output)[colnames(output) == "principalAssetId"] = "principalReserveId"
colnames(output)[colnames(output) == "principalAssetSymbol"] = "principalReserve"
output$principalAmount <- as.double(output$principalAmount)
output <- output[,-15]
output
return(output)
}
temp <- getJson("2022-08-01", "2022-09-01")
temp
```

```{r}
library(dplyr)
We make a sample call to the function which takes the start date and end date and receive back
a data frame containing the aave data.
```{R}
weeklySummaries <- temp %>%
mutate(week = floor_date(datetime, unit = "day")) %>% # Add a new column that rounds the date of each transaction down to the nearest week
group_by(week) %>% # Group the transactions together by the week they were performed.
summarise(transactionCount = n()) # Count the number of transactions in each group.
```

temp <- getDF("2022-08-01", "2022-08-04")
With these weekly summaries computed, we can simply plot the week on the x-axis and the transaction count on the y-axis to visualize this new representation:

```{r}
weeklyTransactionsPlot <- ggplot(data = weeklySummaries, aes(x = week, y = transactionCount)) + geom_line()
weeklyTransactionsPlot
```


0 comments on commit f09e27d

Please sign in to comment.