Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
7b43840c
Commit
7b43840c
authored
Jul 02, 2021
by
Karl Floersch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: tx submitter library
parent
3b132974
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
227 additions
and
0 deletions
+227
-0
index.ts
packages/batch-submitter/src/index.ts
+1
-0
index.ts
packages/batch-submitter/src/utils/index.ts
+1
-0
tx-submission.ts
packages/batch-submitter/src/utils/tx-submission.ts
+94
-0
tx-submission.spec.ts
packages/batch-submitter/test/utils/tx-submission.spec.ts
+131
-0
No files found.
packages/batch-submitter/src/index.ts
View file @
7b43840c
export
*
from
'
./batch-submitter
'
export
*
from
'
./utils
'
export
*
from
'
./transaction-chain-contract
'
packages/batch-submitter/src/utils/index.ts
0 → 100644
View file @
7b43840c
export
*
from
'
./tx-submission
'
packages/batch-submitter/src/utils/tx-submission.ts
0 → 100644
View file @
7b43840c
import
{
Signer
,
utils
,
ethers
,
PopulatedTransaction
}
from
'
ethers
'
import
{
TransactionReceipt
,
TransactionResponse
,
}
from
'
@ethersproject/abstract-provider
'
import
*
as
ynatm
from
'
@eth-optimism/ynatm
'
interface
ResubmissionConfig
{
resubmissionTimeout
:
number
minGasPriceInGwei
:
number
maxGasPriceInGwei
:
number
gasRetryIncrement
:
number
}
export
type
SubmitTransactionFn
=
(
tx
:
PopulatedTransaction
)
=>
Promise
<
TransactionReceipt
>
export
interface
TxSubmissionHooks
{
beforeSendTransaction
:
(
tx
:
PopulatedTransaction
)
=>
void
onTransactionResponse
:
(
txResponse
:
TransactionResponse
)
=>
void
}
const
getGasPriceInGwei
=
async
(
signer
:
Signer
):
Promise
<
number
>
=>
{
return
parseInt
(
ethers
.
utils
.
formatUnits
(
await
signer
.
getGasPrice
(),
'
gwei
'
),
10
)
}
export
const
submitTransactionWithYNATM
=
async
(
tx
:
PopulatedTransaction
,
signer
:
Signer
,
config
:
ResubmissionConfig
,
numConfirmations
:
number
,
hooks
:
TxSubmissionHooks
):
Promise
<
TransactionReceipt
>
=>
{
const
sendTxAndWaitForReceipt
=
async
(
gasPrice
):
Promise
<
TransactionReceipt
>
=>
{
const
fullTx
=
{
...
tx
,
gasPrice
,
}
hooks
.
beforeSendTransaction
(
fullTx
)
const
txResponse
=
await
signer
.
sendTransaction
(
fullTx
)
hooks
.
onTransactionResponse
(
txResponse
)
return
signer
.
provider
.
waitForTransaction
(
txResponse
.
hash
,
numConfirmations
)
}
const
minGasPrice
=
await
getGasPriceInGwei
(
signer
)
const
receipt
=
await
ynatm
.
send
({
sendTransactionFunction
:
sendTxAndWaitForReceipt
,
minGasPrice
:
ynatm
.
toGwei
(
minGasPrice
),
maxGasPrice
:
ynatm
.
toGwei
(
config
.
maxGasPriceInGwei
),
gasPriceScalingFunction
:
ynatm
.
LINEAR
(
config
.
gasRetryIncrement
),
delay
:
config
.
resubmissionTimeout
,
})
return
receipt
}
export
interface
TransactionSubmitter
{
submitTransaction
(
tx
:
PopulatedTransaction
,
hooks
?:
TxSubmissionHooks
):
Promise
<
TransactionReceipt
>
}
export
class
YnatmTransactionSubmitter
implements
TransactionSubmitter
{
constructor
(
readonly
signer
:
Signer
,
readonly
ynatmConfig
:
ResubmissionConfig
,
readonly
numConfirmations
:
number
)
{}
public
async
submitTransaction
(
tx
:
PopulatedTransaction
,
hooks
?:
TxSubmissionHooks
):
Promise
<
TransactionReceipt
>
{
if
(
!
hooks
)
{
hooks
=
{
beforeSendTransaction
:
()
=>
undefined
,
onTransactionResponse
:
()
=>
undefined
,
}
}
return
submitTransactionWithYNATM
(
tx
,
this
.
signer
,
this
.
ynatmConfig
,
this
.
numConfirmations
,
hooks
)
}
}
packages/batch-submitter/test/utils/tx-submission.spec.ts
0 → 100644
View file @
7b43840c
import
{
expect
}
from
'
../setup
'
import
{
ethers
,
BigNumber
,
Signer
}
from
'
ethers
'
import
{
submitTransactionWithYNATM
}
from
'
../../src/utils/tx-submission
'
import
{
ResubmissionConfig
}
from
'
../../src
'
import
{
TransactionReceipt
,
TransactionResponse
,
}
from
'
@ethersproject/abstract-provider
'
const
nullFunction
=
()
=>
undefined
const
nullHooks
=
{
beforeSendTransaction
:
nullFunction
,
onTransactionResponse
:
nullFunction
,
}
describe
(
'
submitTransactionWithYNATM
'
,
async
()
=>
{
it
(
'
calls sendTransaction, waitForTransaction, and hooks with correct inputs
'
,
async
()
=>
{
const
called
=
{
sendTransaction
:
false
,
waitForTransaction
:
false
,
beforeSendTransaction
:
false
,
onTransactionResponse
:
false
,
}
const
dummyHash
=
'
dummy hash
'
const
numConfirmations
=
3
const
tx
=
{
data
:
'
we here though
'
,
}
as
ethers
.
PopulatedTransaction
const
sendTransaction
=
async
(
_tx
:
ethers
.
PopulatedTransaction
):
Promise
<
TransactionResponse
>
=>
{
called
.
sendTransaction
=
true
expect
(
_tx
.
data
).
to
.
equal
(
tx
.
data
)
return
{
hash
:
dummyHash
,
}
as
TransactionResponse
}
const
waitForTransaction
=
async
(
hash
:
string
,
_numConfirmations
:
number
):
Promise
<
TransactionReceipt
>
=>
{
called
.
waitForTransaction
=
true
expect
(
hash
).
to
.
equal
(
dummyHash
)
expect
(
_numConfirmations
).
to
.
equal
(
numConfirmations
)
return
{
to
:
''
,
from
:
''
,
status
:
1
,
}
as
TransactionReceipt
}
const
signer
=
{
getGasPrice
:
async
()
=>
ethers
.
BigNumber
.
from
(
0
),
sendTransaction
,
provider
:
{
waitForTransaction
,
},
}
as
Signer
const
hooks
=
{
beforeSendTransaction
:
(
submittingTx
:
ethers
.
PopulatedTransaction
)
=>
{
called
.
beforeSendTransaction
=
true
expect
(
submittingTx
.
data
).
to
.
equal
(
tx
.
data
)
},
onTransactionResponse
:
(
txResponse
:
TransactionResponse
)
=>
{
called
.
onTransactionResponse
=
true
expect
(
txResponse
.
hash
).
to
.
equal
(
dummyHash
)
},
}
const
config
:
ResubmissionConfig
=
{
resubmissionTimeout
:
1000
,
minGasPriceInGwei
:
0
,
maxGasPriceInGwei
:
0
,
gasRetryIncrement
:
1
,
}
await
submitTransactionWithYNATM
(
tx
,
signer
,
config
,
numConfirmations
,
hooks
)
expect
(
called
.
sendTransaction
).
to
.
be
.
true
expect
(
called
.
waitForTransaction
).
to
.
be
.
true
expect
(
called
.
beforeSendTransaction
).
to
.
be
.
true
expect
(
called
.
onTransactionResponse
).
to
.
be
.
true
})
it
(
'
repeatedly increases the gas limit of the transaction when wait takes too long
'
,
async
()
=>
{
// Make transactions take longer to be included
// than our resubmission timeout
const
resubmissionTimeout
=
100
const
txReceiptDelay
=
resubmissionTimeout
*
3
const
numConfirmations
=
3
let
lastGasPrice
=
BigNumber
.
from
(
0
)
// Create a transaction which has a gas price that we will watch increment
const
tx
=
{
gasPrice
:
lastGasPrice
.
add
(
1
),
data
:
'
hello world!
'
,
}
as
ethers
.
PopulatedTransaction
const
sendTransaction
=
async
(
_tx
:
ethers
.
PopulatedTransaction
):
Promise
<
TransactionResponse
>
=>
{
// Ensure the gas price is always increasing
expect
(
_tx
.
gasPrice
>
lastGasPrice
).
to
.
be
.
true
lastGasPrice
=
_tx
.
gasPrice
return
{
hash
:
'
dummy hash
'
,
}
as
TransactionResponse
}
const
waitForTransaction
=
async
(
hash
:
string
,
_numConfirmations
:
number
):
Promise
<
TransactionReceipt
>
=>
{
await
new
Promise
((
r
)
=>
setTimeout
(
r
,
txReceiptDelay
))
return
{}
as
TransactionReceipt
}
const
signer
=
{
getGasPrice
:
async
()
=>
ethers
.
BigNumber
.
from
(
0
),
sendTransaction
,
provider
:
{
waitForTransaction
,
},
}
as
Signer
const
config
:
ResubmissionConfig
=
{
resubmissionTimeout
,
minGasPriceInGwei
:
0
,
maxGasPriceInGwei
:
1000
,
gasRetryIncrement
:
1
,
}
await
submitTransactionWithYNATM
(
tx
,
signer
,
config
,
0
,
nullHooks
)
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment