good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
28d42508
Commit
28d42508
authored
Oct 28, 2014
by
Marek Kotewicz
Browse files
Options
Downloads
Patches
Plain Diff
cleaner promises
parent
66e439b6
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.js
+137
-276
137 additions, 276 deletions
main.js
with
137 additions
and
276 deletions
main.js
+
137
−
276
View file @
28d42508
...
@@ -3,280 +3,185 @@
...
@@ -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
)
{
obj
[
method
.
name
]
=
function
()
{
return
flattenPromise
(
Array
.
prototype
.
slice
.
call
(
arguments
)).
then
(
function
(
args
)
{
var
call
=
typeof
method
.
call
===
"
function
"
?
method
.
call
(
args
)
:
method
.
call
;
return
{
call
:
call
,
args
:
args
};
}).
then
(
function
(
request
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
params
.
data
=
params
.
data
.
join
(
""
);
web3
.
provider
.
send
(
request
,
function
(
result
)
{
web3
.
provider
.
send
({
call
:
"
transact
"
,
args
:
[
params
]},
function
(
data
)
{
if
(
result
)
{
if
(
data
[
1
])
resolve
(
result
);
reject
(
data
[
0
]);
}
else
{
else
reject
(
result
);
resolve
(
data
[
0
]);
}
});
});
});
});
})
}).
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
)
{
proto
.
set
=
function
(
val
)
{
countAt
:
function
(
address
)
{
return
flattenPromise
([
val
]).
then
(
function
(
args
)
{
var
promises
=
[];
return
new
Promise
(
function
(
resolve
)
{
web3
.
provider
.
send
({
call
:
property
.
setter
,
args
:
args
},
function
(
result
)
{
if
(
isPromise
(
address
))
{
if
(
result
)
{
promises
.
push
(
address
.
then
(
function
(
_address
)
{
address
=
_address
;
}));
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 @@
...
@@ -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 @@
...
@@ -549,4 +409,5 @@
*/
*/
window
.
web3
=
web3
;
window
.
web3
=
web3
;
})(
this
);
})(
this
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment