-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
178 lines (159 loc) · 6.28 KB
/
index.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
const interfacesPojo = require('./lib/interfacesPojo')
const Batch = require('./lib/Batch')
const Protocol = require('./lib/Protocol')
const _execute = require('./lib/execute')
const BlockPoller = require('./lib/BlockPoller')
const Emitter = require('events')
const _ = require('lodash')
const TransactionMonitor = require('./lib/TransactionMonitor')
const TransactionRequest = require('./lib/TransactionRequest')
const SolDeployTransactionRequest = require('./lib/SolDeployTransactionRequest')
const SolWrapper = require('./lib/SolWrapper')
const defunction = require('defunction')
const v = require('./lib/validates')
const Promise = require('bluebird')
const amorphHex = require('amorph-hex')
const amorphNumber = require('amorph-number')
const BlocksWaitedError = require('./lib/errors/BlocksWaited')
const Ultralightbeam = module.exports = defunction([v.object, v.pojo], v.undefined, function Ultralightbeam(provider, options) {
this.options = {
blockPollerInterval: 1000,
maxBlocksToWait: 3,
executionDebounce: 100,
gasMultiplier: 1.2,
approve: defunction([v.transactionRequestish], v.promise, function approve(transactionRequest) {
return Promise.resolve()
})
}
_.merge(this.options, options)
this.id = 0
this.provider = provider
this.batch = new Batch(this)
this.batches = []
this.web3 = new Protocol(this, interfacesPojo.web3)
this.net = new Protocol(this, interfacesPojo.net)
this.eth = new Protocol(this, interfacesPojo.eth)
this.miner = new Protocol(this, interfacesPojo.miner)
this.blockPoller = new BlockPoller(this)
this.debouncedExecute = _.debounce(_execute, this.options.executionDebounce)
this.blockPoller.start(this.options.blockPollerInterval)
this.emitter = new Emitter
this.addressCounters = {}
})
Ultralightbeam.prototype.getLatestBlock = defunction([], v.eventualBlock, function getLatestBlock() {
if (this.blockPoller.block) {
return this.resolve(this.blockPoller.block)
}
return this.blockPoller.blockPromise
})
Ultralightbeam.prototype.execute = defunction([], v.anything, function execute() {
return this.debouncedExecute(this)
})
Ultralightbeam.prototype.defer = defunction([], v.promiseStub, function defer() {
const deferred = {}
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve
deferred.reject = reject
})
return deferred
})
Ultralightbeam.prototype.reject = defunction([v.optionalAnything], v.promise, function reject(reason) {
return Promise.reject(reason)
})
Ultralightbeam.prototype.resolve = defunction([v.optionalAnything], v.promise, function resolve(reason) {
return Promise.resolve(reason)
})
Ultralightbeam.prototype.send = defunction([v.transactionRequestish], v.transactionMonitor, function send(transactionRequest) {
return new TransactionMonitor(this, transactionRequest)
})
Ultralightbeam.prototype.solDeploy = defunction(
[v.amorph, v.array, v.array, v.pojo],
v.eventualSolWrapper,
function solDeploy(bytecode, abi, inputs, options) {
return new SolDeployTransactionRequest(this, bytecode, abi, inputs, options).send().getContractAddress().then((contractAddress) => {
return new SolWrapper(this, abi, contractAddress)
})
}
)
Ultralightbeam.getTransactionRequest = defunction([v.pojo], v.transactionRequest, function getTransactionRequest(options) {
return new TransactionRequest(this, options)
})
Ultralightbeam.prototype.getSolWrapper = defunction([v.amorph, v.array, v.array, v.pojo], v.solWrapper, function getSolWrapper(bytecode, abi, inputs, options) {
return new SolWrapper(this, bytecode, abi, inputs, options)
})
Ultralightbeam.prototype.getNonce = defunction([v.address], v.eventualAmorph, function getNonce(address) {
const addressHex = address.to(amorphHex.unprefixed)
if (!this.addressCounters[addressHex]) {
this.addressCounters[addressHex] = {
startNonce: null,
increment: 0
}
}
const addressCounter = this.addressCounters[addressHex]
const increment = addressCounter.increment
addressCounter.increment ++
if (addressCounter.startNonce) {
return this.resolve(addressCounter.startNonce.as(amorphNumber.unsigned, (number) => {
return number + increment
}))
}
return this.eth.getTransactionCount(address).then((transactionCount) => {
if (!addressCounter.startNonce) {
addressCounter.startNonce = transactionCount
}
return transactionCount.as(amorphNumber.unsigned, (number) => {
return number + increment
})
})
})
Ultralightbeam.prototype.waitForTransaction = defunction([v.amorph], v.eventualTransaction, function waitForTransaction(transactionHash) {
const deferred = this.defer()
let blocksWaited = 0
const onBlock = (block) => {
if (block.transactions) {
block.transactions.forEach((transaction) => {
if (!transaction.hash.equals(transactionHash)) {
return
}
deferred.resolve(transaction)
this.blockPoller.emitter.removeListener('block', onBlock)
})
}
blocksWaited ++
if (blocksWaited > this.options.maxBlocksToWait) {
const blocksWaitedError = new BlocksWaitedError(`No result after waiting for ${blocksWaited} blocks`)
deferred.reject(blocksWaitedError)
this.blockPoller.emitter.removeListener('block', onBlock)
}
}
this.blockPoller.emitter.on('block', onBlock)
this.eth.getTransactionByHash(transactionHash).then((transaction) => {
if (!transaction) {
// transaction has not registered
return
}
if (!transaction.blockNumber) {
// transaction has not been mined
return
}
if (
this.blockPoller.block &&
transaction.blockNumber &&
transaction.blockNumber.to(amorphNumber.unsigned) > this.blockPoller.block.number.to(amorphNumber.unsigned)
) {
return
}
deferred.resolve(transaction)
this.blockPoller.emitter.removeListener('block', onBlock)
})
return deferred.promise
})
Ultralightbeam.prototype.waitForConfirmation = defunction([v.amorph], v.anything, function waitForConfirmation(transactionHash) {
return this.waitForTransaction(transactionHash).then((transaction) => {
return this.eth.getTransactionReceipt(transaction.hash)
}).then((transactionReceipt) => {
if (transactionReceipt.isFailed || transactionReceipt.status === 0) {
throw new FailedTransactionError('Transaction Failed')
}
})
})