This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
190 lines (188 loc) · 7.53 KB
/
cli.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
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
import {glob} from 'glob';
import {resolve, dirname, join} from 'path';
import fire from 'js-fire';
import {Contract, Errors, pytezos} from './__init__';
import {generate_docstring} from './michelson/docstring';
import {OperationResult} from './operation/result';
import {create_deployment, create_deployment_status} from './tools/github';
var _pj;
var kernel_js_path, kernel_json;
function _pj_snippets(container) {
function _assert(comp, msg) {
function PJAssertionError(message) {
this.name = "PJAssertionError";
this.message = (message || "Custom error PJAssertionError");
if (((typeof Error.captureStackTrace) === "function")) {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error(message).stack;
}
}
PJAssertionError.prototype = Object.create(Error.prototype);
PJAssertionError.prototype.constructor = PJAssertionError;
msg = (msg || "Assertion failed.");
if ((! comp)) {
throw new PJAssertionError(msg);
}
}
container["_assert"] = _assert;
return container;
}
_pj = {};
_pj_snippets(_pj);
kernel_js_path = join(dirname(dirname(__file__)), "assets", "kernel.js");
kernel_json = {"argv": ["pytezos", "kernel", "run", "-file", "{connection_file}"], "display_name": "Michelson", "language": "michelson", "codemirror_mode": "michelson"};
function make_bcd_link(network, address) {
var net;
net = {"carthagenet": "carthage", "babylonnet": "babylon", "sandboxnet": "sandbox", "mainnet": "main", "zeronet": "zeronet"};
return `https://better-call.dev/${net[network]}/${address}`;
}
function get_contract(path) {
var address, contract, files, network, ptz, script;
if ((path === null)) {
files = glob("*.tz");
_pj._assert((files.length === 1), null);
contract = Contract.from_file(resolve(files[0]));
} else {
if (any(map((x) => {
return path.startswith(x);
}, ["zeronet", "babylonnet", "mainnet", "carthagenet"]))) {
[network, address] = path.split(":");
ptz = pytezos.using({"shell": network});
script = ptz.shell.contracts[address].script();
contract = Contract.from_micheline(script["code"]);
} else {
contract = Contract.from_file(path);
}
}
return contract;
}
class PyTezosCli {
storage(action, path = null) {
/*
:param action: One of `schema`, `default`
:param path: Path to the .tz file, or the following uri: <network>:<KT-address>
*/
var contract;
contract = get_contract(path);
if ((action === "schema")) {
console.log(generate_docstring(contract.storage.schema, {"title": "storage"}));
} else {
if ((action === "default")) {
console.log(JSON.stringify(contract.storage["default"]()));
} else {
_pj._assert(false, action);
}
}
}
parameter(action, path = null) {
/*
:param action: One of `schema`
:param path: Path to the .tz file, or the following uri: <network>:<KT-address>
*/
var contract;
contract = get_contract(path);
if ((action === "schema")) {
console.log(generate_docstring(contract.parameter.schema, {"title": "parameter"}));
} else {
_pj._assert(false, action);
}
}
activate(path, network = "carthagenet") {
/*
Activates and reveals key from the faucet file
:param path: Path to the .json file downloaded from https://faucet.tzalpha.net/
:param network: Default is Babylonnet
*/
var opg, ptz;
ptz = pytezos.using({"key": path, "shell": network});
console.log(`Activating ${ptz.key.public_key_hash()} in the ${network}`);
if ((ptz.balance() === 0)) {
try {
opg = ptz.activate_account().autofill().sign();
console.log(`Injecting activation operation:`);
console.log(JSON.stringify(opg.json_payload()));
opg.inject({"_async": false});
console.log(`Activation succeeded! Claimed balance: ${ptz.balance()} ꜩ`);
} catch(e) {
if ((e instanceof Errors.RpcError)) {
console.log(JSON.stringify(e));
exit((- 1));
} else {
throw e;
}
}
} else {
console.log("Already activated");
}
try {
opg = ptz.reveal().autofill().sign();
console.log(`Injecting reveal operation:`);
console.log(JSON.stringify(opg.json_payload()));
opg.inject({"_async": false});
console.log(`Your key ${ptz.key.public_key_hash()} is now active and revealed`);
} catch(e) {
if ((e instanceof Errors.RpcError)) {
console.log(JSON.stringify(e));
exit((- 1));
} else {
throw e;
}
}
}
deploy(path, storage = null, network = "carthagenet", key = null, github_repo_slug = null, github_oauth_token = null, dry_run = false) {
/*
Deploy contract to the specified network
:param path: Path to the .tz file
:param storage: Storage in JSON format (not Micheline)
:param network:
:param key:
:param github_repo_slug:
:param github_oauth_token:
:param dry_run: Set this flag if you just want to see what would happen
*/
var bcd_link, contract, deployment, opg, originated_contracts, ptz, status;
ptz = pytezos.using({"shell": network, "key": key});
console.log(`Deploying contract using ${ptz.key.public_key_hash()} in the ${network}`);
contract = get_contract(path);
if ((storage !== null)) {
storage = contract.storage.encode(storage);
}
try {
opg = ptz.origination({"script": contract.script({"storage": storage})}).autofill().sign();
console.log(`Injecting origination operation:`);
console.log(JSON.stringify(opg.json_payload()));
if (dry_run) {
console.log(JSON.stringify(opg.preapply()));
exit(0);
} else {
opg = opg.inject({"_async": false});
}
originated_contracts = OperationResult.originated_contracts(opg);
_pj._assert((originated_contracts.length === 1), null);
bcd_link = make_bcd_link(network, originated_contracts[0]);
console.log(`Contract was successfully deployed: ${bcd_link}`);
if (github_repo_slug) {
deployment = create_deployment(github_repo_slug, github_oauth_token, {"environment": network});
console.log(JSON.stringify(deployment));
status = create_deployment_status(github_repo_slug, github_oauth_token, {"deployment_id": deployment["id"], "state": "success", "environment": network, "environment_url": bcd_link});
console.log(JSON.stringify(status));
}
} catch(e) {
if ((e instanceof Errors.RpcError)) {
console.log(JSON.stringify(e));
exit((- 1));
} else {
throw e;
}
}
}
}
function main() {
return new fire(PyTezosCli);
}
if ((__name__ === "__main__")) {
main();
}
export { PyTezosCli, get_contract, main, make_bcd_link, kernel_js_path, kernel_json }
//# sourceMappingURL=cli.js.map