From 3ea01d125e8e98acede0097b4a018a8c4802bc71 Mon Sep 17 00:00:00 2001 From: Kacy Adams Date: Wed, 30 Nov 2022 00:01:33 -0500 Subject: [PATCH] Include engine R function sample --- R-Code-Samples/aave-protocol-dated-func.Rmd | 89 +++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 R-Code-Samples/aave-protocol-dated-func.Rmd diff --git a/R-Code-Samples/aave-protocol-dated-func.Rmd b/R-Code-Samples/aave-protocol-dated-func.Rmd new file mode 100644 index 00000000..277806c9 --- /dev/null +++ b/R-Code-Samples/aave-protocol-dated-func.Rmd @@ -0,0 +1,89 @@ +```{R} + +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" + + 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/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 + ) + ) + + data <- read.socket(socket) + + + while (TRUE) { + temp <- read.socket(socket) + data <- paste(data, temp, "") + if (grepl("<<>>", temp, fixed=TRUE)) {break} + } + + data <- str_replace(data, "\n<<>>\n ", "") + data <- str_replace_all(data, "\n", ",") + data <- sub(',', '', 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))) + + colnames(output)[colnames(output) == "assetId"] = "reserveId" + colnames(output)[colnames(output) == "assetSymbol"] = "reserve" + + output$amount <- as.double(output$amount) + output$borrowRate <- as.double(output$borrowRate) + + 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] + + + return(output) +} + + +``` + + +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} + +temp <- getDF("2022-08-01", "2022-08-04") + +```