/
BalanceApi.ts
103 lines (94 loc) · 4.01 KB
/
BalanceApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Observable } from "rxjs";
import { flatMap, map } from "rxjs/operators";
import { AccountRef, AssetWithAmount } from "../../DCoreClient";
import { DCoreConstants } from "../../DCoreConstants";
import { Asset } from "../../models/Asset";
import { AssetAmount } from "../../models/AssetAmount";
import { ChainObject } from "../../models/ChainObject";
import { VestingBalance } from "../../models/VestingBalance";
import { GetAccountBalances } from "../../net/models/request/GetAccountBalances";
import { GetNamedAccountBalances } from "../../net/models/request/GetNamedAccountBalances";
import { GetVestingBalances } from "../../net/models/request/GetVestingBalances";
import { BaseApi } from "./BaseApi";
import { DCoreApi } from "./DCoreApi";
export class BalanceApi extends BaseApi {
constructor(api: DCoreApi) {
super(api);
}
/**
* Get account balance.
*
* @param account account name or account object id, 1.2.*
* @param asset object id of the assets, 1.3.*
*
* @return amount for asset
*/
public get(account: AccountRef, asset: ChainObject): Observable<AssetAmount> {
return this.getAll(account, [asset]).pipe(map((list) => list[0]));
}
/**
* Get account balances.
*
* @param account account name or account object id, 1.2.*
* @param assets object ids of the assets, 1.3.*, or undefined to get all nonzero
*
* @return list of amounts for different assets
*/
public getAll(account: AccountRef, assets?: ChainObject[]): Observable<AssetAmount[]> {
return this.getBalanceInternal(account, assets);
}
/**
* Get account balance with asset.
*
* @param account account name or account object id, 1.2.*
* @param assetSymbol asset symbol, eg. DCT
*
* @return a pair of asset to amount
*/
public getWithAsset(account: AccountRef, assetSymbol: string = DCoreConstants.DCT_ASSET_SYMBOL): Observable<AssetWithAmount> {
return this.getAllWithAsset(account, [assetSymbol]).pipe(map((list) => list[0]));
}
/**
* Get account balance with asset.
*
* @param account account name or account object id, 1.2.*
* @param assetSymbols asset symbols, eg. DCT, or undefined to get all nonzero
*
* @return a list of pairs of assets to amounts
*/
public getAllWithAsset(account: AccountRef, assetSymbols?: string[]): Observable<AssetWithAmount[]> {
if (assetSymbols) {
return this.api.assetApi.getAllByName(assetSymbols)
.pipe(flatMap((assets) => this.getAll(account, assets.map((asset) => asset.id))
.pipe(map((balances) => this.createTuple(assets, balances))),
));
} else {
return this.getAll(account)
.pipe(flatMap((balances) => this.api.assetApi.getAll(balances.map((asset) => asset.assetId))
.pipe(map((assets) => this.createTuple(assets, balances))),
));
}
}
/**
* Get information about a vesting balance object.
*
* @param accountId id of the account
*
* @return a list of vesting balances with additional information
*/
public getAllVesting(accountId: ChainObject): Observable<VestingBalance[]> {
return this.api.request(new GetVestingBalances(accountId));
}
private createTuple(assets: Asset[], balances: AssetAmount[]): AssetWithAmount[] {
return assets.map((asset) => [asset, balances.find((balance) => balance.assetId.eq(asset.id))] as AssetWithAmount);
}
private getBalanceInternal(account: AccountRef, assets?: ChainObject[]): Observable<AssetAmount[]> {
if (typeof account === "string" && ChainObject.isValid(account)) {
return this.getBalanceInternal(ChainObject.parse(account), assets);
} else if (typeof account === "string") {
return this.request(new GetNamedAccountBalances(account, assets));
} else {
return this.request(new GetAccountBalances(account, assets));
}
}
}