@@ -8,7 +8,7 @@ import { DEV_PHRASE, entropyToMiniSecret, mnemonicToEntropy, KeyPair } from "@po
8
8
import { getPolkadotSigner } from "polkadot-api/signer"
9
9
import { randomBytes } from 'crypto' ;
10
10
import { Keyring } from '@polkadot/keyring' ;
11
- import { SS58_PREFIX } from "./config" ;
11
+ import { SS58_PREFIX , TX_TIMEOUT } from "./config" ;
12
12
13
13
let api : TypedApi < typeof devnet > | undefined = undefined
14
14
@@ -111,7 +111,7 @@ export async function getNonceChangePromise(api: TypedApi<typeof devnet>, ss58Ad
111
111
subscription . unsubscribe ( ) ;
112
112
console . log ( 'unsubscribed!' ) ;
113
113
resolve ( )
114
- } , 2000 ) ;
114
+ } , TX_TIMEOUT ) ;
115
115
116
116
} )
117
117
}
@@ -129,21 +129,28 @@ export function convertPublicKeyToMultiAddress(publicKey: Uint8Array, ss58Format
129
129
130
130
export async function waitForTransactionCompletion ( api : TypedApi < typeof devnet > , tx : Transaction < { } , string , string , void > , signer : PolkadotSigner , ) {
131
131
const transactionPromise = await getTransactionWatchPromise ( tx , signer )
132
- const ss58Address = convertPublicKeyToSs58 ( signer . publicKey )
133
- const noncePromise = await getNonceChangePromise ( api , ss58Address )
134
-
135
- return new Promise < void > ( ( resolve , reject ) => {
136
- Promise . race ( [ transactionPromise , noncePromise ] )
137
- . then ( resolve )
138
- . catch ( reject ) ;
139
- } )
132
+ return transactionPromise
133
+
134
+ // If we can't always get the finalized event, then add nonce subscribe as other evidence for tx is finalized.
135
+ // Don't need it based on current testing.
136
+ // const ss58Address = convertPublicKeyToSs58(signer.publicKey)
137
+ // const noncePromise = await getNonceChangePromise(api, ss58Address)
138
+
139
+ // return new Promise<void>((resolve, reject) => {
140
+ // Promise.race([transactionPromise, noncePromise])
141
+ // .then(resolve)
142
+ // .catch(reject);
143
+ // })
140
144
}
141
145
142
146
export async function getTransactionWatchPromise ( tx : Transaction < { } , string , string , void > , signer : PolkadotSigner , ) {
143
147
return new Promise < void > ( ( resolve , reject ) => {
148
+ // store the txHash, then use it in timeout. easier to know which tx is not finalized in time
149
+ let txHash = ""
144
150
const subscription = tx . signSubmitAndWatch ( signer ) . subscribe ( {
145
151
next ( value ) {
146
152
console . log ( "Event:" , value ) ;
153
+ txHash = value . txHash
147
154
148
155
// TODO investigate why finalized not for each extrinsic
149
156
if ( value . type === "finalized" ) {
@@ -168,9 +175,9 @@ export async function getTransactionWatchPromise(tx: Transaction<{}, string, str
168
175
169
176
setTimeout ( ( ) => {
170
177
subscription . unsubscribe ( ) ;
171
- console . log ( 'unsubscribed!' ) ;
172
- resolve ( )
173
- } , 2000 ) ;
178
+ console . log ( 'unsubscribed because of timeout for tx {}' , txHash ) ;
179
+ reject ( )
180
+ } , TX_TIMEOUT ) ;
174
181
} ) ;
175
182
}
176
183
0 commit comments