|
@@ -127,6 +127,31 @@ class RuntimeApi {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ async selectBestNonce(accountId) {
|
|
|
+ const cachedNonce = this.nonces[accountId]
|
|
|
+
|
|
|
+
|
|
|
+ const systemNonce = await this.api.query.system.accountNonce(accountId)
|
|
|
+
|
|
|
+ if (cachedNonce) {
|
|
|
+
|
|
|
+ if (systemNonce.gt(cachedNonce)) {
|
|
|
+ return systemNonce
|
|
|
+ } else {
|
|
|
+ return cachedNonce
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return systemNonce
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ incrementAndSaveNonce(accountId, nonce) {
|
|
|
+ this.nonces[accountId] = nonce.addn(1)
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* signAndSend() with nonce tracking, to enable concurrent sending of transacctions
|
|
|
* so that they can be included in the same block. Allows you to use the accountId instead
|
|
@@ -221,15 +246,13 @@ class RuntimeApi {
|
|
|
|
|
|
|
|
|
await this.executeWithAccountLock(accountId, async () => {
|
|
|
- const nonce = this.nonces[accountId] || (await this.api.query.system.accountNonce(accountId))
|
|
|
-
|
|
|
-
|
|
|
+ const nonce = await this.selectBestNonce(accountId)
|
|
|
|
|
|
try {
|
|
|
unsubscribe = await tx.sign(fromKey, { nonce }).send(handleTxUpdates)
|
|
|
debug('TransactionSubmitted')
|
|
|
|
|
|
- this.nonces[accountId] = nonce.addn(1)
|
|
|
+ this.incrementAndSaveNonce(accountId, nonce)
|
|
|
} catch (err) {
|
|
|
const errstr = err.toString()
|
|
|
debug('TransactionRejected:', errstr)
|