-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBaseManager.ts
97 lines (80 loc) · 2.96 KB
/
BaseManager.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { DERIVE_PATHS } from '@/constants';
import type {
Accounts,
Asset,
AssetSourceRelation,
ChainClient,
ChainToDefinition,
Chains,
} from '@/types';
import { sr25519CreateDerive } from '@polkadot-labs/hdkd';
import { DEV_PHRASE, entropyToMiniSecret, mnemonicToEntropy } from '@polkadot-labs/hdkd-helpers';
import type { PolkadotClient, PolkadotSigner, TypedApi } from 'polkadot-api';
import { getPolkadotSigner } from 'polkadot-api/signer';
import { filter, firstValueFrom, take } from 'rxjs';
export abstract class BaseChainManager {
protected clients: Map<Chains, ChainClient<Chains>> = new Map();
protected signers: Map<Accounts, PolkadotSigner> = new Map();
constructor() {
this.initializeSigners();
}
private initializeSigners() {
const entropy = mnemonicToEntropy(DEV_PHRASE);
const miniSecret = entropyToMiniSecret(entropy);
const derive = sr25519CreateDerive(miniSecret);
for (const [account, path] of Object.entries(DERIVE_PATHS)) {
const keyPair = derive(path);
this.signers.set(
account as Accounts,
getPolkadotSigner(keyPair.publicKey, 'Sr25519', keyPair.sign),
);
}
}
getApi<T extends Chains>(chain: T): TypedApi<ChainToDefinition[T]> {
const client = this.clients.get(chain);
if (!client) throw new Error(`Chain ${chain} not initialized`);
return client.api as TypedApi<ChainToDefinition[T]>;
}
getClient<T extends Chains>(chain: T): PolkadotClient {
const client = this.clients.get(chain);
if (!client) throw new Error(`Chain ${chain} not initialized`);
return client.client;
}
getSigner(account: Accounts) {
const signer = this.signers.get(account);
if (!signer) throw new Error(`Signer for ${account} not found`);
return signer;
}
waitForNextBlock(currentBlock: number) {
const api = this.getApi(this.getChainType());
return firstValueFrom(
api.query.System.Number.watchValue().pipe(
filter((newBlock) => newBlock > currentBlock),
take(1),
),
);
}
async getBlockNumber() {
const chain = this.getChainType();
const api = this.getApi(chain);
// Note: Not sure why this is needed, but without it we cannot retrieve the block number.
await api.compatibilityToken;
return api.query.System.Number.getValue();
}
getMessageQueueEvents() {
const api = this.getApi(this.getChainType());
return api.event.MessageQueue.Processed.pull();
}
async getExtrinsicFee() {
const api = this.getApi(this.getChainType());
const events = await api.event.TransactionPayment.TransactionFeePaid.pull();
return events[0]?.payload.actual_fee || 0n;
}
abstract getAssetSourceRelation(asset: Asset): AssetSourceRelation;
abstract getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint>;
// @ts-expect-error - TODO: Not sure which is the correct type for this
abstract getXcmPallet();
abstract getChainType(): Chains;
abstract connect(): void;
abstract disconnect(): void;
}