feat: add minimumLedgerSlot api
This commit is contained in:
committed by
Michael Vines
parent
839e93480c
commit
6558e05fd0
1
web3.js/module.d.ts
vendored
1
web3.js/module.d.ts
vendored
@ -199,6 +199,7 @@ declare module '@solana/web3.js' {
|
|||||||
): Promise<RpcResponseAndContext<number>>;
|
): Promise<RpcResponseAndContext<number>>;
|
||||||
getBalance(publicKey: PublicKey, commitment?: Commitment): Promise<number>;
|
getBalance(publicKey: PublicKey, commitment?: Commitment): Promise<number>;
|
||||||
getBlockTime(slot: number): Promise<number | null>;
|
getBlockTime(slot: number): Promise<number | null>;
|
||||||
|
getMinimumLedgerSlot(): Promise<number>;
|
||||||
getClusterNodes(): Promise<Array<ContactInfo>>;
|
getClusterNodes(): Promise<Array<ContactInfo>>;
|
||||||
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
||||||
getConfirmedTransaction(
|
getConfirmedTransaction(
|
||||||
|
@ -212,6 +212,7 @@ declare module '@solana/web3.js' {
|
|||||||
): Promise<RpcResponseAndContext<number>>;
|
): Promise<RpcResponseAndContext<number>>;
|
||||||
getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise<number>;
|
getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise<number>;
|
||||||
getBlockTime(slot: number): Promise<number | null>;
|
getBlockTime(slot: number): Promise<number | null>;
|
||||||
|
getMinimumLedgerSlot(): Promise<number>;
|
||||||
getClusterNodes(): Promise<Array<ContactInfo>>;
|
getClusterNodes(): Promise<Array<ContactInfo>>;
|
||||||
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
||||||
getConfirmedTransaction(
|
getConfirmedTransaction(
|
||||||
|
@ -370,6 +370,16 @@ const GetBlockTimeRpcResult = struct({
|
|||||||
result: struct.union(['null', 'number']),
|
result: struct.union(['null', 'number']),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expected JSON RPC response for the "minimumLedgerSlot" message
|
||||||
|
*/
|
||||||
|
const MinimumLedgerSlotRpcResult = struct({
|
||||||
|
jsonrpc: struct.literal('2.0'),
|
||||||
|
id: 'string',
|
||||||
|
error: 'any?',
|
||||||
|
result: 'number',
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expected JSON RPC response for the "getVersion" message
|
* Expected JSON RPC response for the "getVersion" message
|
||||||
*/
|
*/
|
||||||
@ -964,6 +974,22 @@ export class Connection {
|
|||||||
return res.result;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the lowest slot that the node has information about in its ledger.
|
||||||
|
* This value may increase over time if the node is configured to purge older ledger data
|
||||||
|
*/
|
||||||
|
async getMinimumLedgerSlot(): Promise<number> {
|
||||||
|
const unsafeRes = await this._rpcRequest('minimumLedgerSlot', []);
|
||||||
|
const res = MinimumLedgerSlotRpcResult(unsafeRes);
|
||||||
|
if (res.error) {
|
||||||
|
throw new Error(
|
||||||
|
'failed to get minimum ledger slot: ' + res.error.message,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert(typeof res.result !== 'undefined');
|
||||||
|
return res.result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch all the account info for the specified public key, return with context
|
* Fetch all the account info for the specified public key, return with context
|
||||||
*/
|
*/
|
||||||
|
@ -1089,6 +1089,25 @@ test('get block time', async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('get minimum ledger slot', async () => {
|
||||||
|
const connection = new Connection(url);
|
||||||
|
|
||||||
|
mockRpc.push([
|
||||||
|
url,
|
||||||
|
{
|
||||||
|
method: 'minimumLedgerSlot',
|
||||||
|
params: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
error: null,
|
||||||
|
result: 0,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const minimumLedgerSlot = await connection.getMinimumLedgerSlot();
|
||||||
|
expect(minimumLedgerSlot).toBeGreaterThanOrEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
test('getVersion', async () => {
|
test('getVersion', async () => {
|
||||||
const connection = new Connection(url);
|
const connection = new Connection(url);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user