-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
176 lines (157 loc) · 6.25 KB
/
client.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// <reference path="./types.d.ts" />
import {
GeneratedType,
OfflineSigner,
EncodeObject,
Registry,
} from "@cosmjs/proto-signing";
import {StdFee} from "@cosmjs/amino";
import {SigningStargateClient} from "@cosmjs/stargate";
import {Env} from "./env";
import {UnionToIntersection, Return, Constructor} from "./helpers";
import {Module} from "./modules";
import {EventEmitter} from "events";
import {ChainInfo} from "@keplr-wallet/types";
const defaultFee = {
amount: [],
gas: "200000",
};
export class IgniteClient extends EventEmitter {
static plugins: Module[] = [];
env: Env;
signer?: OfflineSigner;
registry: Array<[string, GeneratedType]> = [];
static plugin<T extends Module | Module[]>(plugin: T) {
const currentPlugins = this.plugins;
class AugmentedClient extends this {
static plugins = currentPlugins.concat(plugin);
}
if (Array.isArray(plugin)) {
type Extension = UnionToIntersection<Return<T>['module']>
return AugmentedClient as typeof IgniteClient & Constructor<Extension>;
}
type Extension = Return<T>['module']
return AugmentedClient as typeof IgniteClient & Constructor<Extension>;
}
async signAndBroadcast(msgs: EncodeObject[], fee: StdFee, memo: string) {
if (this.signer) {
const {address} = (await this.signer.getAccounts())[0];
const signingClient = await SigningStargateClient.connectWithSigner(this.env.rpcURL, this.signer, {
registry: new Registry(this.registry),
prefix: this.env.prefix
});
return await signingClient.signAndBroadcast(address, msgs, fee ? fee : defaultFee, memo)
} else {
throw new Error(" Signer is not present.");
}
}
constructor(env: Env, signer?: OfflineSigner) {
super();
this.env = env;
this.setMaxListeners(0);
this.signer = signer;
const classConstructor = this.constructor as typeof IgniteClient;
classConstructor.plugins.forEach(plugin => {
const pluginInstance = plugin(this);
Object.assign(this, pluginInstance.module)
if (this.registry) {
this.registry = this.registry.concat(pluginInstance.registry)
}
});
}
useSigner(signer: OfflineSigner) {
this.signer = signer;
this.emit("signer-changed", this.signer);
}
removeSigner() {
this.signer = undefined;
this.emit("signer-changed", this.signer);
}
async useKeplr(keplrChainInfo: Partial<ChainInfo> = {}) {
// Using queryClients directly because BaseClient has no knowledge of the modules at this stage
try {
const queryClient = (
await import("./cosmos.base.tendermint.v1beta1/module")
).queryClient;
const stakingQueryClient = (
await import("./cosmos.staking.v1beta1/module")
).queryClient;
const bankQueryClient = (await import("./cosmos.bank.v1beta1/module"))
.queryClient;
const stakingqc = stakingQueryClient({addr: this.env.apiURL});
const qc = queryClient({addr: this.env.apiURL});
const node_info = await (await qc.serviceGetNodeInfo()).data;
const chainId = node_info.default_node_info?.network ?? "";
const chainName = chainId?.toUpperCase() + " Network";
const staking = await (await stakingqc.queryParams()).data;
const bankqc = bankQueryClient({addr: this.env.apiURL});
const tokens = await (await bankqc.queryTotalSupply()).data;
const addrPrefix = this.env.prefix ?? "cosmos";
const rpc = this.env.rpcURL;
const rest = this.env.apiURL;
let stakeCurrency = {
coinDenom: staking.params?.bond_denom?.toUpperCase() ?? "",
coinMinimalDenom: staking.params?.bond_denom ?? "",
coinDecimals: 0,
};
let bip44 = {
coinType: 118,
};
let bech32Config = {
bech32PrefixAccAddr: addrPrefix,
bech32PrefixAccPub: addrPrefix + "pub",
bech32PrefixValAddr: addrPrefix + "valoper",
bech32PrefixValPub: addrPrefix + "valoperpub",
bech32PrefixConsAddr: addrPrefix + "valcons",
bech32PrefixConsPub: addrPrefix + "valconspub",
};
let currencies =
tokens.supply?.map((x) => {
const y = {
coinDenom: x.denom?.toUpperCase() ?? "",
coinMinimalDenom: x.denom ?? "",
coinDecimals: 0,
};
return y;
}) ?? [];
let feeCurrencies =
tokens.supply?.map((x) => {
const y = {
coinDenom: x.denom?.toUpperCase() ?? "",
coinMinimalDenom: x.denom ?? "",
coinDecimals: 0,
};
return y;
}) ?? [];
let coinType = 118;
if (chainId) {
const suggestOptions: ChainInfo = {
chainId,
chainName,
rpc,
rest,
stakeCurrency,
bip44,
bech32Config,
currencies,
feeCurrencies,
...keplrChainInfo,
};
await window.keplr.experimentalSuggestChain(suggestOptions);
window.keplr.defaultOptions = {
sign: {
preferNoSetFee: true,
preferNoSetMemo: true,
},
};
}
await window.keplr.enable(chainId);
this.signer = window.keplr.getOfflineSigner(chainId);
this.emit("signer-changed", this.signer);
} catch (e) {
throw new Error(
"Could not load tendermint, staking and bank modules. Please ensure your client loads them to use useKeplr()"
);
}
}
}