fix: add missing getSlot API

This commit is contained in:
Michael Vines
2019-08-02 16:06:54 -07:00
parent 60ac6fc5b7
commit 2eeaf668fa
3 changed files with 45 additions and 0 deletions

View File

@ -94,6 +94,7 @@ declare module '@solana/web3.js' {
getClusterNodes(): Promise<Array<ContactInfo>>; getClusterNodes(): Promise<Array<ContactInfo>>;
getEpochVoteAccounts(): Promise<Array<VoteAccountInfo>>; getEpochVoteAccounts(): Promise<Array<VoteAccountInfo>>;
confirmTransaction(signature: TransactionSignature): Promise<boolean>; confirmTransaction(signature: TransactionSignature): Promise<boolean>;
getSlot(): Promise<number>;
getSlotLeader(): Promise<string>; getSlotLeader(): Promise<string>;
getSignatureStatus( getSignatureStatus(
signature: TransactionSignature, signature: TransactionSignature,

View File

@ -160,6 +160,11 @@ const GetProgramAccountsRpcResult = jsonRpcResult(
*/ */
const ConfirmTransactionRpcResult = jsonRpcResult('boolean'); const ConfirmTransactionRpcResult = jsonRpcResult('boolean');
/**
* Expected JSON RPC response for the "getSlot" message
*/
const GetSlot = jsonRpcResult('number');
/** /**
* Expected JSON RPC response for the "getSlotLeader" message * Expected JSON RPC response for the "getSlotLeader" message
*/ */
@ -535,6 +540,20 @@ export class Connection {
return res.result; return res.result;
} }
/**
* Fetch the current slot that the node is processing
*/
async getSlot(): Promise<number> {
const unsafeRes = await this._rpcRequest('getSlot', []);
const res = GetSlot(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/** /**
* Fetch the current slot leader of the cluster * Fetch the current slot leader of the cluster
*/ */

View File

@ -128,6 +128,31 @@ test('get balance', async () => {
expect(balance).toBeGreaterThanOrEqual(0); expect(balance).toBeGreaterThanOrEqual(0);
}); });
test('get slot', async () => {
const connection = new Connection(url);
mockRpc.push([
url,
{
method: 'getSlot',
},
{
error: null,
result: 123,
},
]);
const slotLeader = await connection.getSlot();
if (mockRpcEnabled) {
expect(slotLeader).toBe(123);
} else {
// No idea what the correct slot value should be on a live cluster, so
// just check the type
expect(typeof slotLeader).toBe('number');
}
});
test('get slot leader', async () => { test('get slot leader', async () => {
const connection = new Connection(url); const connection = new Connection(url);