-
Notifications
You must be signed in to change notification settings - Fork 21
/
btc-relay.ts
59 lines (50 loc) · 2.01 KB
/
btc-relay.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
import { ApiPromise } from "@polkadot/api";
import { BitcoinH256Le, BtcRelayRichBlockHeader } from "@polkadot/types/lookup";
import { addHexPrefix } from "../utils";
export const DEFAULT_STABLE_CONFIRMATIONS = 6;
/**
* @category BTC Bridge
*/
export interface BTCRelayAPI {
/**
* @returns A global security parameter: the required block confirmations
* for a transaction to be considered stable on Bitcoin
*/
getStableBitcoinConfirmations(): Promise<number>;
/**
* @returns A global security parameter: the required block confirmations
* for a transaction to be considered stable on the parachain
*/
getStableParachainConfirmations(): Promise<number>;
/**
* @returns The raw transaction data, represented as a Buffer object
*/
getLatestBlock(): Promise<BitcoinH256Le>;
/**
* @returns The height of the latest Bitcoin block that was rekayed by the BTC-Relay
*/
getLatestBlockHeight(): Promise<number>;
/**
* @returns True if the block is in the relay, false otherwise.
*/
isBlockInRelay(blockHash: string): Promise<boolean>;
}
export class DefaultBTCRelayAPI implements BTCRelayAPI {
constructor(private api: ApiPromise) { }
async getStableBitcoinConfirmations(): Promise<number> {
return this.api.query.btcRelay.stableBitcoinConfirmations().then((param) => param.toNumber());
}
async getStableParachainConfirmations(): Promise<number> {
return this.api.query.btcRelay.stableParachainConfirmations().then((param) => param.toNumber());
}
async getLatestBlock(): Promise<BitcoinH256Le> {
return await this.api.query.btcRelay.bestBlock();
}
async getLatestBlockHeight(): Promise<number> {
return (await this.api.query.btcRelay.bestBlockHeight()).toNumber();
}
async isBlockInRelay(blockHash: string): Promise<boolean> {
const value = await this.api.query.btcRelay.blockHeaders<BtcRelayRichBlockHeader>(addHexPrefix(blockHash));
return !value.isEmpty;
}
}