-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ts
649 lines (588 loc) · 17.4 KB
/
helpers.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
import {
Coins,
isTxError,
LCDClient,
LCDClientConfig,
MnemonicKey,
Msg,
MsgExecuteContract,
MsgInstantiateContract,
MsgStoreCode,
Tx,
TxLog,
Wallet,
} from "@terra-money/feather.js";
import { LedgerKey } from "@terra-money/ledger-station-js";
import { SignMode } from "@terra-money/terra.proto/cosmos/tx/signing/v1beta1/signing";
import * as axios from "axios";
import * as fs from "fs";
import { get, set } from "lodash";
import * as promptly from "promptly";
import * as keystore from "./keystore";
import { AssetInfo } from "./types/ampz/eris_ampz_execute";
import { NeutronTxResponse } from "./types/model";
import { AssetInfoBaseFor_Addr } from "./types/ve3/asset-staking/response_to_all_pending_rewards";
const DEFAULT_GAS_SETTINGS = {
// gasPrices: "5.665uluna",
// gasAdjustment: 1.2,
};
// interface LCDExtension {}
const phoenix: LCDClientConfig = {
chainID: "phoenix-1",
// lcd: "https://phoenix-lcd.terra.dev",
lcd: "https://phoenix-lcd.erisprotocol.com",
gasAdjustment: 1.4,
prefix: "terra",
gasPrices: { uluna: 0.015 },
};
const phoenixLowfee: LCDClientConfig = {
chainID: "phoenix-1",
// lcd: "https://phoenix-lcd.terra.dev",
lcd: "https://phoenix-lcd.erisprotocol.com",
gasAdjustment: 1.3,
prefix: "terra",
gasPrices: { uluna: 0.000025 },
};
const columbus: LCDClientConfig = {
chainID: "columbus-5",
lcd: "https://lcd.terra.dev",
gasPrices: { uluna: "28.325" },
gasAdjustment: 1.3,
prefix: "terra",
};
const phoenix_newmetric: LCDClientConfig = {
chainID: "phoenix-1",
// lcd: "https://phoenix-lcd.terra.dev",
lcd: "https://cradle-manager.ec1-prod.newmetric.xyz/cradle/proxy/3d0f9480-3445-4038-b1de-303791b951ff",
gasAdjustment: 1.4,
prefix: "terra",
gasPrices: { uluna: 0.015 },
};
const networks = {
mainnet: phoenix,
["mainnet-lowfee"]: phoenixLowfee,
["mainnet-copy"]: phoenix_newmetric,
ledger: phoenix,
classic: columbus,
["classic-testnet"]: columbus,
["ledger-classic"]: columbus,
testnet: {
chainID: "pisco-1",
// lcd: "https://pisco-lcd.terra.dev",
lcd: "https://pisco-lcd.erisprotocol.com",
gasAdjustment: 1.5,
prefix: "terra",
gasPrices: { uluna: 0.015 },
},
["testnet-kujira"]: {
chainID: "harpoon-4",
lcd: "https://kujira-testnet-api.polkachu.com",
gasAdjustment: 1.3,
prefix: "kujira",
gasPrices: {
ukuji: 0.0025,
},
},
kujira: {
chainID: "kaiyo-1",
lcd: "https://kujira-api.polkachu.com",
gasAdjustment: 1.3,
prefix: "kujira",
gasPrices: {
ukuji: 0.0034,
},
},
"kujira-copy": {
chainID: "kaiyo-1",
lcd: "https://cradle-manager.ec1-prod.newmetric.xyz/cradle/proxy/2128d7dc-6cbb-49b7-987a-a2e706a1898b",
gasAdjustment: 1.3,
prefix: "kujira",
gasPrices: {
ukuji: 0.0034,
},
},
["testnet-migaloo"]: {
chainID: "narwhal-1",
lcd: "https://whitewhale-testnet-api.polkachu.com",
gasAdjustment: 1.5,
prefix: "migaloo",
gasPrices: { uwhale: 0 },
},
["testnet-cosmos"]: {
chainID: "theta-testnet-001",
lcd: "https://rest.sentry-01.theta-testnet.polypore.xyz",
gasAdjustment: 1.5,
prefix: "cosmos",
gasPrices: { uatom: 0.01 },
},
migaloo: {
chainID: "migaloo-1",
// lcd: "https://migaloo-lcd.erisprotocol.com",
lcd: "https://migaloo-api.polkachu.com",
// lcd: "https://api-migaloo.cosmos-spaces.cloud",
gasAdjustment: 1.5,
prefix: "migaloo",
gasPrices: { uwhale: 1 },
},
["archwaytest"]: {
chainID: "constantine-3",
lcd: "https://api.constantine.archway.tech",
gasAdjustment: 1.3,
prefix: "archway",
gasPrices: { aconst: 900000000000 },
},
["archway"]: {
chainID: "archway-1",
lcd: "https://api.mainnet.archway.io",
gasAdjustment: 1.3,
prefix: "archway",
gasPrices: { aarch: 900000500000 },
},
chihuahua: {
chainID: "chihuahua-1",
lcd: "https://api.chihuahua.wtf",
gasAdjustment: 1.3,
prefix: "chihuahua",
gasPrices: { uhuahua: "500" },
},
juno: <LCDClientConfig>{
chainID: "juno-1",
lcd: "https://juno-api.polkachu.com",
gasPrices: {
ujuno: "0.075",
},
gasAdjustment: 1.3,
prefix: "juno",
},
osmosis: {
chainID: "osmosis-1",
lcd: "https://osmosis-api.polkachu.com/",
gasAdjustment: 1.3,
prefix: "osmo",
// gasPrices: { uosmo: 0.0025 },
gasPrices: { uosmo: 0.004 },
},
"testnet-osmosis": {
chainID: "osmo-test-5",
lcd: "https://lcd.osmotest5.osmosis.zone",
gasAdjustment: 1.5,
prefix: "osmo",
gasPrices: { uosmo: 0.01 },
},
neutron: {
chainID: "neutron-1",
lcd: "https://rest-kralum.neutron-1.neutron.org/",
gasAdjustment: 1.4,
prefix: "neutron",
gasPrices: { untrn: 0.01 },
},
"testnet-neutron": {
chainID: "pion-1",
lcd: "https://rest-falcron.pion-1.ntrn.tech/",
gasAdjustment: 1.5,
prefix: "neutron",
gasPrices: { untrn: 0.0053 },
},
sei: {
chainID: "pacific-1",
lcd: "https://sei-api.polkachu.com/",
gasAdjustment: 1.1,
prefix: "sei",
gasPrices: { usei: 0.1 },
},
nibiru: {
chainID: "cataclysm-1",
// lcd: "https://lcd.nibiru.fi/",
lcd: "https://nibiru.api.kjnodes.com",
gasAdjustment: 1.3,
prefix: "nibi",
gasPrices: { unibi: 0.025 },
},
"testnet-nibiru": {
chainID: "nibiru-testnet-1",
// lcd: "https://lcd.nibiru.fi/",
lcd: "https://lcd.testnet-1.nibiru.fi",
gasAdjustment: 1.3,
prefix: "nibi",
gasPrices: { unibi: 0.025 },
},
};
export type Chains = keyof typeof networks;
let current_network: Chains = "testnet";
export function getChainId() {
const result = networks[current_network];
return result.chainID;
}
export function getPrefix() {
const result = networks[current_network];
return result.prefix;
}
export function getCurrent() {
return networks[current_network];
}
/**
* @notice Create an `LCDClient` instance based on provided network identifier
*/
export function createLCDClient(network: string): LCDClient {
current_network = network as any;
const config = networks[current_network];
return new LCDClient({
[config.chainID]: config,
});
}
/**
* @notice Create a `Wallet` instance by loading the private key stored in the keystore
*/
export async function createWallet(terra: LCDClient, keyName: string, keyDir: string): Promise<Wallet> {
if (keyName === "ledger") {
const lk = await LedgerKey.create({
transport: await TransportNodeHid.create(60 * 1000),
});
return terra.wallet(lk as any);
}
if (keyName === "ledger-classic") {
const lk = await LedgerKey.create({
transport: await TransportNodeHid.create(60 * 1000),
});
return terra.wallet(lk as any);
}
if (keyName === "ledger-juno") {
const lk = await LedgerKey.create({
transport: await TransportNodeHid.create(60 * 1000),
coinType: 118,
});
return terra.wallet(lk as any);
}
// if (keyName === "ledger-juno") {
// const lk = await JunoLedgerKey.create(
// await TransportNodeHid.create(60 * 1000)
// );
// return terra.wallet(lk);
// }
const password = await promptly.password("Enter password to decrypt the key:");
if (keyName.startsWith("key-")) {
const words = keystore.loadKey(keyName, keyDir, password);
const mnemonic = new MnemonicKey({ mnemonic: words, coinType: 118 });
return terra.wallet(mnemonic);
}
const key = keystore.load(keyName, keyDir, password);
return terra.wallet(key);
}
/**
* @notice Create a `Wallet` instance by loading the private key stored in the keystore
*/
export async function getMnemonic(keyName: string, keyDir: string): Promise<string> {
const password = await promptly.password("Enter password to decrypt the key:");
const key = keystore.loadKey(keyName, keyDir, password);
return key;
}
/**
* @notice Pause script execution until user confirms
*/
export async function waitForConfirm(msg: string) {
const proceed = await promptly.confirm(`${msg} [y/N]:`);
if (!proceed) {
console.log("User aborted!");
process.exit(1);
}
}
/**
* @notice Same with `sendTransaction`, but requires confirmation for CLI before broadcasting
*/
export async function sendTxWithConfirm(signer: Wallet, msgs: Msg[], memo?: string, gas?: string, confirm = true) {
try {
let signMode: SignMode | undefined;
if (signer.key instanceof LedgerKey) {
signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
}
const x = {
msgs,
...DEFAULT_GAS_SETTINGS,
memo: memo as string,
gas: gas as string,
signMode: signMode as SignMode,
chainID: getChainId(),
};
let tx: Tx;
try {
const info = await signer.accountNumber(x.chainID);
// console.log("signer", signer, info);
tx = await signer.createAndSignTx(x);
} catch (error: any) {
if (error?.response?.data?.message?.includes("not found")) {
// console.log("INSIDE " + error?.response?.data?.message);
const accountNumber = await signer.accountNumber(x.chainID);
// console.log("🚀 ~ file: helpers.ts:223 ~ accountNumber", accountNumber);
tx = await signer.createAndSignTx({
...x,
accountNumber,
sequence: 0,
});
} else {
console.log("fallback axios");
console.log("SETTINGS", x);
const lcd = signer.lcd.config[x.chainID].lcd;
const account = await axios.default.get<{ info: { account_number: string; sequence: string } }>(
`${lcd}/cosmos/auth/v1beta1/account_info/${signer.key.accAddress(getPrefix())}`
);
console.log(account.data);
tx = await signer.createAndSignTx({
...x,
accountNumber: +account.data.info.account_number,
sequence: +account.data.info.sequence,
});
}
}
// if (x.chainID === "chihuahua-1") {
// const fee = new Coin("uchihuahua", tx.auth_info.fee.gas_limit);
// tx = await signer.createAndSignTx({
// ...x,
// fee: new Fee(tx.auth_info.fee.gas_limit, [fee]),
// });
// }
if (confirm) {
console.log("\n" + JSON.stringify(tx).replace(/\\/g, "") + "\n");
await waitForConfirm("Confirm transaction before broadcasting");
}
const result = await signer.lcd.tx.broadcast(tx, getChainId());
console.log("Hash: ", result.txhash);
if (isTxError(result)) {
throw new Error(`tx failed! raw log: ${result.raw_log}`);
}
return result;
} catch (error) {
console.log(error);
throw error;
}
}
/**
* @notice Same with `sendTransaction`, but requires confirmation for CLI before broadcasting
*/
export async function sendTxWithConfirmUnsigned(
wallet: Wallet,
msgs: Msg[],
memo?: string,
gas?: string,
confirm = true
) {
try {
const x = {
msgs,
...DEFAULT_GAS_SETTINGS,
memo: memo as string,
gas: gas as string,
chainID: getChainId(),
};
const tx = await wallet.createTx(x);
if (confirm) {
console.log("\n" + JSON.stringify(tx).replace(/\\/g, "") + "\n");
await waitForConfirm("Confirm transaction before broadcasting");
}
const result = await wallet.lcd.tx.broadcast(tx, getChainId());
console.log("Hash: ", result.txhash);
if (isTxError(result)) {
throw new Error(`tx failed! raw log: ${result.raw_log}`);
}
return result;
} catch (error) {
console.log(error);
throw error;
}
}
/**
* @notice Same with `storeCode`, but requires confirmation for CLI before broadcasting
*/
export async function storeCodeWithConfirm(signer: Wallet, filePath: string, confirm = true) {
const code = fs.readFileSync(filePath).toString("base64");
const result = await sendTxWithConfirm(
signer,
[new MsgStoreCode(signer.key.accAddress(getPrefix()), code)],
undefined,
undefined,
confirm
);
if (!result.logs.length) {
const tx = await getTx(result.txhash);
if (tx) {
return parseInt(
tx.events.find((a) => a.type === "store_code")?.attributes.find((a) => a.key === "code_id")?.value ?? ""
);
}
}
return parseInt(result.logs[0].eventsByType["store_code"]["code_id"][0]);
}
export async function getTx(txhash: string) {
const config = getCurrent();
for (let index = 0; index < 10; index++) {
try {
const tx = (await axios.default.get<NeutronTxResponse>(`${config.lcd}/cosmos/tx/v1beta1/txs/${txhash}`)).data
.tx_response;
if (tx.code === 0) {
return tx;
}
} catch (error) {}
}
return undefined;
}
/**
* @notice Same with `instantiateContract`, but requires confirmation for CLI before broadcasting
*/
export async function instantiateWithConfirm(
signer: Wallet,
admin: string,
codeId: number,
initMsg: object,
label = "Eris Liquid Staking Hub",
initCoins?: Coins.Input
) {
const result = await sendTxWithConfirm(signer, [
new MsgInstantiateContract(signer.key.accAddress(getPrefix()), admin, codeId, initMsg, initCoins, label),
]);
if (!result.logs.length) {
// console.log("SEARCH");
const tx = await getTx(result.txhash);
// console.log("found tx", tx?.events);
const attributes = tx?.events.find((a) => a.type === "instantiate")?.attributes;
// console.log("attributes", attributes);
const address = attributes?.find((a) => a.key === "_contract_address")?.value ?? "";
return {
...result,
tx,
address,
};
}
const extendedResult = {
...result,
address: result.logs.map((a: TxLog) => a.eventsByType["instantiate"]["_contract_address"][0])[0],
};
return extendedResult;
}
export async function instantiateMultipleWithConfirm(
signer: Wallet,
admin: string,
codeId: number,
init: { msg: object; label: string }[]
) {
const result = await sendTxWithConfirm(
signer,
init.map(
(a) => new MsgInstantiateContract(signer.key.accAddress(getPrefix()), admin, codeId, a.msg, undefined, a.label)
)
);
return result;
}
/**
* Encode a JSON object to base64 string
*/
export function encodeBase64(obj: object | string | number) {
return Buffer.from(JSON.stringify(obj)).toString("base64");
}
export const delayPromise = (delayMs: number): Promise<any> => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(delayMs);
}, delayMs);
});
};
export const toBase64 = (obj: any): string => {
return Buffer.from(JSON.stringify(obj)).toString("base64");
};
export const getToken = (asset: AssetInfo | AssetInfoBaseFor_Addr | string) => {
if (typeof asset === "string") {
return asset;
} else if ("token" in asset) {
return asset.token.contract_addr;
} else if ("native_token" in asset) {
return asset.native_token.denom;
} else if ("cw20" in asset) {
return asset.cw20;
} else {
return asset.native;
}
};
export const toNew = (asset: AssetInfo): AssetInfoBaseFor_Addr => {
if ("native_token" in asset) {
return { native: asset.native_token.denom };
} else {
return { cw20: asset.token.contract_addr };
}
};
export const addInfo = (folder: string, network: Chains, path: string, value: string) => {
const filePath = folder + "/data.json";
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath).toString() : "{}";
const data = JSON.parse(existing);
if (!data[network]) {
data[network] = {};
}
const existingValue = get(data[network], path);
if (existingValue) {
console.log(`changing value ${folder}.${network}.${path}: ${existingValue} -> ${value}`);
} else {
console.log(`adding value ${folder}.${network}.${path}: ${value}`);
}
set(data[network], path, value);
fs.writeFileSync(filePath, JSON.stringify(data, undefined, 2));
};
export const getInfo = (folder: string, network: Chains, path: string) => {
const filePath = folder + "/data.json";
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath).toString() : "{}";
const data = JSON.parse(existing);
if (!data[network]) {
data[network] = {};
}
const existingValue = get(data[network], path);
if (existingValue) {
return existingValue;
} else {
throw new Error(`did not find ${folder}.${network}.${path}`);
}
};
export const selectMany = <T, U>(array: T[], selector: (x: T) => U[]): Array<U> => {
const result = array.map((x) => selector(x));
if (!result.length) {
return [];
}
return result.reduce((a, b) => a.concat(b));
};
export function printProposal(msg: MsgExecuteContract): MsgExecuteContract {
console.log("Contract: ", msg.contract);
console.log(JSON.stringify(msg.execute_msg, null, 2));
return msg;
}
let props: MsgExecuteContract[] = [];
export function addProposal(msg: MsgExecuteContract): MsgExecuteContract {
props.push(msg);
return msg;
}
export function done(title: string, description: string, dao: string) {
const data = {
id: "DaoProposalSingle",
data: {
title,
description,
actionData: props.map((a, index) => ({
_id: index.toString(),
actionKey: "execute",
data: {
chainId: "phoenix-1",
sender: dao,
address: a.contract,
message: JSON.stringify(a.execute_msg, null, 2),
funds: [],
cw20: false,
},
})),
},
};
const dataFill = Buffer.from(JSON.stringify(data)).toString("base64");
const urlFill = encodeURIComponent(JSON.stringify(data));
const url = `https://daodao.zone/dao/${dao}/proposals/create?prefill=${dataFill}`;
const url2 = `https://daodao.zone/dao/${dao}/proposals/create?prefill=${urlFill}`;
props = [];
console.log(url);
console.log("---");
console.log(url2);
throw new Error("dont commit");
return undefined;
}