1
+ import '@polkadot/api-augment/polkadot'
2
+
3
+ import { ApiPromise , Keyring } from '@polkadot/api'
1
4
import { abs , add , div , gt , lte , mul , sub } from 'biggystring'
2
5
import {
3
6
EdgeCurrencyEngine ,
@@ -26,13 +29,9 @@ import {
26
29
asTransfer ,
27
30
PolkadotOtherData ,
28
31
PolkadotSettings ,
29
- SdkBalance ,
30
- SdkBlockHeight ,
31
- SdkPaymentInfo ,
32
32
SubscanResponse ,
33
33
SubscanTx
34
34
} from './polkadotTypes'
35
- import { ApiPromise , Keyring } from './polkadotUtils'
36
35
37
36
const ACCOUNT_POLL_MILLISECONDS = 5000
38
37
const BLOCKCHAIN_POLL_MILLISECONDS = 20000
@@ -44,7 +43,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
44
43
settings : PolkadotSettings
45
44
otherData ! : PolkadotOtherData
46
45
api ! : ApiPromise
47
- keypair : Keyring | undefined
46
+ keypair ! : Keyring
48
47
nonce : number
49
48
50
49
constructor (
@@ -80,29 +79,21 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
80
79
return asSubscanResponse ( out )
81
80
}
82
81
83
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
84
- async queryBalance ( ) {
85
- try {
86
- // @ts -expect-error
87
- const response : SdkBalance = await this . api . query . system . account (
88
- this . walletInfo . keys . publicKey
89
- )
90
- this . nonce = response . nonce
91
- this . updateBalance (
92
- this . currencyInfo . currencyCode ,
93
- response . data . free . toString ( )
94
- )
95
- } catch ( e : any ) {
96
- this . warn ( 'queryBalance failed with error: ' , e )
97
- }
82
+ async queryBalance ( ) : Promise < void > {
83
+ const response = await this . api . query . system . account (
84
+ this . walletInfo . keys . publicKey as string
85
+ )
86
+ this . nonce = response . nonce . toNumber ( )
87
+ this . updateBalance (
88
+ this . currencyInfo . currencyCode ,
89
+ response . data . free . toString ( )
90
+ )
98
91
}
99
92
100
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
101
- async queryBlockheight ( ) {
93
+ async queryBlockheight ( ) : Promise < void > {
102
94
try {
103
- // @ts -expect-error
104
- const response : SdkBlockHeight = await this . api . rpc . chain . getBlock ( )
105
- const height = response . block . header . number
95
+ const response = await this . api . rpc . chain . getBlock ( )
96
+ const height = response . block . header . number . toNumber ( )
106
97
if ( height > this . walletLocalData . blockHeight ) {
107
98
this . walletLocalData . blockHeight = height
108
99
this . walletLocalDataDirty = true
@@ -115,8 +106,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
115
106
}
116
107
}
117
108
118
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
119
- processPolkadotTransaction ( tx : SubscanTx ) {
109
+ processPolkadotTransaction ( tx : SubscanTx ) : void {
120
110
const {
121
111
from,
122
112
to,
@@ -161,13 +151,11 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
161
151
this . addTransaction ( this . currencyInfo . currencyCode , edgeTransaction )
162
152
}
163
153
164
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
165
- async queryTransactions ( ) {
154
+ async queryTransactions ( ) : Promise < void > {
166
155
return await queryTxMutex ( async ( ) => await this . queryTransactionsInner ( ) )
167
156
}
168
157
169
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
170
- async queryTransactionsInner ( ) {
158
+ async queryTransactionsInner ( ) : Promise < void > {
171
159
// Skip pages we don't need
172
160
let page = Math . floor (
173
161
this . otherData . txCount / this . settings . subscanQueryLimit
@@ -189,8 +177,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
189
177
transfers = cleanResponse . transfers
190
178
} catch ( e : any ) {
191
179
if (
192
- typeof e ?. message === 'string' &&
193
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
180
+ e instanceof Error &&
194
181
e . message . includes ( 'Subscan /scan/transfers failed with 429' )
195
182
) {
196
183
this . log ( e . message )
@@ -234,8 +221,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
234
221
}
235
222
}
236
223
237
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
238
- initOtherData ( ) {
224
+ initOtherData ( ) : void {
239
225
if ( this . otherData . txCount == null ) {
240
226
this . otherData . txCount = 0
241
227
}
@@ -245,25 +231,22 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
245
231
// // Public methods
246
232
// // ****************************************************************************
247
233
248
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
249
- async startEngine ( ) {
234
+ async startEngine ( ) : Promise < void > {
250
235
this . engineOn = true
251
236
await this . tools . connectApi ( this . walletId )
252
- // @ts -expect-error
253
237
this . api = this . tools . polkadotApi
254
238
this . initOtherData ( )
255
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
256
- this . addToLoop ( 'queryBlockheight' , BLOCKCHAIN_POLL_MILLISECONDS )
257
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
258
- this . addToLoop ( 'queryBalance' , ACCOUNT_POLL_MILLISECONDS )
259
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
260
- this . addToLoop ( 'queryTransactions' , TRANSACTION_POLL_MILLISECONDS )
261
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
262
- super . startEngine ( )
239
+ this . addToLoop ( 'queryBlockheight' , BLOCKCHAIN_POLL_MILLISECONDS ) . catch (
240
+ ( ) => { }
241
+ )
242
+ this . addToLoop ( 'queryBalance' , ACCOUNT_POLL_MILLISECONDS ) . catch ( ( ) => { } )
243
+ this . addToLoop ( 'queryTransactions' , TRANSACTION_POLL_MILLISECONDS ) . catch (
244
+ ( ) => { }
245
+ )
246
+ await super . startEngine ( )
263
247
}
264
248
265
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
266
- async killEngine ( ) {
249
+ async killEngine ( ) : Promise < void > {
267
250
await super . killEngine ( )
268
251
await this . tools . disconnectApi ( this . walletId )
269
252
}
@@ -296,9 +279,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
296
279
const tx = await this . makeSpend ( maxSpendInfo )
297
280
const fee = tx . networkFee
298
281
299
- // @ts -expect-error
300
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
301
- const getMax = ( min , max ) => {
282
+ const getMax = ( min : string , max : string ) : string => {
302
283
const diff = sub ( max , min )
303
284
if ( lte ( diff , '1' ) ) {
304
285
return min
@@ -351,8 +332,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
351
332
nativeAmount
352
333
)
353
334
354
- // @ts -expect-error
355
- const paymentInfo : SdkPaymentInfo = await transfer . paymentInfo (
335
+ const paymentInfo = await transfer . paymentInfo (
356
336
this . walletInfo . keys . publicKey
357
337
)
358
338
@@ -404,14 +384,6 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
404
384
nativeAmount
405
385
)
406
386
407
- if ( this . keypair == null ) {
408
- const keyring = new Keyring ( { ss58Format : 0 } )
409
- // @ts -expect-error
410
- this . keypair = keyring . addFromUri (
411
- this . walletInfo . keys [ `${ this . currencyInfo . pluginId } Mnemonic` ]
412
- )
413
- }
414
-
415
387
const signer = this . api . createType ( 'SignerPayload' , {
416
388
method : transfer ,
417
389
nonce : this . nonce ,
@@ -427,12 +399,19 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
427
399
{ version : this . api . extrinsicVersion }
428
400
)
429
401
430
- // @ts -expect-error
431
- const signedPayload = extrinsicPayload . sign ( this . keypair )
402
+ if ( this . keypair == null ) {
403
+ this . keypair = new Keyring ( { ss58Format : 0 } )
404
+ this . keypair . addFromUri (
405
+ this . walletInfo . keys [ `${ this . currencyInfo . pluginId } Mnemonic` ]
406
+ )
407
+ }
408
+
409
+ const signedPayload = extrinsicPayload . sign (
410
+ this . keypair . getPair ( this . walletInfo . keys . publicKey )
411
+ )
432
412
433
413
transfer . addSignature (
434
- // @ts -expect-error
435
- this . keypair . address ,
414
+ this . walletInfo . keys . publicKey ,
436
415
signedPayload . signature ,
437
416
signer . toPayload ( )
438
417
)
@@ -460,26 +439,12 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
460
439
return edgeTransaction
461
440
}
462
441
463
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
464
- getDisplayPrivateSeed ( ) {
465
- if (
466
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-optional-chain
467
- this . walletInfo . keys &&
468
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
469
- this . walletInfo . keys [ `${ this . currencyInfo . pluginId } Mnemonic` ]
470
- ) {
471
- return this . walletInfo . keys [ `${ this . currencyInfo . pluginId } Mnemonic` ]
472
- }
473
- return ''
442
+ getDisplayPrivateSeed ( ) : string {
443
+ return this . walletInfo . keys [ `${ this . currencyInfo . pluginId } Mnemonic` ] ?? ''
474
444
}
475
445
476
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
477
- getDisplayPublicSeed ( ) {
478
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-optional-chain
479
- if ( this . walletInfo . keys && this . walletInfo . keys . publicKey ) {
480
- return this . walletInfo . keys . publicKey
481
- }
482
- return ''
446
+ getDisplayPublicSeed ( ) : string {
447
+ return this . walletInfo . keys . publicKey ?? ''
483
448
}
484
449
}
485
450
0 commit comments