/
updateAdmin.ts
82 lines (70 loc) · 2.62 KB
/
updateAdmin.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
import { Command, flags } from '@oclif/command';
import * as YAML from 'yaml';
import { cli } from 'cli-ux';
import { existsSync } from 'fs';
import { SigningCosmWasmClient } from 'cosmwasm';
import { loadConnections, loadRefs } from '../../config';
import { getSigner } from '../../lib/signer';
import * as flag from '../../lib/flag';
import CLI from '../../CLI';
import runCommand from '../../lib/runCommand';
import { DefaulrGasPrice } from '../../lib/key';
export default class ContractUpdateAdmin extends Command {
static description = 'Update the admin of a contract.';
static flags = {
signer: flag.signer,
network: flag.network,
'config-path': flags.string({ default: 'config.json' }),
'refs-path': flags.string({ default: 'refs.json' }),
'keys-path': flags.string({ default: 'keys.js' }),
'instance-id': flags.string({ default: 'default' }),
};
static args = [
{ name: 'contract', required: true },
{ name: 'admin', required: true },
];
async run() {
const { args, flags } = this.parse(ContractUpdateAdmin);
// Command execution path.
const execPath = flags['config-path'];
// Command to be performed.
const command = async () => {
const connections = loadConnections(flags['config-path']);
const refs = loadRefs(flags['refs-path']);
const { network } = flags;
const httpEndpoint = connections(flags.network);
const signer = await getSigner({
network: flags.network,
signerId: flags.signer,
keysPath: flags['keys-path'],
});
const contractAddress = refs[network][args.contract].contractAddresses[flags['instance-id']];
cli.action.start(
`Updating contract admin to: ${args.admin}`,
);
const cosmwasmClient = await SigningCosmWasmClient.connectWithSigner(httpEndpoint.URL, signer, { gasPrice: DefaulrGasPrice});
const account = await signer.getAccounts();
const res = await cosmwasmClient.updateAdmin(account[0].address, contractAddress, args.admin, 'auto', 'update admin');
cli.action.stop();
if (res) {
cli.log(YAML.stringify(res));
} else {
cli.error('Transaction not included in block before timeout.');
}
};
// Error check to be performed upon each backtrack iteration.
const errorCheck = () => {
if (existsSync('contracts') && !existsSync(execPath)) {
CLI.error(
`Contract '${args.contract}' not available in 'contracts/' directory.`,
);
}
};
// Attempt to execute command while backtracking through file tree.
await runCommand(
execPath,
command,
errorCheck,
);
}
}