-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
137 lines (137 loc) · 5.4 KB
/
client.js
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
/// <reference path="./types.d.ts" />
import { Registry, } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";
import { EventEmitter } from "events";
const defaultFee = {
amount: [],
gas: "200000",
};
export class IgniteClient extends EventEmitter {
static plugin(plugin) {
const currentPlugins = this.plugins;
class AugmentedClient extends this {
}
AugmentedClient.plugins = currentPlugins.concat(plugin);
if (Array.isArray(plugin)) {
return AugmentedClient;
}
return AugmentedClient;
}
async signAndBroadcast(msgs, fee, memo) {
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, signer) {
super();
this.registry = [];
this.env = env;
this.setMaxListeners(0);
this.signer = signer;
const classConstructor = this.constructor;
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) {
this.signer = signer;
this.emit("signer-changed", this.signer);
}
removeSigner() {
this.signer = undefined;
this.emit("signer-changed", this.signer);
}
async useKeplr(keplrChainInfo = {}) {
// 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 = {
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()");
}
}
}
IgniteClient.plugins = [];