@atomiqlabs/chain-solana 13.5.13 → 13.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +73 -73
- package/dist/index.d.ts +81 -81
- package/dist/index.js +102 -102
- package/dist/node/index.d.ts +9 -9
- package/dist/node/index.js +13 -13
- package/dist/solana/SolanaChainType.d.ts +15 -15
- package/dist/solana/SolanaChainType.js +2 -2
- package/dist/solana/SolanaChains.d.ts +12 -12
- package/dist/solana/SolanaChains.js +45 -45
- package/dist/solana/SolanaInitializer.d.ts +94 -94
- package/dist/solana/SolanaInitializer.js +174 -174
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +222 -222
- package/dist/solana/btcrelay/SolanaBtcRelay.js +455 -455
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +84 -84
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +70 -70
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +92 -92
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +109 -109
- package/dist/solana/btcrelay/program/programIdl.json +671 -671
- package/dist/solana/chain/SolanaAction.d.ts +26 -26
- package/dist/solana/chain/SolanaAction.js +87 -87
- package/dist/solana/chain/SolanaChainInterface.d.ts +224 -224
- package/dist/solana/chain/SolanaChainInterface.js +275 -275
- package/dist/solana/chain/SolanaModule.d.ts +14 -14
- package/dist/solana/chain/SolanaModule.js +13 -13
- package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
- package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
- package/dist/solana/chain/modules/SolanaBlocks.d.ts +32 -32
- package/dist/solana/chain/modules/SolanaBlocks.js +78 -78
- package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
- package/dist/solana/chain/modules/SolanaEvents.js +238 -238
- package/dist/solana/chain/modules/SolanaFees.d.ts +189 -189
- package/dist/solana/chain/modules/SolanaFees.js +434 -434
- package/dist/solana/chain/modules/SolanaSignatures.d.ts +24 -24
- package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
- package/dist/solana/chain/modules/SolanaSlots.d.ts +33 -33
- package/dist/solana/chain/modules/SolanaSlots.js +72 -72
- package/dist/solana/chain/modules/SolanaTokens.d.ts +123 -123
- package/dist/solana/chain/modules/SolanaTokens.js +242 -242
- package/dist/solana/chain/modules/SolanaTransactions.d.ts +149 -149
- package/dist/solana/chain/modules/SolanaTransactions.js +445 -445
- package/dist/solana/connection/ConnectionWithRetries.d.ts +35 -35
- package/dist/solana/connection/ConnectionWithRetries.js +86 -71
- package/dist/solana/events/SolanaChainEvents.d.ts +45 -45
- package/dist/solana/events/SolanaChainEvents.js +108 -108
- package/dist/solana/events/SolanaChainEventsBrowser.d.ts +205 -205
- package/dist/solana/events/SolanaChainEventsBrowser.js +404 -404
- package/dist/solana/program/SolanaProgramBase.d.ts +73 -73
- package/dist/solana/program/SolanaProgramBase.js +54 -54
- package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
- package/dist/solana/program/SolanaProgramModule.js +11 -11
- package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
- package/dist/solana/program/modules/SolanaProgramEvents.js +117 -117
- package/dist/solana/swaps/SolanaSwapData.d.ts +333 -333
- package/dist/solana/swaps/SolanaSwapData.js +535 -535
- package/dist/solana/swaps/SolanaSwapModule.d.ts +11 -11
- package/dist/solana/swaps/SolanaSwapModule.js +12 -12
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +376 -376
- package/dist/solana/swaps/SolanaSwapProgram.js +769 -769
- package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
- package/dist/solana/swaps/SwapTypeEnum.js +43 -43
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +95 -95
- package/dist/solana/swaps/modules/SolanaDataAccount.js +232 -232
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +69 -69
- package/dist/solana/swaps/modules/SolanaLpVault.js +171 -171
- package/dist/solana/swaps/modules/SwapClaim.d.ts +126 -126
- package/dist/solana/swaps/modules/SwapClaim.js +294 -294
- package/dist/solana/swaps/modules/SwapInit.d.ts +213 -213
- package/dist/solana/swaps/modules/SwapInit.js +658 -658
- package/dist/solana/swaps/modules/SwapRefund.d.ts +87 -87
- package/dist/solana/swaps/modules/SwapRefund.js +293 -293
- package/dist/solana/swaps/programIdl.json +945 -945
- package/dist/solana/swaps/programTypes.d.ts +943 -943
- package/dist/solana/swaps/programTypes.js +945 -945
- package/dist/solana/swaps/v1/programIdl.json +945 -945
- package/dist/solana/swaps/v1/programTypes.d.ts +943 -943
- package/dist/solana/swaps/v1/programTypes.js +945 -945
- package/dist/solana/swaps/v2/programIdl.json +952 -952
- package/dist/solana/swaps/v2/programTypes.d.ts +950 -950
- package/dist/solana/swaps/v2/programTypes.js +952 -952
- package/dist/solana/wallet/SolanaKeypairWallet.d.ts +29 -29
- package/dist/solana/wallet/SolanaKeypairWallet.js +50 -50
- package/dist/solana/wallet/SolanaSigner.d.ts +30 -30
- package/dist/solana/wallet/SolanaSigner.js +30 -30
- package/dist/utils/Utils.d.ts +58 -58
- package/dist/utils/Utils.js +170 -170
- package/node/index.d.ts +1 -1
- package/node/index.js +3 -3
- package/package.json +46 -46
- package/src/index.ts +87 -87
- package/src/node/index.ts +9 -9
- package/src/solana/SolanaChainType.ts +32 -32
- package/src/solana/SolanaChains.ts +46 -46
- package/src/solana/SolanaInitializer.ts +278 -278
- package/src/solana/btcrelay/SolanaBtcRelay.ts +615 -615
- package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +116 -116
- package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +148 -148
- package/src/solana/btcrelay/program/programIdl.json +670 -670
- package/src/solana/chain/SolanaAction.ts +109 -109
- package/src/solana/chain/SolanaChainInterface.ts +404 -404
- package/src/solana/chain/SolanaModule.ts +20 -20
- package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
- package/src/solana/chain/modules/SolanaBlocks.ts +89 -89
- package/src/solana/chain/modules/SolanaEvents.ts +271 -271
- package/src/solana/chain/modules/SolanaFees.ts +522 -522
- package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
- package/src/solana/chain/modules/SolanaSlots.ts +85 -85
- package/src/solana/chain/modules/SolanaTokens.ts +300 -300
- package/src/solana/chain/modules/SolanaTransactions.ts +503 -503
- package/src/solana/connection/ConnectionWithRetries.ts +113 -96
- package/src/solana/events/SolanaChainEvents.ts +127 -127
- package/src/solana/events/SolanaChainEventsBrowser.ts +495 -495
- package/src/solana/program/SolanaProgramBase.ts +119 -119
- package/src/solana/program/SolanaProgramModule.ts +15 -15
- package/src/solana/program/modules/SolanaProgramEvents.ts +157 -157
- package/src/solana/swaps/SolanaSwapData.ts +735 -735
- package/src/solana/swaps/SolanaSwapModule.ts +19 -19
- package/src/solana/swaps/SolanaSwapProgram.ts +1074 -1074
- package/src/solana/swaps/SwapTypeEnum.ts +30 -30
- package/src/solana/swaps/modules/SolanaDataAccount.ts +302 -302
- package/src/solana/swaps/modules/SolanaLpVault.ts +208 -208
- package/src/solana/swaps/modules/SwapClaim.ts +387 -387
- package/src/solana/swaps/modules/SwapInit.ts +785 -785
- package/src/solana/swaps/modules/SwapRefund.ts +353 -353
- package/src/solana/swaps/v1/programIdl.json +944 -944
- package/src/solana/swaps/v1/programTypes.ts +1885 -1885
- package/src/solana/swaps/v2/programIdl.json +951 -951
- package/src/solana/swaps/v2/programTypes.ts +1899 -1899
- package/src/solana/wallet/SolanaKeypairWallet.ts +56 -56
- package/src/solana/wallet/SolanaSigner.ts +43 -43
- package/src/utils/Utils.ts +194 -194
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
import {BtcHeader} from "@atomiqlabs/base";
|
|
2
|
-
import {Buffer} from "buffer";
|
|
3
|
-
|
|
4
|
-
export type SolanaBtcHeaderType = {
|
|
5
|
-
version: number,
|
|
6
|
-
reversedPrevBlockhash: number[],
|
|
7
|
-
merkleRoot: number[],
|
|
8
|
-
timestamp: number,
|
|
9
|
-
nbits: number,
|
|
10
|
-
nonce: number,
|
|
11
|
-
hash: Buffer
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Represents bitcoin blockheader data to be submitted to the Solana BTC relay program.
|
|
16
|
-
*
|
|
17
|
-
* @category BTC Relay
|
|
18
|
-
*/
|
|
19
|
-
export class SolanaBtcHeader implements BtcHeader {
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Version field of the blockheader.
|
|
23
|
-
*/
|
|
24
|
-
private readonly version: number;
|
|
25
|
-
/**
|
|
26
|
-
* Previous block hash in little-endian representation.
|
|
27
|
-
*/
|
|
28
|
-
private readonly reversedPrevBlockhash: number[];
|
|
29
|
-
/**
|
|
30
|
-
* Merkle root in little-endian representation.
|
|
31
|
-
*/
|
|
32
|
-
private readonly merkleRoot: number[];
|
|
33
|
-
/**
|
|
34
|
-
* Block timestamp in UNIX seconds.
|
|
35
|
-
*/
|
|
36
|
-
private readonly timestamp: number;
|
|
37
|
-
/**
|
|
38
|
-
* Compact target (`nBits`) field.
|
|
39
|
-
*/
|
|
40
|
-
private readonly nbits: number;
|
|
41
|
-
/**
|
|
42
|
-
* Nonce field.
|
|
43
|
-
*/
|
|
44
|
-
private readonly nonce: number;
|
|
45
|
-
/**
|
|
46
|
-
* Reversed block hash bytes.
|
|
47
|
-
*/
|
|
48
|
-
private readonly hash: Buffer;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Constructs the bitcoin blockheader
|
|
52
|
-
*
|
|
53
|
-
* @param obj Blockheader fields
|
|
54
|
-
*
|
|
55
|
-
* @internal
|
|
56
|
-
*/
|
|
57
|
-
constructor(obj: SolanaBtcHeaderType) {
|
|
58
|
-
this.version = obj.version;
|
|
59
|
-
this.reversedPrevBlockhash = obj.reversedPrevBlockhash;
|
|
60
|
-
this.merkleRoot = obj.merkleRoot;
|
|
61
|
-
this.timestamp = obj.timestamp;
|
|
62
|
-
this.nbits = obj.nbits;
|
|
63
|
-
this.nonce = obj.nonce;
|
|
64
|
-
this.hash = obj.hash;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @inheritDoc
|
|
69
|
-
*/
|
|
70
|
-
getMerkleRoot(): Buffer {
|
|
71
|
-
return Buffer.from(this.merkleRoot);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @inheritDoc
|
|
76
|
-
*/
|
|
77
|
-
getNbits(): number {
|
|
78
|
-
return this.nbits;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @inheritDoc
|
|
83
|
-
*/
|
|
84
|
-
getNonce(): number {
|
|
85
|
-
return this.nonce;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* @inheritDoc
|
|
90
|
-
*/
|
|
91
|
-
getReversedPrevBlockhash(): Buffer {
|
|
92
|
-
return Buffer.from(this.reversedPrevBlockhash);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @inheritDoc
|
|
97
|
-
*/
|
|
98
|
-
getTimestamp(): number {
|
|
99
|
-
return this.timestamp;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @inheritDoc
|
|
104
|
-
*/
|
|
105
|
-
getVersion(): number {
|
|
106
|
-
return this.version;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Returns block hash bytes in little-endian representation.
|
|
111
|
-
*/
|
|
112
|
-
getHash(): Buffer {
|
|
113
|
-
return this.hash;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
}
|
|
1
|
+
import {BtcHeader} from "@atomiqlabs/base";
|
|
2
|
+
import {Buffer} from "buffer";
|
|
3
|
+
|
|
4
|
+
export type SolanaBtcHeaderType = {
|
|
5
|
+
version: number,
|
|
6
|
+
reversedPrevBlockhash: number[],
|
|
7
|
+
merkleRoot: number[],
|
|
8
|
+
timestamp: number,
|
|
9
|
+
nbits: number,
|
|
10
|
+
nonce: number,
|
|
11
|
+
hash: Buffer
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Represents bitcoin blockheader data to be submitted to the Solana BTC relay program.
|
|
16
|
+
*
|
|
17
|
+
* @category BTC Relay
|
|
18
|
+
*/
|
|
19
|
+
export class SolanaBtcHeader implements BtcHeader {
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Version field of the blockheader.
|
|
23
|
+
*/
|
|
24
|
+
private readonly version: number;
|
|
25
|
+
/**
|
|
26
|
+
* Previous block hash in little-endian representation.
|
|
27
|
+
*/
|
|
28
|
+
private readonly reversedPrevBlockhash: number[];
|
|
29
|
+
/**
|
|
30
|
+
* Merkle root in little-endian representation.
|
|
31
|
+
*/
|
|
32
|
+
private readonly merkleRoot: number[];
|
|
33
|
+
/**
|
|
34
|
+
* Block timestamp in UNIX seconds.
|
|
35
|
+
*/
|
|
36
|
+
private readonly timestamp: number;
|
|
37
|
+
/**
|
|
38
|
+
* Compact target (`nBits`) field.
|
|
39
|
+
*/
|
|
40
|
+
private readonly nbits: number;
|
|
41
|
+
/**
|
|
42
|
+
* Nonce field.
|
|
43
|
+
*/
|
|
44
|
+
private readonly nonce: number;
|
|
45
|
+
/**
|
|
46
|
+
* Reversed block hash bytes.
|
|
47
|
+
*/
|
|
48
|
+
private readonly hash: Buffer;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Constructs the bitcoin blockheader
|
|
52
|
+
*
|
|
53
|
+
* @param obj Blockheader fields
|
|
54
|
+
*
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
constructor(obj: SolanaBtcHeaderType) {
|
|
58
|
+
this.version = obj.version;
|
|
59
|
+
this.reversedPrevBlockhash = obj.reversedPrevBlockhash;
|
|
60
|
+
this.merkleRoot = obj.merkleRoot;
|
|
61
|
+
this.timestamp = obj.timestamp;
|
|
62
|
+
this.nbits = obj.nbits;
|
|
63
|
+
this.nonce = obj.nonce;
|
|
64
|
+
this.hash = obj.hash;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @inheritDoc
|
|
69
|
+
*/
|
|
70
|
+
getMerkleRoot(): Buffer {
|
|
71
|
+
return Buffer.from(this.merkleRoot);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @inheritDoc
|
|
76
|
+
*/
|
|
77
|
+
getNbits(): number {
|
|
78
|
+
return this.nbits;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @inheritDoc
|
|
83
|
+
*/
|
|
84
|
+
getNonce(): number {
|
|
85
|
+
return this.nonce;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @inheritDoc
|
|
90
|
+
*/
|
|
91
|
+
getReversedPrevBlockhash(): Buffer {
|
|
92
|
+
return Buffer.from(this.reversedPrevBlockhash);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @inheritDoc
|
|
97
|
+
*/
|
|
98
|
+
getTimestamp(): number {
|
|
99
|
+
return this.timestamp;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @inheritDoc
|
|
104
|
+
*/
|
|
105
|
+
getVersion(): number {
|
|
106
|
+
return this.version;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns block hash bytes in little-endian representation.
|
|
111
|
+
*/
|
|
112
|
+
getHash(): Buffer {
|
|
113
|
+
return this.hash;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
}
|
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
import {BtcStoredHeader, StatePredictorUtils} from "@atomiqlabs/base";
|
|
2
|
-
import {SolanaBtcHeader, SolanaBtcHeaderType} from "./SolanaBtcHeader";
|
|
3
|
-
import {Buffer} from "buffer";
|
|
4
|
-
|
|
5
|
-
export type SolanaBtcStoredHeaderType = {
|
|
6
|
-
chainWork: number[],
|
|
7
|
-
header: SolanaBtcHeaderType | SolanaBtcHeader,
|
|
8
|
-
lastDiffAdjustment: number,
|
|
9
|
-
blockheight: number,
|
|
10
|
-
prevBlockTimestamps: number[]
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Represents a bitcoin blockheader that has already been stored and committed in the Solana BTC relay program.
|
|
15
|
-
*
|
|
16
|
-
* @category BTC Relay
|
|
17
|
-
*/
|
|
18
|
-
export class SolanaBtcStoredHeader implements BtcStoredHeader<SolanaBtcHeader> {
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Total accumulated chainwork for this header.
|
|
22
|
-
*/
|
|
23
|
-
private readonly chainWork: number[];
|
|
24
|
-
/**
|
|
25
|
-
* Stored bitcoin blockheader.
|
|
26
|
-
*/
|
|
27
|
-
private readonly header: SolanaBtcHeader;
|
|
28
|
-
/**
|
|
29
|
-
* Timestamp of the last difficulty adjustment.
|
|
30
|
-
*/
|
|
31
|
-
private readonly lastDiffAdjustment: number;
|
|
32
|
-
/**
|
|
33
|
-
* Blockheight of the stored header.
|
|
34
|
-
*/
|
|
35
|
-
private readonly blockheight: number;
|
|
36
|
-
/**
|
|
37
|
-
* Previous block timestamps tracked for median-time-past checks.
|
|
38
|
-
*/
|
|
39
|
-
private readonly prevBlockTimestamps: number[];
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Constructs the stored bitcoin blockheader from Solana event data.
|
|
43
|
-
*
|
|
44
|
-
* @param obj Decoded stored-header fields
|
|
45
|
-
*
|
|
46
|
-
* @internal
|
|
47
|
-
*/
|
|
48
|
-
constructor(obj: SolanaBtcStoredHeaderType) {
|
|
49
|
-
this.chainWork = obj.chainWork;
|
|
50
|
-
this.header = obj.header instanceof SolanaBtcHeader ? obj.header : new SolanaBtcHeader(obj.header);
|
|
51
|
-
this.lastDiffAdjustment = obj.lastDiffAdjustment;
|
|
52
|
-
this.blockheight = obj.blockheight;
|
|
53
|
-
this.prevBlockTimestamps = obj.prevBlockTimestamps;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @inheritDoc
|
|
58
|
-
*/
|
|
59
|
-
getBlockheight(): number {
|
|
60
|
-
return this.blockheight;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @inheritDoc
|
|
65
|
-
*/
|
|
66
|
-
getChainWork(): Buffer {
|
|
67
|
-
return Buffer.from(this.chainWork);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @inheritDoc
|
|
72
|
-
*/
|
|
73
|
-
getHeader(): SolanaBtcHeader {
|
|
74
|
-
return this.header;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @inheritDoc
|
|
79
|
-
*/
|
|
80
|
-
getLastDiffAdjustment(): number {
|
|
81
|
-
return this.lastDiffAdjustment;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @inheritDoc
|
|
86
|
-
*/
|
|
87
|
-
getPrevBlockTimestamps(): number[] {
|
|
88
|
-
return this.prevBlockTimestamps;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Computes prevBlockTimestamps for a next block, shifting the old block timestamps to the left & appending
|
|
93
|
-
* this block's timestamp to the end
|
|
94
|
-
*
|
|
95
|
-
* @private
|
|
96
|
-
*/
|
|
97
|
-
private computeNextBlockTimestamps(): number[] {
|
|
98
|
-
const prevBlockTimestamps = [...this.prevBlockTimestamps];
|
|
99
|
-
for(let i=1;i<10;i++) {
|
|
100
|
-
prevBlockTimestamps[i-1] = prevBlockTimestamps[i];
|
|
101
|
-
}
|
|
102
|
-
prevBlockTimestamps[9] = this.header.getTimestamp();
|
|
103
|
-
return prevBlockTimestamps;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Computes total chain work after a new header with "nbits" is added to the chain
|
|
108
|
-
*
|
|
109
|
-
* @param nbits
|
|
110
|
-
* @private
|
|
111
|
-
*/
|
|
112
|
-
private computeNextChainWork(nbits: number): number[] {
|
|
113
|
-
const chainWork = [...this.chainWork];
|
|
114
|
-
StatePredictorUtils.addInPlace(chainWork, [...StatePredictorUtils.getDifficulty(nbits)]);
|
|
115
|
-
return chainWork;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Computes lastDiffAdjustment, this changes only once every DIFF_ADJUSTMENT_PERIOD blocks
|
|
120
|
-
*
|
|
121
|
-
* @param headerTimestamp
|
|
122
|
-
* @private
|
|
123
|
-
*/
|
|
124
|
-
private computeNextLastDiffAdjustment(headerTimestamp: number) {
|
|
125
|
-
const blockheight = this.blockheight+1;
|
|
126
|
-
|
|
127
|
-
let lastDiffAdjustment = this.lastDiffAdjustment;
|
|
128
|
-
if(blockheight % StatePredictorUtils.DIFF_ADJUSTMENT_PERIOD === 0) {
|
|
129
|
-
lastDiffAdjustment = headerTimestamp;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return lastDiffAdjustment;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @inheritDoc
|
|
137
|
-
*/
|
|
138
|
-
computeNext(header: SolanaBtcHeader): SolanaBtcStoredHeader {
|
|
139
|
-
return new SolanaBtcStoredHeader({
|
|
140
|
-
chainWork: this.computeNextChainWork(header.getNbits()),
|
|
141
|
-
prevBlockTimestamps: this.computeNextBlockTimestamps(),
|
|
142
|
-
blockheight: this.blockheight+1,
|
|
143
|
-
lastDiffAdjustment: this.computeNextLastDiffAdjustment(header.getTimestamp()),
|
|
144
|
-
header
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
}
|
|
1
|
+
import {BtcStoredHeader, StatePredictorUtils} from "@atomiqlabs/base";
|
|
2
|
+
import {SolanaBtcHeader, SolanaBtcHeaderType} from "./SolanaBtcHeader";
|
|
3
|
+
import {Buffer} from "buffer";
|
|
4
|
+
|
|
5
|
+
export type SolanaBtcStoredHeaderType = {
|
|
6
|
+
chainWork: number[],
|
|
7
|
+
header: SolanaBtcHeaderType | SolanaBtcHeader,
|
|
8
|
+
lastDiffAdjustment: number,
|
|
9
|
+
blockheight: number,
|
|
10
|
+
prevBlockTimestamps: number[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Represents a bitcoin blockheader that has already been stored and committed in the Solana BTC relay program.
|
|
15
|
+
*
|
|
16
|
+
* @category BTC Relay
|
|
17
|
+
*/
|
|
18
|
+
export class SolanaBtcStoredHeader implements BtcStoredHeader<SolanaBtcHeader> {
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Total accumulated chainwork for this header.
|
|
22
|
+
*/
|
|
23
|
+
private readonly chainWork: number[];
|
|
24
|
+
/**
|
|
25
|
+
* Stored bitcoin blockheader.
|
|
26
|
+
*/
|
|
27
|
+
private readonly header: SolanaBtcHeader;
|
|
28
|
+
/**
|
|
29
|
+
* Timestamp of the last difficulty adjustment.
|
|
30
|
+
*/
|
|
31
|
+
private readonly lastDiffAdjustment: number;
|
|
32
|
+
/**
|
|
33
|
+
* Blockheight of the stored header.
|
|
34
|
+
*/
|
|
35
|
+
private readonly blockheight: number;
|
|
36
|
+
/**
|
|
37
|
+
* Previous block timestamps tracked for median-time-past checks.
|
|
38
|
+
*/
|
|
39
|
+
private readonly prevBlockTimestamps: number[];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Constructs the stored bitcoin blockheader from Solana event data.
|
|
43
|
+
*
|
|
44
|
+
* @param obj Decoded stored-header fields
|
|
45
|
+
*
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
constructor(obj: SolanaBtcStoredHeaderType) {
|
|
49
|
+
this.chainWork = obj.chainWork;
|
|
50
|
+
this.header = obj.header instanceof SolanaBtcHeader ? obj.header : new SolanaBtcHeader(obj.header);
|
|
51
|
+
this.lastDiffAdjustment = obj.lastDiffAdjustment;
|
|
52
|
+
this.blockheight = obj.blockheight;
|
|
53
|
+
this.prevBlockTimestamps = obj.prevBlockTimestamps;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
getBlockheight(): number {
|
|
60
|
+
return this.blockheight;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @inheritDoc
|
|
65
|
+
*/
|
|
66
|
+
getChainWork(): Buffer {
|
|
67
|
+
return Buffer.from(this.chainWork);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @inheritDoc
|
|
72
|
+
*/
|
|
73
|
+
getHeader(): SolanaBtcHeader {
|
|
74
|
+
return this.header;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @inheritDoc
|
|
79
|
+
*/
|
|
80
|
+
getLastDiffAdjustment(): number {
|
|
81
|
+
return this.lastDiffAdjustment;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @inheritDoc
|
|
86
|
+
*/
|
|
87
|
+
getPrevBlockTimestamps(): number[] {
|
|
88
|
+
return this.prevBlockTimestamps;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Computes prevBlockTimestamps for a next block, shifting the old block timestamps to the left & appending
|
|
93
|
+
* this block's timestamp to the end
|
|
94
|
+
*
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
private computeNextBlockTimestamps(): number[] {
|
|
98
|
+
const prevBlockTimestamps = [...this.prevBlockTimestamps];
|
|
99
|
+
for(let i=1;i<10;i++) {
|
|
100
|
+
prevBlockTimestamps[i-1] = prevBlockTimestamps[i];
|
|
101
|
+
}
|
|
102
|
+
prevBlockTimestamps[9] = this.header.getTimestamp();
|
|
103
|
+
return prevBlockTimestamps;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Computes total chain work after a new header with "nbits" is added to the chain
|
|
108
|
+
*
|
|
109
|
+
* @param nbits
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
private computeNextChainWork(nbits: number): number[] {
|
|
113
|
+
const chainWork = [...this.chainWork];
|
|
114
|
+
StatePredictorUtils.addInPlace(chainWork, [...StatePredictorUtils.getDifficulty(nbits)]);
|
|
115
|
+
return chainWork;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Computes lastDiffAdjustment, this changes only once every DIFF_ADJUSTMENT_PERIOD blocks
|
|
120
|
+
*
|
|
121
|
+
* @param headerTimestamp
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
private computeNextLastDiffAdjustment(headerTimestamp: number) {
|
|
125
|
+
const blockheight = this.blockheight+1;
|
|
126
|
+
|
|
127
|
+
let lastDiffAdjustment = this.lastDiffAdjustment;
|
|
128
|
+
if(blockheight % StatePredictorUtils.DIFF_ADJUSTMENT_PERIOD === 0) {
|
|
129
|
+
lastDiffAdjustment = headerTimestamp;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return lastDiffAdjustment;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @inheritDoc
|
|
137
|
+
*/
|
|
138
|
+
computeNext(header: SolanaBtcHeader): SolanaBtcStoredHeader {
|
|
139
|
+
return new SolanaBtcStoredHeader({
|
|
140
|
+
chainWork: this.computeNextChainWork(header.getNbits()),
|
|
141
|
+
prevBlockTimestamps: this.computeNextBlockTimestamps(),
|
|
142
|
+
blockheight: this.blockheight+1,
|
|
143
|
+
lastDiffAdjustment: this.computeNextLastDiffAdjustment(header.getTimestamp()),
|
|
144
|
+
header
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
}
|