-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.substrate.js
54 lines (47 loc) · 1.32 KB
/
provider.substrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { WsProvider, ApiPromise } from '@polkadot/api'
import { endpoints } from './endpoints.js';
// const endpoint = 'parity'
export class SubstrateApiProvider {
_endpoint = 'local'
_providers = {
// kusama: new WsProvider(endpoints['kusama'][this._endpoint]),
// polkadot: new WsProvider(endpoints['polkadot'][this._endpoint])
}
api = {
// kusama: any
// polkadot: Promise<ApiPromise>
}
constructor(endpoint = 'local') {
console.debug('SubstrateApiProvider.constructor()', endpoint)
this._endpoint = endpoint
}
async connect() {
console.debug('SubstrateApiProvider.connect()', this._endpoint)
try {
this._providers = {
kusama: new WsProvider(endpoints['kusama'][this._endpoint]),
polkadot: new WsProvider(endpoints['polkadot'][this._endpoint])
}
this.api = {
kusama: await ApiPromise.create({ provider: this._providers.kusama }),
polkadot: await ApiPromise.create({ provider: this._providers.polkadot })
}
return true
} catch (err) {
console.error(err)
return false
}
}
async disconnect() {
try {
await this.api['kusama'].disconnect()
} catch (err) {
console.log(err)
}
try {
await this.api['polkadot'].disconnect()
} catch (err) {
console.log(err)
}
}
}