-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor all api requests to unified format
- Loading branch information
Showing
9 changed files
with
115 additions
and
99 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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
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,98 @@ | ||
import { | ||
LoanTransactionResponse, | ||
LoanMetadataResponse, | ||
LoanRequestModel, | ||
LoanRequestResponse, | ||
LoanAddressesByBorrowerResponse, | ||
LoansAddressesResponse, | ||
LoanAPIInstance, | ||
BaseResponse | ||
} from '../types' | ||
|
||
import BaseService from './BaseService' | ||
|
||
export default class LoanRequest extends BaseService implements LoanAPIInstance { | ||
constructor(token: string) { | ||
super(token) | ||
} | ||
|
||
private async apiRequest( | ||
endpoint: string, | ||
resourceType: string, | ||
errorParam: string = '', | ||
method: 'get' | 'post' = 'get', | ||
params?: object | ||
): Promise<BaseResponse> { | ||
const api = method === 'post' ? this.api.post : this.api.get | ||
|
||
try { | ||
const { data } = await api(endpoint, params) | ||
|
||
return { data, code: 200 } | ||
} catch (e) { | ||
return LoanRequest.errorHandler(e, resourceType, errorParam) | ||
} | ||
} | ||
|
||
public async create(creatorWalletAddress: string, params: LoanRequestModel): Promise<LoanTransactionResponse> { | ||
BaseService.checkAddressChecksum(creatorWalletAddress) | ||
|
||
return await this.apiRequest(`/request/create/${creatorWalletAddress}`, 'loan request creation', '', 'post', params) | ||
} | ||
|
||
public async placeCollateral(loanAddress: string, borrowerAddress: string): Promise<LoanTransactionResponse> { | ||
BaseService.checkAddressChecksum(loanAddress) | ||
BaseService.checkAddressChecksum(borrowerAddress) | ||
|
||
return await this.apiRequest( | ||
`/request/placecollateral/${loanAddress}/${borrowerAddress}`, | ||
'placing loan request collateral', | ||
loanAddress, | ||
'post' | ||
) | ||
} | ||
|
||
public async fund(loanAddress: string, lenderAddress: string, amount: number): Promise<LoanTransactionResponse> { | ||
BaseService.checkAddressChecksum(loanAddress) | ||
BaseService.checkAddressChecksum(lenderAddress) | ||
|
||
return await this.apiRequest( | ||
`/request/fund/${loanAddress}/${lenderAddress}/${amount}`, | ||
'funding loan request', | ||
loanAddress, | ||
'post' | ||
) | ||
} | ||
|
||
public async payback(loanAddress: string, borrowerAddress: string): Promise<LoanTransactionResponse> { | ||
BaseService.checkAddressChecksum(loanAddress) | ||
BaseService.checkAddressChecksum(borrowerAddress) | ||
|
||
return await this.apiRequest( | ||
`/request/payback/${loanAddress}/${borrowerAddress}`, | ||
'placing loan request payback', | ||
loanAddress, | ||
'post' | ||
) | ||
} | ||
|
||
public async getLoanData(loanAddress: string): Promise<LoanRequestResponse> { | ||
BaseService.checkAddressChecksum(loanAddress) | ||
|
||
return await this.apiRequest(`/request/${loanAddress}`, 'loan request', loanAddress) | ||
} | ||
|
||
public async getAllAddresses(): Promise<LoansAddressesResponse> { | ||
return await this.apiRequest('/requests', 'loan request addresses') | ||
} | ||
|
||
public async getLoansByBorrower(borrowerAddress: string): Promise<LoanAddressesByBorrowerResponse> { | ||
BaseService.checkAddressChecksum(borrowerAddress) | ||
|
||
return await this.apiRequest(`/requests/${borrowerAddress}`, 'loan addresses by borrower', borrowerAddress) | ||
} | ||
|
||
public async getMetadata(): Promise<LoanMetadataResponse> { | ||
return await this.apiRequest('/requests/metadata', 'loan requests metadata') | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Marketplace } from '../src/ethlend-js' | ||
import { Marketplace } from '../src/aave-js' | ||
|
||
/** | ||
* Dummy test | ||
|