@atomiqlabs/chain-solana 13.2.1 → 13.2.3

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.
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" />
3
3
  import { BtcHeader } from "@atomiqlabs/base";
4
4
  import { Buffer } from "buffer";
5
- type SolanaBtcHeaderType = {
5
+ export type SolanaBtcHeaderType = {
6
6
  version: number;
7
7
  reversedPrevBlockhash: number[];
8
8
  merkleRoot: number[];
@@ -82,4 +82,3 @@ export declare class SolanaBtcHeader implements BtcHeader {
82
82
  */
83
83
  getHash(): Buffer;
84
84
  }
85
- export {};
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { BtcStoredHeader } from "@atomiqlabs/base";
4
- import { SolanaBtcHeader } from "./SolanaBtcHeader";
4
+ import { SolanaBtcHeader, SolanaBtcHeaderType } from "./SolanaBtcHeader";
5
5
  import { Buffer } from "buffer";
6
6
  export type SolanaBtcStoredHeaderType = {
7
7
  chainWork: number[];
8
- header: SolanaBtcHeader;
8
+ header: SolanaBtcHeaderType | SolanaBtcHeader;
9
9
  lastDiffAdjustment: number;
10
10
  blockheight: number;
11
11
  prevBlockTimestamps: number[];
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SolanaBtcStoredHeader = void 0;
4
4
  const base_1 = require("@atomiqlabs/base");
5
+ const SolanaBtcHeader_1 = require("./SolanaBtcHeader");
5
6
  const buffer_1 = require("buffer");
6
7
  /**
7
8
  * Represents a bitcoin blockheader that has already been stored and committed in the Solana BTC relay program.
@@ -18,7 +19,7 @@ class SolanaBtcStoredHeader {
18
19
  */
19
20
  constructor(obj) {
20
21
  this.chainWork = obj.chainWork;
21
- this.header = obj.header;
22
+ this.header = obj.header instanceof SolanaBtcHeader_1.SolanaBtcHeader ? obj.header : new SolanaBtcHeader_1.SolanaBtcHeader(obj.header);
22
23
  this.lastDiffAdjustment = obj.lastDiffAdjustment;
23
24
  this.blockheight = obj.blockheight;
24
25
  this.prevBlockTimestamps = obj.prevBlockTimestamps;
@@ -1,5 +1,5 @@
1
1
  import { SolanaModule } from "../SolanaModule";
2
- import { Commitment, ParsedAccountsModeBlockResponse } from "@solana/web3.js";
2
+ import { Finality, ParsedAccountsModeBlockResponse } from "@solana/web3.js";
3
3
  export type BlockChecked = ParsedAccountsModeBlockResponse & {
4
4
  blockTime: number;
5
5
  blockHeight: number;
@@ -19,7 +19,7 @@ export declare class SolanaBlocks extends SolanaModule {
19
19
  *
20
20
  * @param commitment
21
21
  */
22
- findLatestParsedBlock(commitment: Commitment): Promise<{
22
+ findLatestParsedBlock(commitment: Finality): Promise<{
23
23
  block: BlockChecked;
24
24
  slot: number;
25
25
  }>;
@@ -40,7 +40,7 @@ class SolanaBlocks extends SolanaModule_1.SolanaModule {
40
40
  */
41
41
  async findLatestParsedBlock(commitment) {
42
42
  let slot = await this.root.Slots.getSlot(commitment);
43
- for (let i = 0; i < 10; i++) {
43
+ for (let i = 0; i < 16; i++) {
44
44
  const block = await this.getParsedBlock(slot).catch(e => {
45
45
  const errorStr = e.toString();
46
46
  if (errorStr.startsWith("SolanaJSONRPCError: failed to get block:") && (errorStr.includes("Block not available for slot") ||
@@ -60,7 +60,7 @@ class SolanaBlocks extends SolanaModule_1.SolanaModule {
60
60
  }
61
61
  slot--;
62
62
  }
63
- throw new Error("Ran out of tries trying to find a parsedBlock");
63
+ throw new Error(`Ran out of tries trying to find a parsedBlock, last attempted slot: ${slot + 1}`);
64
64
  }
65
65
  /**
66
66
  * Gets parsed block for a given slot, uses block cache if the block was already fetched before
@@ -25,6 +25,8 @@ export declare class SolanaSlots extends SolanaModule {
25
25
  * Gets the slot for a given commitment, uses slot cache & tries to estimate current slot based on the cached
26
26
  * value, cache has relatively short expiry of just 12 slots (4.8 seconds)
27
27
  *
28
+ * @remarks Doesn't try to extrapolate the slot number for `finalized` commitment level
29
+ *
28
30
  * @param commitment
29
31
  */
30
32
  getSlot(commitment: Commitment): Promise<number>;
@@ -54,11 +54,15 @@ class SolanaSlots extends SolanaModule_1.SolanaModule {
54
54
  * Gets the slot for a given commitment, uses slot cache & tries to estimate current slot based on the cached
55
55
  * value, cache has relatively short expiry of just 12 slots (4.8 seconds)
56
56
  *
57
+ * @remarks Doesn't try to extrapolate the slot number for `finalized` commitment level
58
+ *
57
59
  * @param commitment
58
60
  */
59
61
  async getSlot(commitment) {
60
62
  let cachedSlotData = this.slotCache[commitment];
61
63
  if (cachedSlotData != null && Date.now() - cachedSlotData.timestamp < this.SLOT_CACHE_TIME) {
64
+ if (commitment === "finalized")
65
+ return await cachedSlotData.slot;
62
66
  return (await cachedSlotData.slot) + Math.floor((Date.now() - cachedSlotData.timestamp) / this.root._SLOT_TIME);
63
67
  }
64
68
  cachedSlotData = this.fetchAndSaveSlot(commitment);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-solana",
3
- "version": "13.2.1",
3
+ "version": "13.2.3",
4
4
  "description": "Solana specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -1,7 +1,7 @@
1
1
  import {BtcHeader} from "@atomiqlabs/base";
2
2
  import {Buffer} from "buffer";
3
3
 
4
- type SolanaBtcHeaderType = {
4
+ export type SolanaBtcHeaderType = {
5
5
  version: number,
6
6
  reversedPrevBlockhash: number[],
7
7
  merkleRoot: number[],
@@ -1,10 +1,10 @@
1
1
  import {BtcStoredHeader, StatePredictorUtils} from "@atomiqlabs/base";
2
- import {SolanaBtcHeader} from "./SolanaBtcHeader";
2
+ import {SolanaBtcHeader, SolanaBtcHeaderType} from "./SolanaBtcHeader";
3
3
  import {Buffer} from "buffer";
4
4
 
5
5
  export type SolanaBtcStoredHeaderType = {
6
6
  chainWork: number[],
7
- header: SolanaBtcHeader,
7
+ header: SolanaBtcHeaderType | SolanaBtcHeader,
8
8
  lastDiffAdjustment: number,
9
9
  blockheight: number,
10
10
  prevBlockTimestamps: number[]
@@ -47,7 +47,7 @@ export class SolanaBtcStoredHeader implements BtcStoredHeader<SolanaBtcHeader> {
47
47
  */
48
48
  constructor(obj: SolanaBtcStoredHeaderType) {
49
49
  this.chainWork = obj.chainWork;
50
- this.header = obj.header;
50
+ this.header = obj.header instanceof SolanaBtcHeader ? obj.header : new SolanaBtcHeader(obj.header);
51
51
  this.lastDiffAdjustment = obj.lastDiffAdjustment;
52
52
  this.blockheight = obj.blockheight;
53
53
  this.prevBlockTimestamps = obj.prevBlockTimestamps;
@@ -1,5 +1,5 @@
1
1
  import {SolanaModule} from "../SolanaModule";
2
- import {Commitment, ParsedAccountsModeBlockResponse} from "@solana/web3.js";
2
+ import {Commitment, Finality, ParsedAccountsModeBlockResponse} from "@solana/web3.js";
3
3
 
4
4
 
5
5
  export type BlockChecked = ParsedAccountsModeBlockResponse & {blockTime: number, blockHeight: number};
@@ -38,13 +38,13 @@ export class SolanaBlocks extends SolanaModule {
38
38
  *
39
39
  * @param commitment
40
40
  */
41
- public async findLatestParsedBlock(commitment: Commitment): Promise<{
41
+ public async findLatestParsedBlock(commitment: Finality): Promise<{
42
42
  block: BlockChecked,
43
43
  slot: number
44
44
  }> {
45
45
  let slot = await this.root.Slots.getSlot(commitment);
46
46
 
47
- for(let i=0;i<10;i++) {
47
+ for(let i=0;i<16;i++) {
48
48
  const block = await this.getParsedBlock(slot).catch(e => {
49
49
  const errorStr = e.toString();
50
50
  if(
@@ -71,7 +71,7 @@ export class SolanaBlocks extends SolanaModule {
71
71
  slot--;
72
72
  }
73
73
 
74
- throw new Error("Ran out of tries trying to find a parsedBlock");
74
+ throw new Error(`Ran out of tries trying to find a parsedBlock, last attempted slot: ${slot + 1}`);
75
75
  }
76
76
 
77
77
  /**
@@ -65,12 +65,15 @@ export class SolanaSlots extends SolanaModule {
65
65
  * Gets the slot for a given commitment, uses slot cache & tries to estimate current slot based on the cached
66
66
  * value, cache has relatively short expiry of just 12 slots (4.8 seconds)
67
67
  *
68
+ * @remarks Doesn't try to extrapolate the slot number for `finalized` commitment level
69
+ *
68
70
  * @param commitment
69
71
  */
70
72
  public async getSlot(commitment: Commitment): Promise<number> {
71
73
  let cachedSlotData = this.slotCache[commitment];
72
74
 
73
75
  if(cachedSlotData!=null && Date.now()-cachedSlotData.timestamp<this.SLOT_CACHE_TIME) {
76
+ if(commitment==="finalized") return await cachedSlotData.slot;
74
77
  return (await cachedSlotData.slot) + Math.floor((Date.now()-cachedSlotData.timestamp)/this.root._SLOT_TIME);
75
78
  }
76
79