-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.rpi.edu/DataINCITE/IDEA-DeFi-CRAFT
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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("<<<end>>>", temp, fixed=TRUE)) {break} | ||
| } | ||
| data <- str_replace(data, "\n<<<end>>>\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") | ||
| ``` |