2018-08-23 10:52:48 -07:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import assert from 'assert';
|
2020-01-15 13:04:26 -08:00
|
|
|
import bs58 from 'bs58';
|
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-06-15 03:32:57 -07:00
|
|
|
import {MS_PER_SLOT} from './timing';
|
2019-03-05 09:53:56 -08:00
|
|
|
import {Transaction} from './transaction';
|
2020-06-10 22:15:14 -07:00
|
|
|
import {Message} from './message';
|
2018-10-22 15:31:56 -07:00
|
|
|
import {sleep} from './util/sleep';
|
2020-09-07 22:12:47 -07:00
|
|
|
import {promiseTimeout} from './util/promise-timeout';
|
2020-02-12 16:25:22 -08:00
|
|
|
import {toBuffer} from './util/to-buffer';
|
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';
|
2020-10-28 07:38:39 -07:00
|
|
|
import type {CompiledInstruction} from './message';
|
2020-09-25 08:52:01 -07:00
|
|
|
import {AgentManager} from './agent-manager';
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2020-06-11 00:00:59 -07:00
|
|
|
export const BLOCKHASH_CACHE_TIMEOUT_MS = 30 * 1000;
|
|
|
|
|
2018-08-23 20:10:30 -07:00
|
|
|
type RpcRequest = (methodName: string, args: Array<any>) => any;
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2020-07-30 21:33:54 -07:00
|
|
|
type TokenAccountsFilter =
|
2020-08-06 08:47:22 -07:00
|
|
|
| {|
|
2020-07-30 21:33:54 -07:00
|
|
|
mint: PublicKey,
|
2020-08-06 08:47:22 -07:00
|
|
|
|}
|
|
|
|
| {|
|
2020-07-30 21:33:54 -07:00
|
|
|
programId: PublicKey,
|
2020-08-06 08:47:22 -07:00
|
|
|
|};
|
2020-07-30 21:33:54 -07:00
|
|
|
|
2020-03-23 07:19:32 -07:00
|
|
|
/**
|
|
|
|
* Extra contextual information for RPC responses
|
|
|
|
*
|
|
|
|
* @typedef {Object} Context
|
|
|
|
* @property {number} slot
|
|
|
|
*/
|
|
|
|
type Context = {
|
|
|
|
slot: number,
|
|
|
|
};
|
|
|
|
|
2020-06-03 04:55:42 -07:00
|
|
|
/**
|
|
|
|
* Options for sending transactions
|
|
|
|
*
|
|
|
|
* @typedef {Object} SendOptions
|
2020-06-15 03:34:58 -07:00
|
|
|
* @property {boolean | undefined} skipPreflight disable transaction verification step
|
2020-09-24 17:49:34 -07:00
|
|
|
* @property {Commitment | undefined} preflightCommitment preflight commitment level
|
2020-06-03 04:55:42 -07:00
|
|
|
*/
|
|
|
|
export type SendOptions = {
|
2020-08-06 04:16:01 -07:00
|
|
|
skipPreflight?: boolean,
|
2020-09-24 17:49:34 -07:00
|
|
|
preflightCommitment?: Commitment,
|
2020-06-03 04:55:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options for confirming transactions
|
|
|
|
*
|
|
|
|
* @typedef {Object} ConfirmOptions
|
2020-06-15 03:34:58 -07:00
|
|
|
* @property {boolean | undefined} skipPreflight disable transaction verification step
|
2020-09-07 22:12:47 -07:00
|
|
|
* @property {Commitment | undefined} commitment desired commitment level
|
2020-09-24 17:49:34 -07:00
|
|
|
* @property {Commitment | undefined} preflightCommitment preflight commitment level
|
2020-06-03 04:55:42 -07:00
|
|
|
*/
|
|
|
|
export type ConfirmOptions = {
|
2020-08-06 04:16:01 -07:00
|
|
|
skipPreflight?: boolean,
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment?: Commitment,
|
2020-09-24 17:49:34 -07:00
|
|
|
preflightCommitment?: Commitment,
|
2020-06-03 04:55:42 -07:00
|
|
|
};
|
|
|
|
|
2020-07-29 22:40:46 -07:00
|
|
|
/**
|
|
|
|
* Options for getConfirmedSignaturesForAddress2
|
|
|
|
*
|
|
|
|
* @typedef {Object} ConfirmedSignaturesForAddress2Options
|
|
|
|
* @property {TransactionSignature | undefined} before start searching backwards from this transaction signature.
|
|
|
|
* If not provided the search starts from the highest max confirmed block.
|
|
|
|
* @property {number | undefined} limit maximum transaction signatures to return (between 1 and 1,000, default: 1,000).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export type ConfirmedSignaturesForAddress2Options = {
|
|
|
|
before?: TransactionSignature,
|
|
|
|
limit?: number,
|
|
|
|
};
|
|
|
|
|
2020-02-14 07:01:01 -08:00
|
|
|
/**
|
|
|
|
* RPC Response with extra contextual information
|
|
|
|
*
|
|
|
|
* @typedef {Object} RpcResponseAndContext
|
2020-03-23 07:19:32 -07:00
|
|
|
* @property {Context} context
|
2020-02-14 07:01:01 -08:00
|
|
|
* @property {T} value response
|
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
type RpcResponseAndContext<T> = {
|
2020-03-23 07:19:32 -07:00
|
|
|
context: Context,
|
2019-11-13 14:31:31 -08:00
|
|
|
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,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:19:32 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
function notificationResultAndContext(resultDescription: any) {
|
|
|
|
return struct({
|
|
|
|
context: struct({
|
|
|
|
slot: 'number',
|
|
|
|
}),
|
|
|
|
value: resultDescription,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-11 10:01:10 -08:00
|
|
|
/**
|
|
|
|
* The level of commitment desired when querying state
|
2020-05-20 02:12:09 -07:00
|
|
|
* <pre>
|
|
|
|
* 'max': Query the most recent block which has been finalized by the cluster
|
|
|
|
* 'recent': Query the most recent block which has reached 1 confirmation by the connected node
|
|
|
|
* 'root': Query the most recent block which has been rooted by the connected node
|
|
|
|
* 'single': Query the most recent block which has reached 1 confirmation by the cluster
|
2020-05-26 14:17:35 -07:00
|
|
|
* 'singleGossip': Query the most recent block which has reached 1 confirmation according to votes seen in gossip
|
2020-05-20 02:12:09 -07:00
|
|
|
* </pre>
|
2019-11-11 10:01:10 -08:00
|
|
|
*
|
2020-05-26 14:17:35 -07:00
|
|
|
* @typedef {'max' | 'recent' | 'root' | 'single' | 'singleGossip'} Commitment
|
2019-11-11 10:01:10 -08:00
|
|
|
*/
|
2020-05-26 14:17:35 -07:00
|
|
|
export type Commitment = 'max' | 'recent' | 'root' | 'single' | 'singleGossip';
|
2019-11-11 10:01:10 -08:00
|
|
|
|
2020-05-22 10:23:29 -07:00
|
|
|
/**
|
|
|
|
* Filter for largest accounts query
|
|
|
|
* <pre>
|
|
|
|
* 'circulating': Return the largest accounts that are part of the circulating supply
|
|
|
|
* 'nonCirculating': Return the largest accounts that are not part of the circulating supply
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @typedef {'circulating' | 'nonCirculating'} LargestAccountsFilter
|
|
|
|
*/
|
|
|
|
export type LargestAccountsFilter = 'circulating' | 'nonCirculating';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration object for changing `getLargestAccounts` query behavior
|
|
|
|
*
|
|
|
|
* @typedef {Object} GetLargestAccountsConfig
|
|
|
|
* @property {Commitment|undefined} commitment The level of commitment desired
|
|
|
|
* @property {LargestAccountsFilter|undefined} filter Filter largest accounts by whether they are part of the circulating supply
|
|
|
|
*/
|
|
|
|
type GetLargestAccountsConfig = {
|
|
|
|
commitment: ?Commitment,
|
|
|
|
filter: ?LargestAccountsFilter,
|
|
|
|
};
|
|
|
|
|
2020-04-06 02:56:26 -07:00
|
|
|
/**
|
|
|
|
* Configuration object for changing query behavior
|
|
|
|
*
|
|
|
|
* @typedef {Object} SignatureStatusConfig
|
|
|
|
* @property {boolean} searchTransactionHistory enable searching status history, not needed for recent transactions
|
|
|
|
*/
|
|
|
|
export type SignatureStatusConfig = {
|
|
|
|
searchTransactionHistory: boolean,
|
|
|
|
};
|
|
|
|
|
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
|
2020-05-13 08:14:03 -07:00
|
|
|
* @property {string|null} gossip Gossip network address for the node
|
|
|
|
* @property {string|null} tpu TPU network address for the node (null if not available)
|
2019-04-23 09:53:26 -07:00
|
|
|
* @property {string|null} rpc JSON RPC network address for the node (null if not available)
|
2020-05-13 08:14:03 -07:00
|
|
|
* @property {string|null} version Software version of the node (null if not available)
|
2019-04-23 09:53:26 -07:00
|
|
|
*/
|
|
|
|
type ContactInfo = {
|
2019-06-12 10:26:55 -07:00
|
|
|
pubkey: string,
|
2020-05-13 08:14:03 -07:00
|
|
|
gossip: string | null,
|
2019-04-23 09:53:26 -07:00
|
|
|
tpu: string | null,
|
|
|
|
rpc: string | null,
|
2020-05-13 08:14:03 -07:00
|
|
|
version: string | null,
|
2019-04-23 09:53:26 -07:00
|
|
|
};
|
|
|
|
|
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
|
|
|
/**
|
2020-05-21 02:11:32 -07:00
|
|
|
* Network Inflation
|
2020-03-29 07:01:01 -07:00
|
|
|
* (see https://docs.solana.com/implemented-proposals/ed_overview)
|
2019-08-28 07:21:39 -07:00
|
|
|
*
|
2020-06-03 08:38:48 -07:00
|
|
|
* @typedef {Object} InflationGovernor
|
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} taper
|
|
|
|
* @property {number} terminal
|
2019-08-28 07:21:39 -07:00
|
|
|
*/
|
2020-06-03 08:38:48 -07:00
|
|
|
type InflationGovernor = {
|
2020-05-21 02:11:32 -07:00
|
|
|
foundation: number,
|
|
|
|
foundationTerm: number,
|
|
|
|
initial: number,
|
|
|
|
taper: number,
|
|
|
|
terminal: number,
|
|
|
|
};
|
|
|
|
|
2020-06-03 08:38:48 -07:00
|
|
|
const GetInflationGovernorResult = struct({
|
2019-08-28 07:21:39 -07:00
|
|
|
foundation: 'number',
|
2020-01-15 13:04:26 -08:00
|
|
|
foundationTerm: 'number',
|
2019-08-28 07:21:39 -07:00
|
|
|
initial: 'number',
|
|
|
|
taper: 'number',
|
|
|
|
terminal: 'number',
|
|
|
|
});
|
|
|
|
|
2019-10-29 09:50:05 -07:00
|
|
|
/**
|
2020-05-21 02:11:32 -07:00
|
|
|
* Information about the current epoch
|
2019-10-29 09:50:05 -07:00
|
|
|
*
|
|
|
|
* @typedef {Object} EpochInfo
|
|
|
|
* @property {number} epoch
|
|
|
|
* @property {number} slotIndex
|
|
|
|
* @property {number} slotsInEpoch
|
|
|
|
* @property {number} absoluteSlot
|
2020-07-21 14:43:40 -07:00
|
|
|
* @property {number} blockHeight
|
2019-10-29 09:50:05 -07:00
|
|
|
*/
|
2020-05-21 02:11:32 -07:00
|
|
|
type EpochInfo = {
|
|
|
|
epoch: number,
|
|
|
|
slotIndex: number,
|
|
|
|
slotsInEpoch: number,
|
|
|
|
absoluteSlot: number,
|
2020-07-21 14:43:40 -07:00
|
|
|
blockHeight: number | null,
|
2020-05-21 02:11:32 -07:00
|
|
|
};
|
|
|
|
|
2019-10-29 09:50:05 -07:00
|
|
|
const GetEpochInfoResult = struct({
|
|
|
|
epoch: 'number',
|
|
|
|
slotIndex: 'number',
|
|
|
|
slotsInEpoch: 'number',
|
|
|
|
absoluteSlot: 'number',
|
2020-07-21 14:43:40 -07:00
|
|
|
blockHeight: 'number?',
|
2019-10-29 09:50:05 -07:00
|
|
|
});
|
|
|
|
|
2019-10-23 06:48:24 -07:00
|
|
|
/**
|
2020-05-21 02:11:32 -07:00
|
|
|
* Epoch schedule
|
2020-03-29 07:01:01 -07:00
|
|
|
* (see https://docs.solana.com/terminology#epoch)
|
2019-10-29 09:50:05 -07:00
|
|
|
*
|
2019-10-23 06:48:24 -07:00
|
|
|
* @typedef {Object} EpochSchedule
|
2020-03-29 07:18:02 -07:00
|
|
|
* @property {number} slotsPerEpoch The maximum number of slots in each epoch
|
|
|
|
* @property {number} leaderScheduleSlotOffset The number of slots before beginning of an epoch to calculate a leader schedule for that epoch
|
|
|
|
* @property {boolean} warmup Indicates whether epochs start short and grow
|
|
|
|
* @property {number} firstNormalEpoch The first epoch with `slotsPerEpoch` slots
|
|
|
|
* @property {number} firstNormalSlot The first slot of `firstNormalEpoch`
|
2019-10-23 06:48:24 -07:00
|
|
|
*/
|
2020-05-21 02:11:32 -07:00
|
|
|
type EpochSchedule = {
|
|
|
|
slotsPerEpoch: number,
|
|
|
|
leaderScheduleSlotOffset: number,
|
|
|
|
warmup: boolean,
|
|
|
|
firstNormalEpoch: number,
|
|
|
|
firstNormalSlot: number,
|
|
|
|
};
|
|
|
|
|
2019-10-23 06:48:24 -07:00
|
|
|
const GetEpochScheduleResult = struct({
|
2020-01-15 13:04:26 -08:00
|
|
|
slotsPerEpoch: 'number',
|
|
|
|
leaderScheduleSlotOffset: 'number',
|
2019-10-23 06:48:24 -07:00
|
|
|
warmup: 'boolean',
|
2020-01-15 13:04:26 -08:00
|
|
|
firstNormalEpoch: 'number',
|
|
|
|
firstNormalSlot: 'number',
|
2019-10-23 06:48:24 -07:00
|
|
|
});
|
|
|
|
|
2020-07-17 08:16:44 -07:00
|
|
|
/**
|
|
|
|
* Leader schedule
|
|
|
|
* (see https://docs.solana.com/terminology#leader-schedule)
|
|
|
|
*
|
|
|
|
* @typedef {Object} LeaderSchedule
|
|
|
|
*/
|
|
|
|
type LeaderSchedule = {
|
|
|
|
[address: string]: number[],
|
|
|
|
};
|
|
|
|
|
|
|
|
const GetLeaderScheduleResult = struct.record([
|
|
|
|
'string',
|
2020-09-04 09:33:23 -07:00
|
|
|
'any', // validating struct.array(['number']) is extremely slow
|
2020-07-17 08:16:44 -07:00
|
|
|
]);
|
|
|
|
|
2020-04-04 06:35:08 -07:00
|
|
|
/**
|
|
|
|
* Transaction error or null
|
|
|
|
*/
|
|
|
|
const TransactionErrorResult = struct.union(['null', 'object']);
|
|
|
|
|
2020-02-03 07:22:11 -08:00
|
|
|
/**
|
|
|
|
* Signature status for a transaction
|
|
|
|
*/
|
2020-04-04 06:35:08 -07:00
|
|
|
const SignatureStatusResult = struct({err: TransactionErrorResult});
|
2020-02-03 07:22:11 -08:00
|
|
|
|
2019-11-11 17:09:00 -08:00
|
|
|
/**
|
|
|
|
* Version info for a node
|
|
|
|
*
|
|
|
|
* @typedef {Object} Version
|
|
|
|
* @property {string} solana-core Version of solana-core
|
|
|
|
*/
|
2020-09-29 10:55:23 -07:00
|
|
|
const Version = struct.pick({
|
2019-11-11 17:09:00 -08:00
|
|
|
'solana-core': 'string',
|
2020-09-29 10:55:23 -07:00
|
|
|
'feature-set': 'number?',
|
2019-11-11 17:09:00 -08:00
|
|
|
});
|
|
|
|
|
2020-08-10 23:35:56 -07:00
|
|
|
type SimulatedTransactionResponse = {
|
|
|
|
err: TransactionError | string | null,
|
|
|
|
logs: Array<string> | null,
|
|
|
|
};
|
|
|
|
|
|
|
|
const SimulatedTransactionResponseValidator = jsonRpcResultAndContext(
|
|
|
|
struct.pick({
|
|
|
|
err: struct.union(['null', 'object', 'string']),
|
|
|
|
logs: struct.union(['null', struct.array(['string'])]),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-10-28 02:13:51 -07:00
|
|
|
type PartiallyDecodedInnerInstruction = {
|
|
|
|
index: number,
|
|
|
|
instructions: PartiallyDecodedInstruction[],
|
|
|
|
};
|
|
|
|
|
|
|
|
type ParsedInnerInstruction = {
|
|
|
|
index: number,
|
|
|
|
instructions: (ParsedInstruction | PartiallyDecodedInnerInstruction)[],
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metadata for a parsed confirmed transaction on the ledger
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedConfirmedTransactionMeta
|
|
|
|
* @property {number} fee The fee charged for processing the transaction
|
|
|
|
* @property {Array<ParsedInnerInstruction>} innerInstructions An array of cross program invoked parsed instructions
|
|
|
|
* @property {Array<number>} preBalances The balances of the transaction accounts before processing
|
|
|
|
* @property {Array<number>} postBalances The balances of the transaction accounts after processing
|
|
|
|
* @property {Array<string>} logMessages An array of program log messages emitted during a transaction
|
|
|
|
* @property {object|null} err The error result of transaction processing
|
|
|
|
*/
|
|
|
|
type ParsedConfirmedTransactionMeta = {
|
|
|
|
fee: number,
|
|
|
|
innerInstructions?: ParsedInnerInstruction[],
|
|
|
|
preBalances: Array<number>,
|
|
|
|
postBalances: Array<number>,
|
|
|
|
logMessages?: Array<string>,
|
|
|
|
err: TransactionError | null,
|
|
|
|
};
|
|
|
|
|
|
|
|
type CompiledInnerInstruction = {
|
|
|
|
index: number,
|
|
|
|
instructions: CompiledInstruction[],
|
|
|
|
};
|
|
|
|
|
2020-04-21 20:12:59 -07:00
|
|
|
/**
|
|
|
|
* Metadata for a confirmed transaction on the ledger
|
|
|
|
*
|
|
|
|
* @typedef {Object} ConfirmedTransactionMeta
|
|
|
|
* @property {number} fee The fee charged for processing the transaction
|
2020-10-28 02:13:51 -07:00
|
|
|
* @property {Array<CompiledInnerInstruction>} innerInstructions An array of cross program invoked instructions
|
2020-04-21 20:12:59 -07:00
|
|
|
* @property {Array<number>} preBalances The balances of the transaction accounts before processing
|
|
|
|
* @property {Array<number>} postBalances The balances of the transaction accounts after processing
|
2020-10-05 10:24:38 -07:00
|
|
|
* @property {Array<string>} logMessages An array of program log messages emitted during a transaction
|
2020-04-21 20:12:59 -07:00
|
|
|
* @property {object|null} err The error result of transaction processing
|
|
|
|
*/
|
|
|
|
type ConfirmedTransactionMeta = {
|
|
|
|
fee: number,
|
2020-10-28 02:13:51 -07:00
|
|
|
innerInstructions?: CompiledInnerInstruction[],
|
2020-04-21 20:12:59 -07:00
|
|
|
preBalances: Array<number>,
|
|
|
|
postBalances: Array<number>,
|
2020-10-05 10:24:38 -07:00
|
|
|
logMessages?: Array<string>,
|
2020-04-21 20:12:59 -07:00
|
|
|
err: TransactionError | null,
|
|
|
|
};
|
|
|
|
|
2020-04-21 00:40:44 -07:00
|
|
|
/**
|
|
|
|
* A confirmed transaction on the ledger
|
|
|
|
*
|
|
|
|
* @typedef {Object} ConfirmedTransaction
|
|
|
|
* @property {number} slot The slot during which the transaction was processed
|
|
|
|
* @property {Transaction} transaction The details of the transaction
|
2020-04-21 20:12:59 -07:00
|
|
|
* @property {ConfirmedTransactionMeta|null} meta Metadata produced from the transaction
|
2020-04-21 00:40:44 -07:00
|
|
|
*/
|
|
|
|
type ConfirmedTransaction = {
|
|
|
|
slot: number,
|
|
|
|
transaction: Transaction,
|
2020-04-21 20:12:59 -07:00
|
|
|
meta: ConfirmedTransactionMeta | null,
|
2020-04-21 00:40:44 -07:00
|
|
|
};
|
|
|
|
|
2020-08-06 04:16:01 -07:00
|
|
|
/**
|
|
|
|
* A partially decoded transaction instruction
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedMessageAccount
|
|
|
|
* @property {PublicKey} pubkey Public key of the account
|
|
|
|
* @property {PublicKey} accounts Indicates if the account signed the transaction
|
|
|
|
* @property {string} data Raw base-58 instruction data
|
|
|
|
*/
|
|
|
|
type PartiallyDecodedInstruction = {|
|
|
|
|
programId: PublicKey,
|
|
|
|
accounts: Array<PublicKey>,
|
|
|
|
data: string,
|
|
|
|
|};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A parsed transaction message account
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedMessageAccount
|
|
|
|
* @property {PublicKey} pubkey Public key of the account
|
|
|
|
* @property {boolean} signer Indicates if the account signed the transaction
|
|
|
|
* @property {boolean} writable Indicates if the account is writable for this transaction
|
|
|
|
*/
|
|
|
|
type ParsedMessageAccount = {
|
|
|
|
pubkey: PublicKey,
|
|
|
|
signer: boolean,
|
|
|
|
writable: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A parsed transaction instruction
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedInstruction
|
|
|
|
* @property {string} program Name of the program for this instruction
|
|
|
|
* @property {PublicKey} programId ID of the program for this instruction
|
|
|
|
* @property {any} parsed Parsed instruction info
|
|
|
|
*/
|
|
|
|
type ParsedInstruction = {|
|
|
|
|
program: string,
|
|
|
|
programId: PublicKey,
|
|
|
|
parsed: any,
|
|
|
|
|};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A parsed transaction message
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedMessage
|
|
|
|
* @property {Array<ParsedMessageAccount>} accountKeys Accounts used in the instructions
|
|
|
|
* @property {Array<ParsedInstruction | PartiallyDecodedInstruction>} instructions The atomically executed instructions for the transaction
|
|
|
|
* @property {string} recentBlockhash Recent blockhash
|
|
|
|
*/
|
|
|
|
type ParsedMessage = {
|
|
|
|
accountKeys: ParsedMessageAccount[],
|
|
|
|
instructions: (ParsedInstruction | PartiallyDecodedInstruction)[],
|
|
|
|
recentBlockhash: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A parsed transaction
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedTransaction
|
|
|
|
* @property {Array<string>} signatures Signatures for the transaction
|
|
|
|
* @property {ParsedMessage} message Message of the transaction
|
|
|
|
*/
|
|
|
|
type ParsedTransaction = {
|
|
|
|
signatures: Array<string>,
|
|
|
|
message: ParsedMessage,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A parsed and confirmed transaction on the ledger
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedConfirmedTransaction
|
|
|
|
* @property {number} slot The slot during which the transaction was processed
|
|
|
|
* @property {ParsedTransaction} transaction The details of the transaction
|
|
|
|
* @property {ConfirmedTransactionMeta|null} meta Metadata produced from the transaction
|
|
|
|
*/
|
|
|
|
type ParsedConfirmedTransaction = {
|
|
|
|
slot: number,
|
|
|
|
transaction: ParsedTransaction,
|
2020-10-28 02:13:51 -07:00
|
|
|
meta: ParsedConfirmedTransactionMeta | null,
|
2020-08-06 04:16:01 -07:00
|
|
|
};
|
|
|
|
|
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
|
2020-01-15 13:04:26 -08:00
|
|
|
* @property {Array<object>} transactions Vector of transactions and status metas
|
2020-02-11 23:06:40 -08:00
|
|
|
* @property {Array<object>} rewards Vector of block rewards
|
2019-11-19 14:38:06 -08:00
|
|
|
*/
|
|
|
|
type ConfirmedBlock = {
|
|
|
|
blockhash: Blockhash,
|
|
|
|
previousBlockhash: Blockhash,
|
|
|
|
parentSlot: number,
|
2020-01-15 13:04:26 -08:00
|
|
|
transactions: Array<{
|
|
|
|
transaction: Transaction,
|
2020-04-21 20:12:59 -07:00
|
|
|
meta: ConfirmedTransactionMeta | null,
|
2020-01-15 13:04:26 -08:00
|
|
|
}>,
|
2020-02-11 23:06:40 -08:00
|
|
|
rewards: Array<{
|
|
|
|
pubkey: string,
|
|
|
|
lamports: number,
|
2020-10-09 12:41:10 -07:00
|
|
|
postBalance: number | null,
|
|
|
|
rewardType: string | null,
|
2020-02-11 23:06:40 -08:00
|
|
|
}>,
|
2019-11-19 14:38:06 -08:00
|
|
|
};
|
|
|
|
|
2020-10-08 20:26:58 -07:00
|
|
|
/**
|
|
|
|
* A performance sample
|
|
|
|
*
|
|
|
|
* @typedef {Object} PerfSample
|
|
|
|
* @property {number} slot Slot number of sample
|
|
|
|
* @property {number} numTransactions Number of transactions in a sample window
|
|
|
|
* @property {number} numSlots Number of slots in a sample window
|
|
|
|
* @property {number} samplePeriodSecs Sample window in seconds
|
|
|
|
*/
|
|
|
|
type PerfSample = {
|
|
|
|
slot: number,
|
|
|
|
numTransactions: number,
|
|
|
|
numSlots: number,
|
|
|
|
samplePeriodSecs: number,
|
|
|
|
};
|
|
|
|
|
2020-10-06 09:41:18 -07:00
|
|
|
function createRpcRequest(url: string, useHttps: boolean): RpcRequest {
|
|
|
|
const agentManager = new AgentManager(useHttps);
|
2020-10-08 20:26:58 -07:00
|
|
|
|
2018-11-04 11:41:21 -08:00
|
|
|
const server = jayson(async (request, callback) => {
|
2020-09-25 08:52:01 -07:00
|
|
|
const agent = agentManager.requestStart();
|
2018-11-04 11:41:21 -08:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
body: request,
|
2020-09-25 08:52:01 -07:00
|
|
|
agent,
|
2018-11-04 11:41:21 -08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-08-17 15:33:58 -07:00
|
|
|
let too_many_requests_retries = 5;
|
|
|
|
let res = {};
|
2020-08-24 11:20:45 -07:00
|
|
|
let waitTime = 500;
|
2020-08-17 15:33:58 -07:00
|
|
|
for (;;) {
|
|
|
|
res = await fetch(url, options);
|
2020-09-01 10:58:40 -07:00
|
|
|
if (res.status !== 429 /* Too many requests */) {
|
2020-08-17 15:33:58 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-08-24 11:20:45 -07:00
|
|
|
too_many_requests_retries -= 1;
|
|
|
|
if (too_many_requests_retries === 0) {
|
|
|
|
break;
|
|
|
|
}
|
2020-08-17 15:33:58 -07:00
|
|
|
console.log(
|
2020-09-01 10:58:40 -07:00
|
|
|
`Server responded with ${res.status} ${res.statusText}. Retrying after ${waitTime}ms delay...`,
|
2020-08-17 15:33:58 -07:00
|
|
|
);
|
2020-08-24 11:20:45 -07:00
|
|
|
await sleep(waitTime);
|
|
|
|
waitTime *= 2;
|
2020-08-17 15:33:58 -07:00
|
|
|
}
|
|
|
|
|
2018-11-04 11:41:21 -08:00
|
|
|
const text = await res.text();
|
2020-08-17 12:42:39 -07:00
|
|
|
if (res.ok) {
|
|
|
|
callback(null, text);
|
|
|
|
} else {
|
|
|
|
callback(new Error(`${res.status} ${res.statusText}: ${text}`));
|
|
|
|
}
|
2018-11-04 11:41:21 -08:00
|
|
|
} catch (err) {
|
|
|
|
callback(err);
|
2020-09-25 08:52:01 -07:00
|
|
|
} finally {
|
|
|
|
agentManager.requestEnd();
|
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
|
|
|
/**
|
2020-06-03 08:38:48 -07:00
|
|
|
* Expected JSON RPC response for the "getInflationGovernor" message
|
2019-08-28 07:21:39 -07:00
|
|
|
*/
|
2020-06-03 08:38:48 -07:00
|
|
|
const GetInflationGovernorRpcResult = struct({
|
2019-08-28 07:21:39 -07:00
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
2020-06-03 08:38:48 -07:00
|
|
|
result: GetInflationGovernorResult,
|
2019-08-28 07:21:39 -07:00
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2020-07-17 08:16:44 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getLeaderSchedule" message
|
|
|
|
*/
|
2020-07-21 14:43:40 -07:00
|
|
|
const GetLeaderScheduleRpcResult = jsonRpcResult(GetLeaderScheduleResult);
|
2020-07-17 08:16:44 -07:00
|
|
|
|
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
|
|
|
|
2020-05-18 20:27:36 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getBlockTime" message
|
|
|
|
*/
|
|
|
|
const GetBlockTimeRpcResult = struct({
|
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
2020-08-14 08:04:49 -07:00
|
|
|
result: struct.union(['null', 'number', 'undefined']),
|
2020-05-18 20:27:36 -07:00
|
|
|
});
|
|
|
|
|
2020-05-21 01:58:17 -07:00
|
|
|
/**
|
2020-05-23 02:33:04 -07:00
|
|
|
* Expected JSON RPC response for the "minimumLedgerSlot" and "getFirstAvailableBlock" messages
|
2020-05-21 01:58:17 -07:00
|
|
|
*/
|
2020-05-23 02:33:04 -07:00
|
|
|
const SlotRpcResult = struct({
|
2020-05-21 01:58:17 -07:00
|
|
|
jsonrpc: struct.literal('2.0'),
|
|
|
|
id: 'string',
|
|
|
|
error: 'any?',
|
|
|
|
result: 'number',
|
|
|
|
});
|
|
|
|
|
2020-05-22 04:30:22 -07:00
|
|
|
/**
|
|
|
|
* Supply
|
|
|
|
*
|
|
|
|
* @typedef {Object} Supply
|
|
|
|
* @property {number} total Total supply in lamports
|
|
|
|
* @property {number} circulating Circulating supply in lamports
|
|
|
|
* @property {number} nonCirculating Non-circulating supply in lamports
|
|
|
|
* @property {Array<PublicKey>} nonCirculatingAccounts List of non-circulating account addresses
|
|
|
|
*/
|
|
|
|
type Supply = {
|
|
|
|
total: number,
|
|
|
|
circulating: number,
|
|
|
|
nonCirculating: number,
|
|
|
|
nonCirculatingAccounts: Array<PublicKey>,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getSupply" message
|
|
|
|
*/
|
|
|
|
const GetSupplyRpcResult = jsonRpcResultAndContext(
|
|
|
|
struct({
|
|
|
|
total: 'number',
|
|
|
|
circulating: 'number',
|
|
|
|
nonCirculating: 'number',
|
|
|
|
nonCirculatingAccounts: struct.array(['string']),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-08-11 02:28:07 -07:00
|
|
|
/**
|
|
|
|
* Token amount object which returns a token amount in different formats
|
|
|
|
* for various client use cases.
|
|
|
|
*
|
|
|
|
* @typedef {Object} TokenAmount
|
|
|
|
* @property {string} amount Raw amount of tokens as string ignoring decimals
|
|
|
|
* @property {number} decimals Number of decimals configured for token's mint
|
|
|
|
* @property {number} uiAmount Token account as float, accounts for decimals
|
|
|
|
*/
|
2020-08-05 21:17:29 -07:00
|
|
|
type TokenAmount = {
|
|
|
|
amount: string,
|
2020-08-11 02:28:07 -07:00
|
|
|
decimals: number,
|
2020-08-05 21:17:29 -07:00
|
|
|
uiAmount: number,
|
|
|
|
};
|
2020-07-30 21:33:54 -07:00
|
|
|
|
|
|
|
/**
|
2020-08-05 21:17:29 -07:00
|
|
|
* Expected JSON RPC structure for token amounts
|
2020-07-30 21:33:54 -07:00
|
|
|
*/
|
2020-08-11 02:28:07 -07:00
|
|
|
const TokenAmountResult = struct.object({
|
2020-08-05 21:17:29 -07:00
|
|
|
amount: 'string',
|
|
|
|
uiAmount: 'number',
|
|
|
|
decimals: 'number',
|
2020-07-30 21:33:54 -07:00
|
|
|
});
|
|
|
|
|
2020-08-11 02:28:07 -07:00
|
|
|
/**
|
|
|
|
* Token address and balance.
|
|
|
|
*
|
|
|
|
* @typedef {Object} TokenAccountBalancePair
|
|
|
|
* @property {PublicKey} address Address of the token account
|
|
|
|
* @property {string} amount Raw amount of tokens as string ignoring decimals
|
|
|
|
* @property {number} decimals Number of decimals configured for token's mint
|
|
|
|
* @property {number} uiAmount Token account as float, accounts for decimals
|
|
|
|
*/
|
|
|
|
type TokenAccountBalancePair = {
|
|
|
|
address: PublicKey,
|
|
|
|
amount: string,
|
|
|
|
decimals: number,
|
|
|
|
uiAmount: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTokenLargestAccounts" message
|
|
|
|
*/
|
|
|
|
const GetTokenLargestAccountsResult = jsonRpcResultAndContext(
|
|
|
|
struct.array([
|
|
|
|
struct.pick({
|
|
|
|
address: 'string',
|
|
|
|
amount: 'string',
|
|
|
|
uiAmount: 'number',
|
|
|
|
decimals: 'number',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2020-07-30 21:33:54 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTokenAccountBalance" message
|
|
|
|
*/
|
2020-08-05 21:17:29 -07:00
|
|
|
const GetTokenAccountBalance = jsonRpcResultAndContext(TokenAmountResult);
|
2020-07-30 21:33:54 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTokenSupply" message
|
|
|
|
*/
|
2020-08-05 21:17:29 -07:00
|
|
|
const GetTokenSupplyRpcResult = jsonRpcResultAndContext(TokenAmountResult);
|
2020-07-30 21:33:54 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTokenAccountsByOwner" message
|
|
|
|
*/
|
|
|
|
const GetTokenAccountsByOwner = jsonRpcResultAndContext(
|
|
|
|
struct.array([
|
2020-08-06 08:47:22 -07:00
|
|
|
struct.object({
|
|
|
|
pubkey: 'string',
|
|
|
|
account: struct.object({
|
|
|
|
executable: 'boolean',
|
|
|
|
owner: 'string',
|
|
|
|
lamports: 'number',
|
2020-08-15 21:57:23 -07:00
|
|
|
data: ['string', struct.literal('base64')],
|
2020-08-06 08:47:22 -07:00
|
|
|
rentEpoch: 'number?',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getTokenAccountsByOwner" message with parsed data
|
|
|
|
*/
|
|
|
|
const GetParsedTokenAccountsByOwner = jsonRpcResultAndContext(
|
|
|
|
struct.array([
|
|
|
|
struct.object({
|
2020-07-30 21:33:54 -07:00
|
|
|
pubkey: 'string',
|
2020-08-06 08:47:22 -07:00
|
|
|
account: struct.object({
|
2020-07-30 21:33:54 -07:00
|
|
|
executable: 'boolean',
|
|
|
|
owner: 'string',
|
|
|
|
lamports: 'number',
|
2020-08-11 01:32:42 -07:00
|
|
|
data: struct.pick({
|
2020-08-06 08:47:22 -07:00
|
|
|
program: 'string',
|
|
|
|
parsed: 'any',
|
2020-08-11 20:44:28 -07:00
|
|
|
space: 'number',
|
2020-08-06 08:47:22 -07:00
|
|
|
}),
|
2020-07-30 21:33:54 -07:00
|
|
|
rentEpoch: 'number?',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2020-05-22 10:23:29 -07:00
|
|
|
/**
|
|
|
|
* Pair of an account address and its balance
|
|
|
|
*
|
|
|
|
* @typedef {Object} AccountBalancePair
|
|
|
|
* @property {PublicKey} address
|
|
|
|
* @property {number} lamports
|
|
|
|
*/
|
|
|
|
type AccountBalancePair = {
|
|
|
|
address: PublicKey,
|
|
|
|
lamports: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getLargestAccounts" message
|
|
|
|
*/
|
|
|
|
const GetLargestAccountsRpcResult = jsonRpcResultAndContext(
|
|
|
|
struct.array([
|
|
|
|
struct({
|
|
|
|
lamports: 'number',
|
|
|
|
address: 'string',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
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',
|
2020-01-15 13:04:26 -08:00
|
|
|
owner: 'string',
|
2019-03-05 17:52:13 -08:00
|
|
|
lamports: 'number',
|
2020-07-30 21:33:54 -07:00
|
|
|
data: 'any',
|
2020-01-15 13:04:26 -08:00
|
|
|
rentEpoch: 'number?',
|
2018-09-20 15:08:52 -07:00
|
|
|
});
|
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ParsedAccountInfoResult = struct.object({
|
|
|
|
executable: 'boolean',
|
|
|
|
owner: 'string',
|
|
|
|
lamports: 'number',
|
|
|
|
data: struct.union([
|
2020-08-15 21:57:23 -07:00
|
|
|
['string', struct.literal('base64')],
|
2020-08-11 01:32:42 -07:00
|
|
|
struct.pick({
|
2020-08-06 08:47:22 -07:00
|
|
|
program: 'string',
|
|
|
|
parsed: 'any',
|
2020-08-11 20:44:28 -07:00
|
|
|
space: 'number',
|
2020-08-06 08:47:22 -07:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
rentEpoch: 'number?',
|
|
|
|
});
|
|
|
|
|
2020-09-16 23:50:13 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const StakeActivationResult = struct.object({
|
|
|
|
state: struct.union([
|
|
|
|
struct.literal('active'),
|
|
|
|
struct.literal('inactive'),
|
|
|
|
struct.literal('activating'),
|
|
|
|
struct.literal('deactivating'),
|
|
|
|
]),
|
|
|
|
active: 'number',
|
|
|
|
inactive: 'number',
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getAccountInfo" message with jsonParsed param
|
|
|
|
*/
|
|
|
|
const GetParsedAccountInfoResult = jsonRpcResultAndContext(
|
|
|
|
struct.union(['null', ParsedAccountInfoResult]),
|
|
|
|
);
|
|
|
|
|
2020-09-16 23:50:13 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getStakeActivation" message with jsonParsed param
|
|
|
|
*/
|
|
|
|
const GetStakeActivationResult = jsonRpcResult(StakeActivationResult);
|
|
|
|
|
2020-04-21 00:40:44 -07:00
|
|
|
/**
|
2020-07-29 22:40:46 -07:00
|
|
|
* Expected JSON RPC response for the "getConfirmedSignaturesForAddress" message
|
2020-04-21 00:40:44 -07:00
|
|
|
*/
|
|
|
|
const GetConfirmedSignaturesForAddressRpcResult = jsonRpcResult(
|
|
|
|
struct.array(['string']),
|
|
|
|
);
|
|
|
|
|
2020-07-29 22:40:46 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getConfirmedSignaturesForAddress2" message
|
|
|
|
*/
|
|
|
|
|
|
|
|
const GetConfirmedSignaturesForAddress2RpcResult = jsonRpcResult(
|
|
|
|
struct.array([
|
|
|
|
struct({
|
|
|
|
signature: 'string',
|
|
|
|
slot: 'number',
|
|
|
|
err: TransactionErrorResult,
|
|
|
|
memo: struct.union(['null', 'string']),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/***
|
|
|
|
* Expected JSON RPC response for the "accountNotification" message
|
|
|
|
*/
|
|
|
|
const AccountNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
2020-03-23 07:19:32 -07:00
|
|
|
result: notificationResultAndContext(AccountInfoResult),
|
2018-10-26 21:37:39 -07:00
|
|
|
});
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2020-01-15 13:04:26 -08:00
|
|
|
const ProgramAccountInfoResult = struct({
|
|
|
|
pubkey: 'string',
|
|
|
|
account: AccountInfoResult,
|
|
|
|
});
|
2019-03-08 16:02:39 -08:00
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ParsedProgramAccountInfoResult = struct({
|
|
|
|
pubkey: 'string',
|
|
|
|
account: ParsedAccountInfoResult,
|
|
|
|
});
|
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/***
|
|
|
|
* Expected JSON RPC response for the "programNotification" message
|
|
|
|
*/
|
|
|
|
const ProgramAccountNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
2020-03-23 07:19:32 -07:00
|
|
|
result: notificationResultAndContext(ProgramAccountInfoResult),
|
2019-03-08 16:02:39 -08:00
|
|
|
});
|
|
|
|
|
2019-11-25 08:04:35 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2020-03-27 07:22:53 -07:00
|
|
|
const SlotInfoResult = struct({
|
2019-11-25 08:04:35 -08:00
|
|
|
parent: 'number',
|
|
|
|
slot: 'number',
|
|
|
|
root: 'number',
|
|
|
|
});
|
|
|
|
|
2020-02-03 07:22:11 -08:00
|
|
|
/**
|
2019-11-25 08:04:35 -08:00
|
|
|
* Expected JSON RPC response for the "slotNotification" message
|
|
|
|
*/
|
|
|
|
const SlotNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
2020-03-27 07:22:53 -07:00
|
|
|
result: SlotInfoResult,
|
2019-11-25 08:04:35 -08:00
|
|
|
});
|
|
|
|
|
2020-02-03 07:22:11 -08:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "signatureNotification" message
|
|
|
|
*/
|
|
|
|
const SignatureNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
2020-03-23 07:19:32 -07:00
|
|
|
result: notificationResultAndContext(SignatureStatusResult),
|
2020-02-03 07:22:11 -08:00
|
|
|
});
|
|
|
|
|
2020-03-27 07:22:53 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "rootNotification" message
|
|
|
|
*/
|
|
|
|
const RootNotificationResult = struct({
|
|
|
|
subscription: 'number',
|
|
|
|
result: 'number',
|
|
|
|
});
|
|
|
|
|
2019-06-28 18:28:06 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getProgramAccounts" message
|
|
|
|
*/
|
|
|
|
const GetProgramAccountsRpcResult = jsonRpcResult(
|
2019-12-19 09:18:36 -08:00
|
|
|
struct.array([ProgramAccountInfoResult]),
|
2019-06-28 18:28:06 -07:00
|
|
|
);
|
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getProgramAccounts" message
|
|
|
|
*/
|
|
|
|
const GetParsedProgramAccountsRpcResult = jsonRpcResult(
|
|
|
|
struct.array([ParsedProgramAccountInfoResult]),
|
|
|
|
);
|
|
|
|
|
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(
|
2019-12-19 09:18:36 -08:00
|
|
|
struct.array([
|
2020-07-10 20:22:32 -07:00
|
|
|
struct.pick({
|
2019-06-12 10:26:55 -07:00
|
|
|
pubkey: 'string',
|
2020-05-13 08:14:03 -07:00
|
|
|
gossip: struct.union(['null', 'string']),
|
2019-04-23 09:53:26 -07:00
|
|
|
tpu: struct.union(['null', 'string']),
|
|
|
|
rpc: struct.union(['null', 'string']),
|
2020-05-13 08:14:03 -07:00
|
|
|
version: struct.union(['null', 'string']),
|
2019-04-23 09:53:26 -07:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
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({
|
2019-12-19 09:18:36 -08:00
|
|
|
current: struct.array([
|
2020-07-10 20:22:32 -07:00
|
|
|
struct.pick({
|
2019-08-19 10:39:08 -07:00
|
|
|
votePubkey: 'string',
|
|
|
|
nodePubkey: 'string',
|
|
|
|
activatedStake: 'number',
|
|
|
|
epochVoteAccount: 'boolean',
|
2019-12-19 09:18:36 -08:00
|
|
|
epochCredits: struct.array([
|
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-12-19 09:18:36 -08:00
|
|
|
delinquent: struct.array([
|
2020-07-10 20:22:32 -07:00
|
|
|
struct.pick({
|
2019-08-19 10:39:08 -07:00
|
|
|
votePubkey: 'string',
|
|
|
|
nodePubkey: 'string',
|
|
|
|
activatedStake: 'number',
|
|
|
|
epochVoteAccount: 'boolean',
|
2019-12-19 09:18:36 -08:00
|
|
|
epochCredits: struct.array([
|
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
|
|
|
);
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
2020-04-01 10:51:30 -07:00
|
|
|
* Expected JSON RPC response for the "getSignatureStatuses" message
|
2018-09-26 19:16:17 -07:00
|
|
|
*/
|
2020-04-01 10:51:30 -07:00
|
|
|
const GetSignatureStatusesRpcResult = jsonRpcResultAndContext(
|
2020-03-23 08:01:12 -07:00
|
|
|
struct.array([
|
|
|
|
struct.union([
|
|
|
|
'null',
|
2020-04-04 06:35:08 -07:00
|
|
|
struct.pick({
|
2020-03-23 08:01:12 -07:00
|
|
|
slot: 'number',
|
2020-03-26 06:37:45 -07:00
|
|
|
confirmations: struct.union(['number', 'null']),
|
2020-04-04 06:35:08 -07:00
|
|
|
err: TransactionErrorResult,
|
2020-03-23 08:01:12 -07:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
]),
|
2020-02-03 07:22:11 -08:00
|
|
|
);
|
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');
|
|
|
|
|
2020-04-21 00:40:44 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ConfirmedTransactionResult = struct({
|
|
|
|
signatures: struct.array(['string']),
|
|
|
|
message: struct({
|
|
|
|
accountKeys: struct.array(['string']),
|
|
|
|
header: struct({
|
|
|
|
numRequiredSignatures: 'number',
|
|
|
|
numReadonlySignedAccounts: 'number',
|
|
|
|
numReadonlyUnsignedAccounts: 'number',
|
|
|
|
}),
|
2020-08-06 04:16:01 -07:00
|
|
|
instructions: struct.array([
|
|
|
|
struct({
|
|
|
|
accounts: struct.array(['number']),
|
|
|
|
data: 'string',
|
|
|
|
programIdIndex: 'number',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
recentBlockhash: 'string',
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ParsedConfirmedTransactionResult = struct({
|
|
|
|
signatures: struct.array(['string']),
|
|
|
|
message: struct({
|
|
|
|
accountKeys: struct.array([
|
|
|
|
struct({
|
|
|
|
pubkey: 'string',
|
|
|
|
signer: 'boolean',
|
|
|
|
writable: 'boolean',
|
|
|
|
}),
|
|
|
|
]),
|
2020-04-21 00:40:44 -07:00
|
|
|
instructions: struct.array([
|
|
|
|
struct.union([
|
|
|
|
struct({
|
2020-08-06 04:16:01 -07:00
|
|
|
accounts: struct.array(['string']),
|
2020-04-21 00:40:44 -07:00
|
|
|
data: 'string',
|
2020-08-06 04:16:01 -07:00
|
|
|
programId: 'string',
|
|
|
|
}),
|
|
|
|
struct({
|
|
|
|
parsed: 'any',
|
|
|
|
program: 'string',
|
|
|
|
programId: 'string',
|
2020-04-21 00:40:44 -07:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
recentBlockhash: 'string',
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ConfirmedTransactionMetaResult = struct.union([
|
|
|
|
'null',
|
|
|
|
struct.pick({
|
|
|
|
err: TransactionErrorResult,
|
|
|
|
fee: 'number',
|
2020-10-28 07:38:39 -07:00
|
|
|
innerInstructions: struct.union([
|
2020-10-28 02:13:51 -07:00
|
|
|
struct.array([
|
|
|
|
struct({
|
|
|
|
index: 'number',
|
|
|
|
instructions: struct.array([
|
|
|
|
struct({
|
|
|
|
accounts: struct.array(['number']),
|
|
|
|
data: 'string',
|
|
|
|
programIdIndex: 'number',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
'null',
|
|
|
|
'undefined',
|
2020-10-28 07:38:39 -07:00
|
|
|
]),
|
2020-10-28 02:13:51 -07:00
|
|
|
preBalances: struct.array(['number']),
|
|
|
|
postBalances: struct.array(['number']),
|
|
|
|
logMessages: struct.union([struct.array(['string']), 'null', 'undefined']),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const ParsedConfirmedTransactionMetaResult = struct.union([
|
|
|
|
'null',
|
|
|
|
struct.pick({
|
|
|
|
err: TransactionErrorResult,
|
|
|
|
fee: 'number',
|
2020-10-28 07:38:39 -07:00
|
|
|
innerInstructions: struct.union([
|
2020-10-28 02:13:51 -07:00
|
|
|
struct.array([
|
|
|
|
struct({
|
|
|
|
index: 'number',
|
|
|
|
instructions: struct.array([
|
|
|
|
struct.union([
|
|
|
|
struct({
|
|
|
|
accounts: struct.array(['string']),
|
|
|
|
data: 'string',
|
|
|
|
programId: 'string',
|
|
|
|
}),
|
|
|
|
struct({
|
|
|
|
parsed: 'any',
|
|
|
|
program: 'string',
|
|
|
|
programId: 'string',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
'null',
|
|
|
|
'undefined',
|
2020-10-28 07:38:39 -07:00
|
|
|
]),
|
2020-04-21 00:40:44 -07:00
|
|
|
preBalances: struct.array(['number']),
|
|
|
|
postBalances: struct.array(['number']),
|
2020-10-12 21:19:49 -07:00
|
|
|
logMessages: struct.union([struct.array(['string']), 'null', 'undefined']),
|
2020-04-21 00:40:44 -07:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
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(
|
2020-01-22 12:05:19 -08:00
|
|
|
struct.union([
|
|
|
|
'null',
|
2020-07-10 20:22:32 -07:00
|
|
|
struct.pick({
|
2020-01-22 12:05:19 -08:00
|
|
|
blockhash: 'string',
|
|
|
|
previousBlockhash: 'string',
|
|
|
|
parentSlot: 'number',
|
|
|
|
transactions: struct.array([
|
|
|
|
struct({
|
2020-04-21 00:40:44 -07:00
|
|
|
transaction: ConfirmedTransactionResult,
|
|
|
|
meta: ConfirmedTransactionMetaResult,
|
2019-11-16 08:28:14 -08:00
|
|
|
}),
|
2020-01-22 12:05:19 -08:00
|
|
|
]),
|
2020-02-11 23:06:40 -08:00
|
|
|
rewards: struct.union([
|
|
|
|
'undefined',
|
|
|
|
struct.array([
|
|
|
|
struct({
|
|
|
|
pubkey: 'string',
|
|
|
|
lamports: 'number',
|
2020-10-09 12:41:10 -07:00
|
|
|
postBalance: struct.union(['number', 'undefined']),
|
|
|
|
rewardType: struct.union(['string', 'undefined']),
|
2020-02-11 23:06:40 -08:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
]),
|
2020-01-22 12:05:19 -08:00
|
|
|
}),
|
|
|
|
]),
|
2019-11-12 08:21:19 -08:00
|
|
|
);
|
|
|
|
|
2020-04-21 00:40:44 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getConfirmedTransaction" message
|
|
|
|
*/
|
|
|
|
const GetConfirmedTransactionRpcResult = jsonRpcResult(
|
|
|
|
struct.union([
|
|
|
|
'null',
|
2020-07-10 20:22:32 -07:00
|
|
|
struct.pick({
|
2020-04-21 00:40:44 -07:00
|
|
|
slot: 'number',
|
|
|
|
transaction: ConfirmedTransactionResult,
|
|
|
|
meta: ConfirmedTransactionMetaResult,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2020-08-06 04:16:01 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getConfirmedTransaction" message
|
|
|
|
*/
|
|
|
|
const GetParsedConfirmedTransactionRpcResult = jsonRpcResult(
|
|
|
|
struct.union([
|
|
|
|
'null',
|
|
|
|
struct.pick({
|
|
|
|
slot: 'number',
|
|
|
|
transaction: ParsedConfirmedTransactionResult,
|
2020-10-28 02:13:51 -07:00
|
|
|
meta: ParsedConfirmedTransactionMetaResult,
|
2020-08-06 04:16:01 -07: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
|
|
|
*/
|
2020-01-15 13:04:26 -08:00
|
|
|
const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
|
2019-06-12 14:36:05 -07:00
|
|
|
struct({
|
2020-01-15 13:04:26 -08:00
|
|
|
blockhash: 'string',
|
|
|
|
feeCalculator: struct({
|
|
|
|
lamportsPerSignature: 'number',
|
|
|
|
}),
|
2019-06-12 14:36:05 -07:00
|
|
|
}),
|
2020-01-15 13:04:26 -08:00
|
|
|
);
|
2019-11-13 14:31:31 -08:00
|
|
|
|
2020-10-08 20:26:58 -07:00
|
|
|
/*
|
|
|
|
* Expected JSON RPC response for "getRecentPerformanceSamples" message
|
|
|
|
*/
|
|
|
|
const GetRecentPerformanceSamplesRpcResult = jsonRpcResult(
|
|
|
|
struct.array([
|
|
|
|
struct.pick({
|
|
|
|
slot: 'number',
|
|
|
|
numTransactions: 'number',
|
|
|
|
numSlots: 'number',
|
|
|
|
samplePeriodSecs: 'number',
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2020-06-04 03:12:59 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getFeeCalculatorForBlockhash" message
|
|
|
|
*/
|
|
|
|
const GetFeeCalculatorRpcResult = jsonRpcResultAndContext(
|
|
|
|
struct.union([
|
|
|
|
'null',
|
|
|
|
struct({
|
|
|
|
feeCalculator: struct({
|
|
|
|
lamportsPerSignature: 'number',
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
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
|
|
|
|
2020-03-27 07:22:53 -07:00
|
|
|
/**
|
|
|
|
* Information about the latest slot being processed by a node
|
|
|
|
*
|
|
|
|
* @typedef {Object} SlotInfo
|
|
|
|
* @property {number} slot Currently processing slot
|
|
|
|
* @property {number} parent Parent of the current slot
|
|
|
|
* @property {number} root The root block of the current slot's fork
|
|
|
|
*/
|
|
|
|
type SlotInfo = {
|
|
|
|
slot: number,
|
|
|
|
parent: number,
|
|
|
|
root: number,
|
|
|
|
};
|
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* Parsed account data
|
|
|
|
*
|
|
|
|
* @typedef {Object} ParsedAccountData
|
|
|
|
* @property {string} program Name of the program that owns this account
|
|
|
|
* @property {any} parsed Parsed account data
|
2020-08-11 20:44:28 -07:00
|
|
|
* @property {number} space Space used by account data
|
2020-08-06 08:47:22 -07:00
|
|
|
*/
|
|
|
|
type ParsedAccountData = {
|
|
|
|
program: string,
|
|
|
|
parsed: any,
|
2020-08-11 20:44:28 -07:00
|
|
|
space: number,
|
2020-08-06 08:47:22 -07:00
|
|
|
};
|
|
|
|
|
2020-09-16 23:50:13 -07:00
|
|
|
/**
|
|
|
|
* Stake Activation data
|
|
|
|
*
|
|
|
|
* @typedef {Object} StakeActivationData
|
|
|
|
* @property {string} state: <string - the stake account's activation state, one of: active, inactive, activating, deactivating
|
|
|
|
* @property {number} active: stake active during the epoch
|
|
|
|
* @property {number} inactive: stake inactive during the epoch
|
|
|
|
*/
|
|
|
|
type StakeActivationData = {
|
|
|
|
state: 'active' | 'inactive' | 'activating' | 'deactivating',
|
|
|
|
active: number,
|
|
|
|
inactive: number,
|
|
|
|
};
|
|
|
|
|
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
|
2020-08-06 08:47:22 -07:00
|
|
|
* @property {T} data Optional data assigned to the account
|
2019-03-14 13:27:47 -07:00
|
|
|
* @property {boolean} executable `true` if this account's data contains a loaded program
|
2018-09-20 15:08:52 -07:00
|
|
|
*/
|
2020-08-06 08:47:22 -07:00
|
|
|
type AccountInfo<T> = {
|
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,
|
2020-08-06 08:47:22 -07:00
|
|
|
data: T,
|
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
|
2020-08-06 08:47:22 -07:00
|
|
|
* @property {AccountInfo<Buffer>} accountInfo
|
2019-03-08 16:02:39 -08:00
|
|
|
*/
|
|
|
|
type KeyedAccountInfo = {
|
|
|
|
accountId: PublicKey,
|
2020-08-06 08:47:22 -07:00
|
|
|
accountInfo: AccountInfo<Buffer>,
|
2019-03-08 16:02:39 -08:00
|
|
|
};
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/**
|
|
|
|
* Callback function for account change notifications
|
|
|
|
*/
|
2020-03-23 07:19:32 -07:00
|
|
|
export type AccountChangeCallback = (
|
2020-08-06 08:47:22 -07:00
|
|
|
accountInfo: AccountInfo<Buffer>,
|
2020-03-23 07:19:32 -07:00
|
|
|
context: Context,
|
|
|
|
) => void;
|
2018-10-26 21:37:39 -07:00
|
|
|
|
2020-02-10 06:45:52 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type SubscriptionId = 'subscribing' | number;
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/**
|
|
|
|
* @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,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment: ?Commitment,
|
2020-02-10 06:45:52 -08:00
|
|
|
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
|
2018-11-04 11:41:21 -08:00
|
|
|
};
|
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,
|
2020-03-23 07:19:32 -07:00
|
|
|
context: Context,
|
2019-03-08 16:02:39 -08:00
|
|
|
) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type ProgramAccountSubscriptionInfo = {
|
|
|
|
programId: string, // PublicKey of the program as a base 58 string
|
|
|
|
callback: ProgramAccountChangeCallback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment: ?Commitment,
|
2020-02-10 06:45:52 -08:00
|
|
|
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
|
2019-03-08 16:02:39 -08:00
|
|
|
};
|
|
|
|
|
2020-02-03 07:22:11 -08:00
|
|
|
/**
|
|
|
|
* Callback function for slot change notifications
|
|
|
|
*/
|
|
|
|
export type SlotChangeCallback = (slotInfo: SlotInfo) => void;
|
|
|
|
|
2019-11-25 08:04:35 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type SlotSubscriptionInfo = {
|
|
|
|
callback: SlotChangeCallback,
|
2020-02-10 06:45:52 -08:00
|
|
|
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
|
2019-11-25 08:04:35 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2020-02-03 07:22:11 -08:00
|
|
|
* Callback function for signature notifications
|
2019-11-25 08:04:35 -08:00
|
|
|
*/
|
2020-02-03 07:22:11 -08:00
|
|
|
export type SignatureResultCallback = (
|
2020-04-04 06:35:08 -07:00
|
|
|
signatureResult: SignatureResult,
|
2020-03-23 07:19:32 -07:00
|
|
|
context: Context,
|
2020-02-03 07:22:11 -08:00
|
|
|
) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type SignatureSubscriptionInfo = {
|
|
|
|
signature: TransactionSignature, // TransactionSignature as a base 58 string
|
|
|
|
callback: SignatureResultCallback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment: ?Commitment,
|
2020-02-10 06:45:52 -08:00
|
|
|
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
|
2020-02-03 07:22:11 -08:00
|
|
|
};
|
2019-11-25 08:04:35 -08:00
|
|
|
|
2020-03-27 07:22:53 -07:00
|
|
|
/**
|
|
|
|
* Callback function for root change notifications
|
|
|
|
*/
|
|
|
|
export type RootChangeCallback = (root: number) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type RootSubscriptionInfo = {
|
|
|
|
callback: RootChangeCallback,
|
|
|
|
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
|
|
|
|
};
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
2020-04-04 06:35:08 -07:00
|
|
|
* Signature result
|
2018-09-26 19:16:17 -07:00
|
|
|
*
|
2020-04-04 06:35:08 -07:00
|
|
|
* @typedef {Object} SignatureResult
|
2018-09-26 19:16:17 -07:00
|
|
|
*/
|
2020-04-04 06:35:08 -07:00
|
|
|
export type SignatureResult = {|
|
|
|
|
err: TransactionError | null,
|
2019-04-10 14:40:49 -07:00
|
|
|
|};
|
|
|
|
|
|
|
|
/**
|
2020-04-04 06:35:08 -07:00
|
|
|
* Transaction error
|
2019-04-10 14:40:49 -07:00
|
|
|
*
|
|
|
|
* @typedef {Object} TransactionError
|
|
|
|
*/
|
2020-04-04 06:35:08 -07:00
|
|
|
export type TransactionError = {};
|
2018-09-26 19:16:17 -07:00
|
|
|
|
2020-03-23 08:01:12 -07:00
|
|
|
/**
|
|
|
|
* Signature status
|
|
|
|
*
|
|
|
|
* @typedef {Object} SignatureStatus
|
|
|
|
* @property {number} slot when the transaction was processed
|
2020-03-26 06:37:45 -07:00
|
|
|
* @property {number | null} confirmations the number of blocks that have been confirmed and voted on in the fork containing `slot` (TODO)
|
2020-04-04 06:35:08 -07:00
|
|
|
* @property {TransactionError | null} err error, if any
|
2020-03-23 08:01:12 -07:00
|
|
|
*/
|
|
|
|
export type SignatureStatus = {
|
|
|
|
slot: number,
|
2020-03-26 06:37:45 -07:00
|
|
|
confirmations: number | null,
|
2020-04-04 06:35:08 -07:00
|
|
|
err: TransactionError | null,
|
2020-03-23 08:01:12 -07:00
|
|
|
};
|
|
|
|
|
2020-07-29 22:40:46 -07:00
|
|
|
/**
|
|
|
|
* A confirmed signature with its status
|
|
|
|
*
|
|
|
|
* @typedef {Object} ConfirmedSignatureInfo
|
|
|
|
* @property {string} signature the transaction signature
|
|
|
|
* @property {number} slot when the transaction was processed
|
|
|
|
* @property {TransactionError | null} err error, if any
|
|
|
|
* @property {string | null} memo memo associated with the transaction, if any
|
|
|
|
*/
|
|
|
|
export type ConfirmedSignatureInfo = {
|
|
|
|
signature: string,
|
|
|
|
slot: number,
|
|
|
|
err: TransactionError | null,
|
|
|
|
memo: string | null,
|
|
|
|
};
|
|
|
|
|
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 {
|
2020-10-21 01:19:51 -07:00
|
|
|
_rpcEndpoint: string;
|
2018-08-23 10:52:48 -07:00
|
|
|
_rpcRequest: RpcRequest;
|
2018-10-26 21:37:39 -07:00
|
|
|
_rpcWebSocket: RpcWebSocketClient;
|
2020-09-07 08:12:22 -07:00
|
|
|
_rpcWebSocketConnected: boolean = false;
|
2020-09-06 19:24:16 -07:00
|
|
|
_rpcWebSocketHeartbeat: IntervalID | null = null;
|
2020-09-07 08:12:22 -07:00
|
|
|
_rpcWebSocketIdleTimeout: TimeoutID | null = null;
|
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,
|
2020-06-11 00:00:59 -07:00
|
|
|
lastFetch: Date,
|
2020-08-10 23:35:56 -07:00
|
|
|
simulatedSignatures: Array<string>,
|
2018-10-22 20:03:44 -07:00
|
|
|
transactionSignatures: Array<string>,
|
|
|
|
};
|
2019-03-04 08:06:33 -08:00
|
|
|
_disableBlockhashCaching: boolean = false;
|
2020-08-25 21:59:56 -07:00
|
|
|
_pollingBlockhash: 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;
|
2020-02-03 07:22:11 -08:00
|
|
|
_signatureSubscriptions: {
|
|
|
|
[number]: SignatureSubscriptionInfo,
|
|
|
|
} = {};
|
|
|
|
_signatureSubscriptionCounter: number = 0;
|
2020-03-27 07:22:53 -07:00
|
|
|
_rootSubscriptions: {
|
|
|
|
[number]: RootSubscriptionInfo,
|
|
|
|
} = {};
|
|
|
|
_rootSubscriptionCounter: 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) {
|
2020-10-21 01:19:51 -07:00
|
|
|
this._rpcEndpoint = endpoint;
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
let url = urlParse(endpoint);
|
2020-10-06 09:41:18 -07:00
|
|
|
const useHttps = url.protocol === 'https:';
|
2018-10-26 21:37:39 -07:00
|
|
|
|
2020-10-06 09:41:18 -07:00
|
|
|
this._rpcRequest = createRpcRequest(url.href, useHttps);
|
2019-11-11 10:01:10 -08:00
|
|
|
this._commitment = commitment;
|
2019-03-04 08:06:33 -08:00
|
|
|
this._blockhashInfo = {
|
|
|
|
recentBlockhash: null,
|
2020-06-11 00:00:59 -07:00
|
|
|
lastFetch: new Date(0),
|
2018-10-22 20:03:44 -07:00
|
|
|
transactionSignatures: [],
|
2020-08-10 23:35:56 -07:00
|
|
|
simulatedSignatures: [],
|
2018-10-22 20:03:44 -07:00
|
|
|
};
|
2018-10-26 21:37:39 -07:00
|
|
|
|
2020-10-06 09:41:18 -07:00
|
|
|
url.protocol = useHttps ? 'wss:' : 'ws:';
|
2018-10-26 21:37:39 -07:00
|
|
|
url.host = '';
|
2020-10-01 21:49:27 -07:00
|
|
|
// Only shift the port by +1 as a convention for ws(s) only if given endpoint
|
|
|
|
// is explictly specifying the endpoint port (HTTP-based RPC), assuming
|
|
|
|
// we're directly trying to connect to solana-validator's ws listening port.
|
|
|
|
// When the endpoint omits the port, we're connecting to the protocol
|
|
|
|
// default ports: http(80) or https(443) and it's assumed we're behind a reverse
|
|
|
|
// proxy which manages WebSocket upgrade and backend port redirection.
|
2020-04-13 17:42:33 -07:00
|
|
|
if (url.port !== null) {
|
|
|
|
url.port = String(Number(url.port) + 1);
|
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),
|
|
|
|
);
|
2020-02-03 07:22:11 -08:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'signatureNotification',
|
|
|
|
this._wsOnSignatureNotification.bind(this),
|
|
|
|
);
|
2020-03-27 07:22:53 -07:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'rootNotification',
|
|
|
|
this._wsOnRootNotification.bind(this),
|
|
|
|
);
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2020-04-06 02:56:26 -07:00
|
|
|
/**
|
|
|
|
* The default commitment used for requests
|
|
|
|
*/
|
|
|
|
get commitment(): ?Commitment {
|
|
|
|
return this._commitment;
|
|
|
|
}
|
|
|
|
|
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>> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([publicKey.toBase58()], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
|
|
|
'failed to get balance for ' +
|
|
|
|
publicKey.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
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 => {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
2020-04-09 09:48:18 -07:00
|
|
|
'failed to get balance of account ' + publicKey.toBase58() + ': ' + e,
|
2020-04-08 16:49:51 -07:00
|
|
|
);
|
2019-11-13 14:31:31 -08:00
|
|
|
});
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2020-05-18 20:27:36 -07:00
|
|
|
/**
|
|
|
|
* Fetch the estimated production time of a block
|
|
|
|
*/
|
|
|
|
async getBlockTime(slot: number): Promise<number | null> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getBlockTime', [slot]);
|
|
|
|
const res = GetBlockTimeRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get block time for slot ' + slot + ': ' + res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2020-05-21 01:58:17 -07:00
|
|
|
/**
|
|
|
|
* 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', []);
|
2020-05-23 02:33:04 -07:00
|
|
|
const res = SlotRpcResult(unsafeRes);
|
2020-05-21 01:58:17 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get minimum ledger slot: ' + res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2020-05-23 02:33:04 -07:00
|
|
|
/**
|
|
|
|
* Fetch the slot of the lowest confirmed block that has not been purged from the ledger
|
|
|
|
*/
|
|
|
|
async getFirstAvailableBlock(): Promise<number> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getFirstAvailableBlock', []);
|
|
|
|
const res = SlotRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get first available block: ' + res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2020-05-22 04:30:22 -07:00
|
|
|
/**
|
|
|
|
* Fetch information about the current supply
|
|
|
|
*/
|
|
|
|
async getSupply(
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<RpcResponseAndContext<Supply>> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2020-05-22 04:30:22 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getSupply', args);
|
|
|
|
const res = GetSupplyRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to get supply: ' + res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
res.result.value.nonCirculatingAccounts = res.result.value.nonCirculatingAccounts.map(
|
|
|
|
account => new PublicKey(account),
|
|
|
|
);
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2020-07-30 21:33:54 -07:00
|
|
|
/**
|
|
|
|
* Fetch the current supply of a token mint
|
|
|
|
*/
|
|
|
|
async getTokenSupply(
|
|
|
|
tokenMintAddress: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
2020-08-05 21:17:29 -07:00
|
|
|
): Promise<RpcResponseAndContext<TokenAmount>> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([tokenMintAddress.toBase58()], commitment);
|
2020-07-30 21:33:54 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getTokenSupply', args);
|
|
|
|
const res = GetTokenSupplyRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to get token supply: ' + res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the current balance of a token account
|
|
|
|
*/
|
|
|
|
async getTokenAccountBalance(
|
|
|
|
tokenAddress: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
2020-08-05 21:17:29 -07:00
|
|
|
): Promise<RpcResponseAndContext<TokenAmount>> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([tokenAddress.toBase58()], commitment);
|
2020-07-30 21:33:54 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getTokenAccountBalance', args);
|
|
|
|
const res = GetTokenAccountBalance(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get token account balance: ' + res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all the token accounts owned by the specified account
|
|
|
|
*
|
2020-08-06 08:47:22 -07:00
|
|
|
* @return {Promise<RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>>>}
|
2020-07-30 21:33:54 -07:00
|
|
|
*/
|
|
|
|
async getTokenAccountsByOwner(
|
|
|
|
ownerAddress: PublicKey,
|
|
|
|
filter: TokenAccountsFilter,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<
|
2020-08-06 08:47:22 -07:00
|
|
|
RpcResponseAndContext<
|
|
|
|
Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>,
|
|
|
|
>,
|
2020-07-30 21:33:54 -07:00
|
|
|
> {
|
|
|
|
let _args = [ownerAddress.toBase58()];
|
2020-08-06 08:47:22 -07:00
|
|
|
if (filter.mint) {
|
|
|
|
_args.push({mint: filter.mint.toBase58()});
|
|
|
|
} else {
|
|
|
|
_args.push({programId: filter.programId.toBase58()});
|
|
|
|
}
|
2020-07-30 21:33:54 -07:00
|
|
|
|
2020-08-15 21:57:23 -07:00
|
|
|
const args = this._buildArgs(_args, commitment, 'base64');
|
2020-07-30 21:33:54 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
|
|
|
|
const res = GetTokenAccountsByOwner(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get token accounts owned by account ' +
|
|
|
|
ownerAddress.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {result} = res;
|
|
|
|
const {context, value} = result;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
return {
|
|
|
|
context,
|
2020-08-15 21:38:11 -07:00
|
|
|
value: value.map(result => {
|
2020-08-15 21:57:23 -07:00
|
|
|
assert(result.account.data[1] === 'base64');
|
2020-08-15 21:38:11 -07:00
|
|
|
return {
|
|
|
|
pubkey: new PublicKey(result.pubkey),
|
|
|
|
account: {
|
|
|
|
executable: result.account.executable,
|
|
|
|
owner: new PublicKey(result.account.owner),
|
|
|
|
lamports: result.account.lamports,
|
|
|
|
data: Buffer.from(result.account.data[0], 'base64'),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}),
|
2020-07-30 21:33:54 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* Fetch parsed token accounts owned by the specified account
|
|
|
|
*
|
|
|
|
* @return {Promise<RpcResponseAndContext<Array<{pubkey: PublicKey, account: AccountInfo<ParsedAccountData>}>>>}
|
|
|
|
*/
|
|
|
|
async getParsedTokenAccountsByOwner(
|
|
|
|
ownerAddress: PublicKey,
|
|
|
|
filter: TokenAccountsFilter,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<
|
|
|
|
RpcResponseAndContext<
|
|
|
|
Array<{pubkey: PublicKey, account: AccountInfo<ParsedAccountData>}>,
|
|
|
|
>,
|
|
|
|
> {
|
|
|
|
let _args = [ownerAddress.toBase58()];
|
|
|
|
if (filter.mint) {
|
|
|
|
_args.push({mint: filter.mint.toBase58()});
|
|
|
|
} else {
|
|
|
|
_args.push({programId: filter.programId.toBase58()});
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs(_args, commitment, 'jsonParsed');
|
2020-08-06 08:47:22 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
|
|
|
|
const res = GetParsedTokenAccountsByOwner(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get token accounts owned by account ' +
|
|
|
|
ownerAddress.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {result} = res;
|
|
|
|
const {context, value} = result;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
return {
|
|
|
|
context,
|
|
|
|
value: value.map(result => ({
|
|
|
|
pubkey: new PublicKey(result.pubkey),
|
|
|
|
account: {
|
|
|
|
executable: result.account.executable,
|
|
|
|
owner: new PublicKey(result.account.owner),
|
|
|
|
lamports: result.account.lamports,
|
|
|
|
data: result.account.data,
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-22 10:23:29 -07:00
|
|
|
/**
|
|
|
|
* Fetch the 20 largest accounts with their current balances
|
|
|
|
*/
|
|
|
|
async getLargestAccounts(
|
|
|
|
config: ?GetLargestAccountsConfig,
|
|
|
|
): Promise<RpcResponseAndContext<Array<AccountBalancePair>>> {
|
|
|
|
const arg = {
|
|
|
|
...config,
|
|
|
|
commitment: (config && config.commitment) || this.commitment,
|
|
|
|
};
|
|
|
|
const args = arg.filter || arg.commitment ? [arg] : [];
|
|
|
|
const unsafeRes = await this._rpcRequest('getLargestAccounts', args);
|
|
|
|
const res = GetLargestAccountsRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to get largest accounts: ' + res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
res.result.value = res.result.value.map(({address, lamports}) => ({
|
|
|
|
address: new PublicKey(address),
|
|
|
|
lamports,
|
|
|
|
}));
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:28:07 -07:00
|
|
|
/**
|
|
|
|
* Fetch the 20 largest token accounts with their current balances
|
|
|
|
* for a given mint.
|
|
|
|
*/
|
|
|
|
async getTokenLargestAccounts(
|
|
|
|
mintAddress: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<RpcResponseAndContext<Array<TokenAccountBalancePair>>> {
|
|
|
|
const args = this._buildArgs([mintAddress.toBase58()], commitment);
|
|
|
|
const unsafeRes = await this._rpcRequest('getTokenLargestAccounts', args);
|
|
|
|
const res = GetTokenLargestAccountsResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get token largest accounts: ' + res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
res.result.value = res.result.value.map(pair => ({
|
|
|
|
...pair,
|
|
|
|
address: new PublicKey(pair.address),
|
|
|
|
}));
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
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,
|
2020-08-06 08:47:22 -07:00
|
|
|
): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> {
|
2020-08-25 09:05:33 -07:00
|
|
|
const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64');
|
2019-11-11 10:01:10 -08:00
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
2020-04-09 09:48:18 -07:00
|
|
|
'failed to get info about account ' +
|
2020-04-08 16:49:51 -07:00
|
|
|
publicKey.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
2018-09-20 15:08:52 -07:00
|
|
|
}
|
2019-11-13 14:31:31 -08:00
|
|
|
assert(typeof res.result !== 'undefined');
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2020-04-05 01:18:45 -07:00
|
|
|
let value = null;
|
|
|
|
if (res.result.value) {
|
|
|
|
const {executable, owner, lamports, data} = res.result.value;
|
2020-08-15 21:57:23 -07:00
|
|
|
assert(data[1] === 'base64');
|
2020-04-05 01:18:45 -07:00
|
|
|
value = {
|
|
|
|
executable,
|
|
|
|
owner: new PublicKey(owner),
|
|
|
|
lamports,
|
2020-08-15 21:38:11 -07:00
|
|
|
data: Buffer.from(data[0], 'base64'),
|
2020-04-05 01:18:45 -07:00
|
|
|
};
|
2019-11-13 14:31:31 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* Fetch parsed account info for the specified public key
|
|
|
|
*/
|
|
|
|
async getParsedAccountInfo(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<
|
|
|
|
RpcResponseAndContext<AccountInfo<Buffer | ParsedAccountData> | null>,
|
|
|
|
> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs(
|
2020-08-06 08:47:22 -07:00
|
|
|
[publicKey.toBase58()],
|
|
|
|
commitment,
|
|
|
|
'jsonParsed',
|
|
|
|
);
|
|
|
|
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
|
|
|
|
const res = GetParsedAccountInfoResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get info about account ' +
|
|
|
|
publicKey.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
|
|
|
|
let value = null;
|
|
|
|
if (res.result.value) {
|
|
|
|
const {executable, owner, lamports, data: resultData} = res.result.value;
|
|
|
|
|
|
|
|
let data = resultData;
|
|
|
|
if (!data.program) {
|
2020-08-15 21:57:23 -07:00
|
|
|
assert(data[1] === 'base64');
|
2020-08-15 21:38:11 -07:00
|
|
|
data = Buffer.from(data[0], 'base64');
|
2020-08-06 08:47:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
value = {
|
|
|
|
executable,
|
|
|
|
owner: new PublicKey(owner),
|
|
|
|
lamports,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
context: {
|
|
|
|
slot: res.result.context.slot,
|
|
|
|
},
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
2020-08-06 08:47:22 -07:00
|
|
|
): Promise<AccountInfo<Buffer> | null> {
|
2019-11-13 14:31:31 -08:00
|
|
|
return await this.getAccountInfoAndContext(publicKey, commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
2020-04-09 09:48:18 -07:00
|
|
|
'failed to get info about account ' + publicKey.toBase58() + ': ' + e,
|
2020-04-08 16:49:51 -07:00
|
|
|
);
|
2019-11-13 14:31:31 -08:00
|
|
|
});
|
|
|
|
}
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2020-09-16 23:50:13 -07:00
|
|
|
/**
|
|
|
|
* Returns epoch activation information for a stake account that has been delegated
|
|
|
|
*/
|
|
|
|
async getStakeActivation(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
epoch: ?number,
|
|
|
|
): Promise<StakeActivationData> {
|
|
|
|
const args = this._buildArgs(
|
|
|
|
[publicKey.toBase58()],
|
|
|
|
commitment,
|
|
|
|
undefined,
|
|
|
|
epoch !== undefined ? {epoch} : undefined,
|
|
|
|
);
|
|
|
|
|
|
|
|
const unsafeRes = await this._rpcRequest('getStakeActivation', args);
|
|
|
|
const res = GetStakeActivationResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
`failed to get Stake Activation ${publicKey.toBase58()}: ${
|
|
|
|
res.error.message
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
|
|
|
|
const {state, active, inactive} = res.result;
|
|
|
|
return {state, active, inactive};
|
|
|
|
}
|
|
|
|
|
2019-06-28 18:28:06 -07:00
|
|
|
/**
|
|
|
|
* Fetch all the accounts owned by the specified program id
|
2020-02-14 07:01:01 -08:00
|
|
|
*
|
2020-08-06 08:47:22 -07:00
|
|
|
* @return {Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>>}
|
2019-06-28 18:28:06 -07:00
|
|
|
*/
|
|
|
|
async getProgramAccounts(
|
|
|
|
programId: PublicKey,
|
2019-11-11 10:01:10 -08:00
|
|
|
commitment: ?Commitment,
|
2020-08-06 08:47:22 -07:00
|
|
|
): Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>> {
|
2020-08-25 09:05:33 -07:00
|
|
|
const args = this._buildArgs([programId.toBase58()], commitment, 'base64');
|
2019-11-11 10:01:10 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
|
2019-06-28 18:28:06 -07:00
|
|
|
const res = GetProgramAccountsRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
2020-04-09 09:48:18 -07:00
|
|
|
'failed to get accounts owned by program ' +
|
2020-04-08 16:49:51 -07:00
|
|
|
programId.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
2019-06-28 18:28:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const {result} = res;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
return result.map(result => {
|
2020-08-15 21:57:23 -07:00
|
|
|
assert(result.account.data[1] === 'base64');
|
2020-01-15 13:04:26 -08:00
|
|
|
return {
|
2020-08-06 08:47:22 -07:00
|
|
|
pubkey: new PublicKey(result.pubkey),
|
2020-01-15 13:04:26 -08:00
|
|
|
account: {
|
|
|
|
executable: result.account.executable,
|
|
|
|
owner: new PublicKey(result.account.owner),
|
|
|
|
lamports: result.account.lamports,
|
2020-08-15 21:38:11 -07:00
|
|
|
data: Buffer.from(result.account.data[0], 'base64'),
|
2019-06-28 18:28:06 -07:00
|
|
|
},
|
2020-01-15 13:04:26 -08:00
|
|
|
};
|
2019-06-28 18:28:06 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:47:22 -07:00
|
|
|
/**
|
|
|
|
* Fetch and parse all the accounts owned by the specified program id
|
|
|
|
*
|
|
|
|
* @return {Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer | ParsedAccountData>}>>}
|
|
|
|
*/
|
|
|
|
async getParsedProgramAccounts(
|
|
|
|
programId: PublicKey,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<
|
|
|
|
Array<{
|
|
|
|
pubkey: PublicKey,
|
|
|
|
account: AccountInfo<Buffer | ParsedAccountData>,
|
|
|
|
}>,
|
|
|
|
> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs(
|
2020-08-06 08:47:22 -07:00
|
|
|
[programId.toBase58()],
|
|
|
|
commitment,
|
|
|
|
'jsonParsed',
|
|
|
|
);
|
|
|
|
const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
|
|
|
|
const res = GetParsedProgramAccountsRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get accounts owned by program ' +
|
|
|
|
programId.toBase58() +
|
|
|
|
': ' +
|
|
|
|
res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {result} = res;
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
|
|
|
|
return result.map(result => {
|
|
|
|
const resultData = result.account.data;
|
|
|
|
|
|
|
|
let data = resultData;
|
|
|
|
if (!data.program) {
|
2020-08-15 21:57:23 -07:00
|
|
|
assert(data[1] === 'base64');
|
2020-08-15 21:38:11 -07:00
|
|
|
data = Buffer.from(data[0], 'base64');
|
2020-08-06 08:47:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
pubkey: new PublicKey(result.pubkey),
|
|
|
|
account: {
|
|
|
|
executable: result.account.executable,
|
|
|
|
owner: new PublicKey(result.account.owner),
|
|
|
|
lamports: result.account.lamports,
|
|
|
|
data,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:44:50 -08:00
|
|
|
/**
|
2020-09-05 00:34:03 -07:00
|
|
|
* Confirm the transaction identified by the specified signature.
|
2020-01-08 13:44:50 -08:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async confirmTransaction(
|
|
|
|
signature: TransactionSignature,
|
2020-09-07 22:12:47 -07:00
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<RpcResponseAndContext<SignatureResult>> {
|
|
|
|
let decodedSignature;
|
|
|
|
try {
|
|
|
|
decodedSignature = bs58.decode(signature);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error('signature must be base58 encoded: ' + signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(decodedSignature.length === 64, 'signature has invalid length');
|
|
|
|
|
2020-06-15 03:32:57 -07:00
|
|
|
const start = Date.now();
|
2020-10-29 10:22:53 -07:00
|
|
|
const subscriptionCommitment = commitment || this.commitment;
|
2020-09-07 22:12:47 -07:00
|
|
|
|
|
|
|
let subscriptionId;
|
|
|
|
let response: RpcResponseAndContext<SignatureResult> | null = null;
|
|
|
|
const confirmPromise = new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
subscriptionId = this.onSignature(
|
|
|
|
signature,
|
|
|
|
(result, context) => {
|
|
|
|
subscriptionId = undefined;
|
|
|
|
response = {
|
|
|
|
context,
|
|
|
|
value: result,
|
|
|
|
};
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
subscriptionCommitment,
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let timeoutMs = 60 * 1000;
|
|
|
|
switch (subscriptionCommitment) {
|
|
|
|
case 'recent':
|
|
|
|
case 'single':
|
|
|
|
case 'singleGossip': {
|
|
|
|
timeoutMs = 10 * 1000;
|
2020-05-20 02:13:21 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-09-07 22:12:47 -07:00
|
|
|
// exhaust enums to ensure full coverage
|
|
|
|
case 'max':
|
|
|
|
case 'root':
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await promiseTimeout(confirmPromise, timeoutMs);
|
|
|
|
} finally {
|
|
|
|
if (subscriptionId) {
|
|
|
|
this.removeSignatureListener(subscriptionId);
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 02:13:21 -07:00
|
|
|
|
2020-09-07 22:12:47 -07:00
|
|
|
if (response === null) {
|
|
|
|
const duration = (Date.now() - start) / 1000;
|
|
|
|
throw new Error(
|
|
|
|
`Transaction was not confirmed in ${duration.toFixed(2)} seconds`,
|
|
|
|
);
|
2020-05-20 02:13:21 -07:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:12:47 -07:00
|
|
|
return response;
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get cluster nodes: ' + res.error.message);
|
2019-04-23 09:53:26 -07:00
|
|
|
}
|
|
|
|
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> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get vote accounts: ' + res.error.message);
|
2019-06-12 10:26:55 -07:00
|
|
|
}
|
|
|
|
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> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('getSlot', args);
|
2019-08-02 16:06:54 -07:00
|
|
|
const res = GetSlot(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get slot: ' + res.error.message);
|
2019-08-02 16:06:54 -07:00
|
|
|
}
|
|
|
|
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> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get slot leader: ' + res.error.message);
|
2019-04-23 09:53:26 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
2020-01-17 09:08:40 -08:00
|
|
|
* Fetch the current status of a signature
|
2018-09-26 19:16:17 -07:00
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
async getSignatureStatus(
|
|
|
|
signature: TransactionSignature,
|
2020-04-06 02:56:26 -07:00
|
|
|
config: ?SignatureStatusConfig,
|
2020-03-26 06:37:45 -07:00
|
|
|
): Promise<RpcResponseAndContext<SignatureStatus | null>> {
|
2020-04-01 10:51:30 -07:00
|
|
|
const {context, value} = await this.getSignatureStatuses(
|
2020-03-26 06:37:45 -07:00
|
|
|
[signature],
|
2020-04-06 02:56:26 -07:00
|
|
|
config,
|
2020-03-26 06:37:45 -07:00
|
|
|
);
|
|
|
|
assert(value.length === 1);
|
|
|
|
return {context, value: value[0]};
|
2020-03-23 08:01:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-06 02:56:26 -07:00
|
|
|
* Fetch the current statuses of a batch of signatures
|
2020-03-23 08:01:12 -07:00
|
|
|
*/
|
2020-04-01 10:51:30 -07:00
|
|
|
async getSignatureStatuses(
|
2020-03-23 08:01:12 -07:00
|
|
|
signatures: Array<TransactionSignature>,
|
2020-04-06 02:56:26 -07:00
|
|
|
config: ?SignatureStatusConfig,
|
2020-03-26 06:37:45 -07:00
|
|
|
): Promise<RpcResponseAndContext<Array<SignatureStatus | null>>> {
|
2020-04-06 02:56:26 -07:00
|
|
|
const params = [signatures];
|
|
|
|
if (config) {
|
|
|
|
params.push(config);
|
|
|
|
}
|
|
|
|
const unsafeRes = await this._rpcRequest('getSignatureStatuses', params);
|
2020-04-01 10:51:30 -07:00
|
|
|
const res = GetSignatureStatusesRpcResult(unsafeRes);
|
2018-09-26 19:16:17 -07:00
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get signature status: ' + res.error.message);
|
2018-09-26 19:16:17 -07:00
|
|
|
}
|
|
|
|
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> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('getTransactionCount', args);
|
2018-08-23 10:52:48 -07:00
|
|
|
const res = GetTransactionCountRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get transaction count: ' + res.error.message);
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
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> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('getTotalSupply', args);
|
2019-06-25 08:31:22 -07:00
|
|
|
const res = GetTotalSupplyRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('faied to get total supply: ' + res.error.message);
|
2019-06-25 08:31:22 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return Number(res.result);
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:21:39 -07:00
|
|
|
/**
|
2020-06-03 08:38:48 -07:00
|
|
|
* Fetch the cluster InflationGovernor parameters
|
2019-08-28 07:21:39 -07:00
|
|
|
*/
|
2020-06-03 08:38:48 -07:00
|
|
|
async getInflationGovernor(
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<InflationGovernor> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2020-06-03 08:38:48 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getInflationGovernor', args);
|
|
|
|
const res = GetInflationGovernorRpcResult(unsafeRes);
|
2019-08-28 07:21:39 -07:00
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get inflation: ' + res.error.message);
|
2019-08-28 07:21:39 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2020-06-03 08:38:48 -07:00
|
|
|
return GetInflationGovernorResult(res.result);
|
2019-08-28 07:21:39 -07:00
|
|
|
}
|
|
|
|
|
2019-10-29 09:50:05 -07:00
|
|
|
/**
|
|
|
|
* Fetch the Epoch Info parameters
|
|
|
|
*/
|
2020-05-21 02:11:32 -07:00
|
|
|
async getEpochInfo(commitment: ?Commitment): Promise<EpochInfo> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('getEpochInfo', args);
|
2019-10-29 09:50:05 -07:00
|
|
|
const res = GetEpochInfoRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get epoch info: ' + res.error.message);
|
2019-10-29 09:50:05 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return GetEpochInfoResult(res.result);
|
|
|
|
}
|
|
|
|
|
2019-10-23 06:48:24 -07:00
|
|
|
/**
|
|
|
|
* Fetch the Epoch Schedule parameters
|
|
|
|
*/
|
2020-05-21 02:11:32 -07:00
|
|
|
async getEpochSchedule(): Promise<EpochSchedule> {
|
2019-10-23 06:48:24 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('getEpochSchedule', []);
|
|
|
|
const res = GetEpochScheduleRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get epoch schedule: ' + res.error.message);
|
2019-10-23 06:48:24 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return GetEpochScheduleResult(res.result);
|
|
|
|
}
|
|
|
|
|
2020-07-17 08:16:44 -07:00
|
|
|
/**
|
|
|
|
* Fetch the leader schedule for the current epoch
|
|
|
|
* @return {Promise<RpcResponseAndContext<LeaderSchedule>>}
|
|
|
|
*/
|
|
|
|
async getLeaderSchedule(): Promise<LeaderSchedule> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getLeaderSchedule', []);
|
|
|
|
const res = GetLeaderScheduleRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to get leader schedule: ' + res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return 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> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([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
|
2020-02-14 07:01:01 -08:00
|
|
|
* @return {Promise<RpcResponseAndContext<{blockhash: Blockhash, feeCalculator: FeeCalculator}>>}
|
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,
|
2020-02-14 07:01:01 -08:00
|
|
|
): Promise<
|
|
|
|
RpcResponseAndContext<{blockhash: Blockhash, feeCalculator: FeeCalculator}>,
|
|
|
|
> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([], commitment);
|
2019-11-11 10:01:10 -08:00
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get recent blockhash: ' + res.error.message);
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2020-10-08 20:26:58 -07:00
|
|
|
/**
|
|
|
|
* Fetch recent performance samples
|
|
|
|
* @return {Promise<Array<PerfSample>>}
|
|
|
|
*/
|
|
|
|
async getRecentPerformanceSamples(
|
|
|
|
limit: ?number,
|
|
|
|
): Promise<Array<PerfSample>> {
|
|
|
|
const args = this._buildArgs(limit ? [limit] : []);
|
|
|
|
const unsafeRes = await this._rpcRequest(
|
|
|
|
'getRecentPerformanceSamples',
|
|
|
|
args,
|
|
|
|
);
|
|
|
|
|
|
|
|
const res = GetRecentPerformanceSamplesRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get recent performance samples: ' + res.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:12:59 -07:00
|
|
|
/**
|
|
|
|
* Fetch the fee calculator for a recent blockhash from the cluster, return with context
|
|
|
|
*/
|
|
|
|
async getFeeCalculatorForBlockhash(
|
|
|
|
blockhash: Blockhash,
|
|
|
|
commitment: ?Commitment,
|
|
|
|
): Promise<RpcResponseAndContext<FeeCalculator | null>> {
|
2020-08-10 01:26:48 -07:00
|
|
|
const args = this._buildArgs([blockhash], commitment);
|
2020-06-04 03:12:59 -07:00
|
|
|
const unsafeRes = await this._rpcRequest(
|
|
|
|
'getFeeCalculatorForBlockhash',
|
|
|
|
args,
|
|
|
|
);
|
|
|
|
|
|
|
|
const res = GetFeeCalculatorRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to get fee calculator: ' + res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
const {context, value} = res.result;
|
|
|
|
return {
|
|
|
|
context,
|
|
|
|
value: value && value.feeCalculator,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:44:50 -08:00
|
|
|
/**
|
|
|
|
* Fetch a recent blockhash from the cluster
|
2020-02-14 07:01:01 -08:00
|
|
|
* @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}
|
2020-01-08 13:44:50 -08:00
|
|
|
*/
|
2019-11-13 14:31:31 -08:00
|
|
|
async getRecentBlockhash(
|
|
|
|
commitment: ?Commitment,
|
2020-02-14 07:01:01 -08:00
|
|
|
): Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}> {
|
2019-11-13 14:31:31 -08:00
|
|
|
return await this.getRecentBlockhashAndContext(commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get recent blockhash: ' + e);
|
2019-11-13 14:31:31 -08:00
|
|
|
});
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to get version: ' + res.error.message);
|
2019-11-11 17:09:00 -08:00
|
|
|
}
|
|
|
|
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]);
|
2020-10-28 20:23:07 -07:00
|
|
|
const res = GetConfirmedBlockRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to get confirmed block: ' + res.error.message);
|
2019-11-12 08:21:19 -08:00
|
|
|
}
|
2020-10-28 20:23:07 -07:00
|
|
|
const result = res.result;
|
2020-06-10 22:15:14 -07:00
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
if (!result) {
|
2020-02-03 00:59:39 -08:00
|
|
|
throw new Error('Confirmed block ' + slot + ' not found');
|
2020-01-22 12:05:19 -08:00
|
|
|
}
|
2019-11-19 14:38:06 -08:00
|
|
|
return {
|
2020-06-10 22:15:14 -07:00
|
|
|
blockhash: new PublicKey(result.blockhash).toString(),
|
|
|
|
previousBlockhash: new PublicKey(result.previousBlockhash).toString(),
|
|
|
|
parentSlot: result.parentSlot,
|
|
|
|
transactions: result.transactions.map(result => {
|
|
|
|
const {message, signatures} = result.transaction;
|
2020-01-15 13:04:26 -08:00
|
|
|
return {
|
2020-06-10 22:15:14 -07:00
|
|
|
transaction: Transaction.populate(new Message(message), signatures),
|
2020-01-15 13:04:26 -08:00
|
|
|
meta: result.meta,
|
|
|
|
};
|
2019-11-19 14:38:06 -08:00
|
|
|
}),
|
2020-06-10 22:15:14 -07:00
|
|
|
rewards: result.rewards || [],
|
2019-11-19 14:38:06 -08:00
|
|
|
};
|
2019-11-12 08:21:19 -08:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:40:44 -07:00
|
|
|
/**
|
|
|
|
* Fetch a transaction details for a confirmed transaction
|
|
|
|
*/
|
|
|
|
async getConfirmedTransaction(
|
|
|
|
signature: TransactionSignature,
|
|
|
|
): Promise<ConfirmedTransaction | null> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', [
|
|
|
|
signature,
|
|
|
|
]);
|
|
|
|
const {result, error} = GetConfirmedTransactionRpcResult(unsafeRes);
|
|
|
|
if (error) {
|
|
|
|
throw new Error('failed to get confirmed transaction: ' + error.message);
|
|
|
|
}
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
if (result === null) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-10 22:15:14 -07:00
|
|
|
const {message, signatures} = result.transaction;
|
2020-04-21 00:40:44 -07:00
|
|
|
return {
|
|
|
|
slot: result.slot,
|
2020-06-10 22:15:14 -07:00
|
|
|
transaction: Transaction.populate(new Message(message), signatures),
|
2020-04-21 00:40:44 -07:00
|
|
|
meta: result.meta,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-06 04:16:01 -07:00
|
|
|
/**
|
|
|
|
* Fetch parsed transaction details for a confirmed transaction
|
|
|
|
*/
|
|
|
|
async getParsedConfirmedTransaction(
|
|
|
|
signature: TransactionSignature,
|
|
|
|
): Promise<ParsedConfirmedTransaction | null> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', [
|
|
|
|
signature,
|
|
|
|
'jsonParsed',
|
|
|
|
]);
|
|
|
|
const {result, error} = GetParsedConfirmedTransactionRpcResult(unsafeRes);
|
|
|
|
if (error) {
|
|
|
|
throw new Error('failed to get confirmed transaction: ' + error.message);
|
|
|
|
}
|
|
|
|
assert(typeof result !== 'undefined');
|
|
|
|
if (result === null) return result;
|
|
|
|
|
|
|
|
const {
|
|
|
|
accountKeys,
|
|
|
|
instructions,
|
|
|
|
recentBlockhash,
|
|
|
|
} = result.transaction.message;
|
|
|
|
return {
|
|
|
|
slot: result.slot,
|
|
|
|
meta: result.meta,
|
|
|
|
transaction: {
|
|
|
|
signatures: result.transaction.signatures,
|
|
|
|
message: {
|
|
|
|
accountKeys: accountKeys.map(accountKey => ({
|
|
|
|
pubkey: new PublicKey(accountKey.pubkey),
|
|
|
|
signer: accountKey.signer,
|
|
|
|
writable: accountKey.writable,
|
|
|
|
})),
|
|
|
|
instructions: instructions.map(ix => {
|
|
|
|
let mapped: any = {programId: new PublicKey(ix.programId)};
|
|
|
|
if ('accounts' in ix) {
|
|
|
|
mapped.accounts = ix.accounts.map(key => new PublicKey(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...ix,
|
|
|
|
...mapped,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
recentBlockhash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:40:44 -07:00
|
|
|
/**
|
|
|
|
* Fetch a list of all the confirmed signatures for transactions involving an address
|
|
|
|
* within a specified slot range. Max range allowed is 10,000 slots.
|
|
|
|
*
|
|
|
|
* @param address queried address
|
|
|
|
* @param startSlot start slot, inclusive
|
|
|
|
* @param endSlot end slot, inclusive
|
|
|
|
*/
|
|
|
|
async getConfirmedSignaturesForAddress(
|
|
|
|
address: PublicKey,
|
|
|
|
startSlot: number,
|
|
|
|
endSlot: number,
|
|
|
|
): Promise<Array<TransactionSignature>> {
|
|
|
|
const unsafeRes = await this._rpcRequest(
|
|
|
|
'getConfirmedSignaturesForAddress',
|
|
|
|
[address.toBase58(), startSlot, endSlot],
|
|
|
|
);
|
|
|
|
const result = GetConfirmedSignaturesForAddressRpcResult(unsafeRes);
|
|
|
|
if (result.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get confirmed signatures for address: ' +
|
|
|
|
result.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof result.result !== 'undefined');
|
|
|
|
return result.result;
|
|
|
|
}
|
|
|
|
|
2020-07-29 22:40:46 -07:00
|
|
|
/**
|
|
|
|
* Returns confirmed signatures for transactions involving an
|
|
|
|
* address backwards in time from the provided signature or most recent confirmed block
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param address queried address
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
async getConfirmedSignaturesForAddress2(
|
|
|
|
address: PublicKey,
|
|
|
|
options: ?ConfirmedSignaturesForAddress2Options,
|
|
|
|
): Promise<Array<ConfirmedSignatureInfo>> {
|
|
|
|
const unsafeRes = await this._rpcRequest(
|
|
|
|
'getConfirmedSignaturesForAddress2',
|
|
|
|
[address.toBase58(), options],
|
|
|
|
);
|
|
|
|
const result = GetConfirmedSignaturesForAddress2RpcResult(unsafeRes);
|
|
|
|
if (result.error) {
|
|
|
|
throw new Error(
|
|
|
|
'failed to get confirmed signatures for address: ' +
|
|
|
|
result.error.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
assert(typeof result.result !== 'undefined');
|
|
|
|
return result.result;
|
|
|
|
}
|
|
|
|
|
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,
|
2020-04-05 01:18:45 -07:00
|
|
|
): Promise<RpcResponseAndContext<NonceAccount | null>> {
|
|
|
|
const {context, value: accountInfo} = await this.getAccountInfoAndContext(
|
|
|
|
nonceAccount,
|
2020-01-02 17:54:43 -08:00
|
|
|
commitment,
|
|
|
|
);
|
|
|
|
|
2020-04-05 01:18:45 -07:00
|
|
|
let value = null;
|
|
|
|
if (accountInfo !== null) {
|
|
|
|
value = NonceAccount.fromAccountData(accountInfo.data);
|
|
|
|
}
|
2020-01-02 17:54:43 -08:00
|
|
|
|
|
|
|
return {
|
2020-04-05 01:18:45 -07:00
|
|
|
context,
|
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,
|
2020-04-05 01:18:45 -07:00
|
|
|
): Promise<NonceAccount | null> {
|
2020-01-02 17:54:43 -08:00
|
|
|
return await this.getNonceAndContext(nonceAccount, commitment)
|
|
|
|
.then(x => x.value)
|
|
|
|
.catch(e => {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
|
|
|
'failed to get nonce for account ' +
|
|
|
|
nonceAccount.toBase58() +
|
|
|
|
': ' +
|
|
|
|
e,
|
|
|
|
);
|
2020-01-02 17:54:43 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
): Promise<TransactionSignature> {
|
2020-06-10 09:10:25 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('requestAirdrop', [
|
|
|
|
to.toBase58(),
|
|
|
|
amount,
|
|
|
|
]);
|
2018-08-23 16:39:52 -07:00
|
|
|
const res = RequestAirdropRpcResult(unsafeRes);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
|
|
|
'airdrop to ' + to.toBase58() + ' failed: ' + res.error.message,
|
|
|
|
);
|
2018-08-23 16:39:52 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
2018-09-12 17:41:20 -07:00
|
|
|
return res.result;
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
|
|
|
|
2020-08-10 23:35:56 -07:00
|
|
|
async _recentBlockhash(disableCache: boolean): Promise<Blockhash> {
|
|
|
|
if (!disableCache) {
|
2020-08-25 21:59:56 -07:00
|
|
|
// Wait for polling to finish
|
|
|
|
while (this._pollingBlockhash) {
|
|
|
|
await sleep(100);
|
|
|
|
}
|
2020-08-10 23:35:56 -07:00
|
|
|
// Attempt to use a recent blockhash for up to 30 seconds
|
|
|
|
const expired =
|
|
|
|
Date.now() - this._blockhashInfo.lastFetch >=
|
|
|
|
BLOCKHASH_CACHE_TIMEOUT_MS;
|
|
|
|
if (this._blockhashInfo.recentBlockhash !== null && !expired) {
|
|
|
|
return this._blockhashInfo.recentBlockhash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this._pollNewBlockhash();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _pollNewBlockhash(): Promise<Blockhash> {
|
2020-08-25 21:59:56 -07:00
|
|
|
this._pollingBlockhash = true;
|
|
|
|
try {
|
|
|
|
const startTime = Date.now();
|
|
|
|
for (let i = 0; i < 50; i++) {
|
|
|
|
const {blockhash} = await this.getRecentBlockhash('max');
|
|
|
|
|
|
|
|
if (this._blockhashInfo.recentBlockhash != blockhash) {
|
|
|
|
this._blockhashInfo = {
|
|
|
|
recentBlockhash: blockhash,
|
|
|
|
lastFetch: new Date(),
|
|
|
|
transactionSignatures: [],
|
|
|
|
simulatedSignatures: [],
|
|
|
|
};
|
|
|
|
return blockhash;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep for approximately half a slot
|
|
|
|
await sleep(MS_PER_SLOT / 2);
|
2020-08-10 23:35:56 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 21:59:56 -07:00
|
|
|
throw new Error(
|
|
|
|
`Unable to obtain a new blockhash after ${Date.now() - startTime}ms`,
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
this._pollingBlockhash = false;
|
2020-08-10 23:35:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulate a transaction
|
|
|
|
*/
|
|
|
|
async simulateTransaction(
|
|
|
|
transaction: Transaction,
|
|
|
|
signers?: Array<Account>,
|
|
|
|
): Promise<RpcResponseAndContext<SimulatedTransactionResponse>> {
|
|
|
|
if (transaction.nonceInfo && signers) {
|
|
|
|
transaction.sign(...signers);
|
|
|
|
} else {
|
|
|
|
let disableCache = this._disableBlockhashCaching;
|
|
|
|
for (;;) {
|
|
|
|
transaction.recentBlockhash = await this._recentBlockhash(disableCache);
|
|
|
|
|
|
|
|
if (!signers) break;
|
|
|
|
|
|
|
|
transaction.sign(...signers);
|
|
|
|
if (!transaction.signature) {
|
|
|
|
throw new Error('!signature'); // should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the signature of this transaction has not been seen before with the
|
|
|
|
// current recentBlockhash, all done.
|
|
|
|
const signature = transaction.signature.toString('base64');
|
|
|
|
if (
|
|
|
|
!this._blockhashInfo.simulatedSignatures.includes(signature) &&
|
|
|
|
!this._blockhashInfo.transactionSignatures.includes(signature)
|
|
|
|
) {
|
|
|
|
this._blockhashInfo.simulatedSignatures.push(signature);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
disableCache = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const signData = transaction.serializeMessage();
|
|
|
|
const wireTransaction = transaction._serialize(signData);
|
2020-10-16 08:54:30 -07:00
|
|
|
const encodedTransaction = wireTransaction.toString('base64');
|
|
|
|
const config: any = {encoding: 'base64'};
|
|
|
|
const args = [encodedTransaction, config];
|
2020-08-10 23:35:56 -07:00
|
|
|
|
|
|
|
if (signers) {
|
2020-10-16 08:54:30 -07:00
|
|
|
config.sigVerify = true;
|
2020-08-10 23:35:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsafeRes = await this._rpcRequest('simulateTransaction', args);
|
|
|
|
const res = SimulatedTransactionResponseValidator(unsafeRes);
|
|
|
|
if (res.error) {
|
|
|
|
throw new Error('failed to simulate transaction: ' + res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
assert(res.result);
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
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,
|
2020-05-20 02:13:21 -07:00
|
|
|
signers: Array<Account>,
|
2020-06-03 04:55:42 -07:00
|
|
|
options?: SendOptions,
|
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 {
|
2020-08-10 23:35:56 -07:00
|
|
|
let disableCache = this._disableBlockhashCaching;
|
2020-01-07 16:57:56 -08:00
|
|
|
for (;;) {
|
2020-08-10 23:35:56 -07:00
|
|
|
transaction.recentBlockhash = await this._recentBlockhash(disableCache);
|
|
|
|
transaction.sign(...signers);
|
|
|
|
if (!transaction.signature) {
|
|
|
|
throw new Error('!signature'); // should never happen
|
2018-10-22 20:03:44 -07:00
|
|
|
}
|
|
|
|
|
2020-08-10 23:35:56 -07:00
|
|
|
// If the signature of this transaction has not been seen before with the
|
|
|
|
// current recentBlockhash, all done.
|
|
|
|
const signature = transaction.signature.toString('base64');
|
|
|
|
if (!this._blockhashInfo.transactionSignatures.includes(signature)) {
|
|
|
|
this._blockhashInfo.transactionSignatures.push(signature);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
disableCache = true;
|
2020-01-07 16:57:56 -08:00
|
|
|
}
|
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();
|
2020-06-03 04:55:42 -07:00
|
|
|
return await this.sendRawTransaction(wireTransaction, options);
|
2018-11-27 21:06:03 -08:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('validator exit failed: ' + res.error.message);
|
2019-04-24 15:22:50 -07:00
|
|
|
}
|
|
|
|
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(
|
2020-02-12 16:25:22 -08:00
|
|
|
rawTransaction: Buffer | Uint8Array | Array<number>,
|
2020-06-03 04:55:42 -07:00
|
|
|
options: ?SendOptions,
|
2020-01-21 21:16:56 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2020-10-16 08:54:30 -07:00
|
|
|
const encodedTransaction = toBuffer(rawTransaction).toString('base64');
|
2020-06-03 04:55:42 -07:00
|
|
|
const result = await this.sendEncodedTransaction(
|
|
|
|
encodedTransaction,
|
|
|
|
options,
|
|
|
|
);
|
2020-01-21 21:16:56 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a transaction that has already been signed, serialized into the
|
2020-10-16 08:54:30 -07:00
|
|
|
* wire format, and encoded as a base64 string
|
2020-01-21 21:16:56 -08:00
|
|
|
*/
|
|
|
|
async sendEncodedTransaction(
|
|
|
|
encodedTransaction: string,
|
2020-06-03 04:55:42 -07:00
|
|
|
options: ?SendOptions,
|
2018-11-27 21:06:03 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2020-10-16 08:54:30 -07:00
|
|
|
const config: any = {encoding: 'base64'};
|
|
|
|
const args = [encodedTransaction, config];
|
2020-06-03 04:55:42 -07:00
|
|
|
const skipPreflight = options && options.skipPreflight;
|
2020-09-24 17:49:34 -07:00
|
|
|
const preflightCommitment = options && options.preflightCommitment;
|
|
|
|
|
|
|
|
if (skipPreflight && preflightCommitment) {
|
|
|
|
throw new Error(
|
|
|
|
'cannot set preflightCommitment when skipPreflight is enabled',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skipPreflight) {
|
2020-10-16 08:54:30 -07:00
|
|
|
config.skipPreflight = skipPreflight;
|
2020-09-24 17:49:34 -07:00
|
|
|
} else if (preflightCommitment) {
|
2020-10-16 08:54:30 -07:00
|
|
|
config.preflightCommitment = preflightCommitment;
|
2020-09-24 17:49:34 -07:00
|
|
|
}
|
|
|
|
|
2020-06-03 04:55:42 -07:00
|
|
|
const unsafeRes = await this._rpcRequest('sendTransaction', args);
|
2018-11-28 11:56:50 -08:00
|
|
|
const res = SendTransactionRpcResult(unsafeRes);
|
2018-08-23 20:10:30 -07:00
|
|
|
if (res.error) {
|
2020-10-28 17:59:16 -07:00
|
|
|
if (res.error.data) {
|
|
|
|
const logs = res.error.data.logs;
|
|
|
|
if (logs && Array.isArray(logs)) {
|
|
|
|
const traceIndent = '\n ';
|
|
|
|
const logTrace = traceIndent + logs.join(traceIndent);
|
|
|
|
console.error(res.error.message, logTrace);
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('failed to send transaction: ' + res.error.message);
|
2018-08-23 20:10:30 -07:00
|
|
|
}
|
|
|
|
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() {
|
2020-09-07 08:12:22 -07:00
|
|
|
this._rpcWebSocketConnected = true;
|
2020-09-06 19:24:16 -07:00
|
|
|
this._rpcWebSocketHeartbeat = setInterval(() => {
|
|
|
|
// Ping server every 5s to prevent idle timeouts
|
|
|
|
this._rpcWebSocket.notify('ping').catch(() => {});
|
|
|
|
}, 5000);
|
2018-10-26 21:37:39 -07:00
|
|
|
this._updateSubscriptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnError(err: Error) {
|
2020-04-09 04:34:33 -07:00
|
|
|
console.error('ws error:', err.message);
|
2018-10-26 21:37:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2020-09-07 08:12:22 -07:00
|
|
|
_wsOnClose(code: number) {
|
2020-09-06 19:24:16 -07:00
|
|
|
clearInterval(this._rpcWebSocketHeartbeat);
|
|
|
|
this._rpcWebSocketHeartbeat = null;
|
2020-09-07 08:12:22 -07:00
|
|
|
|
|
|
|
if (code === 1000) {
|
|
|
|
// explicit close, check if any subscriptions have been made since close
|
|
|
|
this._updateSubscriptions();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// implicit close, prepare subscriptions for auto-reconnect
|
2020-04-09 04:34:33 -07:00
|
|
|
this._resetSubscriptions();
|
2018-10-26 21:37:39 -07:00
|
|
|
}
|
|
|
|
|
2020-02-10 06:45:52 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _subscribe<SubInfo: {subscriptionId: ?SubscriptionId}, RpcArgs>(
|
|
|
|
sub: SubInfo,
|
|
|
|
rpcMethod: string,
|
|
|
|
rpcArgs: RpcArgs,
|
|
|
|
) {
|
|
|
|
if (sub.subscriptionId == null) {
|
|
|
|
sub.subscriptionId = 'subscribing';
|
|
|
|
try {
|
|
|
|
const id = await this._rpcWebSocket.call(rpcMethod, rpcArgs);
|
|
|
|
if (sub.subscriptionId === 'subscribing') {
|
|
|
|
// eslint-disable-next-line require-atomic-updates
|
|
|
|
sub.subscriptionId = id;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (sub.subscriptionId === 'subscribing') {
|
|
|
|
// eslint-disable-next-line require-atomic-updates
|
|
|
|
sub.subscriptionId = null;
|
|
|
|
}
|
|
|
|
console.error(`${rpcMethod} error for argument`, rpcArgs, err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _unsubscribe<SubInfo: {subscriptionId: ?SubscriptionId}>(
|
|
|
|
sub: SubInfo,
|
|
|
|
rpcMethod: string,
|
|
|
|
) {
|
|
|
|
const subscriptionId = sub.subscriptionId;
|
|
|
|
if (subscriptionId != null && typeof subscriptionId != 'string') {
|
|
|
|
const unsubscribeId: number = subscriptionId;
|
|
|
|
try {
|
|
|
|
await this._rpcWebSocket.call(rpcMethod, [unsubscribeId]);
|
|
|
|
} catch (err) {
|
2020-04-09 04:34:33 -07:00
|
|
|
console.error(`${rpcMethod} error:`, err.message);
|
2020-02-10 06:45:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 04:34:33 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_resetSubscriptions() {
|
|
|
|
(Object.values(this._accountChangeSubscriptions): any).forEach(
|
|
|
|
s => (s.subscriptionId = null),
|
|
|
|
);
|
|
|
|
(Object.values(this._programAccountChangeSubscriptions): any).forEach(
|
|
|
|
s => (s.subscriptionId = null),
|
|
|
|
);
|
|
|
|
(Object.values(this._signatureSubscriptions): any).forEach(
|
|
|
|
s => (s.subscriptionId = null),
|
|
|
|
);
|
|
|
|
(Object.values(this._slotSubscriptions): any).forEach(
|
|
|
|
s => (s.subscriptionId = null),
|
|
|
|
);
|
|
|
|
(Object.values(this._rootSubscriptions): any).forEach(
|
|
|
|
s => (s.subscriptionId = null),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2020-02-11 06:33:50 -08:00
|
|
|
_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);
|
2020-02-03 07:22:11 -08:00
|
|
|
const signatureKeys = Object.keys(this._signatureSubscriptions).map(Number);
|
2020-03-27 07:22:53 -07:00
|
|
|
const rootKeys = Object.keys(this._rootSubscriptions).map(Number);
|
2019-11-25 08:04:35 -08:00
|
|
|
if (
|
|
|
|
accountKeys.length === 0 &&
|
|
|
|
programKeys.length === 0 &&
|
2020-02-03 07:22:11 -08:00
|
|
|
slotKeys.length === 0 &&
|
2020-03-27 07:22:53 -07:00
|
|
|
signatureKeys.length === 0 &&
|
|
|
|
rootKeys.length === 0
|
2019-11-25 08:04:35 -08:00
|
|
|
) {
|
2020-09-07 08:12:22 -07:00
|
|
|
if (this._rpcWebSocketConnected) {
|
|
|
|
this._rpcWebSocketConnected = false;
|
|
|
|
this._rpcWebSocketIdleTimeout = setTimeout(() => {
|
|
|
|
this._rpcWebSocketIdleTimeout = null;
|
|
|
|
this._rpcWebSocket.close();
|
|
|
|
}, 500);
|
|
|
|
}
|
2018-10-26 21:37:39 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-07 08:12:22 -07:00
|
|
|
if (this._rpcWebSocketIdleTimeout !== null) {
|
|
|
|
clearTimeout(this._rpcWebSocketIdleTimeout);
|
|
|
|
this._rpcWebSocketIdleTimeout = null;
|
|
|
|
this._rpcWebSocketConnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._rpcWebSocketConnected) {
|
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) {
|
2020-02-11 06:33:50 -08:00
|
|
|
const sub = this._accountChangeSubscriptions[id];
|
2020-05-20 21:17:38 -07:00
|
|
|
this._subscribe(
|
|
|
|
sub,
|
|
|
|
'accountSubscribe',
|
2020-08-15 21:57:23 -07:00
|
|
|
this._buildArgs([sub.publicKey], sub.commitment, 'base64'),
|
2020-05-20 21:17:38 -07:00
|
|
|
);
|
2018-10-26 21:37:39 -07:00
|
|
|
}
|
2020-02-10 06:45:52 -08:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
for (let id of programKeys) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const sub = this._programAccountChangeSubscriptions[id];
|
2020-05-20 21:17:38 -07:00
|
|
|
this._subscribe(
|
|
|
|
sub,
|
|
|
|
'programSubscribe',
|
2020-08-15 21:57:23 -07:00
|
|
|
this._buildArgs([sub.programId], sub.commitment, 'base64'),
|
2020-05-20 21:17:38 -07:00
|
|
|
);
|
2019-03-08 16:02:39 -08:00
|
|
|
}
|
2020-02-10 06:45:52 -08:00
|
|
|
|
2019-11-25 08:04:35 -08:00
|
|
|
for (let id of slotKeys) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const sub = this._slotSubscriptions[id];
|
2020-02-11 06:33:50 -08:00
|
|
|
this._subscribe(sub, 'slotSubscribe', []);
|
2019-11-25 08:04:35 -08:00
|
|
|
}
|
2020-02-10 06:45:52 -08:00
|
|
|
|
2020-02-03 07:22:11 -08:00
|
|
|
for (let id of signatureKeys) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const sub = this._signatureSubscriptions[id];
|
2020-05-20 21:17:38 -07:00
|
|
|
this._subscribe(
|
|
|
|
sub,
|
|
|
|
'signatureSubscribe',
|
2020-08-10 01:26:48 -07:00
|
|
|
this._buildArgs([sub.signature], sub.commitment),
|
2020-05-20 21:17:38 -07:00
|
|
|
);
|
2020-02-03 07:22:11 -08:00
|
|
|
}
|
2020-03-27 07:22:53 -07:00
|
|
|
|
|
|
|
for (let id of rootKeys) {
|
|
|
|
const sub = this._rootSubscriptions[id];
|
|
|
|
this._subscribe(sub, 'rootSubscribe', []);
|
|
|
|
}
|
2019-03-08 16:02:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnAccountNotification(notification: Object) {
|
|
|
|
const res = AccountNotificationResult(notification);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('account notification failed: ' + res.error.message);
|
2019-03-08 16:02:39 -08:00
|
|
|
}
|
2020-03-23 07:19:32 -07:00
|
|
|
assert(typeof res.result !== 'undefined');
|
2019-03-08 16:02:39 -08:00
|
|
|
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;
|
2020-03-23 07:19:32 -07:00
|
|
|
const {value, context} = result;
|
|
|
|
|
2020-08-19 21:36:42 -07:00
|
|
|
assert(value.data[1] === 'base64');
|
2020-03-23 07:19:32 -07:00
|
|
|
sub.callback(
|
|
|
|
{
|
|
|
|
executable: value.executable,
|
|
|
|
owner: new PublicKey(value.owner),
|
|
|
|
lamports: value.lamports,
|
2020-08-19 21:36:42 -07:00
|
|
|
data: Buffer.from(value.data[0], 'base64'),
|
2020-03-23 07:19:32 -07:00
|
|
|
},
|
|
|
|
context,
|
|
|
|
);
|
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
|
|
|
|
*
|
2020-05-20 21:17:38 -07:00
|
|
|
* @param publicKey Public key of the account to monitor
|
2018-10-26 21:37:39 -07:00
|
|
|
* @param callback Function to invoke whenever the account is changed
|
2020-05-20 21:17:38 -07:00
|
|
|
* @param commitment Specify the commitment level account changes must reach before notification
|
2018-10-26 21:37:39 -07:00
|
|
|
* @return subscription id
|
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
onAccountChange(
|
|
|
|
publicKey: PublicKey,
|
|
|
|
callback: AccountChangeCallback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment: ?Commitment,
|
2018-11-04 11:41:21 -08:00
|
|
|
): number {
|
2018-10-26 21:37:39 -07:00
|
|
|
const id = ++this._accountChangeSubscriptionCounter;
|
|
|
|
this._accountChangeSubscriptions[id] = {
|
|
|
|
publicKey: publicKey.toBase58(),
|
|
|
|
callback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment,
|
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]) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const subInfo = this._accountChangeSubscriptions[id];
|
2018-10-26 21:37:39 -07:00
|
|
|
delete this._accountChangeSubscriptions[id];
|
2020-02-10 06:45:52 -08:00
|
|
|
await this._unsubscribe(subInfo, 'accountUnsubscribe');
|
2018-10-26 21:37:39 -07:00
|
|
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error(
|
|
|
|
'program account notification failed: ' + res.error.message,
|
|
|
|
);
|
2019-03-08 16:02:39 -08:00
|
|
|
}
|
2020-03-23 07:19:32 -07:00
|
|
|
assert(typeof res.result !== 'undefined');
|
2019-03-08 16:02:39 -08:00
|
|
|
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;
|
2020-03-23 07:19:32 -07:00
|
|
|
const {value, context} = result;
|
|
|
|
|
2020-08-15 21:57:23 -07:00
|
|
|
assert(value.account.data[1] === 'base64');
|
2020-03-23 07:19:32 -07:00
|
|
|
sub.callback(
|
|
|
|
{
|
|
|
|
accountId: value.pubkey,
|
|
|
|
accountInfo: {
|
|
|
|
executable: value.account.executable,
|
|
|
|
owner: new PublicKey(value.account.owner),
|
|
|
|
lamports: value.account.lamports,
|
2020-08-15 21:38:11 -07:00
|
|
|
data: Buffer.from(value.account.data[0], 'base64'),
|
2020-03-23 07:19:32 -07:00
|
|
|
},
|
2019-03-08 16:02:39 -08:00
|
|
|
},
|
2020-03-23 07:19:32 -07:00
|
|
|
context,
|
|
|
|
);
|
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
|
2020-05-20 21:17:38 -07:00
|
|
|
* @param commitment Specify the commitment level account changes must reach before notification
|
2019-03-08 16:02:39 -08:00
|
|
|
* @return subscription id
|
|
|
|
*/
|
|
|
|
onProgramAccountChange(
|
|
|
|
programId: PublicKey,
|
|
|
|
callback: ProgramAccountChangeCallback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment: ?Commitment,
|
2019-03-08 16:02:39 -08:00
|
|
|
): number {
|
|
|
|
const id = ++this._programAccountChangeSubscriptionCounter;
|
|
|
|
this._programAccountChangeSubscriptions[id] = {
|
|
|
|
programId: programId.toBase58(),
|
|
|
|
callback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment,
|
2019-03-08 16:02:39 -08:00
|
|
|
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]) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const subInfo = this._programAccountChangeSubscriptions[id];
|
2019-03-08 16:02:39 -08:00
|
|
|
delete this._programAccountChangeSubscriptions[id];
|
2020-02-10 06:45:52 -08:00
|
|
|
await this._unsubscribe(subInfo, 'programUnsubscribe');
|
2019-03-08 16:02:39 -08:00
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
2020-02-10 06:45:52 -08:00
|
|
|
throw new Error(`Unknown program account change id: ${id}`);
|
2019-03-08 16:02:39 -08:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('slot notification failed: ' + res.error.message);
|
2019-11-25 08:04:35 -08:00
|
|
|
}
|
|
|
|
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,
|
2020-02-03 08:18:55 -08:00
|
|
|
subscriptionId: null,
|
2019-11-25 08:04:35 -08:00
|
|
|
};
|
|
|
|
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]) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const subInfo = this._slotSubscriptions[id];
|
2019-11-25 08:04:35 -08:00
|
|
|
delete this._slotSubscriptions[id];
|
2020-02-10 06:45:52 -08:00
|
|
|
await this._unsubscribe(subInfo, 'slotUnsubscribe');
|
2019-11-25 08:04:35 -08:00
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown slot change id: ${id}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:26:48 -07:00
|
|
|
_buildArgs(
|
2020-08-06 08:47:22 -07:00
|
|
|
args: Array<any>,
|
|
|
|
override: ?Commitment,
|
2020-08-15 21:57:23 -07:00
|
|
|
encoding?: 'jsonParsed' | 'base64',
|
2020-09-16 23:50:13 -07:00
|
|
|
extra?: any,
|
2020-08-06 08:47:22 -07:00
|
|
|
): Array<any> {
|
2019-11-11 10:01:10 -08:00
|
|
|
const commitment = override || this._commitment;
|
2020-09-16 23:50:13 -07:00
|
|
|
if (commitment || encoding || extra) {
|
2020-08-06 08:47:22 -07:00
|
|
|
let options: any = {};
|
|
|
|
if (encoding) {
|
|
|
|
options.encoding = encoding;
|
|
|
|
}
|
|
|
|
if (commitment) {
|
|
|
|
options.commitment = commitment;
|
|
|
|
}
|
2020-09-16 23:50:13 -07:00
|
|
|
if (extra) {
|
|
|
|
options = Object.assign(options, extra);
|
|
|
|
}
|
2020-08-06 08:47:22 -07:00
|
|
|
args.push(options);
|
2019-11-11 10:01:10 -08:00
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
2020-02-03 07:22:11 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnSignatureNotification(notification: Object) {
|
|
|
|
const res = SignatureNotificationResult(notification);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('signature notification failed: ' + res.error.message);
|
2020-02-03 07:22:11 -08:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
const keys = Object.keys(this._signatureSubscriptions).map(Number);
|
|
|
|
for (let id of keys) {
|
|
|
|
const sub = this._signatureSubscriptions[id];
|
|
|
|
if (sub.subscriptionId === res.subscription) {
|
|
|
|
// Signatures subscriptions are auto-removed by the RPC service so
|
|
|
|
// no need to explicitly send an unsubscribe message
|
|
|
|
delete this._signatureSubscriptions[id];
|
|
|
|
this._updateSubscriptions();
|
2020-03-23 07:19:32 -07:00
|
|
|
sub.callback(res.result.value, res.result.context);
|
2020-02-03 07:22:11 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked upon signature updates
|
|
|
|
*
|
2020-02-10 06:45:52 -08:00
|
|
|
* @param signature Transaction signature string in base 58
|
2020-02-03 07:22:11 -08:00
|
|
|
* @param callback Function to invoke on signature notifications
|
2020-05-20 21:17:38 -07:00
|
|
|
* @param commitment Specify the commitment level signature must reach before notification
|
2020-02-03 07:22:11 -08:00
|
|
|
* @return subscription id
|
|
|
|
*/
|
|
|
|
onSignature(
|
|
|
|
signature: TransactionSignature,
|
|
|
|
callback: SignatureResultCallback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment: ?Commitment,
|
2020-02-03 07:22:11 -08:00
|
|
|
): number {
|
|
|
|
const id = ++this._signatureSubscriptionCounter;
|
|
|
|
this._signatureSubscriptions[id] = {
|
|
|
|
signature,
|
|
|
|
callback,
|
2020-05-20 21:17:38 -07:00
|
|
|
commitment,
|
2020-02-03 07:22:11 -08:00
|
|
|
subscriptionId: null,
|
|
|
|
};
|
|
|
|
this._updateSubscriptions();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister a signature notification callback
|
|
|
|
*
|
|
|
|
* @param id subscription id to deregister
|
|
|
|
*/
|
|
|
|
async removeSignatureListener(id: number): Promise<void> {
|
|
|
|
if (this._signatureSubscriptions[id]) {
|
2020-02-10 06:45:52 -08:00
|
|
|
const subInfo = this._signatureSubscriptions[id];
|
2020-02-03 07:22:11 -08:00
|
|
|
delete this._signatureSubscriptions[id];
|
2020-02-10 06:45:52 -08:00
|
|
|
await this._unsubscribe(subInfo, 'signatureUnsubscribe');
|
2020-02-03 07:22:11 -08:00
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
2020-02-10 06:45:52 -08:00
|
|
|
throw new Error(`Unknown signature result id: ${id}`);
|
2020-02-03 07:22:11 -08:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 07:22:53 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnRootNotification(notification: Object) {
|
|
|
|
const res = RootNotificationResult(notification);
|
|
|
|
if (res.error) {
|
2020-04-08 16:49:51 -07:00
|
|
|
throw new Error('root notification failed: ' + res.error.message);
|
2020-03-27 07:22:53 -07:00
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
const root = res.result;
|
|
|
|
const keys = Object.keys(this._rootSubscriptions).map(Number);
|
|
|
|
for (let id of keys) {
|
|
|
|
const sub = this._rootSubscriptions[id];
|
|
|
|
if (sub.subscriptionId === res.subscription) {
|
|
|
|
sub.callback(root);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to be invoked upon root changes
|
|
|
|
*
|
|
|
|
* @param callback Function to invoke whenever the root changes
|
|
|
|
* @return subscription id
|
|
|
|
*/
|
|
|
|
onRootChange(callback: RootChangeCallback): number {
|
|
|
|
const id = ++this._rootSubscriptionCounter;
|
|
|
|
this._rootSubscriptions[id] = {
|
|
|
|
callback,
|
|
|
|
subscriptionId: null,
|
|
|
|
};
|
|
|
|
this._updateSubscriptions();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deregister a root notification callback
|
|
|
|
*
|
|
|
|
* @param id subscription id to deregister
|
|
|
|
*/
|
|
|
|
async removeRootChangeListener(id: number): Promise<void> {
|
|
|
|
if (this._rootSubscriptions[id]) {
|
|
|
|
const subInfo = this._rootSubscriptions[id];
|
|
|
|
delete this._rootSubscriptions[id];
|
|
|
|
await this._unsubscribe(subInfo, 'rootUnsubscribe');
|
|
|
|
this._updateSubscriptions();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unknown root change id: ${id}`);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|