Skip to content

Instantly share code, notes, and snippets.

@kushti
Created June 6, 2021 21:13
Show Gist options
  • Save kushti/ecd1b91b33b284f50e4b195d8df73048 to your computer and use it in GitHub Desktop.
Save kushti/ecd1b91b33b284f50e4b195d8df73048 to your computer and use it in GitHub Desktop.
import scalaj.http.{Http, HttpOptions}
import scala.util.Try
object Connector extends App {
var prevPoolDatapoint = 0L
(1 to 10000000).foreach {_ =>
val t = Try {
val poolStatus = Http("http://127.0.0.1:9010/poolStatus")
.header("Content-Type", "application/json")
.header("Charset", "UTF-8")
.option(HttpOptions.readTimeout(10000))
.asString
.body
val toFind = "\\\"latest_datapoint\\\":"
val index = poolStatus.indexOf(toFind) + toFind.length
val lastPoolPointString = poolStatus.slice(index, index + 8)
val lastPoolPoint = lastPoolPointString.toLong
println(lastPoolPoint)
val statusToFind = "current_pool_stage\\\":\\\""
val statusIndex = poolStatus.indexOf(statusToFind) + statusToFind.length
val statusString = poolStatus.slice(statusIndex, statusIndex + 4)
if (statusString == "Live" && lastPoolPoint != prevPoolDatapoint) {
val coinGeckoResponse = Http("https://api.coingecko.com/api/v3/simple/price?ids=ergo&vs_currencies=USD")
.header("Content-Type", "application/json")
.header("Charset", "UTF-8")
.option(HttpOptions.readTimeout(10000))
.asString
.body
val priceStartString = "{\"ergo\":{\"usd\":"
val priceIndexStart = coinGeckoResponse.indexOf(priceStartString) + priceStartString.length
val priceEndString = "}}"
val priceIndexEnd = coinGeckoResponse.indexOf(priceEndString)
val coinGeckoPriceString = coinGeckoResponse.slice(priceIndexStart, priceIndexEnd)
val coinGeckoPrice = ((1 / coinGeckoPriceString.toDouble) * 1000000000.0).toLong
println(coinGeckoPrice)
val datapointToPost = if (coinGeckoPrice > lastPoolPoint / 1000 * 1005) {
lastPoolPoint / 1000 * 1005
} else if (coinGeckoPrice < lastPoolPoint / 1000 * 995) {
lastPoolPoint / 1000 * 995
} else {
coinGeckoPrice
}
println("datapoint to post: " + datapointToPost)
val result = Http("http://127.0.0.1:9011/submitDatapoint").postData(s"""{"datapoint":"$datapointToPost"}""")
.header("Content-Type", "application/json")
.header("Charset", "UTF-8")
.option(HttpOptions.readTimeout(10000))
.asString
.body
println(result)
prevPoolDatapoint = lastPoolPoint
}
}
println(t)
Thread.sleep(1000 * 60 * 2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment