-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathethereum-blockchain-service.ts
489 lines (448 loc) · 18.9 KB
/
ethereum-blockchain-service.ts
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import type { CID } from 'multiformats/cid'
import { base16 } from 'multiformats/bases/base16'
import {
BigNumberish,
Contract,
ethers,
TransactionResponse,
TransactionRequest,
isError,
} from 'ethers'
import { Config } from 'node-config-ts'
import * as uint8arrays from 'uint8arrays'
import { logger, logEvent, logMetric } from '../../../logger/index.js'
import { Transaction } from '../../../models/transaction.js'
import { BlockchainService } from '../blockchain-service.js'
import { Utils } from '../../../utils.js'
const BASE_CHAIN_ID = 'eip155'
const TX_SUCCESS = 1
const NUM_BLOCKS_TO_WAIT = 4
export const MAX_RETRIES = 3
const ABI = ['function anchorDagCbor(bytes32)']
class WrongChainIdError extends Error {
constructor(expected: bigint, actual: bigint) {
super(
`Chain ID of connected blockchain changed from ${caipChainId(expected)} to ${caipChainId(
actual
)}`
)
}
}
/**
* Adds padding to our fees
*/
function addPadding(number: bigint): bigint {
return number + number / BigInt(10)
}
/**
* Do up to +max+ attempts of an +operation+. Expect the +operation+ to return a defined value.
* If no defined value is returned, iterate at most +max+ times.
*
* @param max - Maximum number of attempts.
* @param operation - Operation to run.
*/
async function attempt<T>(
max: number,
operation: (attempt: number) => Promise<T | undefined | void>
): Promise<T> {
let attempt = 0
while (attempt < max) {
const result = await operation(attempt)
if (result) {
return result
}
attempt++
logger.warn(`Failed to send transaction; ${max - attempt} retries remain`)
await Utils.delay(5000)
}
// All attempts spent
throw new Error('Failed to send transaction')
}
/**
* Throw if a transaction requires more funds than available.
*
* @param txData - Transaction to write.
* @param walletBalance - Available funds.
*/
function handleInsufficientFundsError(txData: TransactionRequest, walletBalance: bigint): void {
const gasLimit = Utils.coalesce(() => txData.gasLimit, 'No gas limit')
const maxFeePerGas = Utils.coalesce(() => txData.maxFeePerGas, 'No max fee per gas')
const txCost = BigInt(gasLimit) * BigInt(maxFeePerGas)
if (txCost > walletBalance) {
logEvent.ethereum({
type: 'insufficientFunds',
txCost: txCost,
balance: ethers.formatUnits(walletBalance, 'gwei'),
})
const errMsg = `Transaction cost is greater than our current balance. [txCost: ${txCost.toString(
16
)}, balance: ${walletBalance.toString(16)}]`
logger.err(errMsg)
throw new Error(errMsg)
}
}
/**
* Represent chainId in CAIP format.
* @param chainId - Numeric chain id.
*/
function caipChainId(chainId: bigint | number) {
return `${BASE_CHAIN_ID}:${chainId}`
}
/**
* Throw if +actual+ and +expected+ chain ids are not equal.
*
* @param actual - Chain id we received.
* @param expected - Chain id we expect.
*/
function assertSameChainId(actual: bigint, expected: bigint) {
if (actual != expected) {
// TODO: This should be process-fatal
throw new WrongChainIdError(expected, actual)
}
}
/**
* Just log a timeout error.
*/
function handleTimeoutError(transactionTimeoutSecs: number): void {
logEvent.ethereum({
type: 'transactionTimeout',
transactionTimeoutSecs: transactionTimeoutSecs,
})
logger.err(`Transaction timed out after ${transactionTimeoutSecs} seconds without being mined`)
}
function make(config: Config): EthereumBlockchainService {
const ethereum = config.blockchain.connectors.ethereum
const { host, port, url } = ethereum.rpc
let provider
if (url) {
logger.imp(`Connecting ethereum provider to url: ${url}`)
provider = new ethers.JsonRpcProvider(url)
} else if (host && port) {
logger.imp(`Connecting ethereum provider to host: ${host} and port ${port}`)
provider = new ethers.JsonRpcProvider(`${host}:${port}`)
} else {
logger.imp(`Connecting ethereum to default provider for network ${ethereum.network}`)
provider = ethers.getDefaultProvider(ethereum.network)
}
const wallet = new ethers.Wallet(ethereum.account.privateKey, provider)
return new EthereumBlockchainService(config, wallet)
}
make.inject = ['config'] as const
/**
* Ethereum blockchain service
*/
export class EthereumBlockchainService implements BlockchainService {
private _chainId: bigint | undefined
private readonly network: string
private readonly transactionTimeoutSecs: number
private readonly contract: Contract
private readonly overrideGasConfig: boolean
private readonly gasLimit: bigint
private readonly useSmartContractAnchors: boolean
private readonly contractAddress: string
constructor(config: Config, private readonly wallet: ethers.Wallet) {
this.useSmartContractAnchors = config.useSmartContractAnchors
const ethereumConfig = config.blockchain.connectors.ethereum
this.network = ethereumConfig.network
this.transactionTimeoutSecs = ethereumConfig.transactionTimeoutSecs
this.contract = new ethers.Contract(ethereumConfig.contractAddress, ABI, this.wallet)
this.overrideGasConfig = ethereumConfig.overrideGasConfig
this.gasLimit = BigInt(ethereumConfig.gasLimit)
this.contractAddress = ethereumConfig.contractAddress
}
static make = make
/**
* Connects to blockchain
*/
async connect(): Promise<void> {
logger.imp(`Connecting to ${this.network} blockchain...`)
await this._loadChainId()
logger.imp(`Connected to ${this.network} blockchain with chain ID ${this.chainId}`)
}
/**
* Returns a string representing the CAIP-2 ID of the configured blockchain by querying the
* connected blockchain to ask for it.
*/
private async _loadChainId(): Promise<void> {
const provider = Utils.coalesce(() => this.wallet.provider, 'No wallet provider')
const network = await provider.getNetwork()
this._chainId = network.chainId
}
/**
* Sets the gas price for the transaction request.
* For pre-1559 transaction we increase vanilla gasPrice by 10% each time. For a 1559 transaction, we increase maxPriorityFeePerGas,
* again by 10% each time.
*
* For 1559 there are two parameters that can be set on a transaction: maxPriorityFeePerGas and maxFeePerGas.
* maxFeePerGas should equal to `maxPriorityFeePerGas` (our tip to a miner) plus `baseFee` (ETH burned according to current network conditions).
* To estimate the current parameters, we use `getFeeData` function, which returns two of our parameters.
* Here we _can_ calculate `baseFee`, but also we can avoid doing that. Remember, we increase just `maxPriorityFeePerGas`.
* Here we calculate a difference between previously sent `maxPriorityFeePerGas` and the increased one. It is our voluntary
* increase in gas price we agree to pay to mine our transaction.
* We just add the difference to a currently estimated `maxFeePerGas` so that we conform to the equality
* `maxFeePerGas = baseFee + maxPriorityFeePerGas`.
*
* NB. EIP1559 now uses two components of gas cost: `baseFee` and `maxPriorityFeePerGas`. `maxPriorityFeePerGas` is a
* tip to a miner to include a transaction into a block. `baseFee` is a slowly changing amount of gas or ether that is
* going to be burned. `baseFee` is set by _network_. Since we do not know what `baseFee` will be, EIP1559 introduces
* `maxFeePerGas` which is an absolute maximum you are willing to pay for a transaction. `maxFeePerGas` must be
* `>= maxPriorityFeePerGas + baseFee`. The inequality here is to accommodate for changes in `baseFee`. If
* `maxFeePerGas` appears to be less than the sum, the transaction is underpriced. If it is greater than the sum
* (`maxFeePerGas = maxPriorityFeePerGas + baseFee + δ`):
* - if `baseFee` changes up to `δ`, the transaction can be mined still; `δ` is like a safety buffer;
* - transaction fee that is deducted from your wallet still equals `maxPriorityFeePerGas + baseFee`, no matter what `maxFeePerGas` you have set.
*
* To price a 1559 transaction, we use an estimate from `provider.getFeeData`. It returns `maxFeePerGas` and `maxPriorityFeePerGas`. It is worth noting here that `maxFeePerGas` returned from ethers uses a [widely recommended](https://www.blocknative.com/blog/eip-1559-fees) formula: `(baseFee of the latest block)*2 + (constant 2.5Gwei)`. If we only increase `maxPriorityFeePerGas` per attempt, we effectively deduct from our baseFee safety buffer `δ` which reduces transaction's chances. Our intent though is to increase a transaction's "mineability". So, when increasing `maxPriorityFeePerGas` we also increase `maxFeePerGas` by the same amount. Now the safety buffer reflects current network conditions, and we actually increase our transaction's "mineability".
*
* @param txData - transaction request data
* @param attempt - what number attempt this is at submitting the transaction. We increase
* the gas price we set by a 10% multiple with each subsequent attempt
* @private
*/
async setGasPrice(txData: TransactionRequest): Promise<void> {
if (this.overrideGasConfig) {
txData.gasLimit = this.gasLimit
logger.debug('Overriding Gas limit: ' + txData.gasLimit.toString())
return
}
const walletProvider = Utils.coalesce(() => this.wallet.provider, 'No wallet provider')
const feeData = await Utils.coalesceAsync(() => walletProvider.getFeeData(), 'No fee data')
// Add extra to gas price for each subsequent attempt
const maxFeePerGas = feeData.maxFeePerGas
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas
// Is EIP-1559
if (maxPriorityFeePerGas && maxFeePerGas) {
const baseFee = maxFeePerGas - maxPriorityFeePerGas
const prevPriorityFee = BigInt(txData.maxPriorityFeePerGas || feeData.maxPriorityFeePerGas)
const nextPriorityFee = EthereumBlockchainService.increaseGasPricePerAttempt(
maxPriorityFeePerGas,
prevPriorityFee
)
txData.maxPriorityFeePerGas = nextPriorityFee
// must be greater than baseFee + maxPriorityFeePerGas
txData.maxFeePerGas = addPadding(baseFee + nextPriorityFee)
logger.debug(
`Estimated maxPriorityFeePerGas: ${txData.maxPriorityFeePerGas.toString()} wei; maxFeePerGas: ${txData.maxFeePerGas.toString()} wei`
)
} else {
const feeDataGasPrice = Utils.coalesce(
() => feeData.gasPrice,
`Unavailable gas price for pre-EIP-1559 transaction`
)
// When attempt 0, use currently estimated gasPrice; otherwise use previous transaction gasPrice
const prevGasPrice = BigInt(txData.gasPrice || feeDataGasPrice)
txData.gasPrice = EthereumBlockchainService.increaseGasPricePerAttempt(
feeDataGasPrice,
prevGasPrice
)
logger.debug(`Estimated gasPrice: ${txData.gasPrice.toString()} wei`)
}
txData.gasLimit = await walletProvider.estimateGas(txData)
logger.debug('Estimated Gas limit: ' + txData.gasLimit.toString())
}
/**
* Take current gas price (or maxPriorityFeePerGas for 1559 transaction), and attempt number,
* and return new gas price (or maxPriorityFeePerGas for 1559 transaction) with 10% increase per attempt.
* If this isn't the first attempt, also ensures that the new gas is at least 10% greater than the
* previous attempt's gas, even if the gas price on chain has gone down since then. This is because
* retries of the same transaction (using the same nonce) need to have a gas price at least 10% higher
* than any previous attempts or the transaction will fail.
*
* @param estimate - Currently estimated gas price
* @param attempt - Index of a current attempt, starts with 0.
* @param previousGas - Either gasPrice for pre-1559 tx or maxPriorityFeePerGas for 1559 tx.
*/
static increaseGasPricePerAttempt(
estimate: BigNumberish,
previousGas: BigNumberish | undefined
): bigint {
if (previousGas == undefined) {
return addPadding(BigInt(estimate))
} else {
return addPadding(BigInt(estimate > previousGas ? estimate : previousGas))
}
}
/**
* Returns the cached 'chainId' representing the CAIP-2 ID of the configured blockchain.
* Invalid to call before calling connect()
*/
get chainId(): string {
if (!this._chainId) throw new Error(`No chainId available`)
return caipChainId(this._chainId)
}
async _buildTransactionRequest(rootCid: CID): Promise<TransactionRequest> {
logger.debug('Preparing ethereum transaction')
const provider = Utils.coalesce(() => this.wallet.provider, 'No wallet provider')
const baseNonce = await provider.getTransactionCount(this.wallet.address)
if (!this.useSmartContractAnchors) {
const rootStrHex = rootCid.toString(base16)
const hexEncoded = '0x' + (rootStrHex.length % 2 == 0 ? rootStrHex : '0' + rootStrHex)
logger.debug(`Hex encoded root CID ${hexEncoded}`)
return {
to: this.wallet.address,
data: hexEncoded,
nonce: baseNonce,
from: this.wallet.address,
}
}
const hexEncoded = '0x' + uint8arrays.toString(rootCid.bytes.slice(4), 'base16')
// @ts-ignore `anchorDagCbor` is a Solidity function
const transactionRequest = await this.contract.anchorDagCbor(hexEncoded)
return {
to: this.contractAddress,
data: transactionRequest.data,
nonce: baseNonce,
from: this.wallet.address,
}
}
/**
* One attempt at submitting the prepared TransactionRequest to the ethereum blockchain.
* @param txData
*/
async _trySendTransaction(txData: TransactionRequest): Promise<TransactionResponse> {
logger.imp(
'Transaction data:' +
JSON.stringify(txData, (_, v) => (typeof v === 'bigint' ? v.toString() : v))
)
logEvent.ethereum({
type: 'txRequest',
tx: txData,
})
logger.imp(`Sending transaction to Ethereum ${this.network} network...`)
const txResponse = await Utils.coalesceAsync(
() => this.wallet.sendTransaction(txData),
'No transaction'
)
logEvent.ethereum({
type: 'txResponse',
hash: txResponse.hash,
blockNumber: txResponse.blockNumber,
blockHash: txResponse.blockHash,
from: txResponse.from,
raw: txResponse.data,
})
if (!this._chainId) throw new Error(`No chainId available`)
assertSameChainId(txResponse.chainId, this._chainId)
return txResponse
}
/**
* Queries the blockchain to see if the submitted transaction was successfully mined, and returns
* the transaction info if so.
* @param txResponse - response from when the transaction was submitted to the mempool
*/
async _confirmTransactionSuccess(txResponse: TransactionResponse): Promise<Transaction> {
logger.imp(`Waiting to confirm transaction with hash ${txResponse.hash}`)
const walletProvider = await Utils.coalesce(() => this.wallet.provider, 'No wallet provider')
const txReceipt = await Utils.coalesceAsync(
() =>
walletProvider.waitForTransaction(
txResponse.hash,
NUM_BLOCKS_TO_WAIT,
this.transactionTimeoutSecs * 1000
),
'No transaction receipt'
)
logEvent.ethereum({
type: 'txReceipt',
tx: txReceipt,
})
const block = await Utils.coalesceAsync(
() => walletProvider.getBlock(txReceipt.blockHash),
'No block found'
)
const blockDate = Utils.coalesce(() => block.date, 'Block has no date')
const statusMessage = txReceipt.status == TX_SUCCESS ? 'success' : 'failure'
logger.imp(
`Transaction completed on Ethereum ${this.network} network. Transaction hash: ${txReceipt.blockHash}. Status: ${statusMessage}.`
)
if (txReceipt.status != TX_SUCCESS) {
throw new Error('Transaction completed with a failure status')
}
return new Transaction(this.chainId, txReceipt.hash, txReceipt.blockNumber, blockDate)
}
/**
* Queries the blockchain to see if any of the previously submitted transactions that had timed
* out went on to be successfully mined, and returns the transaction info if so.
* @param txResponses - responses from previous transaction submissions.
*/
async _checkForPreviousTransactionSuccess(
txResponses: Array<TransactionResponse>
): Promise<Transaction> {
for (let i = txResponses.length - 1; i >= 0; i--) {
const txResponse = txResponses[i]
if (!txResponse) continue
try {
return await this._confirmTransactionSuccess(txResponse)
} catch (err: any) {
logger.err(err)
}
}
throw new Error('Failed to confirm any previous transaction attempts')
}
/**
* Sends transaction with root CID as data
*/
async sendTransaction(rootCid: CID): Promise<Transaction> {
const txData = await this._buildTransactionRequest(rootCid)
const txResponses: Array<TransactionResponse> = []
return this.withWalletBalance((walletBalance) => {
return attempt(MAX_RETRIES, async (attemptNum) => {
try {
await this.setGasPrice(txData)
const txResponse = await this._trySendTransaction(txData)
txResponses.push(txResponse)
return await this._confirmTransactionSuccess(txResponse)
} catch (err: any) {
logger.err(err)
if (isError(err, 'INSUFFICIENT_FUNDS')) {
return handleInsufficientFundsError(txData, walletBalance)
} else if (isError(err, 'TIMEOUT')) {
return handleTimeoutError(this.transactionTimeoutSecs)
} else if (isError(err, 'NONCE_EXPIRED') || isNonceError(err)) {
// If this happens it most likely means that one of our previous attempts timed out, but
// then actually wound up being successfully mined
logEvent.ethereum({
type: 'nonceExpired',
nonce: txData.nonce,
})
if (attemptNum == 0 || txResponses.length == 0) {
throw err
}
return this._checkForPreviousTransactionSuccess(txResponses)
} else {
throw new Error(`Unhandled error in attempt: ${err}`)
}
}
})
})
}
/**
* Report wallet balance before and after +operation+.
* @param operation
*/
private async withWalletBalance<T>(operation: (balance: bigint) => Promise<T>): Promise<T> {
const provider = Utils.coalesce(() => this.wallet.provider, 'No wallet provider')
const startingWalletBalance = await Utils.coalesceAsync(
() => provider.getBalance(this.wallet.address),
`No wallet balance for ${this.wallet.address}`
)
logMetric.ethereum({
type: 'walletBalance',
balance: ethers.formatUnits(startingWalletBalance, 'gwei'),
})
logger.debug(`Current wallet balance is ` + startingWalletBalance)
const result = await operation(startingWalletBalance)
const endingWalletBalance = await Utils.coalesceAsync(
() => provider.getBalance(this.wallet.address),
`No wallet balance for ${this.wallet.address}`
)
logMetric.ethereum({
type: 'walletBalance',
balance: ethers.formatUnits(endingWalletBalance, 'gwei'),
})
return result
}
}
function isNonceError(err: any): boolean {
return isError(err, 'UNKNOWN_ERROR') && err.message.includes('correct nonce')
}