-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.js
321 lines (319 loc) · 9.64 KB
/
custom.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
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
var editor = ace.edit("code");
var abi, ml;
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/fi");
editor.session.setOption("useWorker", false);
$('docmenut').ready(function(){
compile();
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
switch($(e.target).attr('id')){
case "run-tab":
$("#inputCol").show();
$("#storageCol").show();
$("#deployCol").hide();
$("#callCol").hide();
$("button#run").show();
$("button#deploy").hide();
$("button#call").hide();
$('#runpre').show();
$('#deploypre').hide();
$('#callpre').hide();
break;
case "deploy-tab":
console.log($(e.target).attr('id'));
$("#inputCol").hide();
$("#storageCol").show();
$("#deployCol").show();
$("#callCol").hide();
$("button#run").hide();
$("button#deploy").show();
$("button#call").hide();
$('#runpre').hide();
$('#deploypre').show();
$('#callpre').hide();
break;
case "call-tab":
$("#inputCol").show();
$("#storageCol").hide();
$("#deployCol").hide();
$("#callCol").show();
$("button#run").hide();
$("button#deploy").hide();
$("button#call").show();
$('#runpre').hide();
$('#deploypre').hide();
$('#callpre').show();
break;
}
});
$('button#copyMichelson').click(function(){
copyToClipboard($("#compiled").html());
alert("Compiled code has been copied");
});
$('button#copyAbi').click(function(){
copyToClipboard($("#abi_text").html());
alert("Compiled ABI has been copied");
});
$('button#clear').click(function(){
editor.setValue("");
$("#compiled").html("");
$("#abi_text").html("");
$("#stacktrace").html("");
});
$('button#compile').click(function(){
$("#compiled").html("");
compile();
});
$('button#run').click(function(){
run();
});
$('button#deploy').click(function(){
deploy();
});
$('button#call').click(function(){
call();
});
});
function compile(){
var code = editor.getValue();
if (!code) return false;
$("#compiled").html('');
$("#abi_text").html('');
$("#stacktrace").html('');
$("#inputFields").html('');
$('#entryPoint').children('option:not(:first)').remove();
$("#storage").attr("placeholder", "");
try{
optimizedMl = $('input#optimized').is(':checked');
if (optimizedMl) {
$("#compiled").addClass("wrapped");
} else {
$("#compiled").removeClass("wrapped");
}
var compiled = fi.compile(code, {
ml_format : (optimizedMl ? "compact" : "readable")
});
ml = compiled.ml;
fi.abi.load(compiled.abi);
abi = JSON.parse(compiled.abi);
if (typeof abi.storage != 'undefined'){
$("#storage").attr("disabled", false);
$("#storage").attr("placeholder", fi._core.compile.namedType(abi.storage));
$("#storageFields").html('');
buildFields('storage', abi.storage, '#storageFields');
}else {
$("#storage").attr("placeholder", 'unit');
$("#storage").val("Unit");
$("#storage").attr("disabled", true);
}
$("#compiled").html(ml);
$("#abi_text").html(compiled.abi);
buildInputInterface();
$('#stacktrace').html("Loading...");
$.post("https://api.fi-code.com/typecheck", {code:ml}, function(result){
if (result.success){
$('#stacktrace').html(result.data.stdout);
} else {
$('#stacktrace').html("Typecheck error");
}
});
$('#runtrace').html("Please complete storage and input fields above to run a test");
$('#deploytrace').html("Please complete storage and deployment details above to generate origination");
$('#calltrace').html("Please complete input and transfer details above to generate call");
return ml;
} catch(e){
$("#compiled").html(e);
$("#abi_text").html("Error with compiler");
$("#stacktrace").html("Error with compiler");
}
}
var currentEntry;
function buildInputInterface(){
$('#entryPoint').children('option:not(:first)').remove();
for(var i = 0; i < abi.entry.length; i++){
$("#entryPoint").append('<option value="'+i+'">'+abi.entry[i].name+'</option>');
}
$('#entryPoint').on('change', function() {
if (!this.value) {
currentEntry = false;
return false;
}
currentEntry = abi.entry[this.value];
$("#inputFields").html('');
buildFields('input', currentEntry.input, '#inputFields');
return true;
});
}
function buildFields(lab, cc, id){
for(var i = 0; i < cc.length; i++){
var nn = lab + '.' + cc[i].name;
if (typeof abi.struct != 'undefined'){
ind = fi._core.helper.findInObjArray(abi.struct, 'name', cc[i].type[0]);
} else ind = -1;
if (ind >= 0){
buildFields(nn, abi.struct[ind].type, id);
} else {
$(id).append('<div class="col-md-12"><label style="text-transform:capitalize;">'+nn+'</label><input type="text" class="form-control" name="'+nn+'" placeholder="' + fi._core.compile.type(cc[i].type) + '"></div>');
}
}
}
function formatMlLines(t, ti){
var tl = 0, fm = '', lns = [], bl = 0, instring = false, escaped = false, inline = false, incode = [];
for(var i = 0; i < t.length; i++){
if (t[i] == '{') {
bl++;
if (bl == 1) {
if (incode.length == 0){
inline = true;
lns.push(fm);
fm = '';
}
continue;
}
} else if (t[i] == '}') {
bl--;
if (bl == 0){
incode.push(fm);
fm = '';
continue;
}
}
else if (bl == 0){
if (t[i] == ';') {
if (inline){
var fm1 = lns.pop();
fm1 = fm1.slice(0);
var fm2 = formatMlLines(incode.shift(), (ti+fm1.length + 2));
fm = fm1 + "{ " + fm2 + " }";
while (incode.length){
fm += "\n" + (" ").repeat(ti+fm1.length) + "{ " + formatMlLines(incode.shift(), (ti+fm1.length + 2)) + " }"
}
fm += ";";
lns.push(fm);
fm = '';
inline = false;
continue;
}
fm += ";";
lns.push(fm);
fm = '';
continue;
}
}
fm += t[i];
}
if (inline){
var fm1 = lns.pop();
fm1 = fm1.slice(0);
var fm2 = formatMlLines(incode.shift(), (ti+fm1.length + 2));
fm = fm1 + "{ " + fm2 + " }";
while (incode.length){
fm += "\n" + (" ").repeat(ti+fm1.length) + "{ " + formatMlLines(incode.shift(), (ti+fm1.length + 2)) + " }"
}
fm += ";";
lns.push(fm);
fm = '';
inline = false;
}
if (fm) lns.push(fm);
return lns.join("\n" + (" ").repeat(ti));
}
function formatMl(ml){
return formatMlLines(ml, 0).replace(/; }/g, " }")
}
function deploy(){
$('#deploytrace').html("Loading...");
try {
var manager = $("[name=manager]").val();
var contractName = $("[name=contractName]").val();
var initialBalance = $("[name=initialBalance]").val();
if (!manager) throw "Please enter a manager (local alias or address)";
var
storage = fi.abi.storage(getJsonFromInput('storage').storage);
if (!storage) throw "Please enter storage variables";
$('#deploytrace').html("./tezos-client originate contract "+contractName+" for "+manager+" transferring "+initialBalance+" from "+manager+" running '"+$("#compiled").html()+"' --init '"+storage+"'");
} catch(e){
$('#deploytrace').html("Error generating storage, please check your storage variables");
}
}
function call(){
$('#calltrace').html("Loading...");
try {
var source = $("[name=source]").val();
var destination = $("[name=destination]").val();
var amount = $("[name=amount]").val();
if (!source) throw "Please enter a from/source (local alias or address)";
if (!destination) throw "Please enter the smart contract address (local alias or address)";
var
input = fi.abi.entry(currentEntry.name, getJsonFromInput('input').input);
if (!input) throw "Please select an entry and enter your input variables";
$('#calltrace').html("./tezos-client transfer "+amount+" from "+source+" to "+destination+" --arg '"+input+"'");
} catch(e){
$('#calltrace').html("Error generating call, please check your input variables");
}
}
function run(){
$('#runtrace').html("Loading...");
try {
var
input = fi.abi.entry(currentEntry.name, getJsonFromInput('input').input),
storage = fi.abi.storage(getJsonFromInput('storage').storage);
if (!storage || !input) throw "Please enter storage and input";
$.post("https://api.fi-code.com/run", {
code:ml,
storage : storage,
input : input
}, function(result){
if (result.success){
console.log(result.data.stdout.split("\n")[1].trim());
$('#runtrace').html(result.data.stdout);
}
else {
console.log(result);
$('#runtrace').html("There was an unknown error");
}
});
} catch(e){
$('#runtrace').html("Error running program, please check input and storage variables");
}
}
function getJsonFromInput(i){
var obj = {};
var t = $("input[name^='"+i+"']").map(function(){
var name = $(this).attr('name');
return [name.split(".").concat([$(this).val()])];
}).get();
for (var i = 0; i < t.length; i++){
obj = buildJson(t[i], obj);
}
return obj;
}
function buildJson(t, oo){
var tn = t.shift();
if (t.length > 1){
if (typeof oo[tn] == 'undefined') oo[tn] = {};
oo[tn] = buildJson(t, oo[tn]);
} else {
oo[tn] = t.shift();
}
return oo;
}
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed";
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy");
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}