2018-08-23 10:52:48 -07:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import assert from 'assert';
|
2018-11-04 11:41:21 -08:00
|
|
|
import {parse as urlParse, format as urlFormat} from 'url';
|
2018-08-23 10:52:48 -07:00
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import jayson from 'jayson/lib/client/browser';
|
|
|
|
import {struct} from 'superstruct';
|
2018-10-26 21:37:39 -07:00
|
|
|
import {Client as RpcWebSocketClient} from 'rpc-websockets';
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2020-01-02 17:54:43 -08:00
|
|
|
import {NonceAccount} from './nonce-account';
|
2018-09-30 18:42:45 -07:00
|
|
|
import {PublicKey} from './publickey';
|
2020-01-02 17:54:43 -08:00
|
|
|
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from './timing';
|
2019-03-05 09:53:56 -08:00
|
|
|
import {Transaction} from './transaction';
|
2018-10-22 15:31:56 -07:00
|
|
|
import {sleep} from './util/sleep';
|
2019-03-04 08:06:33 -08:00
|
|
|
import type {Blockhash} from './blockhash';
|
2019-06-12 14:36:05 -07:00
|
|
|
import type {FeeCalculator} from './fee-calculator';
|
2018-09-30 18:42:45 -07:00
|
|
|
import type {Account} from './account';
|
2019-03-04 08:06:33 -08:00
|
|
|
import type {TransactionSignature} from './transaction';
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-08-23 20:10:30 -07:00
|
|
|
type RpcRequest = (methodName: string, args: Array<any>) => any;
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2019-11-13 14:31:31 -08:00
|
|
|
type RpcResponseAndContext<T> = {
|
|
|
|
context: {
|
|
|
|
slot: number,
|
|
|
|
},
|
|
|
|
value: T,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function jsonRpcResultAndContext(resultDescription: any) {
|
2020-01-08 12:59:58 -08:00
|
|
|
return jsonRpcResult({
|
|
|
|
context: struct({
|
|
|
|
slot: 'number',
|
2019-11-13 14:31:31 -08:00
|
|
|
}),
|
2020-01-08 12:59:58 -08:00
|
|
|
value: resultDescription,
|
|
|
|
});
|
2019-11-13 14:31:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function jsonRpcResult(resultDescription: any) {
|
|
|
|
const jsonRpcVersion = struct.literal('2.0');
|
|
|
|
return struct.union([
|
|
|
|
struct({
|
|
|
|
jsonrpc: jsonRpcVersion,
|
|
|
|
id: 'string',
|
|
|
|
error: 'any',
|
|
|
|
}),
|
|
|
|
struct({
|
|
|
|
jsonrpc: jsonRpcVersion,
|
|
|
|
id: 'string',
|
|
|
|
error: 'null?',
|
|
|
|
result: resultDescription,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-11-11 10:01:10 -08:00
|
|
|
/**
|
|
|
|
* The level of commitment desired when querying state
|
|
|
|
* 'max': Query the most recent block which has reached max voter lockout
|
|
|
|
* 'recent': Query the most recent block
|
|
|
|
*
|
|
|
|
* @typedef {'max' | 'recent'} Commitment
|
|
|
|
*/
|
|
|
|
export type Commitment = 'max' | 'recent';
|
|
|
|
|
2019-04-23 09:53:26 -07:00
|
|
|
/**
|
|
|
|
* Information describing a cluster node
|
|
|
|
*
|
|
|
|
* @typedef {Object} ContactInfo
|
2019-06-12 10:26:55 -07:00
|
|
|
* @property {string} pubkey Identity public key of the node
|
2019-04-23 09:53:26 -07:00
|
|
|
* @property {string} gossip Gossip network address for the node
|
|
|
|
* @property {string} tpu TPU network address for the node (null if not available)
|
|
|
|
* @property {string|null} rpc JSON RPC network address for the node (null if not available)
|
|
|
|
*/
|
|
|
|
type ContactInfo = {
|
2019-06-12 10:26:55 -07:00
|
|
|
pubkey: string,
|
2019-04-23 09:53:26 -07:00
|
|
|
gossip: string,
|
|
|
|
tpu: string | null,
|
|
|
|
rpc: string | null,
|
|
|
|
};
|
|
|
|
|
2019-06-12 10:26:55 -07:00
|
|
|
/**
|
|
|
|
* Information describing a vote account
|
|
|
|
*
|
|
|
|
* @typedef {Object} VoteAccountInfo
|
|
|
|
* @property {string} votePubkey Public key of the vote account
|
|
|
|
* @property {string} nodePubkey Identity public key of the node voting with this account
|
2019-08-19 10:39:08 -07:00
|
|
|
* @property {number} activatedStake The stake, in lamports, delegated to this vote account and activated
|
|
|
|
* @property {boolean} epochVoteAccount Whether the vote account is staked for this epoch
|
2019-12-12 16:38:17 -08:00
|
|
|
* @property {Array<Array<number>>} epochCredits Recent epoch voting credit history for this voter
|
2019-12-03 17:54:32 -08:00
|
|
|
* @property {number} commission A percentage (0-100) of rewards payout owed to the voter
|
2019-08-19 10:39:08 -07:00
|
|
|
* @property {number} lastVote Most recent slot voted on by this vote account
|
2019-06-12 10:26:55 -07:00
|
|
|
*/
|
|
|
|
type VoteAccountInfo = {
|
|
|
|
votePubkey: string,
|
|
|
|
nodePubkey: string,
|
2019-08-19 10:39:08 -07:00
|
|
|
activatedStake: number,
|
|
|
|
epochVoteAccount: boolean,
|
2019-12-12 16:38:17 -08:00
|
|
|
epochCredits: Array<[number, number, number]>,
|
2019-06-12 10:26:55 -07:00
|
|
|
commission: number,
|
2019-08-19 10:39:08 -07:00
|
|
|
lastVote: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of cluster vote accounts
|
|
|
|
*
|
|
|
|
* @typedef {Object} VoteAccountStatus
|
|
|
|
* @property {Array<VoteAccountInfo>} current Active vote accounts
|
|
|
|
* @property {Array<VoteAccountInfo>} delinquent Inactive vote accounts
|
|
|
|
*/
|
|
|
|
type VoteAccountStatus = {
|
|
|
|
current: Array<VoteAccountInfo>,
|
|
|
|
delinquent: Array<VoteAccountInfo>,
|
2019-06-12 10:26:55 -07:00
|
|
|
};
|
|
|
|
|
2019-08-28 07:21:39 -07:00
|
|
|
/**
|
|
|
|
* Network Inflation parameters
|
2019-10-23 06:48:24 -07:00
|
|
|
* (see https://docs.solana.com/book/v/master/implemented-proposals/ed_overview)
|
2019-08-28 07:21:39 -07:00
|
|
|
*
|
2019-10-23 06:48:24 -07:00
|
|
|
* @typedef {Object} Inflation
|
2019-10-29 09:50:05 -07:00
|
|
|
* @property {number} foundation
|
2019-10-23 06:48:24 -07:00
|
|
|
* @property {number} foundation_term
|
|
|
|
* @property {number} initial
|
|
|
|
* @property {number} storage
|
|
|
|
* @property {number} taper
|
|
|
|
* @property {number} terminal
|
2019-08-28 07:21:39 -07:00
|
|
|
*/
|
|
|
|
const GetInflationResult = struct({
|
|
|
|
foundation: 'number',
|
|
|
|
foundation_term: 'number',
|
|
|
|
initial: 'number',
|
|
|
|
storage: 'number',
|
|
|
|
taper: 'number',
|
|
|
|
terminal: 'number',
|
|
|
|
});
|
|
|
|
|
2019-10-29 09:50:05 -07:00
|
|
|
/**
|
|
|
|
* EpochInfo parameters
|
|
|
|
* (see https://docs.solana.com/book/v/master/terminology#epoch)
|
|
|
|
*
|
|
|
|
* @typedef {Object} EpochInfo
|
|
|
|
* @property {number} epoch
|
|
|
|
* @property {number} slotIndex
|
|
|
|
* @property {number} slotsInEpoch
|
|
|
|
* @property {number} absoluteSlot
|
|
|
|
*/
|
|
|
|
const GetEpochInfoResult = struct({
|
|
|
|
epoch: 'number',
|
|
|
|
slotIndex: 'number',
|
|
|
|
slotsInEpoch: 'number',
|
|
|
|
absoluteSlot: 'number',
|
|
|
|
});
|
|
|
|
|
2019-10-23 06:48:24 -07:00
|
|
|
/**
|
|
|
|
* EpochSchedule parameters
|
|
|
|
* (see https://docs.solana.com/book/v/master/terminology#epoch)
|
2019-10-29 09:50:05 -07:00
|
|
|
*
|
2019-10-23 06:48:24 -07:00
|
|
|
* @typedef {Object} EpochSchedule
|
|
|
|
* @property {number} slots_per_epoch
|
|
|
|
* @property {number} leader_schedule_slot_offset
|
|
|
|
* @property {boolean} warmup
|
|
|
|
* @property {number} first_normal_epoch
|
|
|
|
* @property {number} first_normal_slot
|
|
|
|
*/
|
|
|
|
const GetEpochScheduleResult = struct({
|
|
|
|
slots_per_epoch: 'number',
|
|
|
|
leader_schedule_slot_offset: 'number',
|
|
|
|
warmup: 'boolean',
|
|
|
|
first_normal_epoch: 'number',
|
|
|
|
first_normal_slot: 'number',
|
|
|
|
});
|
|
|
|
|
2019-11-11 17:09:00 -08:00
|
|
|
/**
|
|
|
|
* Version info for a node
|
|
|
|
*
|
|
|
|
* @typedef {Object} Version
|
|
|
|
* @property {string} solana-core Version of solana-core
|
|
|
|
*/
|
|
|
|
const Version = struct({
|
|
|
|
'solana-core': 'string',
|
|
|
|
});
|
|
|
|
|
2019-11-19 14:38:06 -08:00
|
|
|
/**
|
|
|
|
* A ConfirmedBlock on the ledger
|
|
|
|
*
|
|
|
|
* @typedef {Object} ConfirmedBlock
|
|
|
|
* @property {Blockhash} blockhash Blockhash of this block
|
|
|
|
* @property {Blockhash} previousBlockhash Blockhash of this block's parent
|
|
|
|
* @property {number} parentSlot Slot index of this block's parent
|
|
|
|
* @property {Array<Array<object>>} transactions Vector of transactions paired with statuses
|
|
|
|
*/
|
|
|
|
type ConfirmedBlock = {
|
|
|
|
blockhash: Blockhash,
|
|
|
|
previousBlockhash: Blockhash,
|
|
|
|
parentSlot: number,
|
|
|
|
transactions: Array<[Transaction, GetSignatureStatusRpcResult]>,
|
|
|
|
};
|
|
|
|
|
2018-08-23 10:52:48 -07:00
|
|
|
function createRpcRequest(url): RpcRequest {
|
2018-11-04 11:41:21 -08:00
|
|
|
const server = jayson(async (request, callback) => {
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
body: request,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(url, options);
|
|
|
|
const text = await res.text();
|
|
|
|
callback(null, text);
|
|
|
|
} catch (err) {
|
|
|
|
callback(err);
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
2018-11-04 11:41:21 -08:00
|
|
|
});
|
2018-08-23 10:52:48 -07:00
|
|
|
|
|
|
|
return (method, args) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
server.request(method, args, (err, response) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:21:39 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getInflation" message
|
|
|
|
*/
|
|
|
|
const GetInflationRpcResult = struct({
|
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
|
|
|
result: GetInflationResult,
|
|
|
|
});
|
|
|
|
|
2019-10-29 09:50:05 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getEpochInfo" message
|
|
|
|
*/
|
|
|
|
const GetEpochInfoRpcResult = struct({
|
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
|
|
|
result: GetEpochInfoResult,
|
|
|
|
});
|
|
|
|
|
2019-10-23 06:48:24 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getEpochSchedule" message
|
|
|
|
*/
|
|
|
|
const GetEpochScheduleRpcResult = struct({
|
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
|
|
|
result: GetEpochScheduleResult,
|
|
|
|
});
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getBalance" message
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
const GetBalanceAndContextRpcResult = jsonRpcResultAndContext('number?');
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2019-11-11 17:09:00 -08:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getVersion" message
|
|
|
|
*/
|
|
|
|
const GetVersionRpcResult = struct({
|
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
|
|
|
result: Version,
|
|
|
|
});
|
|
|
|
|
2018-09-20 15:08:52 -07:00
|
|
|
/**
|
2018-10-26 21:37:39 -07:00
|
|
|
* @private
|
2018-09-20 15:08:52 -07:00
|
|
|
*/
|
2018-10-26 21:37:39 -07:00
|
|
|
const AccountInfoResult = struct({
|
2018-10-17 09:35:24 -07:00
|
|
|
executable: 'boolean',
|
2018-11-14 10:06:13 -08:00
|
|
|
owner: 'array',
|
2019-03-05 17:52:13 -08:00
|
|
|
lamports: 'number',
|
2019-03-14 13:27:47 -07:00
|
|
|
data: 'array',
|
2019-08-24 23:30:10 -07:00
|
|
|
rent_epoch: 'number?',
|
2018-09-20 15:08:52 -07:00
|
|
|
});
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getAccountInfo" message
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
const GetAccountInfoAndContextRpcResult = jsonRpcResultAndContext(
|
|
|
|
struct.union(['null', AccountInfoResult]),
|
|
|
|
);
|
2018-10-26 21:37:39 -07:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Expected JSON RPC response for the "accountNotification" message
|
|
|
|
*/
|
|
|
|
const AccountNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
|
|
|
result: AccountInfoResult,
|
|
|
|
});
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ProgramAccountInfoResult = struct(['string', AccountInfoResult]);
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Expected JSON RPC response for the "programNotification" message
|
|
|
|
*/
|
|
|
|
const ProgramAccountNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
|
|
|
result: ProgramAccountInfoResult,
|
|
|
|
});
|
|
|
|
|
2019-11-25 08:04:35 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const SlotInfo = struct({
|
|
|
|
parent: 'number',
|
|
|
|
slot: 'number',
|
|
|
|
root: 'number',
|
|
|
|
});
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Expected JSON RPC response for the "slotNotification" message
|
|
|
|
*/
|
|
|
|
const SlotNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
|
|
|
result: SlotInfo,
|
|
|
|
});
|
|
|
|
|
2019-06-28 18:28:06 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getProgramAccounts" message
|
|
|
|
*/
|
|
|
|
const GetProgramAccountsRpcResult = jsonRpcResult(
|
|
|
|
struct.list([ProgramAccountInfoResult]),
|
|
|
|
);
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "confirmTransaction" message
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
const ConfirmTransactionAndContextRpcResult = jsonRpcResultAndContext(
|
|
|
|
'boolean',
|
|
|
|
);
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2019-08-02 16:06:54 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getSlot" message
|
|
|
|
*/
|
|
|
|
const GetSlot = jsonRpcResult('number');
|
|
|
|
|
2019-04-23 09:53:26 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getSlotLeader" message
|
|
|
|
*/
|
|
|
|
const GetSlotLeader = jsonRpcResult('string');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getClusterNodes" message
|
|
|
|
*/
|
|
|
|
const GetClusterNodes = jsonRpcResult(
|
|
|
|
struct.list([
|
|
|
|
struct({
|
2019-06-12 10:26:55 -07:00
|
|
|
pubkey: 'string',
|
2019-04-23 09:53:26 -07:00
|
|
|
gossip: 'string',
|
|
|
|
tpu: struct.union(['null', 'string']),
|
|
|
|
rpc: struct.union(['null', 'string']),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2019-06-12 10:26:55 -07:00
|
|
|
/**
|
2019-08-19 10:39:08 -07:00
|
|
|
* Expected JSON RPC response for the "getVoteAccounts" message
|
2019-06-12 10:26:55 -07:00
|
|
|
*/
|
2019-08-19 10:39:08 -07:00
|
|
|
const GetVoteAccounts = jsonRpcResult(
|
|
|
|
struct({
|
|
|
|
current: struct.list([
|
|
|
|
struct({
|
|
|
|
votePubkey: 'string',
|
|
|
|
nodePubkey: 'string',
|
|
|
|
activatedStake: 'number',
|
|
|
|
epochVoteAccount: 'boolean',
|
2019-12-12 22:46:50 -08:00
|
|
|
epochCredits: struct.list([
|
2019-12-12 16:38:17 -08:00
|
|
|
struct.tuple(['number', 'number', 'number']),
|
2019-12-12 22:46:50 -08:00
|
|
|
]),
|
2019-08-19 10:39:08 -07:00
|
|
|
commission: 'number',
|
|
|
|
lastVote: 'number',
|
2019-10-15 13:06:18 -07:00
|
|
|
rootSlot: 'number?',
|
2019-08-19 10:39:08 -07:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
delinquent: struct.list([
|
|
|
|
struct({
|
|
|
|
votePubkey: 'string',
|
|
|
|
nodePubkey: 'string',
|
|
|
|
activatedStake: 'number',
|
|
|
|
epochVoteAccount: 'boolean',
|
2019-12-12 22:46:50 -08:00
|
|
|
epochCredits: struct.list([
|
2019-12-12 16:38:17 -08:00
|
|
|
struct.tuple(['number', 'number', 'number']),
|
2019-12-12 22:46:50 -08:00
|
|
|
]),
|
2019-08-19 10:39:08 -07:00
|
|
|
commission: 'number',
|
|
|
|
lastVote: 'number',
|
2019-10-15 13:06:18 -07:00
|
|
|
rootSlot: 'number?',
|
2019-08-19 10:39:08 -07:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
2019-06-12 10:26:55 -07:00
|
|
|
);
|
|
|
|
|
2019-11-26 00:34:42 -08:00
|
|
|
const SignatureStatusResult = struct.union([
|
|
|
|
'null',
|
|
|
|
struct.union([struct({Ok: 'null'}), struct({Err: 'object'})]),
|
|
|
|
]);
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getSignatureStatus" message
|
|
|
|
*/
|
2019-11-26 00:34:42 -08:00
|
|
|
const GetSignatureStatusRpcResult = jsonRpcResult(SignatureStatusResult);
|
2018-09-26 19:16:17 -07:00
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTransactionCount" message
|
|
|
|
*/
|
2018-09-26 19:54:59 -07:00
|
|
|
const GetTransactionCountRpcResult = jsonRpcResult('number');
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2019-06-25 08:31:22 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTotalSupply" message
|
|
|
|
*/
|
|
|
|
const GetTotalSupplyRpcResult = jsonRpcResult('number');
|
|
|
|
|
2019-09-26 13:13:57 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getMinimumBalanceForRentExemption" message
|
|
|
|
*/
|
|
|
|
const GetMinimumBalanceForRentExemptionRpcResult = jsonRpcResult('number');
|
|
|
|
|
2019-11-11 11:08:00 -08:00
|
|
|
/**
|
2019-11-16 08:28:14 -08:00
|
|
|
* Expected JSON RPC response for the "getConfirmedBlock" message
|
2019-11-11 11:08:00 -08:00
|
|
|
*/
|
2019-11-16 08:28:14 -08:00
|
|
|
export const GetConfirmedBlockRpcResult = jsonRpcResult(
|
2019-11-19 14:38:06 -08:00
|
|
|
struct({
|
2020-01-13 15:17:32 -08:00
|
|
|
blockhash: 'string',
|
|
|
|
previousBlockhash: 'string',
|
2019-11-19 14:38:06 -08:00
|
|
|
parentSlot: 'number',
|
|
|
|
transactions: struct.list([
|
|
|
|
struct.tuple([
|
|
|
|
struct({
|
2020-01-13 15:17:32 -08:00
|
|
|
signatures: struct.list(['string']),
|
2019-11-19 14:38:06 -08:00
|
|
|
message: struct({
|
2020-01-13 15:17:32 -08:00
|
|
|
accountKeys: struct.list(['string']),
|
2019-11-19 14:38:06 -08:00
|
|
|
header: struct({
|
|
|
|
numRequiredSignatures: 'number',
|
|
|
|
numReadonlySignedAccounts: 'number',
|
|
|
|
numReadonlyUnsignedAccounts: 'number',
|
|
|
|
}),
|
|
|
|
instructions: struct.list([
|
|
|
|
struct.union([
|
|
|
|
struct.list(['number']),
|
|
|
|
struct({
|
2020-01-13 15:17:32 -08:00
|
|
|
accounts: struct.list(['number']),
|
|
|
|
data: 'string',
|
2019-11-19 14:38:06 -08:00
|
|
|
programIdIndex: 'number',
|
|
|
|
}),
|
|
|
|
]),
|
2019-11-16 08:28:14 -08:00
|
|
|
]),
|
2020-01-13 15:17:32 -08:00
|
|
|
recentBlockhash: 'string',
|
2019-11-19 14:38:06 -08:00
|
|
|
}),
|
2019-11-16 08:28:14 -08:00
|
|
|
}),
|
2019-11-19 14:38:06 -08:00
|
|
|
struct.union([
|
|
|
|
'null',
|
|
|
|
struct({
|
2019-11-26 00:34:42 -08:00
|
|
|
status: SignatureStatusResult,
|
2019-11-19 14:38:06 -08:00
|
|
|
fee: 'number',
|
2019-12-26 09:38:59 -08:00
|
|
|
preBalances: struct.list(['number']),
|
|
|
|
postBalances: struct.list(['number']),
|
2019-11-19 14:38:06 -08:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
]),
|
2019-11-12 08:21:19 -08:00
|
|
|
]),
|
2019-11-19 14:38:06 -08:00
|
|
|
}),
|
2019-11-12 08:21:19 -08:00
|
|
|
);
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2019-03-04 08:06:33 -08:00
|
|
|
* Expected JSON RPC response for the "getRecentBlockhash" message
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext([
|
2019-06-12 14:36:05 -07:00
|
|
|
'string',
|
|
|
|
struct({
|
2019-06-26 13:49:29 -07:00
|
|
|
burnPercent: 'number',
|
2019-06-12 14:36:05 -07:00
|
|
|
lamportsPerSignature: 'number',
|
2019-06-21 16:38:16 -07:00
|
|
|
maxLamportsPerSignature: 'number',
|
|
|
|
minLamportsPerSignature: 'number',
|
2019-06-12 14:36:05 -07:00
|
|
|
targetLamportsPerSignature: 'number',
|
|
|
|
targetSignaturesPerSlot: 'number',
|
|
|
|
}),
|
|
|
|
]);
|
2019-11-13 14:31:31 -08:00
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "requestAirdrop" message
|
|
|
|
*/
|
2018-09-26 19:54:59 -07:00
|
|
|
const RequestAirdropRpcResult = jsonRpcResult('string');
|
2018-08-24 09:05:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "sendTransaction" message
|
|
|
|
*/
|
2018-11-28 11:56:50 -08:00
|
|
|
const SendTransactionRpcResult = jsonRpcResult('string');
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-09-20 15:08:52 -07:00
|
|
|
/**
|
|
|
|
* Information describing an account
|
2018-09-20 15:35:41 -07:00
|
|
|
*
|
|
|
|
* @typedef {Object} AccountInfo
|
2019-03-05 17:52:13 -08:00
|
|
|
* @property {number} lamports Number of lamports assigned to the account
|
2018-11-14 10:06:13 -08:00
|
|
|
* @property {PublicKey} owner Identifier of the program that owns the account
|
2019-03-14 13:27:47 -07:00
|
|
|
* @property {?Buffer} data Optional data assigned to the account
|
|
|
|
* @property {boolean} executable `true` if this account's data contains a loaded program
|
2018-09-20 15:08:52 -07:00
|
|
|
*/
|
|
|
|
type AccountInfo = {
|
2018-11-04 11:41:21 -08:00
|
|
|
executable: boolean,
|
2018-11-14 10:06:13 -08:00
|
|
|
owner: PublicKey,
|
2019-03-05 17:52:13 -08:00
|
|
|
lamports: number,
|
2019-03-14 13:27:47 -07:00
|
|
|
data: Buffer,
|
2018-11-04 11:41:21 -08:00
|
|
|
};
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/**
|
|
|
|
* Account information identified by pubkey
|
|
|
|
*
|
|
|
|
* @typedef {Object} KeyedAccountInfo
|
|
|
|
* @property {PublicKey} accountId
|
|
|
|
* @property {AccountInfo} accountInfo
|
|
|
|
*/
|
|
|
|
type KeyedAccountInfo = {
|
|
|
|
accountId: PublicKey,
|
|
|
|
accountInfo: AccountInfo,
|
|
|
|
};
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/**
|
|
|
|
* Callback function for account change notifications
|
|
|
|
*/
|
|
|
|
export type AccountChangeCallback = (accountInfo: AccountInfo) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type AccountSubscriptionInfo = {
|
2018-11-04 11:41:21 -08:00
|
|
|
publicKey: string, // PublicKey of the account as a base 58 string
|
2018-10-26 21:37:39 -07:00
|
|
|
callback: AccountChangeCallback,
|
2018-11-04 11:41:21 -08:00
|
|
|
subscriptionId: null | number, // null when there's no current server subscription id
|
|
|
|
};
|
2018-10-26 21:37:39 -07:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/**
|
|
|
|
* Callback function for program account change notifications
|
|
|
|
*/
|
|
|
|
export type ProgramAccountChangeCallback = (
|
|
|
|
keyedAccountInfo: KeyedAccountInfo,
|
|
|
|
) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type ProgramAccountSubscriptionInfo = {
|
|
|
|
programId: string, // PublicKey of the program as a base 58 string
|
|
|
|
callback: ProgramAccountChangeCallback,
|
|
|
|
subscriptionId: null | number, // null when there's no current server subscription id
|
|
|
|
};
|
|
|
|
|
2019-11-25 08:04:35 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type SlotSubscriptionInfo = {
|
|
|
|
callback: SlotChangeCallback,
|
|
|
|
subscriptionId: null | number, // null when there's no current server subscription id
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for slot change notifications
|
|
|
|
*/
|
|
|
|
export type SlotChangeCallback = (slotInfo: SlotInfo) => void;
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
2019-04-10 14:40:49 -07:00
|
|
|
* Signature status: Success
|
2018-09-26 19:16:17 -07:00
|
|
|
*
|
2019-04-10 14:40:49 -07:00
|
|
|
* @typedef {Object} SignatureSuccess
|
2018-09-26 19:16:17 -07:00
|
|
|
*/
|
2019-04-10 14:40:49 -07:00
|
|
|
export type SignatureSuccess = {|
|
|
|
|
Ok: null,
|
|
|
|
|};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signature status: TransactionError
|
|
|
|
*
|
|
|
|
* @typedef {Object} TransactionError
|
|
|
|
*/
|
|
|
|
export type TransactionError = {|
|
|
|
|
Err: Object,
|
|
|
|
|};
|
2018-09-26 19:16:17 -07:00
|
|
|
|
2019-06-12 14:36:05 -07:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
type BlockhashAndFeeCalculator = [Blockhash, FeeCalculator]; // This type exists to workaround an esdoc parse error
|
|
|
|
|
2019-06-28 18:28:06 -07:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
type PublicKeyAndAccount = [PublicKey, AccountInfo]; // This type exists to workaround an esdoc parse error
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* A connection to a fullnode JSON RPC endpoint
|
|
|
|
*/
|
2018-08-23 10:52:48 -07:00
|
|
|
export class Connection {
|
|
|
|
_rpcRequest: RpcRequest;
|
2018-10-26 21:37:39 -07:00
|
|
|
_rpcWebSocket: RpcWebSocketClient;
|
|
|
|
_rpcWebSocketConnected: boolean = false;
|
2018-10-22 20:03:44 -07:00
|
|
|
|
2019-11-11 10:01:10 -08:00
|
|
|
_commitment: ?Commitment;
|
2019-03-04 08:06:33 -08:00
|
|
|
_blockhashInfo: {
|
|
|
|
recentBlockhash: Blockhash | null,
|
2018-10-22 20:03:44 -07:00
|
|
|
seconds: number,
|
|
|
|
transactionSignatures: Array<string>,
|
|
|
|
};
|
2019-03-04 08:06:33 -08:00
|
|
|
_disableBlockhashCaching: boolean = false;
|
2018-10-26 21:37:39 -07:00
|
|
|
_accountChangeSubscriptions: {[number]: AccountSubscriptionInfo} = {};
|
|
|
|
_accountChangeSubscriptionCounter: number = 0;
|
2019-03-08 16:02:39 -08:00
|
|
|
_programAccountChangeSubscriptions: {
|
|
|
|
[number]: ProgramAccountSubscriptionInfo,
|
|
|
|
} = {};
|
|
|
|
_programAccountChangeSubscriptionCounter: number = 0;
|
2019-11-25 08:04:35 -08:00
|
|
|
_slotSubscriptions: {
|
|
|
|
[number]: SlotSubscriptionInfo,
|
|
|
|
} = {};
|
|
|
|
_slotSubscriptionCounter: number = 0;
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Establish a JSON RPC connection
|
|
|
|
*
|
|
|
|
* @param endpoint URL to the fullnode JSON RPC endpoint
|
2019-11-11 10:01:10 -08:00
|
|
|
* @param commitment optional default commitment level
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
constructor(endpoint: string, commitment: ?Commitment) {
|
2018-10-26 21:37:39 -07:00
|
|
|
let url = urlParse(endpoint);
|
|
|
|
|
|
|
|
this._rpcRequest = createRpcRequest(url.href);
|
2019-11-11 10:01:10 -08:00
|
|
|
this._commitment = commitment;
|
2019-03-04 08:06:33 -08:00
|
|
|
this._blockhashInfo = {
|
|
|
|
recentBlockhash: null,
|
2018-10-22 20:03:44 -07:00
|
|
|
seconds: -1,
|
|
|
|
transactionSignatures: [],
|
|
|
|
};
|
2018-10-26 21:37:39 -07:00
|
|
|
|
2018-11-02 16:09:34 -07:00
|
|
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
2018-10-26 21:37:39 -07:00
|
|
|
url.host = '';
|
|
|
|
url.port = String(Number(url.port) + 1);
|
2018-11-01 20:42:14 -07:00
|
|
|
if (url.port === '1') {
|
2018-11-02 16:09:34 -07:00
|
|
|
url.port = url.protocol === 'wss:' ? '8901' : '8900';
|
2018-11-01 20:42:14 -07:00
|
|
|
}
|
2018-11-04 11:41:21 -08:00
|
|
|
this._rpcWebSocket = new RpcWebSocketClient(urlFormat(url), {
|
|
|
|
autoconnect: false,
|
|
|
|
max_reconnects: Infinity,
|
|
|
|
});
|
2018-10-26 21:37:39 -07:00
|
|
|
this._rpcWebSocket.on('open', this._wsOnOpen.bind(this));
|
|
|
|
this._rpcWebSocket.on('error', this._wsOnError.bind(this));
|
|
|
|
this._rpcWebSocket.on('close', this._wsOnClose.bind(this));
|
2018-11-04 11:41:21 -08:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'accountNotification',
|
|
|
|
this._wsOnAccountNotification.bind(this),
|
|
|
|
);
|
2019-03-08 16:02:39 -08:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'programNotification',
|
|
|
|
this._wsOnProgramAccountNotification.bind(this),
|
|
|
|
);
|
2019-11-25 08:04:35 -08:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'slotNotification',
|
|
|
|
this._wsOnSlotNotification.bind(this),
|
|
|
|
);
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2020-01-08 13:44:50 -08:00
|
|
|
* Fetch the balance for the specified public key, return with context
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getBalanceAndContext(
|
2019-11-11 10:01:10 -08:00
|
|
|
publicKey: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
2019-11-13 14:31:31 -08:00
|
|
|
): Promise<RpcResponseAndContext<number>> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([publicKey.toBase58()], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getBalance', args);
|
2019-11-13 14:31:31 -08:00
|
|
|
const res = GetBalanceAndContextRpcResult(unsafeRes);
|
2018-08-23 10:52:48 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2020-01-08 12:59:58 -08:00
|
|
|
return res.result;
|
2019-11-13 14:31:31 -08:00
|
|
|
}
|
2020-01-08 13:44:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the balance for the specified public key
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getBalance(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<number> {
|
|
|
|
return await this.getBalanceAndContext(publicKey, commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2018-09-20 15:08:52 -07:00
|
|
|
/**
|
2020-01-08 13:44:50 -08:00
|
|
|
* Fetch all the account info for the specified public key, return with context
|
2018-09-20 15:08:52 -07:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getAccountInfoAndContext(
|
2019-11-11 10:01:10 -08:00
|
|
|
publicKey: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
2019-11-13 14:31:31 -08:00
|
|
|
): Promise<RpcResponseAndContext<AccountInfo>> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([publicKey.toBase58()], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
|
2019-11-13 14:31:31 -08:00
|
|
|
const res = GetAccountInfoAndContextRpcResult(unsafeRes);
|
2018-09-20 15:08:52 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
2019-11-13 14:31:31 -08:00
|
|
|
assert(typeof res.result !== 'undefined');
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2020-01-08 12:59:58 -08:00
|
|
|
if (!res.result.value) {
|
2019-11-13 14:31:31 -08:00
|
|
|
throw new Error('Invalid request');
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:59:58 -08:00
|
|
|
const {executable, owner, lamports, data} = res.result.value;
|
2019-11-13 14:31:31 -08:00
|
|
|
const value = {
|
|
|
|
executable,
|
|
|
|
owner: new PublicKey(owner),
|
|
|
|
lamports,
|
|
|
|
data: Buffer.from(data),
|
|
|
|
};
|
2018-09-20 15:08:52 -07:00
|
|
|
|
|
|
|
return {
|
2019-11-13 14:31:31 -08:00
|
|
|
context: {
|
2020-01-08 12:59:58 -08:00
|
|
|
slot: res.result.context.slot,
|
2019-11-13 14:31:31 -08:00
|
|
|
},
|
|
|
|
value,
|
2018-09-20 15:08:52 -07:00
|
|
|
};
|
|
|
|
}
|
2020-01-08 13:44:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all the account info for the specified public key
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getAccountInfo(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<AccountInfo> {
|
|
|
|
return await this.getAccountInfoAndContext(publicKey, commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2019-06-28 18:28:06 -07:00
|
|
|
/**
|
|
|
|
* Fetch all the accounts owned by the specified program id
|
|
|
|
*/
|
|
|
|
async getProgramAccounts(
|
|
|
|
programId: PublicKey,
|
2019-11-11 10:01:10 -08:00
|
|
|
commitment: ?Commitment,
|
2019-06-28 18:28:06 -07:00
|
|
|
): Promise<Array<PublicKeyAndAccount>> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([programId.toBase58()], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
|
2019-06-28 18:28:06 -07:00
|
|
|
const res = GetProgramAccountsRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {result} = res;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
return result.map(result => {
|
|
|
|
return [
|
|
|
|
result[0],
|
|
|
|
{
|
|
|
|
executable: result[1].executable,
|
|
|
|
owner: new PublicKey(result[1].owner),
|
|
|
|
lamports: result[1].lamports,
|
|
|
|
data: Buffer.from(result[1].data),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2020-01-08 13:44:50 -08:00
|
|
|
* Confirm the transaction identified by the specified signature, return with context
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async confirmTransactionAndContext(
|
2019-11-11 10:01:10 -08:00
|
|
|
signature: TransactionSignature,
|
|
|
|
commitment: ?Commitment,
|
2019-11-13 14:31:31 -08:00
|
|
|
): Promise<RpcResponseAndContext<boolean>> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([signature], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('confirmTransaction', args);
|
2019-11-13 14:31:31 -08:00
|
|
|
const res = ConfirmTransactionAndContextRpcResult(unsafeRes);
|
2018-08-23 10:52:48 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2020-01-08 12:59:58 -08:00
|
|
|
return res.result;
|
2019-11-13 14:31:31 -08:00
|
|
|
}
|
2020-01-08 13:44:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirm the transaction identified by the specified signature
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async confirmTransaction(
|
|
|
|
signature: TransactionSignature,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<boolean> {
|
|
|
|
return await this.confirmTransactionAndContext(signature, commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2019-04-23 09:53:26 -07:00
|
|
|
/**
|
2019-06-12 10:26:55 -07:00
|
|
|
* Return the list of nodes that are currently participating in the cluster
|
2019-04-23 09:53:26 -07:00
|
|
|
*/
|
2019-06-12 10:26:55 -07:00
|
|
|
async getClusterNodes(): Promise<Array<ContactInfo>> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getClusterNodes', []);
|
2019-06-12 19:24:45 -07:00
|
|
|
|
2019-06-12 10:26:55 -07:00
|
|
|
const res = GetClusterNodes(unsafeRes);
|
2019-04-23 09:53:26 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-24 08:23:10 -07:00
|
|
|
* Return the list of nodes that are currently participating in the cluster
|
2019-04-23 09:53:26 -07:00
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getVoteAccounts(commitment: ?Commitment): Promise<VoteAccountStatus> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getVoteAccounts', args);
|
2019-08-19 10:39:08 -07:00
|
|
|
const res = GetVoteAccounts(unsafeRes);
|
2019-06-12 10:26:55 -07:00
|
|
|
//const res = unsafeRes;
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2019-08-09 06:04:34 -07:00
|
|
|
/**
|
2019-08-02 16:06:54 -07:00
|
|
|
* Fetch the current slot that the node is processing
|
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getSlot(commitment: ?Commitment): Promise<number> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getSlot', args);
|
2019-08-02 16:06:54 -07:00
|
|
|
const res = GetSlot(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2019-06-12 10:26:55 -07:00
|
|
|
/**
|
|
|
|
* Fetch the current slot leader of the cluster
|
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getSlotLeader(commitment: ?Commitment): Promise<string> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getSlotLeader', args);
|
2019-06-12 10:26:55 -07:00
|
|
|
const res = GetSlotLeader(unsafeRes);
|
2019-04-23 09:53:26 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
2019-03-04 08:06:33 -08:00
|
|
|
* Fetch the current transaction count of the cluster
|
2018-09-26 19:16:17 -07:00
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
async getSignatureStatus(
|
|
|
|
signature: TransactionSignature,
|
2019-11-11 10:01:10 -08:00
|
|
|
commitment: ?Commitment,
|
2019-04-10 14:40:49 -07:00
|
|
|
): Promise<SignatureSuccess | TransactionError | null> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([signature], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getSignatureStatus', args);
|
2018-09-26 19:16:17 -07:00
|
|
|
const res = GetSignatureStatusRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2019-03-04 08:06:33 -08:00
|
|
|
* Fetch the current transaction count of the cluster
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getTransactionCount(commitment: ?Commitment): Promise<number> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getTransactionCount', args);
|
2018-08-23 10:52:48 -07:00
|
|
|
const res = GetTransactionCountRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return Number(res.result);
|
|
|
|
}
|
|
|
|
|
2019-06-25 08:31:22 -07:00
|
|
|
/**
|
2019-06-29 08:48:45 -07:00
|
|
|
* Fetch the current total currency supply of the cluster in lamports
|
2019-06-25 08:31:22 -07:00
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getTotalSupply(commitment: ?Commitment): Promise<number> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getTotalSupply', args);
|
2019-06-25 08:31:22 -07:00
|
|
|
const res = GetTotalSupplyRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return Number(res.result);
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:21:39 -07:00
|
|
|
/**
|
2019-10-23 06:48:24 -07:00
|
|
|
* Fetch the cluster Inflation parameters
|
2019-08-28 07:21:39 -07:00
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getInflation(commitment: ?Commitment): Promise<GetInflationRpcResult> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getInflation', args);
|
2019-08-28 07:21:39 -07:00
|
|
|
const res = GetInflationRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return GetInflationResult(res.result);
|
|
|
|
}
|
|
|
|
|
2019-10-29 09:50:05 -07:00
|
|
|
/**
|
|
|
|
* Fetch the Epoch Info parameters
|
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getEpochInfo(commitment: ?Commitment): Promise<GetEpochInfoRpcResult> {
|
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getEpochInfo', args);
|
2019-10-29 09:50:05 -07:00
|
|
|
const res = GetEpochInfoRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return GetEpochInfoResult(res.result);
|
|
|
|
}
|
|
|
|
|
2019-10-23 06:48:24 -07:00
|
|
|
/**
|
|
|
|
* Fetch the Epoch Schedule parameters
|
|
|
|
*/
|
|
|
|
async getEpochSchedule(): Promise<GetEpochScheduleRpcResult> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getEpochSchedule', []);
|
|
|
|
const res = GetEpochScheduleRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return GetEpochScheduleResult(res.result);
|
|
|
|
}
|
|
|
|
|
2019-09-26 13:13:57 -07:00
|
|
|
/**
|
|
|
|
* Fetch the minimum balance needed to exempt an account of `dataLength`
|
|
|
|
* size from rent
|
|
|
|
*/
|
2019-11-11 10:01:10 -08:00
|
|
|
async getMinimumBalanceForRentExemption(
|
|
|
|
dataLength: number,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<number> {
|
|
|
|
const args = this._argsWithCommitment([dataLength], commitment);
|
2019-09-26 13:13:57 -07:00
|
|
|
const unsafeRes = await this._rpcRequest(
|
|
|
|
'getMinimumBalanceForRentExemption',
|
2019-11-11 10:01:10 -08:00
|
|
|
args,
|
2019-09-26 13:13:57 -07:00
|
|
|
);
|
|
|
|
const res = GetMinimumBalanceForRentExemptionRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2019-10-19 11:36:06 -07:00
|
|
|
console.warn('Unable to fetch minimum balance for rent exemption');
|
2019-09-29 10:14:18 -07:00
|
|
|
return 0;
|
2019-09-26 13:13:57 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return Number(res.result);
|
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2020-01-08 13:44:50 -08:00
|
|
|
* Fetch a recent blockhash from the cluster, return with context
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getRecentBlockhashAndContext(
|
2019-11-11 10:01:10 -08:00
|
|
|
commitment: ?Commitment,
|
2019-11-13 14:31:31 -08:00
|
|
|
): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getRecentBlockhash', args);
|
2019-05-10 20:28:39 -07:00
|
|
|
|
2019-11-13 14:31:31 -08:00
|
|
|
const res = GetRecentBlockhashAndContextRpcResult(unsafeRes);
|
2018-08-23 10:52:48 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2020-01-08 12:59:58 -08:00
|
|
|
return res.result;
|
2019-11-13 14:31:31 -08:00
|
|
|
}
|
2020-01-08 13:44:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a recent blockhash from the cluster
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getRecentBlockhash(
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<BlockhashAndFeeCalculator> {
|
|
|
|
return await this.getRecentBlockhashAndContext(commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
2019-11-11 11:08:00 -08:00
|
|
|
}
|
|
|
|
|
2019-11-11 17:09:00 -08:00
|
|
|
/**
|
|
|
|
* Fetch the node version
|
|
|
|
*/
|
|
|
|
async getVersion(): Promise<Version> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getVersion', []);
|
|
|
|
const res = GetVersionRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2019-06-12 14:36:05 -07:00
|
|
|
return res.result;
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2019-11-12 08:21:19 -08:00
|
|
|
/**
|
|
|
|
* Fetch a list of Transactions and transaction statuses from the cluster
|
2019-11-16 08:28:14 -08:00
|
|
|
* for a confirmed block
|
2019-11-12 08:21:19 -08:00
|
|
|
*/
|
2019-11-19 14:38:06 -08:00
|
|
|
async getConfirmedBlock(slot: number): Promise<ConfirmedBlock> {
|
2019-11-16 08:28:14 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('getConfirmedBlock', [slot]);
|
|
|
|
const result = GetConfirmedBlockRpcResult(unsafeRes);
|
2019-11-12 08:21:19 -08:00
|
|
|
if (result.error) {
|
|
|
|
throw new Error(result.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof result.result !== 'undefined');
|
2019-11-19 14:38:06 -08:00
|
|
|
return {
|
|
|
|
blockhash: new PublicKey(result.result.blockhash).toString(),
|
|
|
|
previousBlockhash: new PublicKey(
|
|
|
|
result.result.previousBlockhash,
|
|
|
|
).toString(),
|
|
|
|
parentSlot: result.result.parentSlot,
|
|
|
|
transactions: result.result.transactions.map(result => {
|
|
|
|
return [Transaction.fromRpcResult(result[0]), result[1]];
|
|
|
|
}),
|
|
|
|
};
|
2019-11-12 08:21:19 -08:00
|
|
|
}
|
|
|
|
|
2020-01-02 17:54:43 -08:00
|
|
|
/**
|
2020-01-08 13:44:50 -08:00
|
|
|
* Fetch the contents of a Nonce account from the cluster, return with context
|
2020-01-02 17:54:43 -08:00
|
|
|
*/
|
|
|
|
async getNonceAndContext(
|
|
|
|
nonceAccount: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<RpcResponseAndContext<NonceAccount>> {
|
|
|
|
const args = this._argsWithCommitment(
|
|
|
|
[nonceAccount.toBase58()],
|
|
|
|
commitment,
|
|
|
|
);
|
|
|
|
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
|
|
|
|
const res = GetAccountInfoAndContextRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2020-01-08 12:59:58 -08:00
|
|
|
if (!res.result.value) {
|
2020-01-02 17:54:43 -08:00
|
|
|
throw new Error('Invalid request');
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:59:58 -08:00
|
|
|
const value = NonceAccount.fromAccountData(
|
|
|
|
Buffer.from(res.result.value.data),
|
|
|
|
);
|
2020-01-02 17:54:43 -08:00
|
|
|
|
|
|
|
return {
|
|
|
|
context: {
|
2020-01-08 12:59:58 -08:00
|
|
|
slot: res.result.context.slot,
|
2020-01-02 17:54:43 -08:00
|
|
|
},
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
2020-01-08 13:44:50 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the contents of a Nonce account from the cluster
|
|
|
|
*/
|
2020-01-02 17:54:43 -08:00
|
|
|
async getNonce(
|
|
|
|
nonceAccount: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<NonceAccount> {
|
|
|
|
return await this.getNonceAndContext(nonceAccount, commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2019-03-05 17:52:13 -08:00
|
|
|
* Request an allocation of lamports to the specified account
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
async requestAirdrop(
|
|
|
|
to: PublicKey,
|
|
|
|
amount: number,
|
2019-11-11 10:01:10 -08:00
|
|
|
commitment: ?Commitment,
|
2018-11-04 11:41:21 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const args = this._argsWithCommitment([to.toBase58(), amount], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('requestAirdrop', args);
|
2018-08-23 16:39:52 -07:00
|
|
|
const res = RequestAirdropRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2018-09-12 17:41:20 -07:00
|
|
|
return res.result;
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
2018-09-14 08:27:40 -07:00
|
|
|
* Sign and send a transaction
|
2018-08-24 09:05:23 -07:00
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
async sendTransaction(
|
|
|
|
transaction: Transaction,
|
2018-11-18 08:48:14 -08:00
|
|
|
...signers: Array<Account>
|
2018-11-04 11:41:21 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2020-01-07 16:57:56 -08:00
|
|
|
if (transaction.nonceInfo) {
|
|
|
|
transaction.sign(...signers);
|
|
|
|
} else {
|
|
|
|
for (;;) {
|
|
|
|
// Attempt to use a recent blockhash for up to 30 seconds
|
|
|
|
const seconds = new Date().getSeconds();
|
|
|
|
if (
|
|
|
|
this._blockhashInfo.recentBlockhash != null &&
|
|
|
|
this._blockhashInfo.seconds < seconds + 30
|
|
|
|
) {
|
|
|
|
transaction.recentBlockhash = this._blockhashInfo.recentBlockhash;
|
|
|
|
transaction.sign(...signers);
|
|
|
|
if (!transaction.signature) {
|
|
|
|
throw new Error('!signature'); // should never happen
|
|
|
|
}
|
2018-10-22 20:03:44 -07:00
|
|
|
|
2020-01-07 16:57:56 -08:00
|
|
|
// If the signature of this transaction has not been seen before with the
|
|
|
|
// current recentBlockhash, all done.
|
|
|
|
const signature = transaction.signature.toString();
|
|
|
|
if (!this._blockhashInfo.transactionSignatures.includes(signature)) {
|
|
|
|
this._blockhashInfo.transactionSignatures.push(signature);
|
|
|
|
if (this._disableBlockhashCaching) {
|
|
|
|
this._blockhashInfo.seconds = -1;
|
|
|
|
}
|
|
|
|
break;
|
2018-10-22 20:03:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:57:56 -08:00
|
|
|
// Fetch a new blockhash
|
|
|
|
let attempts = 0;
|
|
|
|
const startTime = Date.now();
|
|
|
|
for (;;) {
|
|
|
|
const [
|
2019-03-04 08:06:33 -08:00
|
|
|
recentBlockhash,
|
2020-01-07 16:57:56 -08:00
|
|
|
//feeCalculator,
|
|
|
|
] = await this.getRecentBlockhash();
|
|
|
|
|
|
|
|
if (this._blockhashInfo.recentBlockhash != recentBlockhash) {
|
|
|
|
this._blockhashInfo = {
|
|
|
|
recentBlockhash,
|
|
|
|
seconds: new Date().getSeconds(),
|
|
|
|
transactionSignatures: [],
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (attempts === 50) {
|
|
|
|
throw new Error(
|
|
|
|
`Unable to obtain a new blockhash after ${Date.now() -
|
|
|
|
startTime}ms`,
|
|
|
|
);
|
|
|
|
}
|
2019-03-05 09:53:56 -08:00
|
|
|
|
2020-01-07 16:57:56 -08:00
|
|
|
// Sleep for approximately half a slot
|
|
|
|
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
|
2019-03-05 09:53:56 -08:00
|
|
|
|
2020-01-07 16:57:56 -08:00
|
|
|
++attempts;
|
|
|
|
}
|
2018-10-22 15:31:56 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 17:14:58 -07:00
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
const wireTransaction = transaction.serialize();
|
2018-11-27 21:06:03 -08:00
|
|
|
return await this.sendRawTransaction(wireTransaction);
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:22:50 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2019-10-11 10:55:08 -07:00
|
|
|
async validatorExit(): Promise<boolean> {
|
|
|
|
const unsafeRes = await this._rpcRequest('validatorExit', []);
|
2019-04-24 15:22:50 -07:00
|
|
|
const res = jsonRpcResult('boolean')(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2018-11-27 21:06:03 -08:00
|
|
|
/**
|
|
|
|
* Send a transaction that has already been signed and serialized into the
|
|
|
|
* wire format
|
|
|
|
*/
|
|
|
|
async sendRawTransaction(
|
2018-11-28 11:56:50 -08:00
|
|
|
rawTransaction: Buffer,
|
2018-11-27 21:06:03 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2018-11-04 11:41:21 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('sendTransaction', [
|
2019-04-10 12:33:42 -07:00
|
|
|
[...rawTransaction],
|
2018-11-04 11:41:21 -08:00
|
|
|
]);
|
2018-11-28 11:56:50 -08:00
|
|
|
const res = SendTransactionRpcResult(unsafeRes);
|
2018-08-23 20:10:30 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
assert(res.result);
|
|
|
|
return res.result;
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
2018-10-26 21:37:39 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnOpen() {
|
|
|
|
this._rpcWebSocketConnected = true;
|
|
|
|
this._updateSubscriptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnError(err: Error) {
|
|
|
|
console.log('ws error:', err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnClose(code: number, message: string) {
|
|
|
|
// 1000 means _rpcWebSocket.close() was called explicitly
|
|
|
|
if (code !== 1000) {
|
|
|
|
console.log('ws close:', code, message);
|
2019-11-06 16:46:49 -08:00
|
|
|
} else {
|
|
|
|
// Only after an explicit close do we need to explicitly connect again
|
|
|
|
this._rpcWebSocketConnected = false;
|
2018-10-26 21:37:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _updateSubscriptions() {
|
2019-03-08 16:02:39 -08:00
|
|
|
const accountKeys = Object.keys(this._accountChangeSubscriptions).map(
|
|
|
|
Number,
|
|
|
|
);
|
|
|
|
const programKeys = Object.keys(
|
|
|
|
this._programAccountChangeSubscriptions,
|
|
|
|
).map(Number);
|
2019-11-25 08:04:35 -08:00
|
|
|
const slotKeys = Object.keys(this._slotSubscriptions).map(Number);
|
|
|
|
if (
|
|
|
|
accountKeys.length === 0 &&
|
|
|
|
programKeys.length === 0 &&
|
|
|
|
slotKeys.length === 0
|
|
|
|
) {
|
2018-10-26 21:37:39 -07:00
|
|
|
this._rpcWebSocket.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._rpcWebSocketConnected) {
|
2019-03-08 16:02:39 -08:00
|
|
|
for (let id of accountKeys) {
|
2018-10-26 21:37:39 -07:00
|
|
|
this._accountChangeSubscriptions[id].subscriptionId = null;
|
|
|
|
}
|
2019-03-08 16:02:39 -08:00
|
|
|
for (let id of programKeys) {
|
|
|
|
this._programAccountChangeSubscriptions[id].subscriptionId = null;
|
|
|
|
}
|
2019-11-25 08:04:35 -08:00
|
|
|
for (let id of slotKeys) {
|
|
|
|
this._slotSubscriptions[id].subscriptionId = null;
|
|
|
|
}
|
2018-10-26 21:37:39 -07:00
|
|
|
this._rpcWebSocket.connect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
for (let id of accountKeys) {
|
2018-10-26 21:37:39 -07:00
|
|
|
const {subscriptionId, publicKey} = this._accountChangeSubscriptions[id];
|
|
|
|
if (subscriptionId === null) {
|
|
|
|
try {
|
2018-11-04 11:41:21 -08:00
|
|
|
this._accountChangeSubscriptions[
|
|
|
|
id
|
|
|
|
].subscriptionId = await this._rpcWebSocket.call('accountSubscribe', [
|
|
|
|
publicKey,
|
|
|
|
]);
|
2018-10-26 21:37:39 -07:00
|
|
|
} catch (err) {
|
2018-11-04 11:41:21 -08:00
|
|
|
console.log(
|
|
|
|
`accountSubscribe error for ${publicKey}: ${err.message}`,
|
|
|
|
);
|
2018-10-26 21:37:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 16:02:39 -08:00
|
|
|
for (let id of programKeys) {
|
|
|
|
const {
|
|
|
|
subscriptionId,
|
|
|
|
programId,
|
|
|
|
} = this._programAccountChangeSubscriptions[id];
|
|
|
|
if (subscriptionId === null) {
|
|
|
|
try {
|
|
|
|
this._programAccountChangeSubscriptions[
|
|
|
|
id
|
|
|
|
].subscriptionId = await this._rpcWebSocket.call('programSubscribe', [
|
|
|
|
programId,
|
|
|
|
]);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(
|
|
|
|
`programSubscribe error for ${programId}: ${err.message}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 08:04:35 -08:00
|
|
|
for (let id of slotKeys) {
|
|
|
|
const {subscriptionId} = this._slotSubscriptions[id];
|
|
|
|
if (subscriptionId === null) {
|
|
|
|
try {
|
|
|
|
this._slotSubscriptions[
|
|
|
|
id
|
|
|
|
].subscriptionId = await this._rpcWebSocket.call('slotSubscribe', []);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(`slotSubscribe error: ${err.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 16:02:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnAccountNotification(notification: Object) {
|
|
|
|
const res = AccountNotificationResult(notification);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const keys = Object.keys(this._accountChangeSubscriptions).map(Number);
|
|
|
|
for (let id of keys) {
|
|
|
|
const sub = this._accountChangeSubscriptions[id];
|
|
|
|
if (sub.subscriptionId === res.subscription) {
|
|
|
|
const {result} = res;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
sub.callback({
|
|
|
|
executable: result.executable,
|
|
|
|
owner: new PublicKey(result.owner),
|
|
|
|
lamports: result.lamports,
|
2019-03-14 13:27:47 -07:00
|
|
|
data: Buffer.from(result.data),
|
2019-03-08 16:02:39 -08:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 21:37:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked whenever the specified account changes
|
|
|
|
*
|
|
|
|
* @param publickey Public key of the account to monitor
|
|
|
|
* @param callback Function to invoke whenever the account is changed
|
|
|
|
* @return subscription id
|
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
onAccountChange(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
callback: AccountChangeCallback,
|
|
|
|
): number {
|
2018-10-26 21:37:39 -07:00
|
|
|
const id = ++this._accountChangeSubscriptionCounter;
|
|
|
|
this._accountChangeSubscriptions[id] = {
|
|
|
|
publicKey: publicKey.toBase58(),
|
|
|
|
callback,
|
2018-11-04 11:41:21 -08:00
|
|
|
subscriptionId: null,
|
2018-10-26 21:37:39 -07:00
|
|
|
};
|
|
|
|
this._updateSubscriptions();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister an account notification callback
|
|
|
|
*
|
|
|
|
* @param id subscription id to deregister
|
|
|
|
*/
|
|
|
|
async removeAccountChangeListener(id: number): Promise<void> {
|
|
|
|
if (this._accountChangeSubscriptions[id]) {
|
|
|
|
const {subscriptionId} = this._accountChangeSubscriptions[id];
|
|
|
|
delete this._accountChangeSubscriptions[id];
|
|
|
|
if (subscriptionId !== null) {
|
|
|
|
try {
|
|
|
|
await this._rpcWebSocket.call('accountUnsubscribe', [subscriptionId]);
|
|
|
|
} catch (err) {
|
|
|
|
console.log('accountUnsubscribe error:', err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown account change id: ${id}`);
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 16:02:39 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnProgramAccountNotification(notification: Object) {
|
|
|
|
const res = ProgramAccountNotificationResult(notification);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const keys = Object.keys(this._programAccountChangeSubscriptions).map(
|
|
|
|
Number,
|
|
|
|
);
|
|
|
|
for (let id of keys) {
|
|
|
|
const sub = this._programAccountChangeSubscriptions[id];
|
|
|
|
if (sub.subscriptionId === res.subscription) {
|
|
|
|
const {result} = res;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
sub.callback({
|
|
|
|
accountId: result[0],
|
|
|
|
accountInfo: {
|
|
|
|
executable: result[1].executable,
|
|
|
|
owner: new PublicKey(result[1].owner),
|
|
|
|
lamports: result[1].lamports,
|
2019-03-14 13:27:47 -07:00
|
|
|
data: Buffer.from(result[1].data),
|
2019-03-08 16:02:39 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked whenever accounts owned by the
|
|
|
|
* specified program change
|
|
|
|
*
|
|
|
|
* @param programId Public key of the program to monitor
|
|
|
|
* @param callback Function to invoke whenever the account is changed
|
|
|
|
* @return subscription id
|
|
|
|
*/
|
|
|
|
onProgramAccountChange(
|
|
|
|
programId: PublicKey,
|
|
|
|
callback: ProgramAccountChangeCallback,
|
|
|
|
): number {
|
|
|
|
const id = ++this._programAccountChangeSubscriptionCounter;
|
|
|
|
this._programAccountChangeSubscriptions[id] = {
|
|
|
|
programId: programId.toBase58(),
|
|
|
|
callback,
|
|
|
|
subscriptionId: null,
|
|
|
|
};
|
|
|
|
this._updateSubscriptions();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister an account notification callback
|
|
|
|
*
|
|
|
|
* @param id subscription id to deregister
|
|
|
|
*/
|
|
|
|
async removeProgramAccountChangeListener(id: number): Promise<void> {
|
|
|
|
if (this._programAccountChangeSubscriptions[id]) {
|
|
|
|
const {subscriptionId} = this._programAccountChangeSubscriptions[id];
|
|
|
|
delete this._programAccountChangeSubscriptions[id];
|
|
|
|
if (subscriptionId !== null) {
|
|
|
|
try {
|
|
|
|
await this._rpcWebSocket.call('programUnsubscribe', [subscriptionId]);
|
|
|
|
} catch (err) {
|
|
|
|
console.log('programUnsubscribe error:', err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown account change id: ${id}`);
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 10:01:10 -08:00
|
|
|
|
2019-11-25 08:04:35 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnSlotNotification(notification: Object) {
|
|
|
|
const res = SlotNotificationResult(notification);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
const {parent, slot, root} = res.result;
|
|
|
|
|
|
|
|
const keys = Object.keys(this._slotSubscriptions).map(Number);
|
|
|
|
for (let id of keys) {
|
|
|
|
const sub = this._slotSubscriptions[id];
|
|
|
|
if (sub.subscriptionId === res.subscription) {
|
|
|
|
sub.callback({
|
|
|
|
parent,
|
|
|
|
slot,
|
|
|
|
root,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked upon slot changes
|
|
|
|
*
|
|
|
|
* @param callback Function to invoke whenever the slot changes
|
|
|
|
* @return subscription id
|
|
|
|
*/
|
|
|
|
onSlotChange(callback: SlotChangeCallback): number {
|
|
|
|
const id = ++this._slotSubscriptionCounter;
|
|
|
|
this._slotSubscriptions[id] = {
|
|
|
|
callback,
|
|
|
|
subscriptionId: id,
|
|
|
|
};
|
|
|
|
this._updateSubscriptions();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister a slot notification callback
|
|
|
|
*
|
|
|
|
* @param id subscription id to deregister
|
|
|
|
*/
|
|
|
|
async removeSlotChangeListener(id: number): Promise<void> {
|
|
|
|
if (this._slotSubscriptions[id]) {
|
|
|
|
const {subscriptionId} = this._slotSubscriptions[id];
|
|
|
|
delete this._slotSubscriptions[id];
|
|
|
|
if (subscriptionId !== null) {
|
|
|
|
try {
|
|
|
|
await this._rpcWebSocket.call('slotUnsubscribe', [subscriptionId]);
|
|
|
|
} catch (err) {
|
|
|
|
console.log('slotUnsubscribe error:', err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown slot change id: ${id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 10:01:10 -08:00
|
|
|
_argsWithCommitment(args: Array<any>, override: ?Commitment): Array<any> {
|
|
|
|
const commitment = override || this._commitment;
|
|
|
|
if (commitment) {
|
|
|
|
args.push({commitment});
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|