Browse Source

reindent lines in eth.js

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
bd42cb12b0
  1. 467
      libethrpc/eth.js

467
libethrpc/eth.js

@ -2,262 +2,263 @@
if (typeof(window.eth) === "undefined") if (typeof(window.eth) === "undefined")
{ {
if (typeof(require) !== "undefined") if (typeof(require) !== "undefined")
require( ['ethString'], function() {} ) require( ['ethString'], function() {} )
else if (typeof(String.prototype.pad) === "undefined") else if (typeof(String.prototype.pad) === "undefined")
{ {
var scriptTag = document.getElementsByTagName('script'); var scriptTag = document.getElementsByTagName('script');
scriptTag = scriptTag[scriptTag.length - 1]; scriptTag = scriptTag[scriptTag.length - 1];
var scriptPath = scriptTag.src; var scriptPath = scriptTag.src;
var path = scriptPath.substr(0, scriptPath.lastIndexOf( '/' )); var path = scriptPath.substr(0, scriptPath.lastIndexOf( '/' ));
var start = '<script src="' + path + '/'; var start = '<script src="' + path + '/';
var slash = '"><'+'/script>'; var slash = '"><'+'/script>';
document.write(start + 'BigInteger.js' + slash); document.write(start + 'BigInteger.js' + slash);
document.write(start + 'ethString.js' + slash); document.write(start + 'ethString.js' + slash);
} }
var spec = [
{ "method": "coinbase", "params": null, "order": [], "returns" : "" },
{ "method": "setCoinbase", "params": { "address": "" }, "order": ["address"], "returns" : true },
{ "method": "listening", "params": null, "order": [], "returns" : false },
{ "method": "setListening", "params": { "listening": false }, "order" : ["listening"], "returns" : true },
{ "method": "mining", "params": null, "order": [], "returns" : false },
{ "method": "setMining", "params": { "mining": false }, "order" : ["mining"], "returns" : true },
{ "method": "gasPrice", "params": null, "order": [], "returns" : "" },
{ "method": "key", "params": null, "order": [], "returns" : "" },
{ "method": "keys", "params": null, "order": [], "returns" : [] },
{ "method": "peerCount", "params": null, "order": [], "returns" : 0 },
{ "method": "defaultBlock", "params": null, "order": [], "returns" : 0},
{ "method": "number", "params": null, "order": [], "returns" : 0},
{ "method": "balanceAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns" : ""},
{ "method": "stateAt", "params": { "address": "", "storage": "", "block": 0}, "order": ["address", "storage", "block"], "returns": ""},
{ "method": "countAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns" : 0.0},
{ "method": "codeAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns": ""},
var spec = [ { "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "coinbase", "params": null, "order": [], "returns" : "" }, { "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""},
{ "method": "setCoinbase", "params": { "address": "" }, "order": ["address"], "returns" : true },
{ "method": "listening", "params": null, "order": [], "returns" : false },
{ "method": "setListening", "params": { "listening": false }, "order" : ["listening"], "returns" : true },
{ "method": "mining", "params": null, "order": [], "returns" : false },
{ "method": "setMining", "params": { "mining": false }, "order" : ["mining"], "returns" : true },
{ "method": "gasPrice", "params": null, "order": [], "returns" : "" },
{ "method": "key", "params": null, "order": [], "returns" : "" },
{ "method": "keys", "params": null, "order": [], "returns" : [] },
{ "method": "peerCount", "params": null, "order": [], "returns" : 0 },
{ "method": "defaultBlock", "params": null, "order": [], "returns" : 0},
{ "method": "number", "params": null, "order": [], "returns" : 0},
{ "method": "balanceAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns" : ""}, { "method": "block", "params": { "params": {}}, "order": ["params"], "returns": {}},
{ "method": "stateAt", "params": { "address": "", "storage": "", "block": 0}, "order": ["address", "storage", "block"], "returns": ""}, { "method": "transaction", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "countAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns" : 0.0}, { "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}},
{ "method": "codeAt", "params": { "address": "", "block": 0}, "order": ["address", "block"], "returns": ""},
{ "method": "transact", "params": { "json": {}}, "order": ["json"], "returns": ""}, { "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []},
{ "method": "call", "params": { "json": {}}, "order": ["json"], "returns": ""}, { "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": 0},
{ "method": "check", "params": { "id": 0}, "order": [], "returns": true},
{ "method": "killWatch", "params": { "id": 0}, "order": ["params"], "returns": true},
{ "method": "block", "params": { "params": {}}, "order": ["params"], "returns": {}}, { "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "transaction", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}}, { "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "uncle", "params": { "params": {}, "i": 0}, "order": ["params", "i"], "returns": {}}, { "method": "sha3", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "toAscii", "params": { "s": ""}, "order": ["s"], "returns": ""},
{ "method": "fromAscii", "params": { "s": "", "padding": 0}, "order": ["s", "padding"], "returns": ""},
{ "method": "toDecimal", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": 0.0}, "order": ["s"], "returns" : ""},
{ "method": "fromFixed", "params": {"s": ""}, "order": ["s"], "returns" : 0.0},
{ "method": "offset", "params": {"s": "", "o": 0}, "order": ["s", "o"], "returns" : ""}
];
{ "method": "messages", "params": { "params": {}}, "order": ["params"], "returns": []}, window.eth = (function ethScope() {
{ "method": "watch", "params": { "params": ""}, "order": ["params"], "returns": 0}, var m_reqId = 0
{ "method": "check", "params": { "id": 0}, "order": [], "returns": true}, var ret = {}
{ "method": "killWatch", "params": { "id": 0}, "order": ["params"], "returns": true}, function reformat(m, d) { return m == "lll" ? d.bin() : d; };
function reqSync(m, p) {
var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId }
m_reqId++
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080", false)
request.send(JSON.stringify(req))
return reformat(m, JSON.parse(request.responseText).result)
};
function reqAsync(m, p, f) {
var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId }
m_reqId++
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080", true)
request.send(JSON.stringify(req))
request.onreadystatechange = function() {
if (request.readyState === 4)
if (f)
f(reformat(m, JSON.parse(request.responseText).result));
};
};
{ "method": "secretToAddress", "params": { "s": ""}, "order": ["s"], "returns": ""}, var getParams = function (spec, name, args) {
{ "method": "lll", "params": { "s": ""}, "order": ["s"], "returns": ""}, var setup = spec.filter(function (s) {
{ "method": "sha3", "params": { "s": ""}, "order": ["s"], "returns": ""}, return s.method === name;
{ "method": "toAscii", "params": { "s": ""}, "order": ["s"], "returns": ""}, });
{ "method": "fromAscii", "params": { "s": "", "padding": 0}, "order": ["s", "padding"], "returns": ""},
{ "method": "toDecimal", "params": {"s": ""}, "order": ["s"], "returns" : ""},
{ "method": "toFixed", "params": {"s": 0.0}, "order": ["s"], "returns" : ""},
{ "method": "fromFixed", "params": {"s": ""}, "order": ["s"], "returns" : 0.0},
{ "method": "offset", "params": {"s": "", "o": 0}, "order": ["s", "o"], "returns" : ""}
];
window.eth = (function ethScope() { if (setup.length === 0) {
var m_reqId = 0 return {};
var ret = {} }
function reformat(m, d) { return m == "lll" ? d.bin() : d; };
function reqSync(m, p) {
var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId }
m_reqId++
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080", false)
request.send(JSON.stringify(req))
return reformat(m, JSON.parse(request.responseText).result)
};
function reqAsync(m, p, f) {
var req = { "jsonrpc": "2.0", "method": m, "params": p, "id": m_reqId }
m_reqId++
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8080", true)
request.send(JSON.stringify(req))
request.onreadystatechange = function() {
if (request.readyState === 4)
if (f)
f(reformat(m, JSON.parse(request.responseText).result));
};
};
var getParams = function (spec, name, args) {
var setup = spec.filter(function (s) {
return s.method === name;
});
if (setup.length === 0) { var paramSetup = setup[0];
return {};
}
var paramSetup = setup[0]; var p = paramSetup.params ? {} : null;
for (j in paramSetup.order)
p[paramSetup.order[j]] = args[j];
return p;
};
var p = paramSetup.params ? {} : null; var addPrefix = function (s, prefix) {
for (j in paramSetup.order) if (!s) {
p[paramSetup.order[j]] = args[j]; return s;
return p; }
}; return prefix + s.slice(0, 1).toUpperCase() + s.slice(1);
};
var addPrefix = function (s, prefix) { var toGetter = function (s) {
if (!s) { return addPrefix(s, "get");
return s; };
}
return prefix + s.slice(0, 1).toUpperCase() + s.slice(1);
};
var toGetter = function (s) { var toSetter = function (s) {
return addPrefix(s, "get"); return addPrefix(s, "set");
}; };
var toSetter = function (s) { var defaults = function (def, obj) {
return addPrefix(s, "set"); if (!def) {
}; return obj;
}
var rewriteProperties = function (dst, p) {
Object.keys(p).forEach(function (key) {
if (p[key] !== undefined) {
dst[key] = p[key];
}
});
};
var res = {};
rewriteProperties(res, def);
rewriteProperties(res, obj);
return res;
};
var defaults = function (def, obj) { var setupProperties = function (root, spec) {
if (!def) { var properties = [
return obj; { name: "coinbase", getter: "coinbase", setter: "setCoinbase" },
} { name: "listening", getter: "listening", setter: "setListening" },
var rewriteProperties = function (dst, p) { { name: "mining", getter: "mining", setter: "setMining" },
Object.keys(p).forEach(function (key) { { name: "gasPrice", getter: "gasPrice"},
if (p[key] !== undefined) { { name: "key", getter: "key" },
dst[key] = p[key]; { name: "keys", getter: "keys" },
} { name: "peerCount", getter: "peerCount" },
}); { name: "defaultBlock", getter: "defaultBlock" },
}; { name: "number", getter: "number" }
var res = {}; ];
rewriteProperties(res, def);
rewriteProperties(res, obj);
return res;
};
var setupProperties = function (root, spec) { properties.forEach(function (property) {
var properties = [ var p = {};
{ name: "coinbase", getter: "coinbase", setter: "setCoinbase" }, if (property.getter) {
{ name: "listening", getter: "listening", setter: "setListening" }, p.get = function () {
{ name: "mining", getter: "mining", setter: "setMining" }, return reqSync(property.getter, {});
{ name: "gasPrice", getter: "gasPrice"}, };
{ name: "key", getter: "key" }, root[toGetter(property.name)] = function (f) {
{ name: "keys", getter: "keys" }, return reqAsync(property.getter, null, f);
{ name: "peerCount", getter: "peerCount" }, };
{ name: "defaultBlock", getter: "defaultBlock" }, }
{ name: "number", getter: "number" }]; if (property.setter) {
p.set = function (newVal) {
properties.forEach(function (property) { return reqSync(property.setter, getParams(spec, property.setter, arguments));
var p = {}; };
if (property.getter) { root[toSetter(property.name)] = function (newVal, f) {
p.get = function () { return reqAsync(property.setter, getParams(spec, property.setter, arguments), f);
return reqSync(property.getter, {}); };
}; }
root[toGetter(property.name)] = function (f) {
return reqAsync(property.getter, null, f);
};
}
if (property.setter) {
p.set = function (newVal) {
return reqSync(property.setter, getParams(spec, property.setter, arguments));
};
root[toSetter(property.name)] = function (newVal, f) {
return reqAsync(property.setter, getParams(spec, property.setter, arguments), f);
};
}
Object.defineProperty(root, property.name, p); Object.defineProperty(root, property.name, p);
}); });
}; };
var setupMethods = function (root, spec) { var setupMethods = function (root, spec) {
var methods = [ var methods = [
{ name: "balanceAt", async: "getBalanceAt", default: {block: 0} }, { name: "balanceAt", async: "getBalanceAt", default: {block: 0} },
{ name: "stateAt", async: "getStateAt", default: {block: 0} }, { name: "stateAt", async: "getStateAt", default: {block: 0} },
{ name: "countAt", async: "getCountAt", default: {block: 0} }, { name: "countAt", async: "getCountAt", default: {block: 0} },
{ name: "codeAt", async: "getCodeAt", default: {block: 0} }, { name: "codeAt", async: "getCodeAt", default: {block: 0} },
{ name: "transact", async: "doTransact"}, { name: "transact", async: "doTransact"},
{ name: "call", async: "doCall" }, { name: "call", async: "doCall" },
{ name: "messages", async: "getMessages" }, { name: "messages", async: "getMessages" },
{ name: "block", async: "getBlock" }, { name: "block", async: "getBlock" },
{ name: "transaction", async: "getTransaction" }, { name: "transaction", async: "getTransaction" },
{ name: "uncle", async: "getUncle" }, { name: "uncle", async: "getUncle" },
{ name: "secretToAddress" }, { name: "secretToAddress" },
{ name: "lll" }, { name: "lll" },
{ name: "sha3" }, { name: "sha3" },
{ name: "toAscii" }, { name: "toAscii" },
{ name: "fromAscii" }, { name: "fromAscii" },
{ name: "toDecimal" }, { name: "toDecimal" },
{ name: "toFixed" }, { name: "toFixed" },
{ name: "fromFixed" }, { name: "fromFixed" },
{ name: "offset" } { name: "offset" }
]; ];
methods.forEach(function (method) { methods.forEach(function (method) {
root[method.name] = function () { root[method.name] = function () {
return reqSync(method.name, defaults(method.default, getParams(spec, method.name, arguments))); return reqSync(method.name, defaults(method.default, getParams(spec, method.name, arguments)));
}; };
if (method.async) { if (method.async) {
root[method.async] = function () { root[method.async] = function () {
return reqAsync(method.name, defaults(method.default, getParams(spec, method.name, arguments)), arguments[arguments.length - 1]); return reqAsync(method.name, defaults(method.default, getParams(spec, method.name, arguments)), arguments[arguments.length - 1]);
}; };
}; };
}); });
}; };
var setupWatch = function (root) { var setupWatch = function (root) {
root.watch = function (val) { root.watch = function (val) {
if (typeof val !== 'string') { if (typeof val !== 'string') {
val = JSON.stringify(val); val = JSON.stringify(val);
} }
var id;
reqAsync('watch', {params: val}, function (result) {
id = result;
}); // async send watch
var callbacks = [];
var exist = true;
var w = {
changed: function (f) {
callbacks.push(f);
},
uninstall: function (f) {
reqAsync('killWatch', {id: id});
exist = false;
},
messages: function () {
// TODO!
},
getMessages: function (f) {
// TODO!
}
};
var check = function () { var id;
if (!exist) { reqAsync('watch', {params: val}, function (result) {
return; id = result;
} }); // async send watch
if (callbacks.length) { var callbacks = [];
reqAsync('check', {id: id}, function (res) { var exist = true;
if (!res) { var w = {
return; changed: function (f) {
} callbacks.push(f);
callbacks.forEach(function (f) { },
f(); uninstall: function (f) {
}); reqAsync('killWatch', {id: id});
}); exist = false;
} },
window.setTimeout(check, 12000); messages: function () {
}; // TODO!
},
getMessages: function (f) {
// TODO!
}
};
check(); var check = function () {
return w; if (!exist) {
} return;
}; }
if (callbacks.length) {
reqAsync('check', {id: id}, function (res) {
if (!res) {
return;
}
callbacks.forEach(function (f) {
f();
});
});
}
window.setTimeout(check, 12000);
};
setupProperties(ret, window.spec); check();
setupMethods(ret, window.spec); return w;
setupWatch(ret); }
};
return ret; setupProperties(ret, window.spec);
}()); setupMethods(ret, window.spec);
setupWatch(ret);
return ret;
}());
} }

Loading…
Cancel
Save