Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match heum #24

Merged
merged 9 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .123trigger
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

0.8.0
0.9.6
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true
quote_type=single
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/build/*
**/coverage/*
**/node_modules/*
packages/types/src/metadata/static-latest.ts
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@open-web3/dev-config/config/prettier.cjs');
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# CHANGELOG

## 1.0.0 Oct 23, 2021
Changes:
- Setheum v0.9.6
- Bump polkadot.js version to 6.3.1
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# @setheum-js
Promise and RxJS APIs around Setheum RPC calls.
This library provides additional typing for users to access Setheum networks by using [setheum.js](https://github.com/setheum-js/api).
This library provides additional typing for users to access Setheum networks by using [setheum.js](https://github.com/setheum-js/api) using [polkadot.js](https://github.com/polkadot-js/api).

# Getting Started

Expand Down Expand Up @@ -37,17 +37,17 @@ main()

```ts
// query and display account data
const data = await api.query.system.account('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
const data = await api.query.system.account('5F98oWfz2r5rcRVnP9VCndg33DAAsky3iuoBSpaPUbgN9AJn');
console.log(data.toHuman())
```

## overview
## Packages Overview

The API is split up into a number of internal packages -

- [@setheum-js/api](packages/api/) The API library, providing both Promise and RxJS Observable-based interfaces. This is the main user-facing entry point.
- [@setheum-js/api-derive](packages/api-derive/) Additional polkadot.js derives for Setheum Network.
Derived results that are injected into the API, allowing for combinations of various query results (only used internally and exposed on the Api instances via `api.derive.*`)
Derived results that are injected into the API, allowing for combinations of various query results (only used internally and exposed on the Api instances via `api.derive.*`).
- [@setheum-js/app-util](./packages/app-util)
- Utilities to work with Setheum
- [@setheum-js/types](packages/types/) Codecs for all Polkadot.js type definations for Setheum primitives
- Utilities to work with Setheum.
- [@setheum-js/types](packages/types/) Codecs for all Polkadot.js type definations for Setheum primitives.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"HuangLeiWan. <[email protected]>"
],
"license": "Apache-2",
"version": "0.8.0",
"version": "1.0.0",
"private": true,
"engines": {
"yarn": "^1.10.1"
Expand All @@ -15,9 +15,8 @@
"packages/*"
],
"resolutions": {
"typescript": "^4.3.2",
"@polkadot/api": "^4.14.1",
"@polkadot/types": "^4.14.1"
"@polkadot/api": "^6.3.1",
"@polkadot/types": "^6.3.1"
},
"scripts": {
"build": "yarn build:interfaces && polkadot-dev-build-ts",
Expand All @@ -35,10 +34,10 @@
"@babel/register": "^7.13.6",
"@babel/runtime": "^7.14.0",
"@open-web3/dev-config": "^0.1.13",
"@open-web3/util": "^0.9.4-7",
"@open-web3/util": "^1.0.2-0",
"@types/jest": "^25.2.3",
"ts-node": "^8.6.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.4"
"typescript": "^4.3.5"
}
}
3 changes: 1 addition & 2 deletions packages/api-derive/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# @setheum-js/api-derive

Additional polkadot.js derives for Setheum Network.
Derived results that are injected into the API, allowing for combinations of various query results (only used internally and exposed on the Api instances via `api.derive.*`)
Additional polkadot.js derives for Setheum Network
12 changes: 4 additions & 8 deletions packages/api-derive/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"name": "@setheum-js/api-derive",
"version": "0.8.0",
"version": "1.0.0",
"description": "Additional polkadot.js derives for Setheum Network",
"main": "index.js",
"author": "Setheum Labs",
"maintainers": [
"Muhammad-Jibril B.A. <[email protected]>",
"HuangLeiWan. <[email protected]>"
],
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
Expand All @@ -19,9 +15,9 @@
},
"homepage": "https://github.com/setheum-js/setheum.js",
"dependencies": {
"@setheum-js/types": "0.8.0",
"@setheum-js/types": "1.0.0",
"@babel/runtime": "^7.10.2",
"@open-web3/orml-types": "^0.9.4-7",
"@polkadot/api-derive": "^4.14.1"
"@open-web3/orml-types": "^1.0.2-0",
"@polkadot/api-derive": "^6.3.1"
}
}
4 changes: 2 additions & 2 deletions packages/api-derive/src/dex/pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { map } from '@polkadot/x-rxjs/operators';
import { map } from 'rxjs/operators';
import primitivesConfig from '@setheum-js/type-definitions/primitives';
import { Observable } from '@polkadot/x-rxjs';
import { Observable } from 'rxjs';

import { CurrencyId, Balance } from '@setheum-js/types/interfaces';
import { memo } from '@polkadot/api-derive/util';
Expand Down
5 changes: 4 additions & 1 deletion packages/api-derive/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { DeriveCustom } from '@polkadot/api-derive';
import * as loan from './loan';
import * as dex from './dex';

export const derive: DeriveCustom = { loan, dex };
export const derive: DeriveCustom = {
loan: loan as unknown as DeriveCustom[string],
dex: dex as unknown as DeriveCustom[string],
};

export * from './types';
105 changes: 105 additions & 0 deletions packages/api-derive/src/loan/loan-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiInterfaceRx } from '@polkadot/api/types';
import { memo } from '@polkadot/api-derive/util';

import { CurrencyId, Rate, ExchangeRate, Balance, Position, Ratio } from '@setheum-js/types/interfaces';

import { DerivedLoanConstants, DerivedLoanType, DerivedLoanOverView, CollateralParams } from '../types/loan';
import { getAllCollateralCurrencyIds } from '../utils';

/**
* @name loanConstants
* @description get constants in loan module
*/
function loanConstants(api: ApiInterfaceRx): DerivedLoanConstants {
return {
minimumDebitValue: api.consts.cdpEngine.minimumDebitValue as Balance,
defaultDebitExchangeRate: api.consts.cdpEngine.defaultDebitExchangeRate as ExchangeRate,
defaultLiquidationRatio: api.consts.cdpEngine.defaultLiquidationRatio as Ratio,
defaultLiquidationPenalty: api.consts.cdpEngine.defaultLiquidationPenalty as Rate
};
}

/**
* @name loanType
* @description get loan type
* @param {(CurrencyId | string)} currency
*/
export function loanType(
instanceId: string,
api: ApiInterfaceRx
): (currncy: CurrencyId | string) => Observable<DerivedLoanType> {
return memo(instanceId, (currency: CurrencyId | string) => {
return combineLatest([
api.query.cdpEngine.debitExchangeRate<Rate>(currency),
api.query.cdpEngine.collateralParams<CollateralParams>(currency)
]).pipe(
map((result) => {
const constants = loanConstants(api);
const [globalInterestRatePerSec, debitExchangeRate, collateralParams] = result;

return {
currency,
debitExchangeRate: debitExchangeRate.isEmpty ? constants.defaultDebitExchangeRate : debitExchangeRate,
liquidationPenalty: collateralParams.liquidationPenalty.isEmpty
? constants.defaultLiquidationPenalty
: collateralParams.liquidationPenalty,
liquidationRatio: collateralParams.liquidationRatio.isEmpty
? constants.defaultLiquidationRatio
: collateralParams.liquidationRatio,
requiredCollateralRatio: collateralParams.requiredCollateralRatio,
maximumTotalDebitValue: collateralParams.maximumTotalDebitValue,
minimumDebitValue: constants.minimumDebitValue
};
})
);
});
}

/**
* @name allLoanTypes
* @description get loan types of all kinds of collateral
*/
export function allLoanTypes(instanceId: string, api: ApiInterfaceRx): () => Observable<DerivedLoanType[]> {
return memo(instanceId, () => {
const collateralCurrencyIds = getAllCollateralCurrencyIds(api);
const loanTypeQuery = loanType(instanceId, api);

return combineLatest(collateralCurrencyIds.map((currencyId) => loanTypeQuery(currencyId)));
});
}

/**
* @name loanOverview
* @description get loan overview includes total debit, total collateral
* @param {(CurrencyId | string)} currency
*/
export function loanOverview(
instanceId: string,
api: ApiInterfaceRx
): (currency: CurrencyId) => Observable<DerivedLoanOverView> {
return memo(instanceId, (currency: CurrencyId) =>
api.query.loans.totalPositions<Position>(currency).pipe(
map((result) => {
const { collateral, debit } = result;

return { currency, totalDebit: debit, totalCollateral: collateral };
})
)
);
}

/**
* @name allLoanOverview
* @description get loan overviews of all kinds of collatearl
*/
export function allLoanOverviews(instanceId: string, api: ApiInterfaceRx): () => Observable<DerivedLoanOverView[]> {
return memo(instanceId, () => {
const collateralCurrencyIds = getAllCollateralCurrencyIds(api);
const loanOverViewQuery = loanOverview(instanceId, api);

return combineLatest(collateralCurrencyIds.map((currencyId) => loanOverViewQuery(currencyId)));
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Observable, combineLatest } from '@polkadot/x-rxjs';
import { map } from '@polkadot/x-rxjs/operators';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';

import { CurrencyId, AccountId, Position } from '@setheum-js/types/interfaces';
Expand Down
94 changes: 0 additions & 94 deletions packages/api-derive/src/settmint-manager/loan-type.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/api-derive/src/types/loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface DerivedLoanConstants {

export interface CollateralParams {
maximumTotalDebitValue: Balance;
interestRatePerSec: OptionRate;
liquidationRatio: OptionRatio;
liquidationPenalty: OptionRate;
requiredCollateralRatio: OptionRatio;
Expand All @@ -28,7 +27,6 @@ export interface DerivedLoanType extends Omit<CollateralParams, 'liquidationRati
debitExchangeRate: Rate;
liquidationRatio: OptionRatio | ExchangeRate;
liquidationPenalty: OptionRatio | Rate;
globalInterestRatePerSec: Rate;
}

export interface DerivedUserLoan {
Expand Down
Loading