Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into main
  • Loading branch information
Conor Flynn committed Nov 30, 2022
2 parents fcef552 + 3ea01d1 commit 4422ec2
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions R-Code-Samples/aave-protocol-dated-func.Rmd
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")
```

0 comments on commit 4422ec2

Please sign in to comment.