-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
149 lines (120 loc) · 4.75 KB
/
utils.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
138
139
140
141
142
143
144
145
146
147
148
149
const { readFileSync, writeFileSync, existsSync, mkdirSync } = require('fs');
const path = require('path');
const { SigningCosmWasmClient } = require("@cosmjs/cosmwasm-stargate");
const { DirectSecp256k1HdWallet } = require("@cosmjs/proto-signing");
const { coins } = require("@cosmjs/amino");
const { assertIsDeliverTxSuccess, calculateFee, } = require("@cosmjs/stargate");
// example:
// juno/accounts.json
// juno/alice_accounts.json
const getWalletFile = (config, label = '', onlyDir = false) => {
if (onlyDir) return path.join(__dirname, 'data', `${config.bech32_prefix}`)
else return path.join(__dirname, 'data', `${config.bech32_prefix}/${label ? label + '_' : ''}accounts.json`)
};
const getWallet = async (config, label = '') => {
const accountFile = getWalletFile(config, label)
try {
const res = await readFileSync(accountFile, 'utf8')
return res;
} catch (e) { }
}
const getClient = async (mnemonic, config) => {
const endpoint = config.apis.rpc[0].address;
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: config.bech32_prefix });
return SigningCosmWasmClient.connectWithSigner(endpoint, wallet);
}
const getAgentClient = async (config, label) => {
const accountData = await getLabelledWallet(config, label)
if (!accountData) return;
return getClient(accountData.mnemonic, config)
}
const getLabelledWallet = async (config, label) => {
const accountFile = await getWalletFile(config, label)
try {
return JSON.parse(await readFileSync(accountFile, 'utf8'))
} catch (e) { }
}
const generateAndStoreWallet = async (config, label = '') => {
const accountFileDir = getWalletFile(config, label, true)
const accountFile = getWalletFile(config, label)
try {
return JSON.parse(await readFileSync(accountFile, 'utf8'))
} catch (e) { }
const wallet = await DirectSecp256k1HdWallet.generate(24, { prefix: config.bech32_prefix });
let accounts = await wallet.getAccounts();
let mnemonic = wallet.mnemonic;
// console.log(`New ${config.bech32_prefix} account:`, accounts[0].address);
const accountsFile = {
mnemonic,
accounts,
}
if (!existsSync(accountFileDir)) await mkdirSync(accountFileDir)
await writeFileSync(accountFile, JSON.stringify(accountsFile), 'utf8')
return accountsFile
}
const updateContractWallet = async (config, label = '', options = {}) => {
const accountFileDir = getWalletFile(config, label, true)
const accountFile = getWalletFile(config, label)
let walletData = {}
try {
walletData = JSON.parse(await readFileSync(accountFile, 'utf8'))
} catch (e) {
return;
}
let newData = {
...options,
...walletData,
}
if (!existsSync(accountFileDir)) await mkdirSync(accountFileDir)
await writeFileSync(accountFile, JSON.stringify(newData), 'utf8')
return newData
}
const getChainCoinConfig = config => {
const prefix = config.bech32_prefix;
if (prefix === 'wasm') return { prefix, gas: `ucosm`, denom: `cosm` }
return { prefix, gas: `u${prefix}`, denom: `${prefix}` }
}
module.exports = {
catnyms: ['manager','ifttt_simple'],
agentnyms: ['angela', 'bob', 'creed', 'dwight', 'jim', 'kevin', 'michael', 'oscar', 'pam'],
getChainConfig: async chain => {
if (!chain) {
return JSON.parse(await readFileSync(__dirname + '/data/local_chain.json', 'utf8'))
} else {
// return chainRegistry[chain].chain
const p = path.join(__dirname, '/node_modules/chain-registry/', chain, 'chain.json')
return JSON.parse(await readFileSync(p, 'utf8'))
}
},
getChainCoinConfig,
getClient,
getAgentClient,
faucetSendCoins: async (config, recipient, amount) => {
const { prefix, gas, denom } = getChainCoinConfig(config)
const endpoint = config.apis.rpc[0].address;
const walletFile = path.join(__dirname, 'data', `faucet_account.json`)
const walletData = JSON.parse(await readFileSync(walletFile, 'utf8'))
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(walletData.mnemonic, { prefix });
const faucet_address = (await wallet.getAccounts())[0].address;
const client = await SigningCosmWasmClient.connectWithSigner(endpoint, wallet);
const amt = coins(amount, gas);
const fee = calculateFee(100_000, `0.025u${denom}`);
const memo = "Faucet Send";
const sendResult = await client.sendTokens(faucet_address, recipient, amt, fee, memo);
console.log(`${memo} :: ${recipient} got ${amount}u${denom}`);
return assertIsDeliverTxSuccess(sendResult)
},
updateContractWallet,
getWalletFile,
getWallet,
getLabelledWallet,
generateAndStoreWallet,
generateWalletsFromLabelList: async (config, list = []) => {
// return mapped accounts
let accounts = {}
for await (const l of list) {
accounts[l] = await generateAndStoreWallet(config, l)
}
return accounts
},
}