|
@ -3,280 +3,185 @@ |
|
|
return o instanceof Promise |
|
|
return o instanceof Promise |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var web3 = { |
|
|
function flattenPromise (obj) { |
|
|
_callbacks: {}, |
|
|
if (obj instanceof Promise) { |
|
|
_events: {}, |
|
|
return Promise.resolve(obj); |
|
|
providers: {}, |
|
|
|
|
|
toHex: function(str) { |
|
|
|
|
|
var hex = ""; |
|
|
|
|
|
for(var i = 0; i < str.length; i++) { |
|
|
|
|
|
var n = str.charCodeAt(i).toString(16); |
|
|
|
|
|
hex += n.length < 2 ? '0' + n : n; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return hex; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
toAscii: function(hex) { |
|
|
|
|
|
// Find termination
|
|
|
|
|
|
var str = ""; |
|
|
|
|
|
var i = 0, l = hex.length; |
|
|
|
|
|
for(; i < l; i+=2) { |
|
|
|
|
|
var code = hex.charCodeAt(i) |
|
|
|
|
|
if(code == 0) { |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); |
|
|
if (obj instanceof Array) { |
|
|
} |
|
|
return new Promise(function (resolve) { |
|
|
|
|
|
var promises = obj.map(function (o) { |
|
|
return str; |
|
|
return flattenPromise(o); |
|
|
}, |
|
|
}); |
|
|
|
|
|
|
|
|
fromAscii: function(str, pad) { |
|
|
return Promise.all(promises).then(function (res) { |
|
|
if(pad === undefined) { |
|
|
for (var i = 0; i < obj.length; i++) { |
|
|
pad = 32 |
|
|
obj[i] = res[i]; |
|
|
} |
|
|
} |
|
|
|
|
|
resolve(obj); |
|
|
var hex = this.toHex(str); |
|
|
|
|
|
|
|
|
|
|
|
while(hex.length < pad*2) |
|
|
|
|
|
hex += "00"; |
|
|
|
|
|
|
|
|
|
|
|
return hex |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
eth: { |
|
|
|
|
|
prototype: Object(), |
|
|
|
|
|
|
|
|
|
|
|
block: function(numberOrHash) { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
var call = typeof numberOrHash === "string" ? 'blockByHash' : 'blockByNumber'; |
|
|
|
|
|
web3.provider.send({call: call, args: [numberOrHash]}, function(block) { |
|
|
|
|
|
if(block) |
|
|
|
|
|
resolve(block); |
|
|
|
|
|
else |
|
|
|
|
|
reject("not found"); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
} |
|
|
|
|
|
|
|
|
transaction: function(numberOrHash, nth) { |
|
|
if (obj instanceof Object) { |
|
|
return new Promise(function(resolve, reject) { |
|
|
return new Promise(function (resolve) { |
|
|
var call = typeof numberOrHash === "string" ? 'transactionByHash' : 'transactonByNumber'; |
|
|
var keys = Object.keys(obj); |
|
|
web3.provider.send({call: call, args: [numberOrHash, nth]}, function(block) { |
|
|
var promises = keys.map(function (key) { |
|
|
if(block) |
|
|
return flattenPromise(obj[key]); |
|
|
resolve(block); |
|
|
|
|
|
else |
|
|
|
|
|
reject("not found"); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
uncle: function(numberOrHash, nth) { |
|
|
return Promise.all(promises).then(function (res) { |
|
|
return new Promise(function(resolve, reject) { |
|
|
for (var i = 0; i < keys.length; i++) { |
|
|
var call = typeof numberOrHash === "string" ? 'uncleByHash' : 'uncleByNumber'; |
|
|
obj[keys[i]] = res[i]; |
|
|
web3.provider.send({call: call, args: [numberOrHash, nth]}, function(block) { |
|
|
} |
|
|
if(block) |
|
|
resolve(obj); |
|
|
resolve(block); |
|
|
|
|
|
else |
|
|
|
|
|
reject("not found"); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
transact: function(params) { |
|
|
|
|
|
if(params === undefined) { |
|
|
|
|
|
params = {}; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(params.endowment !== undefined) |
|
|
return Promise.resolve(obj); |
|
|
params.value = params.endowment; |
|
|
}; |
|
|
if(params.code !== undefined) |
|
|
|
|
|
params.data = params.code; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ethMethods = function () { |
|
|
|
|
|
var blockCall = function (args) { |
|
|
|
|
|
return typeof args[0] === "string" ? "blockByHash" : "blockByNumber"; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
var promises = [] |
|
|
var transactionCall = function (args) { |
|
|
if(isPromise(params.to)) { |
|
|
return typeof args[0] === "string" ? 'transactionByHash' : 'transactonByNumber'; |
|
|
promises.push(params.to.then(function(_to) { params.to = _to; })); |
|
|
}; |
|
|
} |
|
|
|
|
|
if(isPromise(params.from)) { |
|
|
|
|
|
promises.push(params.from.then(function(_from) { params.from = _from; })); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(typeof params.data !== "object" || isPromise(params.data)) { |
|
|
var uncleCall = function (args) { |
|
|
params.data = [params.data] |
|
|
return typeof args[0] === "string" ? 'uncleByHash' : 'uncleByNumber'; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
var data = params.data; |
|
|
var methods = [ |
|
|
for(var i = 0; i < params.data.length; i++) { |
|
|
{ name: 'balanceAt', call: 'balanceAt' }, |
|
|
if(isPromise(params.data[i])) { |
|
|
{ name: 'stateAt', call: 'stateAt' }, |
|
|
var promise = params.data[i]; |
|
|
{ name: 'countAt', call: 'countAt'}, |
|
|
var _i = i; |
|
|
{ name: 'codeAt', call: 'codeAt' }, |
|
|
promises.push(promise.then(function(_arg) { params.data[_i] = _arg; })); |
|
|
{ name: 'transact', call: 'transact' }, |
|
|
} |
|
|
{ name: 'call', call: 'call' }, |
|
|
} |
|
|
{ name: 'block', call: blockCall }, |
|
|
|
|
|
{ name: 'transaction', call: transactionCall }, |
|
|
|
|
|
{ name: 'uncle', call: uncleCall }, |
|
|
|
|
|
{ name: 'compile', call: 'compile' } |
|
|
|
|
|
]; |
|
|
|
|
|
return methods; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// Make sure everything is string
|
|
|
var ethProperties = function () { |
|
|
var fields = ["value", "gas", "gasPrice"]; |
|
|
return [ |
|
|
for(var i = 0; i < fields.length; i++) { |
|
|
{ name: 'coinbase', getter: 'coinbase', setter: 'setCoinbase' }, |
|
|
if(params[fields[i]] === undefined) { |
|
|
{ name: 'listening', getter: 'listening', setter: 'setListening' }, |
|
|
params[fields[i]] = ""; |
|
|
{ name: 'mining', getter: 'mining', setter: 'setMining' }, |
|
|
} |
|
|
{ name: 'gasPrice', getter: 'gasPrice' }, |
|
|
params[fields[i]] = params[fields[i]].toString(); |
|
|
{ name: 'accounts', getter: 'accounts' }, |
|
|
} |
|
|
{ name: 'peerCount', getter: 'peerCount' }, |
|
|
|
|
|
{ name: 'defaultBlock', getter: 'defaultBlock', setter: 'setDefaultBlock' }, |
|
|
|
|
|
{ name: 'number', getter: 'number'} |
|
|
|
|
|
]; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
// Load promises then call the last "transact".
|
|
|
var setupMethods = function (obj, methods) { |
|
|
return Promise.all(promises).then(function() { |
|
|
methods.forEach(function (method) { |
|
|
return new Promise(function(resolve, reject) { |
|
|
obj[method.name] = function () { |
|
|
params.data = params.data.join(""); |
|
|
return flattenPromise(Array.prototype.slice.call(arguments)).then(function (args) { |
|
|
web3.provider.send({call: "transact", args: [params]}, function(data) { |
|
|
var call = typeof method.call === "function" ? method.call(args) : method.call; |
|
|
if(data[1]) |
|
|
return {call: call, args: args}; |
|
|
reject(data[0]); |
|
|
}).then(function (request) { |
|
|
else |
|
|
return new Promise(function (resolve, reject) { |
|
|
resolve(data[0]); |
|
|
web3.provider.send(request, function (result) { |
|
|
|
|
|
if (result) { |
|
|
|
|
|
resolve(result); |
|
|
|
|
|
} else { |
|
|
|
|
|
reject(result); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}) |
|
|
}).catch(function( err) { |
|
|
}, |
|
|
console.error(err); |
|
|
|
|
|
|
|
|
compile: function(code) { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "compile", args: [code]}, function(data) { |
|
|
|
|
|
if(data[1]) |
|
|
|
|
|
reject(data[0]); |
|
|
|
|
|
else |
|
|
|
|
|
resolve(data[0]); |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
}; |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}; |
|
|
|
|
|
|
|
|
balanceAt: function(address) { |
|
|
|
|
|
var promises = []; |
|
|
|
|
|
|
|
|
|
|
|
if(isPromise(address)) { |
|
|
|
|
|
promises.push(address.then(function(_address) { address = _address; })); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return Promise.all(promises).then(function() { |
|
|
var setupProperties = function (obj, properties) { |
|
|
|
|
|
properties.forEach(function (property) { |
|
|
|
|
|
var proto = {}; |
|
|
|
|
|
proto.get = function () { |
|
|
return new Promise(function(resolve, reject) { |
|
|
return new Promise(function(resolve, reject) { |
|
|
web3.provider.send({call: "balanceAt", args: [address]}, function(balance) { |
|
|
web3.provider.send({call: property.getter}, function(result) { |
|
|
resolve(balance); |
|
|
resolve(result); |
|
|
}); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}; |
|
|
|
|
|
if (property.setter) { |
|
|
countAt: function(address) { |
|
|
proto.set = function (val) { |
|
|
var promises = []; |
|
|
return flattenPromise([val]).then(function (args) { |
|
|
|
|
|
return new Promise(function (resolve) { |
|
|
if(isPromise(address)) { |
|
|
web3.provider.send({call: property.setter, args: args}, function (result) { |
|
|
promises.push(address.then(function(_address) { address = _address; })); |
|
|
if (result) { |
|
|
|
|
|
resolve(result); |
|
|
|
|
|
} else { |
|
|
|
|
|
reject(result); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return Promise.all(promises).then(function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "countAt", args: [address]}, function(count) { |
|
|
|
|
|
resolve(count); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
}).catch(function (err) { |
|
|
|
|
|
console.error(err); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
codeAt: function(address) { |
|
|
|
|
|
var promises = []; |
|
|
|
|
|
|
|
|
|
|
|
if(isPromise(address)) { |
|
|
|
|
|
promises.push(address.then(function(_address) { address = _address; })); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return Promise.all(promises).then(function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "codeAt", args: [address]}, function(code) { |
|
|
|
|
|
resolve(code); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
storageAt: function(address, storageAddress) { |
|
|
|
|
|
var promises = []; |
|
|
|
|
|
|
|
|
|
|
|
if(isPromise(address)) { |
|
|
|
|
|
promises.push(address.then(function(_address) { address = _address; })); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
Object.defineProperty(obj, property.name, proto); |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
if(isPromise(storageAddress)) { |
|
|
var web3 = { |
|
|
promises.push(storageAddress.then(function(_sa) { storageAddress = _sa; })); |
|
|
_callbacks: {}, |
|
|
|
|
|
_events: {}, |
|
|
|
|
|
providers: {}, |
|
|
|
|
|
toHex: function(str) { |
|
|
|
|
|
var hex = ""; |
|
|
|
|
|
for(var i = 0; i < str.length; i++) { |
|
|
|
|
|
var n = str.charCodeAt(i).toString(16); |
|
|
|
|
|
hex += n.length < 2 ? '0' + n : n; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return Promise.all(promises).then(function() { |
|
|
return hex; |
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "stateAt", args: [address, storageAddress]}, function(entry) { |
|
|
|
|
|
resolve(entry); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
stateAt: function(address, storageAddress) { |
|
|
|
|
|
return this.storageAt(address, storageAddress); |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
call: function(params) { |
|
|
toAscii: function(hex) { |
|
|
if(params === undefined) { |
|
|
// Find termination
|
|
|
params = {}; |
|
|
var str = ""; |
|
|
|
|
|
var i = 0, l = hex.length; |
|
|
|
|
|
for(; i < l; i+=2) { |
|
|
|
|
|
var code = hex.charCodeAt(i) |
|
|
|
|
|
if(code == 0) { |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(params.endowment !== undefined) |
|
|
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); |
|
|
params.value = params.endowment; |
|
|
} |
|
|
if(params.code !== undefined) |
|
|
|
|
|
params.data = params.code; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return str; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
var promises = [] |
|
|
fromAscii: function(str, pad) { |
|
|
if(isPromise(params.to)) { |
|
|
if(pad === undefined) { |
|
|
promises.push(params.to.then(function(_to) { params.to = _to; })); |
|
|
pad = 32 |
|
|
} |
|
|
|
|
|
if(isPromise(params.from)) { |
|
|
|
|
|
promises.push(params.from.then(function(_from) { params.from = _from; })); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(isPromise(params.data)) { |
|
|
var hex = this.toHex(str); |
|
|
promises.push(params.data.then(function(_code) { params.data = _code; })); |
|
|
|
|
|
} else { |
|
|
|
|
|
if(typeof params.data === "object") { |
|
|
|
|
|
data = ""; |
|
|
|
|
|
for(var i = 0; i < params.data.length; i++) { |
|
|
|
|
|
data += params.data[i] |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
data = params.data; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Make sure everything is string
|
|
|
while(hex.length < pad*2) |
|
|
var fields = ["value", "gas", "gasPrice"]; |
|
|
hex += "00"; |
|
|
for(var i = 0; i < fields.length; i++) { |
|
|
|
|
|
if(params[fields[i]] === undefined) { |
|
|
|
|
|
params[fields[i]] = ""; |
|
|
|
|
|
} |
|
|
|
|
|
params[fields[i]] = params[fields[i]].toString(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Load promises then call the last "transact".
|
|
|
return hex |
|
|
return Promise.all(promises).then(function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "call", args: params}, function(data) { |
|
|
|
|
|
if(data[1]) |
|
|
|
|
|
reject(data[0]); |
|
|
|
|
|
else |
|
|
|
|
|
resolve(data[0]); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}) |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
eth: { |
|
|
|
|
|
prototype: Object(), |
|
|
watch: function(params) { |
|
|
watch: function(params) { |
|
|
return new Filter(params); |
|
|
return new Filter(params); |
|
|
}, |
|
|
}, |
|
@ -322,53 +227,8 @@ |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
var eth = web3.eth; |
|
|
var eth = web3.eth; |
|
|
// Eth object properties
|
|
|
setupMethods(eth, ethMethods()); |
|
|
Object.defineProperty(eth, "gasPrice", { |
|
|
setupProperties(eth, ethProperties()); |
|
|
get: function() { |
|
|
|
|
|
return "10000000000000" |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(eth, "coinbase", { |
|
|
|
|
|
get: function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "coinbase"}, function(coinbase) { |
|
|
|
|
|
resolve(coinbase); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(eth, "listening", { |
|
|
|
|
|
get: function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "listening"}, function(listening) { |
|
|
|
|
|
resolve(listening); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(eth, "mining", { |
|
|
|
|
|
get: function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "mining"}, function(mining) { |
|
|
|
|
|
resolve(mining); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(eth, "peerCount", { |
|
|
|
|
|
get: function() { |
|
|
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
|
|
web3.provider.send({call: "peerCount"}, function(peerCount) { |
|
|
|
|
|
resolve(peerCount); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
var ProviderManager = function() { |
|
|
var ProviderManager = function() { |
|
|
this.queued = []; |
|
|
this.queued = []; |
|
@ -549,4 +409,5 @@ |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
window.web3 = web3; |
|
|
window.web3 = web3; |
|
|
|
|
|
|
|
|
})(this); |
|
|
})(this); |
|
|