-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.js
49 lines (39 loc) · 1.61 KB
/
connect.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
const { GasPrice, DirectSecp256k1HdWallet, SigningCosmWasmClient, makeCosmoshubPath } = require("cosmwasm");
const { env } = require("process");
const pprint = x => console.log(JSON.stringify(x, undefined, 2));
// Check "MNEMONIC" env variable and ensure it is set to a reasonable value
function getMnemonic() {
const mnemonic = env["MNEMONIC"];
if (!mnemonic || mnemonic.length < 48) {
throw new Error("Must set MNEMONIC to a 12 word phrase");
}
return mnemonic;
}
const junoConfig = {
chainId: "juno-1",
rpcEndpoint: "https://rpc.juno-1.deuslabs.fi:443",
prefix: "juno",
gasPrice: GasPrice.fromString("0.005ujuno"),
feeToken: "ujuno",
};
async function connect(mnemonic) {
const { prefix, gasPrice, feeToken, rpcEndpoint } = junoConfig;
const hdPath = makeCosmoshubPath(0);
// Setup signer
const offlineSigner = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix, hdPaths: [hdPath] });
const { address } = (await offlineSigner.getAccounts())[0];
console.log(`Connected to ${address}`);
// Init SigningCosmWasmClient client
const client = await SigningCosmWasmClient.connectWithSigner(rpcEndpoint, offlineSigner, {
prefix,
gasPrice,
});
const balance = await client.getBalance(address, feeToken);
console.log(`Balance: ${balance.amount} ${balance.denom}`);
const chainId = await client.getChainId();
if (chainId !== junoConfig.chainId) {
throw Error("Given ChainId doesn't match the clients ChainID!");
}
return { client, address };
}
module.exports = { connect, getMnemonic, pprint };