Skip to content

Commit

Permalink
add borrow and supply calls
Browse files Browse the repository at this point in the history
  • Loading branch information
janndriessen committed Jul 4, 2021
1 parent 1b800e9 commit 8230c85
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ First get your test addresses and private keys.
$ npx ganache-cli --account_keys_path keys.json
```

Start a fork of mainnet locally.
Start a fork of mainnet locally (using infura).

```
$ npx ganache-cli \
Expand Down
16 changes: 13 additions & 3 deletions api/api/borrow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ const compound = new Compound("http://127.0.0.1:8545", {
"0xb8c1b5c1d81f9475fdf2e334517d29f733bdfa40682207571b12fc1142cbf329"
});

// const result = await contract.getAccountLiquidity(walletAddress);
// const { 0: error, 1: liquidity, 2: shortfall } = result;

// console.log(error);
// console.log(liquidity, BigNumber.from(liquidity._hex).toString());
// console.log(shortfall);
async function borrowUSDC(amount: number) {
console.log("Borrowing USDC on compound.");

try {
if (isNaN(amount) || amount === 0) {
throw Error("insufficuent amount");
}
const asset = Compound.USDC;
const trx = await compound.borrow(asset, amount);
const trxEnterMarkets = await compound.enterMarkets(asset);
const trxBorrow = await compound.borrow(asset, amount);
console.log("Borrow", asset, amount);
console.log(trx.hash);
console.log(trxEnterMarkets.hash);
console.log(trxBorrow.hash);
return {
trx: trx.hash,
trx: trxBorrow.hash,
error: null
};
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions api/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ const balance = async function(contractAdress: string) {
const trxStatus = async function() {
try {
const transaction = await provider.getTransaction(
"0x3dd1893809922fdc4652a48495671b4567965480296343760ca1c87c408237fe"
"0x25950dc43e0af9ba362215807b11f4964ef4ce06d0b25c8b6de081c1503a4973"
);
console.log(transaction);
} catch (error) {
console.log(error);
}
};

trxStatus();
// trxStatus();
balance(cTokenContractAddress);
balance(usdcContractAddress);

Expand Down
69 changes: 69 additions & 0 deletions app/eazy/data/BorrowApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ enum BorrowApiError: Error {
}

final class BorrowApi {
func borrow(amount: Int, completion: @escaping (Result<String, Error>) -> Void) {
let path = "/borrow"
let payload = AmountPayload(amount: amount)
let payloadData = try? JSONEncoder().encode(payload)

let requestBuilder = ApiRequestBuilder()
guard let request = requestBuilder.buildRequest(for: path, method: .post, payload: payloadData) else { return }

let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
if let error = error {
completion(.failure(error))
return
}

guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
let error = BorrowApiError.unexpected(message: "Error with the response - unexpected status code")
completion(.failure(error))
return
}

if let data = data, let result = try? JSONDecoder().decode(TransactionResponse.self, from: data) {
print(result)
completion(.success(result.trx))
return
}

completion(.failure(BorrowApiError.missingResponse))
})
task.resume()
}

func getCollateral(for amount: Int, completion: @escaping (Result<Int, Error>) -> Void) {
let path = "/borrow/liquidity"
let payload = AmountPayload(amount: amount)
Expand All @@ -42,6 +74,38 @@ final class BorrowApi {
if let data = data, let result = try? JSONDecoder().decode(CollateralResponse.self, from: data) {
print(result)
completion(.success(result.collateralNeeded))
return
}

completion(.failure(BorrowApiError.missingResponse))
})
task.resume()
}

func supply(amount: Int, completion: @escaping (Result<String, Error>) -> Void) {
let path = "/supply"
let payload = AmountPayload(amount: amount)
let payloadData = try? JSONEncoder().encode(payload)

let requestBuilder = ApiRequestBuilder()
guard let request = requestBuilder.buildRequest(for: path, method: .post, payload: payloadData) else { return }

let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
if let error = error {
completion(.failure(error))
return
}

guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
let error = BorrowApiError.unexpected(message: "Error with the response - unexpected status code")
completion(.failure(error))
return
}

if let data = data, let result = try? JSONDecoder().decode(TransactionResponse.self, from: data) {
print(result)
completion(.success(result.trx))
}

completion(.failure(BorrowApiError.missingResponse))
Expand All @@ -54,3 +118,8 @@ private struct CollateralResponse: Decodable {
let borrowAmount: Int
let collateralNeeded: Int
}

private struct TransactionResponse: Decodable {
let trx: String
let error: String
}
17 changes: 17 additions & 0 deletions app/eazy/ui/DashboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct DashboardView: View {
let transactions = Transaction.transactions

private let paymentsApi = PaymentsApi()
private let borrowApi = BorrowApi()

var body: some View {
ZStack(alignment: .top) {
Expand All @@ -55,6 +56,22 @@ struct DashboardView: View {
// CardsApi().testAuth()
// PayoutsApi().payout(amount: "1000")
// PaymentsApi().checkPayment(with: "fc988ed5-c129-4f70-a064-e5beb7eb8e32")
// borrowApi.supply(amount: 715) { result in
// switch result {
// case .failure(let error):
// print(error)
// case .success(let trxHash):
// print(trxHash)
// }
// }
borrowApi.borrow(amount: 500) { result in
switch result {
case .failure(let error):
print(error)
case .success(let trxHash):
print(trxHash)
}
}
}
}
.padding()
Expand Down

0 comments on commit 8230c85

Please sign in to comment.