-
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.
add supply apy. integrate apy's into dashboard
- Loading branch information
1 parent
0acf3de
commit 9fb0666
Showing
5 changed files
with
136 additions
and
2 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,36 @@ | ||
const Compound = require("@compound-finance/compound-js"); | ||
import { VercelRequest, VercelResponse } from "@vercel/node"; | ||
|
||
const compound = new Compound("http://127.0.0.1:8545", { | ||
privateKey: | ||
"0xb8c1b5c1d81f9475fdf2e334517d29f733bdfa40682207571b12fc1142cbf329" | ||
}); | ||
|
||
async function calculateApy(asset: string) { | ||
const srpb = await Compound.eth.read( | ||
Compound.util.getAddress("c" + asset), | ||
"function supplyRatePerBlock() returns (uint256)", | ||
[], | ||
{ provider: compound.provider } | ||
); | ||
|
||
const mantissa = Math.pow(10, 18); | ||
const blocksPerDay = (60 * 60 * 24) / 13.15; // ~13.15 second block time | ||
const daysPerYear = 365; | ||
|
||
const supplyApy = | ||
(Math.pow((+srpb.toString() / mantissa) * blocksPerDay + 1, daysPerYear) - | ||
1) * | ||
100; | ||
return supplyApy; | ||
} | ||
|
||
export default async (_: VercelRequest, res: VercelResponse) => { | ||
const asset = "USDC"; | ||
const apy = await calculateApy(asset); | ||
console.log(apy); | ||
res.json({ | ||
apy, | ||
asset | ||
}); | ||
}; |
File renamed without changes.
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
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,92 @@ | ||
// | ||
// ApyApi.swift | ||
// eazy | ||
// | ||
// Created by Jann Driessen on 04.07.21. | ||
// | ||
|
||
import Foundation | ||
|
||
import SwiftUI | ||
|
||
enum ApyApiError: Error { | ||
case unexpected(message: String) | ||
case missingResponse | ||
} | ||
|
||
final class ApyApi: ObservableObject { | ||
@Published var borrowApy: String = "" | ||
@Published var supplyApy: String = "" | ||
|
||
func fetch() { | ||
fetchBorrowApy() | ||
fetchSupplyApy() | ||
} | ||
} | ||
|
||
extension ApyApi { | ||
private func fetchBorrowApy() { | ||
let path = "/borrow/apy" | ||
let requestBuilder = ApiRequestBuilder() | ||
guard let request = requestBuilder.buildRequest(for: path, method: .get) else { return } | ||
|
||
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in | ||
if let error = error { | ||
let error = ApyApiError.unexpected(message: error.localizedDescription) | ||
print(error) | ||
return | ||
} | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, | ||
(200...299).contains(httpResponse.statusCode) else { | ||
let error = ApyApiError.unexpected(message: "Error with the response - unexpected status code") | ||
print(error) | ||
return | ||
} | ||
|
||
if let data = data, let result = try? JSONDecoder().decode(ApyResponse.self, from: data) { | ||
DispatchQueue.main.async { | ||
print(result) | ||
self.borrowApy = String(format: "%.2f%%", result.apy) | ||
} | ||
} | ||
}) | ||
task.resume() | ||
} | ||
} | ||
|
||
extension ApyApi { | ||
private func fetchSupplyApy() { | ||
let path = "/supply/apy" | ||
let requestBuilder = ApiRequestBuilder() | ||
guard let request = requestBuilder.buildRequest(for: path, method: .get) else { return } | ||
|
||
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in | ||
if let error = error { | ||
let error = ApyApiError.unexpected(message: error.localizedDescription) | ||
print(error) | ||
return | ||
} | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, | ||
(200...299).contains(httpResponse.statusCode) else { | ||
let error = ApyApiError.unexpected(message: "Error with the response - unexpected status code") | ||
print(error) | ||
return | ||
} | ||
|
||
if let data = data, let result = try? JSONDecoder().decode(ApyResponse.self, from: data) { | ||
DispatchQueue.main.async { | ||
print(result) | ||
self.supplyApy = String(format: "%.2f%%", result.apy) | ||
} | ||
} | ||
}) | ||
task.resume() | ||
} | ||
} | ||
|
||
private struct ApyResponse: Decodable { | ||
let apy: Double | ||
let asset: String | ||
} |
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