From f09e27df79f08be90a0f3d65c90a252c308b538e Mon Sep 17 00:00:00 2001 From: Kacy Adams Date: Wed, 14 Dec 2022 18:03:33 -0500 Subject: [PATCH] update r code engine samples --- R-Code-Samples/aave-protocol-dated-func.Rmd | 77 +++++++++++---------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/R-Code-Samples/aave-protocol-dated-func.Rmd b/R-Code-Samples/aave-protocol-dated-func.Rmd index 277806c9..d6636421 100644 --- a/R-Code-Samples/aave-protocol-dated-func.Rmd +++ b/R-Code-Samples/aave-protocol-dated-func.Rmd @@ -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("<<>>", temp, fixed=TRUE)) {break} } + data - data <- str_replace(data, "\n<<>>\n ", "") - data <- str_replace_all(data, "\n", ",") - data <- sub(',', '', data) + data <- str_replace(data, " <<>> ", "") 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" @@ -62,8 +53,8 @@ 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" @@ -71,19 +62,31 @@ getDF <- function(startdate, enddate) { 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 ``` + +