-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNolusClient.ts
More file actions
124 lines (108 loc) · 3.85 KB
/
NolusClient.ts
File metadata and controls
124 lines (108 loc) · 3.85 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate';
import { CometClient, connectComet } from '@cosmjs/tendermint-rpc';
import { Coin } from '@cosmjs/proto-signing';
import { StargateClient } from '@cosmjs/stargate';
import { QuerySpendableBalanceByDenomRequest, QuerySpendableBalanceByDenomResponse } from 'cosmjs-types/cosmos/bank/v1beta1/query';
/**
* Nolus Client service class.
*
* Usage:
*
* ```ts
* import { NolusClient } from '@nolus/nolusjs';
*
* NolusClient.setInstance(tendermintRpc);
* ```
*/
export class NolusClient {
private static instance: NolusClient | null = null;
protected cosmWasmClient: Promise<CosmWasmClient> | undefined;
protected tmClient: Promise<CometClient> | undefined;
protected stargateClient: Promise<StargateClient> | undefined;
private constructor(tendermintRpc: string) {
this.cosmWasmClient = CosmWasmClient.connect(tendermintRpc);
this.tmClient = connectComet(tendermintRpc);
this.stargateClient = StargateClient.connect(tendermintRpc);
}
static getInstance() {
if (this.instance === null) {
throw new Error('Set the Tendermint RPC address before getting instance');
}
return this.instance;
}
static setInstance(tendermintRpc: string) {
this.instance = new NolusClient(tendermintRpc);
}
public async getCosmWasmClient(): Promise<CosmWasmClient> {
const client = await this.cosmWasmClient;
if (!client) {
throw new Error('Missing CosmWasm client');
}
return client;
}
public async getStargateClient(): Promise<StargateClient> {
const client = await this.stargateClient;
if (!client) {
throw new Error('Missing StargateClient client');
}
return client;
}
public async getTendermintClient(): Promise<CometClient> {
const client = await this.tmClient;
if (!client) {
throw new Error('Missing Tendermint client');
}
return client;
}
public getChainId = async (): Promise<string> => {
const client = await this.cosmWasmClient;
const chainId = await client?.getChainId();
if (!chainId) {
throw new Error('Chain ID is missing!');
}
return chainId;
};
public async getBalance(address: string, denom: string): Promise<Coin> {
const client = await this.cosmWasmClient;
const balance = client?.getBalance(address, denom);
if (!balance) {
throw new Error('Balance is missing!');
}
return await balance;
}
public async getSpendableBalance(address: string, denom: string): Promise<QuerySpendableBalanceByDenomResponse> {
const data = QuerySpendableBalanceByDenomRequest.encode({
address: address,
denom: denom,
}).finish();
const client = await this.tmClient;
const query: {
path: string;
data: Uint8Array;
prove: boolean;
height?: number;
} = {
path: '/cosmos.bank.v1beta1.Query/SpendableBalanceByDenom',
data,
prove: true,
};
if (!client) {
throw 'Tendermint client not initialized';
}
const response = await client.abciQuery(query);
return QuerySpendableBalanceByDenomResponse.decode(response.value);
// const balance = client?.getBalance(address, denom);
// if (!balance) {
// throw new Error('Balance is missing!');
// }
// return await balance;
}
public async getBlockHeight(): Promise<number> {
const client = await this.cosmWasmClient;
const block = await client?.getBlock();
if (!block?.header) {
throw new Error('Block height is missing!');
}
return block?.header.height;
}
}