Skip to content

Commit 82d7422

Browse files
authored
Merge pull request #495 from EdgeApp/matthew/clelanupDot
Cleanup Polkadot and fix runtime queryInfo error
2 parents e485067 + e0bc167 commit 82d7422

8 files changed

+388
-477
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@ethereumjs/tx": "^3.3.0",
7878
"@fioprotocol/fiosdk": "^1.5.0",
7979
"@hashgraph/sdk": "^1.1.9",
80+
"@polkadot/api": "^9.11.3",
8081
"@solana/web3.js": "^1.32.0",
8182
"@tronscan/client": "^0.2.81",
8283
"@walletconnect/client": "1.6.6",
@@ -105,7 +106,6 @@
105106
"@babel/core": "^7.17.8",
106107
"@grpc/grpc-js": "^1.7.3",
107108
"@grpc/proto-loader": "^0.7.3",
108-
"@polkadot/api": "^9.3.3",
109109
"@sucrase/webpack-loader": "^2.0.0",
110110
"@types/chai": "^4.2.9",
111111
"@types/mocha": "^7.0.1",

src/polkadot/polkadotEngine.ts

+48-83
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import '@polkadot/api-augment/polkadot'
2+
3+
import { ApiPromise, Keyring } from '@polkadot/api'
14
import { abs, add, div, gt, lte, mul, sub } from 'biggystring'
25
import {
36
EdgeCurrencyEngine,
@@ -26,13 +29,9 @@ import {
2629
asTransfer,
2730
PolkadotOtherData,
2831
PolkadotSettings,
29-
SdkBalance,
30-
SdkBlockHeight,
31-
SdkPaymentInfo,
3232
SubscanResponse,
3333
SubscanTx
3434
} from './polkadotTypes'
35-
import { ApiPromise, Keyring } from './polkadotUtils'
3635

3736
const ACCOUNT_POLL_MILLISECONDS = 5000
3837
const BLOCKCHAIN_POLL_MILLISECONDS = 20000
@@ -44,7 +43,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
4443
settings: PolkadotSettings
4544
otherData!: PolkadotOtherData
4645
api!: ApiPromise
47-
keypair: Keyring | undefined
46+
keypair!: Keyring
4847
nonce: number
4948

5049
constructor(
@@ -80,29 +79,21 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
8079
return asSubscanResponse(out)
8180
}
8281

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+
)
9891
}
9992

100-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
101-
async queryBlockheight() {
93+
async queryBlockheight(): Promise<void> {
10294
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()
10697
if (height > this.walletLocalData.blockHeight) {
10798
this.walletLocalData.blockHeight = height
10899
this.walletLocalDataDirty = true
@@ -115,8 +106,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
115106
}
116107
}
117108

118-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
119-
processPolkadotTransaction(tx: SubscanTx) {
109+
processPolkadotTransaction(tx: SubscanTx): void {
120110
const {
121111
from,
122112
to,
@@ -161,13 +151,11 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
161151
this.addTransaction(this.currencyInfo.currencyCode, edgeTransaction)
162152
}
163153

164-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
165-
async queryTransactions() {
154+
async queryTransactions(): Promise<void> {
166155
return await queryTxMutex(async () => await this.queryTransactionsInner())
167156
}
168157

169-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
170-
async queryTransactionsInner() {
158+
async queryTransactionsInner(): Promise<void> {
171159
// Skip pages we don't need
172160
let page = Math.floor(
173161
this.otherData.txCount / this.settings.subscanQueryLimit
@@ -189,8 +177,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
189177
transfers = cleanResponse.transfers
190178
} catch (e: any) {
191179
if (
192-
typeof e?.message === 'string' &&
193-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
180+
e instanceof Error &&
194181
e.message.includes('Subscan /scan/transfers failed with 429')
195182
) {
196183
this.log(e.message)
@@ -234,8 +221,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
234221
}
235222
}
236223

237-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
238-
initOtherData() {
224+
initOtherData(): void {
239225
if (this.otherData.txCount == null) {
240226
this.otherData.txCount = 0
241227
}
@@ -245,25 +231,22 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
245231
// // Public methods
246232
// // ****************************************************************************
247233

248-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
249-
async startEngine() {
234+
async startEngine(): Promise<void> {
250235
this.engineOn = true
251236
await this.tools.connectApi(this.walletId)
252-
// @ts-expect-error
253237
this.api = this.tools.polkadotApi
254238
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()
263247
}
264248

265-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
266-
async killEngine() {
249+
async killEngine(): Promise<void> {
267250
await super.killEngine()
268251
await this.tools.disconnectApi(this.walletId)
269252
}
@@ -296,9 +279,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
296279
const tx = await this.makeSpend(maxSpendInfo)
297280
const fee = tx.networkFee
298281

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 => {
302283
const diff = sub(max, min)
303284
if (lte(diff, '1')) {
304285
return min
@@ -351,8 +332,7 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
351332
nativeAmount
352333
)
353334

354-
// @ts-expect-error
355-
const paymentInfo: SdkPaymentInfo = await transfer.paymentInfo(
335+
const paymentInfo = await transfer.paymentInfo(
356336
this.walletInfo.keys.publicKey
357337
)
358338

@@ -404,14 +384,6 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
404384
nativeAmount
405385
)
406386

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-
415387
const signer = this.api.createType('SignerPayload', {
416388
method: transfer,
417389
nonce: this.nonce,
@@ -427,12 +399,19 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
427399
{ version: this.api.extrinsicVersion }
428400
)
429401

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+
)
432412

433413
transfer.addSignature(
434-
// @ts-expect-error
435-
this.keypair.address,
414+
this.walletInfo.keys.publicKey,
436415
signedPayload.signature,
437416
signer.toPayload()
438417
)
@@ -460,26 +439,12 @@ export class PolkadotEngine extends CurrencyEngine<PolkadotTools> {
460439
return edgeTransaction
461440
}
462441

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`] ?? ''
474444
}
475445

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 ?? ''
483448
}
484449
}
485450

src/polkadot/polkadotPlugin.ts

+14-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api'
2+
import * as utilCrypto from '@polkadot/util-crypto'
13
import { div } from 'biggystring'
24
import { entropyToMnemonic, validateMnemonic } from 'bip39'
35
import { Buffer } from 'buffer'
@@ -15,28 +17,22 @@ import {
1517
import { PluginEnvironment } from '../common/innerPlugin'
1618
import { encodeUriCommon, parseUriCommon } from '../common/uriHelpers'
1719
import { getDenomInfo, isHex } from '../common/utils'
18-
import {
19-
ApiPromise,
20-
ed25519PairFromSeed,
21-
isAddress,
22-
Keyring,
23-
mnemonicToMiniSecret,
24-
WsProvider
25-
} from './polkadotUtils'
20+
21+
const { ed25519PairFromSeed, isAddress, mnemonicToMiniSecret } = utilCrypto
2622

2723
export class PolkadotTools implements EdgeCurrencyTools {
2824
io: EdgeIo
2925
currencyInfo: EdgeCurrencyInfo
3026

3127
// The SDK is wallet-agnostic and we need to track how many wallets are relying on it and disconnect if zero
32-
polkadotApi: ApiPromise | undefined
33-
polkadotApiSubscribers: { [walletId: string]: boolean }
28+
polkadotApi!: ApiPromise
29+
polkadotApiSubscribers: Set<string>
3430

3531
constructor(env: PluginEnvironment<{}>) {
3632
const { io, currencyInfo } = env
3733
this.io = io
3834
this.currencyInfo = currencyInfo
39-
this.polkadotApiSubscribers = {}
35+
this.polkadotApiSubscribers = new Set()
4036
}
4137

4238
async importPrivateKey(userInput: string): Promise<JsonObject> {
@@ -88,15 +84,10 @@ export class PolkadotTools implements EdgeCurrencyTools {
8884
this.currencyInfo,
8985
uri,
9086
networks,
91-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-nullish-coalescing
92-
currencyCode || this.currencyInfo.currencyCode,
87+
currencyCode ?? this.currencyInfo.currencyCode,
9388
customTokens
9489
)
95-
let address = ''
96-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
97-
if (edgeParsedUri.publicAddress) {
98-
address = edgeParsedUri.publicAddress
99-
}
90+
const address = edgeParsedUri.publicAddress ?? ''
10091

10192
if (!isAddress(address)) {
10293
throw new Error('InvalidPublicAddressError')
@@ -121,8 +112,7 @@ export class PolkadotTools implements EdgeCurrencyTools {
121112
if (typeof nativeAmount === 'string') {
122113
const denom = getDenomInfo(
123114
this.currencyInfo,
124-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/prefer-nullish-coalescing
125-
currencyCode || this.currencyInfo.currencyCode,
115+
currencyCode ?? this.currencyInfo.currencyCode,
126116
customTokens
127117
)
128118
if (denom == null) {
@@ -143,17 +133,15 @@ export class PolkadotTools implements EdgeCurrencyTools {
143133
)
144134
})
145135
}
146-
this.polkadotApiSubscribers[walletId] = true
136+
this.polkadotApiSubscribers.add(walletId)
147137
return this.polkadotApi
148138
}
149139

150140
async disconnectApi(walletId: string): Promise<void> {
151-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
152-
delete this.polkadotApiSubscribers[walletId]
153-
// @ts-expect-error
154-
if (Object.keys(this.polkadotApiSubscribers) === 0) {
141+
this.polkadotApiSubscribers.delete(walletId)
142+
if (this.polkadotApiSubscribers.size === 0) {
143+
await this.polkadotApi.disconnect()
155144
// @ts-expect-error
156-
await this.polkadotApi.disconnectApi()
157145
this.polkadotApi = undefined
158146
}
159147
}

src/polkadot/polkadotTypes.ts

-30
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,3 @@ export const asTransactions = asObject({
4949
count: asNumber,
5050
transfers: asMaybe(asArray(asTransfer), [])
5151
})
52-
53-
export interface SdkBalance {
54-
nonce: number
55-
consumers: number
56-
providers: number
57-
sufficients: number
58-
data: {
59-
free: number
60-
reserved: number
61-
miscFrozen: number
62-
feeFrozen: number
63-
}
64-
}
65-
66-
export interface SdkBlockHeight {
67-
block: {
68-
header: {
69-
parentHash: string
70-
number: number
71-
stateRoot: string
72-
extrinsicsRoot: string
73-
}
74-
}
75-
}
76-
77-
export interface SdkPaymentInfo {
78-
weight: number // 137709000,
79-
class: string // 'Normal',
80-
partialFee: number // s152000016
81-
}

src/polkadot/polkadotUtils.ts

-13
This file was deleted.

0 commit comments

Comments
 (0)