solana/web3.js/src/connection.ts

3327 lines
91 KiB
TypeScript
Raw Normal View History

2018-08-23 10:52:48 -07:00
import assert from 'assert';
import bs58 from 'bs58';
import {Buffer} from 'buffer';
2018-11-04 11:41:21 -08:00
import {parse as urlParse, format as urlFormat} from 'url';
2021-03-14 20:01:35 -07:00
import fetch, {Response} from 'node-fetch';
import {
type as pick,
number,
string,
array,
boolean,
literal,
record,
union,
optional,
nullable,
coerce,
instance,
create,
tuple,
unknown,
2021-03-14 20:01:35 -07:00
any,
} from 'superstruct';
import type {Struct} from 'superstruct';
2018-10-26 21:37:39 -07:00
import {Client as RpcWebSocketClient} from 'rpc-websockets';
2021-03-14 20:01:35 -07:00
import RpcClient from 'jayson/lib/client/browser';
import {IWSRequestParams} from 'rpc-websockets/dist/lib/client';
2018-08-23 10:52:48 -07:00
import {AgentManager} from './agent-manager';
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';
import {MS_PER_SLOT} from './timing';
import {Transaction} from './transaction';
import {Message} from './message';
import {sleep} from './util/sleep';
import {promiseTimeout} from './util/promise-timeout';
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';
import type {CompiledInstruction} from './message';
2018-08-23 10:52:48 -07:00
const PublicKeyFromString = coerce(
instance(PublicKey),
string(),
value => new PublicKey(value),
);
const RawAccountDataResult = tuple([string(), literal('base64')]);
const BufferFromRawAccountData = coerce(
instance(Buffer),
RawAccountDataResult,
value => Buffer.from(value[0], 'base64'),
);
2021-03-14 22:08:10 -07:00
/**
* Attempt to use a recent blockhash for up to 30 seconds
* @internal
*/
export const BLOCKHASH_CACHE_TIMEOUT_MS = 30 * 1000;
type RpcRequest = (methodName: string, args: Array<any>) => any;
2018-08-23 10:52:48 -07:00
2021-03-14 20:01:35 -07:00
export type TokenAccountsFilter =
| {
mint: PublicKey;
}
| {
programId: PublicKey;
};
2020-07-30 21:33:54 -07:00
/**
* Extra contextual information for RPC responses
*/
2021-03-14 20:01:35 -07:00
export type Context = {
slot: number;
};
2020-06-03 04:55:42 -07:00
/**
* Options for sending transactions
*/
export type SendOptions = {
2021-03-14 20:01:35 -07:00
/** disable transaction verification step */
skipPreflight?: boolean;
/** preflight commitment level */
preflightCommitment?: Commitment;
2020-06-03 04:55:42 -07:00
};
/**
* Options for confirming transactions
*/
export type ConfirmOptions = {
2021-03-14 20:01:35 -07:00
/** disable transaction verification step */
skipPreflight?: boolean;
/** desired commitment level */
commitment?: Commitment;
/** preflight commitment level */
preflightCommitment?: Commitment;
2020-06-03 04:55:42 -07:00
};
/**
* Options for getConfirmedSignaturesForAddress2
*
* @typedef {Object} ConfirmedSignaturesForAddress2Options
*/
export type ConfirmedSignaturesForAddress2Options = {
2021-03-14 20:01:35 -07:00
/**
* Start searching backwards from this transaction signature.
* @remark If not provided the search starts from the highest max confirmed block.
*/
before?: TransactionSignature;
/** Maximum transaction signatures to return (between 1 and 1,000, default: 1,000). */
limit?: number;
};
2020-02-14 07:01:01 -08:00
/**
* RPC Response with extra contextual information
*/
2021-03-14 20:01:35 -07:00
export type RpcResponseAndContext<T> = {
/** response context */
context: Context;
/** response value */
value: T;
};
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
function createRpcResult<T, U>(result: Struct<T, U>) {
return union([
pick({
jsonrpc: literal('2.0'),
id: string(),
result,
}),
pick({
jsonrpc: literal('2.0'),
id: string(),
error: pick({
code: unknown(),
message: string(),
2021-03-14 20:01:35 -07:00
data: optional(any()),
}),
}),
]);
}
const UnknownRpcResult = createRpcResult(unknown());
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
function jsonRpcResult<T, U>(schema: Struct<T, U>) {
return coerce(createRpcResult(schema), UnknownRpcResult, value => {
if ('error' in value) {
return value;
} else {
return {
...value,
result: create(value.result, schema),
};
}
2020-01-08 12:59:58 -08:00
});
}
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
function jsonRpcResultAndContext<T, U>(value: Struct<T, U>) {
return jsonRpcResult(
pick({
context: pick({
slot: number(),
}),
value,
}),
);
}
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
function notificationResultAndContext<T, U>(value: Struct<T, U>) {
return pick({
context: pick({
slot: number(),
}),
value,
});
}
/**
* The level of commitment desired when querying state
* <pre>
* 'processed': Query the most recent block which has reached 1 confirmation by the connected node
* 'confirmed': Query the most recent block which has reached 1 confirmation by the cluster
* 'finalized': Query the most recent block which has been finalized by the cluster
* </pre>
*
* @typedef {'processed' | 'confirmed' | 'finalized'} Commitment
*/
export type Commitment =
| 'processed'
| 'confirmed'
| 'finalized'
| 'recent' // Deprecated as of v1.5.5
| 'single' // Deprecated as of v1.5.5
| 'singleGossip' // Deprecated as of v1.5.5
| 'root' // Deprecated as of v1.5.5
| 'max'; // Deprecated as of v1.5.5
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
*/
2021-03-14 20:01:35 -07:00
export type GetLargestAccountsConfig = {
commitment?: Commitment;
filter?: LargestAccountsFilter;
2020-05-22 10:23:29 -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 = {
2021-03-14 20:01:35 -07:00
searchTransactionHistory: boolean;
};
/**
* Information describing a cluster node
*
* @typedef {Object} ContactInfo
* @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)
* @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)
*/
2021-03-14 20:01:35 -07:00
export type ContactInfo = {
pubkey: string;
gossip: string | null;
tpu: string | null;
rpc: string | null;
version: string | null;
};
/**
* 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
* @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
* @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
* @property {number} lastVote Most recent slot voted on by this vote account
*/
2021-03-14 20:01:35 -07:00
export type VoteAccountInfo = {
votePubkey: string;
nodePubkey: string;
activatedStake: number;
epochVoteAccount: boolean;
epochCredits: Array<[number, number, number]>;
commission: number;
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
*/
2021-03-14 20:01:35 -07:00
export type VoteAccountStatus = {
current: Array<VoteAccountInfo>;
delinquent: Array<VoteAccountInfo>;
};
/**
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)
*
* @typedef {Object} InflationGovernor
* @property {number} foundation
2019-10-23 06:48:24 -07:00
* @property {number} foundation_term
* @property {number} initial
* @property {number} taper
* @property {number} terminal
*/
2021-03-14 20:01:35 -07:00
export type InflationGovernor = {
foundation: number;
foundationTerm: number;
initial: number;
taper: number;
terminal: number;
2020-05-21 02:11:32 -07:00
};
const GetInflationGovernorResult = pick({
foundation: number(),
foundationTerm: number(),
initial: number(),
taper: number(),
terminal: number(),
});
/**
2020-05-21 02:11:32 -07:00
* Information about the current epoch
*
* @typedef {Object} EpochInfo
* @property {number} epoch
* @property {number} slotIndex
* @property {number} slotsInEpoch
* @property {number} absoluteSlot
* @property {number} blockHeight
* @property {number} transactionCount
*/
2021-03-14 20:01:35 -07:00
export type EpochInfo = {
epoch: number;
slotIndex: number;
slotsInEpoch: number;
absoluteSlot: number;
blockHeight?: number;
transactionCount?: number;
2020-05-21 02:11:32 -07:00
};
const GetEpochInfoResult = pick({
epoch: number(),
slotIndex: number(),
slotsInEpoch: number(),
absoluteSlot: number(),
blockHeight: optional(number()),
transactionCount: optional(number()),
});
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-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
*/
2021-03-14 20:01:35 -07:00
export type EpochSchedule = {
slotsPerEpoch: number;
leaderScheduleSlotOffset: number;
warmup: boolean;
firstNormalEpoch: number;
firstNormalSlot: number;
2020-05-21 02:11:32 -07:00
};
const GetEpochScheduleResult = pick({
slotsPerEpoch: number(),
leaderScheduleSlotOffset: number(),
warmup: boolean(),
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
*/
2021-03-14 20:01:35 -07:00
export type LeaderSchedule = {
[address: string]: number[];
2020-07-17 08:16:44 -07:00
};
2021-03-14 20:01:35 -07:00
const GetLeaderScheduleResult = record(string(), array(number()));
2020-07-17 08:16:44 -07:00
2020-04-04 06:35:08 -07:00
/**
* Transaction error or null
*/
const TransactionErrorResult = nullable(pick({}));
2020-04-04 06:35:08 -07:00
2020-02-03 07:22:11 -08:00
/**
* Signature status for a transaction
*/
const SignatureStatusResult = pick({
err: TransactionErrorResult,
});
2020-02-03 07:22:11 -08:00
/**
* Transaction signature received notification
*/
const SignatureReceivedResult = literal('receivedSignature');
2021-03-14 20:01:35 -07:00
export type Version = {
'solana-core': string;
'feature-set'?: number;
};
2019-11-11 17:09:00 -08:00
/**
* Version info for a node
*
* @typedef {Object} Version
* @property {string} solana-core Version of solana-core
*/
const VersionResult = pick({
'solana-core': string(),
'feature-set': optional(number()),
2019-11-11 17:09:00 -08:00
});
2021-03-14 20:01:35 -07:00
export type SimulatedTransactionResponse = {
err: TransactionError | string | null;
logs: Array<string> | null;
2020-08-10 23:35:56 -07:00
};
const SimulatedTransactionResponseStruct = jsonRpcResultAndContext(
pick({
err: nullable(union([pick({}), string()])),
logs: nullable(array(string())),
2020-08-10 23:35:56 -07:00
}),
);
2021-03-14 20:01:35 -07:00
export type ParsedInnerInstruction = {
index: number;
instructions: (ParsedInstruction | PartiallyDecodedInstruction)[];
};
2021-03-14 20:01:35 -07:00
export type TokenBalance = {
accountIndex: number;
mint: string;
uiTokenAmount: TokenAmount;
};
/**
* 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 {Array<TokenBalance>} preTokenBalances The token balances of the transaction accounts before processing
* @property {Array<TokenBalance>} postTokenBalances The token balances of the transaction accounts after processing
* @property {object|null} err The error result of transaction processing
*/
2021-03-14 20:01:35 -07:00
export type ParsedConfirmedTransactionMeta = {
fee: number;
innerInstructions?: ParsedInnerInstruction[] | null;
preBalances: Array<number>;
postBalances: Array<number>;
logMessages?: Array<string> | null;
preTokenBalances?: Array<TokenBalance> | null;
postTokenBalances?: Array<TokenBalance> | null;
err: TransactionError | null;
};
2021-03-14 20:01:35 -07:00
export 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
* @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
* @property {Array<string>} logMessages An array of program log messages emitted during a transaction
* @property {Array<TokenBalance>} preTokenBalances The token balances of the transaction accounts before processing
* @property {Array<TokenBalance>} postTokenBalances The token balances of the transaction accounts after processing
2020-04-21 20:12:59 -07:00
* @property {object|null} err The error result of transaction processing
*/
2021-03-14 20:01:35 -07:00
export type ConfirmedTransactionMeta = {
fee: number;
innerInstructions?: CompiledInnerInstruction[] | null;
preBalances: Array<number>;
postBalances: Array<number>;
logMessages?: Array<string> | null;
preTokenBalances?: Array<TokenBalance> | null;
postTokenBalances?: Array<TokenBalance> | null;
err: TransactionError | null;
2020-04-21 20:12:59 -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
* @property {number|null|undefined} blockTime The unix timestamp of when the transaction was processed
*/
2021-03-14 20:01:35 -07:00
export type ConfirmedTransaction = {
slot: number;
transaction: Transaction;
meta: ConfirmedTransactionMeta | null;
blockTime?: number | null;
};
/**
* A partially decoded transaction instruction
*/
2021-03-14 20:01:35 -07:00
export type PartiallyDecodedInstruction = {
/** Program id called by this instruction */
programId: PublicKey;
/** Public keys of accounts passed to this instruction */
accounts: Array<PublicKey>;
/** Raw base-58 instruction data */
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
*/
2021-03-14 20:01:35 -07:00
export 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
*/
2021-03-14 20:01:35 -07:00
export 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
*/
2021-03-14 20:01:35 -07:00
export 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
*/
2021-03-14 20:01:35 -07:00
export 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
* @property {number|null|undefined} blockTime The unix timestamp of when the transaction was processed
*/
2021-03-14 20:01:35 -07:00
export type ParsedConfirmedTransaction = {
slot: number;
transaction: ParsedTransaction;
meta: ParsedConfirmedTransactionMeta | null;
blockTime?: number | null;
};
/**
* A ConfirmedBlock on the ledger
*
* @typedef {Object} ConfirmedBlock
* @property {Blockhash} blockhash Blockhash of this block
* @property {Blockhash} previousBlockhash Blockhash of this block's parent
* @property {number} parentSlot Slot index of this block's parent
* @property {Array<object>} transactions Vector of transactions and status metas
* @property {Array<object>} rewards Vector of block rewards
* @property {number|null} blockTime The unix timestamp of when the block was processed
*/
2021-03-14 20:01:35 -07:00
export type ConfirmedBlock = {
blockhash: Blockhash;
previousBlockhash: Blockhash;
parentSlot: number;
transactions: Array<{
2021-03-14 20:01:35 -07:00
transaction: Transaction;
meta: ConfirmedTransactionMeta | null;
}>;
rewards?: Array<{
pubkey: string;
lamports: number;
postBalance: number | null;
rewardType: string | null;
}>;
blockTime: number | null;
};
/**
* 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
*/
2021-03-14 20:01:35 -07:00
export type PerfSample = {
slot: number;
numTransactions: number;
numSlots: number;
samplePeriodSecs: number;
};
function createRpcRequest(url: string, useHttps: boolean): RpcRequest {
2021-03-14 20:01:35 -07:00
let agentManager: AgentManager | undefined;
if (!process.env.BROWSER) {
agentManager = new AgentManager(useHttps);
}
2021-03-14 20:01:35 -07:00
const clientBrowser = new RpcClient(async (request, callback) => {
const agent = agentManager ? agentManager.requestStart() : undefined;
2018-11-04 11:41:21 -08:00
const options = {
method: 'POST',
body: request,
agent,
2018-11-04 11:41:21 -08:00
headers: {
'Content-Type': 'application/json',
},
};
try {
let too_many_requests_retries = 5;
2021-03-14 20:01:35 -07:00
let res: Response;
let waitTime = 500;
for (;;) {
res = await fetch(url, options);
2020-09-01 10:58:40 -07:00
if (res.status !== 429 /* Too many requests */) {
break;
}
too_many_requests_retries -= 1;
if (too_many_requests_retries === 0) {
break;
}
console.log(
2020-09-01 10:58:40 -07:00
`Server responded with ${res.status} ${res.statusText}. Retrying after ${waitTime}ms delay...`,
);
await sleep(waitTime);
waitTime *= 2;
}
2018-11-04 11:41:21 -08:00
const text = await res.text();
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);
} finally {
agentManager && agentManager.requestEnd();
2018-08-23 10:52:48 -07:00
}
2021-03-14 20:01:35 -07:00
}, {});
2018-08-23 10:52:48 -07:00
return (method, args) => {
return new Promise((resolve, reject) => {
2021-03-14 20:01:35 -07:00
clientBrowser.request(method, args, (err: any, response: any) => {
2018-08-23 10:52:48 -07:00
if (err) {
reject(err);
return;
}
resolve(response);
});
});
};
}
/**
* Expected JSON RPC response for the "getInflationGovernor" message
*/
const GetInflationGovernorRpcResult = jsonRpcResult(GetInflationGovernorResult);
/**
* Expected JSON RPC response for the "getEpochInfo" message
*/
const GetEpochInfoRpcResult = jsonRpcResult(GetEpochInfoResult);
2019-10-23 06:48:24 -07:00
/**
* Expected JSON RPC response for the "getEpochSchedule" message
*/
const GetEpochScheduleRpcResult = jsonRpcResult(GetEpochScheduleResult);
2019-10-23 06:48:24 -07:00
2020-07-17 08:16:44 -07:00
/**
* Expected JSON RPC response for the "getLeaderSchedule" message
*/
const GetLeaderScheduleRpcResult = jsonRpcResult(GetLeaderScheduleResult);
2020-07-17 08:16:44 -07:00
2020-05-21 01:58:17 -07:00
/**
* Expected JSON RPC response for the "minimumLedgerSlot" and "getFirstAvailableBlock" messages
2020-05-21 01:58:17 -07:00
*/
const SlotRpcResult = jsonRpcResult(number());
2020-05-21 01:58:17 -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
*/
2021-03-14 20:01:35 -07:00
export type Supply = {
total: number;
circulating: number;
nonCirculating: number;
nonCirculatingAccounts: Array<PublicKey>;
};
/**
* Expected JSON RPC response for the "getSupply" message
*/
const GetSupplyRpcResult = jsonRpcResultAndContext(
pick({
total: number(),
circulating: number(),
nonCirculating: number(),
nonCirculatingAccounts: array(PublicKeyFromString),
}),
);
/**
* 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 | null} uiAmount Token amount as float, accounts for decimals
* @property {string | undefined} uiAmountString Token amount as string, accounts for decimals
*/
2021-03-14 20:01:35 -07:00
export type TokenAmount = {
amount: string;
decimals: number;
uiAmount: number | null;
uiAmountString?: string;
2020-08-05 21:17:29 -07:00
};
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
*/
const TokenAmountResult = pick({
amount: string(),
uiAmount: nullable(number()),
decimals: number(),
uiAmountString: optional(string()),
2020-07-30 21:33:54 -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 | null} uiAmount Token amount as float, accounts for decimals
* @property {string | undefined} uiAmountString Token amount as string, accounts for decimals
*/
2021-03-14 20:01:35 -07:00
export type TokenAccountBalancePair = {
address: PublicKey;
amount: string;
decimals: number;
uiAmount: number | null;
uiAmountString?: string;
};
/**
* Expected JSON RPC response for the "getTokenLargestAccounts" message
*/
const GetTokenLargestAccountsResult = jsonRpcResultAndContext(
array(
pick({
address: PublicKeyFromString,
amount: string(),
uiAmount: nullable(number()),
decimals: number(),
uiAmountString: optional(string()),
}),
),
);
2020-07-30 21:33:54 -07:00
/**
* Expected JSON RPC response for the "getTokenAccountsByOwner" message
*/
const GetTokenAccountsByOwner = jsonRpcResultAndContext(
array(
pick({
pubkey: PublicKeyFromString,
account: pick({
executable: boolean(),
owner: PublicKeyFromString,
lamports: number(),
data: BufferFromRawAccountData,
rentEpoch: number(),
2020-08-06 08:47:22 -07:00
}),
}),
),
2020-08-06 08:47:22 -07:00
);
const ParsedAccountDataResult = pick({
program: string(),
parsed: unknown(),
space: number(),
});
2020-08-06 08:47:22 -07:00
/**
* Expected JSON RPC response for the "getTokenAccountsByOwner" message with parsed data
*/
const GetParsedTokenAccountsByOwner = jsonRpcResultAndContext(
array(
pick({
pubkey: PublicKeyFromString,
account: pick({
executable: boolean(),
owner: PublicKeyFromString,
lamports: number(),
data: ParsedAccountDataResult,
rentEpoch: number(),
2020-07-30 21:33:54 -07:00
}),
}),
),
2020-07-30 21:33:54 -07:00
);
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
*/
2021-03-14 20:01:35 -07:00
export type AccountBalancePair = {
address: PublicKey;
lamports: number;
2020-05-22 10:23:29 -07:00
};
/**
* Expected JSON RPC response for the "getLargestAccounts" message
*/
const GetLargestAccountsRpcResult = jsonRpcResultAndContext(
array(
pick({
lamports: number(),
address: PublicKeyFromString,
2020-05-22 10:23:29 -07:00
}),
),
2020-05-22 10:23:29 -07:00
);
2019-11-11 17:09:00 -08:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2019-11-11 17:09:00 -08:00
*/
const AccountInfoResult = pick({
executable: boolean(),
owner: PublicKeyFromString,
lamports: number(),
data: BufferFromRawAccountData,
rentEpoch: number(),
2019-11-11 17:09:00 -08:00
});
2018-09-20 15:08:52 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2018-09-20 15:08:52 -07:00
*/
const KeyedAccountInfoResult = pick({
pubkey: PublicKeyFromString,
account: AccountInfoResult,
2018-09-20 15:08:52 -07:00
});
const ParsedOrRawAccountData = coerce(
union([instance(Buffer), ParsedAccountDataResult]),
union([RawAccountDataResult, ParsedAccountDataResult]),
value => {
if (Array.isArray(value)) {
return create(value, BufferFromRawAccountData);
} else {
return value;
}
},
);
2020-08-06 08:47:22 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2020-08-06 08:47:22 -07:00
*/
const ParsedAccountInfoResult = pick({
executable: boolean(),
owner: PublicKeyFromString,
lamports: number(),
data: ParsedOrRawAccountData,
rentEpoch: number(),
});
const KeyedParsedAccountInfoResult = pick({
pubkey: PublicKeyFromString,
account: ParsedAccountInfoResult,
2020-08-06 08:47:22 -07:00
});
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
const StakeActivationResult = pick({
state: union([
literal('active'),
literal('inactive'),
literal('activating'),
literal('deactivating'),
]),
active: number(),
inactive: number(),
});
/**
* Expected JSON RPC response for the "getConfirmedSignaturesForAddress" message
*/
const GetConfirmedSignaturesForAddressRpcResult = jsonRpcResult(
array(string()),
);
/**
* Expected JSON RPC response for the "getConfirmedSignaturesForAddress2" message
*/
const GetConfirmedSignaturesForAddress2RpcResult = jsonRpcResult(
array(
pick({
signature: string(),
slot: number(),
err: TransactionErrorResult,
memo: nullable(string()),
blockTime: optional(nullable(number())),
}),
),
);
2018-10-26 21:37:39 -07:00
/***
* Expected JSON RPC response for the "accountNotification" message
*/
const AccountNotificationResult = pick({
subscription: number(),
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
/**
2021-03-14 20:01:35 -07:00
* @internal
2019-03-08 16:02:39 -08:00
*/
const ProgramAccountInfoResult = pick({
pubkey: PublicKeyFromString,
account: AccountInfoResult,
});
2019-03-08 16:02:39 -08:00
/***
* Expected JSON RPC response for the "programNotification" message
*/
const ProgramAccountNotificationResult = pick({
subscription: number(),
result: notificationResultAndContext(ProgramAccountInfoResult),
2019-03-08 16:02:39 -08:00
});
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
const SlotInfoResult = pick({
parent: number(),
slot: number(),
root: number(),
});
2020-02-03 07:22:11 -08:00
/**
* Expected JSON RPC response for the "slotNotification" message
*/
const SlotNotificationResult = pick({
subscription: number(),
2020-03-27 07:22:53 -07:00
result: SlotInfoResult,
});
2020-02-03 07:22:11 -08:00
/**
* Expected JSON RPC response for the "signatureNotification" message
*/
const SignatureNotificationResult = pick({
subscription: number(),
result: notificationResultAndContext(
union([SignatureStatusResult, SignatureReceivedResult]),
),
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 = pick({
subscription: number(),
result: number(),
2020-03-27 07:22:53 -07:00
});
const ContactInfoResult = pick({
pubkey: string(),
gossip: nullable(string()),
tpu: nullable(string()),
rpc: nullable(string()),
version: nullable(string()),
});
const VoteAccountInfoResult = pick({
votePubkey: string(),
nodePubkey: string(),
activatedStake: number(),
epochVoteAccount: boolean(),
epochCredits: array(tuple([number(), number(), number()])),
commission: number(),
lastVote: number(),
rootSlot: nullable(number()),
});
/**
* Expected JSON RPC response for the "getVoteAccounts" message
*/
const GetVoteAccounts = jsonRpcResult(
pick({
current: array(VoteAccountInfoResult),
delinquent: array(VoteAccountInfoResult),
}),
);
const ConfirmationStatus = union([
literal('processed'),
literal('confirmed'),
literal('finalized'),
]);
const SignatureStatusResponse = pick({
slot: number(),
confirmations: nullable(number()),
err: TransactionErrorResult,
confirmationStatus: optional(ConfirmationStatus),
});
2018-09-26 19:16:17 -07:00
/**
* Expected JSON RPC response for the "getSignatureStatuses" message
2018-09-26 19:16:17 -07:00
*/
const GetSignatureStatusesRpcResult = jsonRpcResultAndContext(
array(nullable(SignatureStatusResponse)),
2020-02-03 07:22:11 -08:00
);
2018-09-26 19:16:17 -07:00
/**
* Expected JSON RPC response for the "getMinimumBalanceForRentExemption" message
*/
const GetMinimumBalanceForRentExemptionRpcResult = jsonRpcResult(number());
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
const ConfirmedTransactionResult = pick({
signatures: array(string()),
message: pick({
accountKeys: array(string()),
header: pick({
numRequiredSignatures: number(),
numReadonlySignedAccounts: number(),
numReadonlyUnsignedAccounts: number(),
}),
instructions: array(
pick({
accounts: array(number()),
data: string(),
programIdIndex: number(),
}),
),
recentBlockhash: string(),
}),
});
const TransactionFromConfirmed = coerce(
instance(Transaction),
ConfirmedTransactionResult,
result => {
const {message, signatures} = result;
return Transaction.populate(new Message(message), signatures);
},
);
const ParsedInstructionResult = pick({
parsed: unknown(),
program: string(),
programId: PublicKeyFromString,
});
const RawInstructionResult = pick({
accounts: array(PublicKeyFromString),
data: string(),
programId: PublicKeyFromString,
});
const InstructionResult = union([
RawInstructionResult,
ParsedInstructionResult,
]);
const UnknownInstructionResult = union([
pick({
parsed: unknown(),
program: string(),
programId: string(),
}),
pick({
accounts: array(string()),
data: string(),
programId: string(),
}),
]);
const ParsedOrRawInstruction = coerce(
InstructionResult,
UnknownInstructionResult,
value => {
if ('accounts' in value) {
return create(value, RawInstructionResult);
} else {
return create(value, ParsedInstructionResult);
}
},
);
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
const ParsedConfirmedTransactionResult = pick({
signatures: array(string()),
message: pick({
accountKeys: array(
pick({
pubkey: PublicKeyFromString,
signer: boolean(),
writable: boolean(),
}),
),
instructions: array(ParsedOrRawInstruction),
recentBlockhash: string(),
}),
});
const TokenBalanceResult = pick({
accountIndex: number(),
mint: string(),
uiTokenAmount: TokenAmountResult,
});
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
const ConfirmedTransactionMetaResult = pick({
err: TransactionErrorResult,
fee: number(),
innerInstructions: optional(
nullable(
array(
pick({
index: number(),
instructions: array(
pick({
accounts: array(number()),
data: string(),
programIdIndex: number(),
}),
),
}),
),
),
),
preBalances: array(number()),
postBalances: array(number()),
logMessages: optional(nullable(array(string()))),
preTokenBalances: optional(nullable(array(TokenBalanceResult))),
postTokenBalances: optional(nullable(array(TokenBalanceResult))),
});
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
const ParsedConfirmedTransactionMetaResult = pick({
err: TransactionErrorResult,
fee: number(),
innerInstructions: optional(
nullable(
array(
pick({
index: number(),
instructions: array(ParsedOrRawInstruction),
}),
),
),
),
preBalances: array(number()),
postBalances: array(number()),
logMessages: optional(nullable(array(string()))),
preTokenBalances: optional(nullable(array(TokenBalanceResult))),
postTokenBalances: optional(nullable(array(TokenBalanceResult))),
});
/**
2019-11-16 08:28:14 -08:00
* Expected JSON RPC response for the "getConfirmedBlock" message
*/
2019-11-16 08:28:14 -08:00
export const GetConfirmedBlockRpcResult = jsonRpcResult(
nullable(
pick({
blockhash: string(),
previousBlockhash: string(),
parentSlot: number(),
transactions: array(
pick({
transaction: TransactionFromConfirmed,
meta: nullable(ConfirmedTransactionMetaResult),
2019-11-16 08:28:14 -08:00
}),
),
rewards: optional(
array(
pick({
pubkey: string(),
lamports: number(),
postBalance: nullable(number()),
rewardType: nullable(string()),
}),
),
),
blockTime: nullable(number()),
}),
),
2019-11-12 08:21:19 -08:00
);
/**
* Expected JSON RPC response for the "getConfirmedTransaction" message
*/
const GetConfirmedTransactionRpcResult = jsonRpcResult(
nullable(
pick({
slot: number(),
transaction: TransactionFromConfirmed,
meta: ConfirmedTransactionMetaResult,
blockTime: optional(nullable(number())),
}),
),
);
/**
* Expected JSON RPC response for the "getConfirmedTransaction" message
*/
const GetParsedConfirmedTransactionRpcResult = jsonRpcResult(
nullable(
pick({
slot: number(),
transaction: ParsedConfirmedTransactionResult,
meta: nullable(ParsedConfirmedTransactionMetaResult),
blockTime: optional(nullable(number())),
}),
),
);
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
*/
const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
pick({
blockhash: string(),
feeCalculator: pick({
lamportsPerSignature: number(),
}),
2019-06-12 14:36:05 -07:00
}),
);
const PerfSampleResult = pick({
slot: number(),
numTransactions: number(),
numSlots: number(),
samplePeriodSecs: number(),
});
/*
* Expected JSON RPC response for "getRecentPerformanceSamples" message
*/
const GetRecentPerformanceSamplesRpcResult = jsonRpcResult(
array(PerfSampleResult),
);
/**
* Expected JSON RPC response for the "getFeeCalculatorForBlockhash" message
*/
const GetFeeCalculatorRpcResult = jsonRpcResultAndContext(
nullable(
pick({
feeCalculator: pick({
lamportsPerSignature: number(),
}),
}),
),
);
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "requestAirdrop" message
*/
const RequestAirdropRpcResult = jsonRpcResult(string());
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "sendTransaction" message
*/
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
*/
2021-02-07 08:57:12 -08:00
export type SlotInfo = {
2021-03-14 20:01:35 -07:00
slot: number;
parent: number;
root: number;
2020-03-27 07:22:53 -07:00
};
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
* @property {number} space Space used by account data
2020-08-06 08:47:22 -07:00
*/
2021-03-14 20:01:35 -07:00
export type ParsedAccountData = {
program: string;
parsed: any;
space: number;
2020-08-06 08:47:22 -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
*/
2021-03-14 20:01:35 -07:00
export 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
* @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
*/
2021-03-14 20:01:35 -07:00
export type AccountInfo<T> = {
executable: boolean;
owner: PublicKey;
lamports: number;
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
*/
2021-02-07 08:57:12 -08:00
export type KeyedAccountInfo = {
2021-03-14 20:01:35 -07:00
accountId: PublicKey;
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
*/
export type AccountChangeCallback = (
2020-08-06 08:47:22 -07:00
accountInfo: AccountInfo<Buffer>,
context: Context,
) => void;
2018-10-26 21:37:39 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
type SubscriptionId = 'subscribing' | number;
2018-10-26 21:37:39 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2018-10-26 21:37:39 -07:00
*/
type AccountSubscriptionInfo = {
2021-03-14 20:01:35 -07:00
publicKey: string; // PublicKey of the account as a base 58 string
callback: AccountChangeCallback;
commitment?: Commitment;
subscriptionId: SubscriptionId | null; // 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,
context: Context,
2019-03-08 16:02:39 -08:00
) => void;
/**
2021-03-14 20:01:35 -07:00
* @internal
2019-03-08 16:02:39 -08:00
*/
type ProgramAccountSubscriptionInfo = {
2021-03-14 20:01:35 -07:00
programId: string; // PublicKey of the program as a base 58 string
callback: ProgramAccountChangeCallback;
commitment?: Commitment;
subscriptionId: SubscriptionId | null; // 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;
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
type SlotSubscriptionInfo = {
2021-03-14 20:01:35 -07:00
callback: SlotChangeCallback;
subscriptionId: SubscriptionId | null; // null when there's no current server subscription id
};
/**
* Callback function for signature status notifications
*/
2020-02-03 07:22:11 -08:00
export type SignatureResultCallback = (
2020-04-04 06:35:08 -07:00
signatureResult: SignatureResult,
context: Context,
2020-02-03 07:22:11 -08:00
) => void;
/**
* Signature status notification with transaction result
*/
export type SignatureStatusNotification = {
type: 'status';
result: SignatureResult;
};
/**
* Signature received notification
*/
export type SignatureReceivedNotification = {
type: 'received';
};
/**
* Callback function for signature notifications
*/
export type SignatureSubscriptionCallback = (
notification: SignatureStatusNotification | SignatureReceivedNotification,
context: Context,
) => void;
/**
* Signature subscription options
*/
export type SignatureSubscriptionOptions = {
commitment?: Commitment;
enableReceivedNotification?: boolean;
};
2020-02-03 07:22:11 -08:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2020-02-03 07:22:11 -08:00
*/
type SignatureSubscriptionInfo = {
2021-03-14 20:01:35 -07:00
signature: TransactionSignature; // TransactionSignature as a base 58 string
callback: SignatureSubscriptionCallback;
options?: SignatureSubscriptionOptions;
2021-03-14 20:01:35 -07:00
subscriptionId: SubscriptionId | null; // null when there's no current server subscription id
2020-02-03 07:22:11 -08:00
};
2020-03-27 07:22:53 -07:00
/**
* Callback function for root change notifications
*/
export type RootChangeCallback = (root: number) => void;
/**
2021-03-14 20:01:35 -07:00
* @internal
2020-03-27 07:22:53 -07:00
*/
type RootSubscriptionInfo = {
2021-03-14 20:01:35 -07:00
callback: RootChangeCallback;
subscriptionId: SubscriptionId | null; // null when there's no current server subscription id
2020-03-27 07:22:53 -07:00
};
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
*/
2021-03-14 20:01:35 -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
/**
* Transaction confirmation status
* <pre>
* 'processed': Transaction landed in a block which has reached 1 confirmation by the connected node
* 'confirmed': Transaction landed in a block which has reached 1 confirmation by the cluster
* 'finalized': Transaction landed in a block which has been finalized by the cluster
* </pre>
*/
export type TransactionConfirmationStatus =
| 'processed'
| 'confirmed'
| 'finalized';
2020-03-23 08:01:12 -07:00
/**
* Signature status
*/
export type SignatureStatus = {
2021-03-14 20:01:35 -07:00
/** when the transaction was processed */
slot: number;
/** the number of blocks that have been confirmed and voted on in the fork containing `slot` */
confirmations: number | null;
/** transaction error, if any */
err: TransactionError | null;
/** cluster confirmation status, if data available. Possible responses: `processed`, `confirmed`, `finalized` */
confirmationStatus?: TransactionConfirmationStatus;
2020-03-23 08:01:12 -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
* @property {number | null | undefined} blockTime The unix timestamp of when the transaction was processed
*/
export type ConfirmedSignatureInfo = {
2021-03-14 20:01:35 -07:00
signature: string;
slot: number;
err: TransactionError | null;
memo: string | null;
blockTime?: number | 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 {
2021-03-14 20:01:35 -07:00
/** @internal */ _commitment?: Commitment;
/** @internal */ _rpcEndpoint: string;
/** @internal */ _rpcRequest: RpcRequest;
/** @internal */ _rpcWebSocket: RpcWebSocketClient;
/** @internal */ _rpcWebSocketConnected: boolean = false;
/** @internal */ _rpcWebSocketHeartbeat: ReturnType<
typeof setInterval
> | null = null;
/** @internal */ _rpcWebSocketIdleTimeout: ReturnType<
typeof setTimeout
> | null = null;
/** @internal */ _disableBlockhashCaching: boolean = false;
/** @internal */ _pollingBlockhash: boolean = false;
/** @internal */ _blockhashInfo: {
recentBlockhash: Blockhash | null;
lastFetch: number;
simulatedSignatures: Array<string>;
transactionSignatures: Array<string>;
2018-10-22 20:03:44 -07:00
};
2021-03-14 20:01:35 -07:00
/** @internal */ _accountChangeSubscriptionCounter: number = 0;
/** @internal */ _accountChangeSubscriptions: {
[id: number]: AccountSubscriptionInfo;
2019-03-08 16:02:39 -08:00
} = {};
2021-03-14 20:01:35 -07:00
/** @internal */ _programAccountChangeSubscriptionCounter: number = 0;
/** @internal */ _programAccountChangeSubscriptions: {
[id: number]: ProgramAccountSubscriptionInfo;
} = {};
2021-03-14 20:01:35 -07:00
/** @internal */ _rootSubscriptionCounter: number = 0;
/** @internal */ _rootSubscriptions: {
[id: number]: RootSubscriptionInfo;
2020-02-03 07:22:11 -08:00
} = {};
2021-03-14 20:01:35 -07:00
/** @internal */ _signatureSubscriptionCounter: number = 0;
/** @internal */ _signatureSubscriptions: {
[id: number]: SignatureSubscriptionInfo;
} = {};
/** @internal */ _slotSubscriptionCounter: number = 0;
/** @internal */ _slotSubscriptions: {
[id: number]: SlotSubscriptionInfo;
2020-03-27 07:22:53 -07:00
} = {};
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
* @param commitment optional default commitment level
2018-08-24 09:05:23 -07:00
*/
2021-03-14 20:01:35 -07:00
constructor(endpoint: string, commitment?: Commitment) {
this._rpcEndpoint = endpoint;
2018-10-26 21:37:39 -07:00
let url = urlParse(endpoint);
const useHttps = url.protocol === 'https:';
2018-10-26 21:37:39 -07:00
this._rpcRequest = createRpcRequest(url.href, useHttps);
this._commitment = commitment;
2019-03-04 08:06:33 -08:00
this._blockhashInfo = {
recentBlockhash: null,
2021-03-14 20:01:35 -07:00
lastFetch: 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
url.protocol = useHttps ? 'wss:' : 'ws:';
2018-10-26 21:37:39 -07:00
url.host = '';
// 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.
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),
);
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
}
/**
* The default commitment used for requests
*/
2021-03-14 20:01:35 -07:00
get commitment(): Commitment | undefined {
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
*/
async getBalanceAndContext(
publicKey: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<RpcResponseAndContext<number>> {
const args = this._buildArgs([publicKey.toBase58()], commitment);
const unsafeRes = await this._rpcRequest('getBalance', args);
const res = create(unsafeRes, jsonRpcResultAndContext(number()));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
2020-01-08 12:59:58 -08:00
return res.result;
}
2020-01-08 13:44:50 -08:00
/**
* Fetch the balance for the specified public key
*/
async getBalance(
publicKey: PublicKey,
2021-03-14 20:01:35 -07:00
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
);
});
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 = create(unsafeRes, jsonRpcResult(nullable(number())));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-05-18 20:27:36 -07:00
throw new Error(
'failed to get block time for slot ' + slot + ': ' + res.error.message,
);
}
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', []);
const res = create(unsafeRes, jsonRpcResult(number()));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-05-21 01:58:17 -07:00
throw new Error(
'failed to get minimum ledger slot: ' + res.error.message,
);
}
return res.result;
}
/**
* 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 = create(unsafeRes, SlotRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get first available block: ' + res.error.message,
);
}
return res.result;
}
/**
* Fetch information about the current supply
*/
async getSupply(
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<RpcResponseAndContext<Supply>> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getSupply', args);
const res = create(unsafeRes, GetSupplyRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error('failed to get supply: ' + res.error.message);
}
return res.result;
}
2020-07-30 21:33:54 -07:00
/**
* Fetch the current supply of a token mint
*/
async getTokenSupply(
tokenMintAddress: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-05 21:17:29 -07:00
): Promise<RpcResponseAndContext<TokenAmount>> {
const args = this._buildArgs([tokenMintAddress.toBase58()], commitment);
2020-07-30 21:33:54 -07:00
const unsafeRes = await this._rpcRequest('getTokenSupply', args);
const res = create(unsafeRes, jsonRpcResultAndContext(TokenAmountResult));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-07-30 21:33:54 -07:00
throw new Error('failed to get token supply: ' + res.error.message);
}
return res.result;
}
/**
* Fetch the current balance of a token account
*/
async getTokenAccountBalance(
tokenAddress: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-05 21:17:29 -07:00
): Promise<RpcResponseAndContext<TokenAmount>> {
const args = this._buildArgs([tokenAddress.toBase58()], commitment);
2020-07-30 21:33:54 -07:00
const unsafeRes = await this._rpcRequest('getTokenAccountBalance', args);
const res = create(unsafeRes, jsonRpcResultAndContext(TokenAmountResult));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-07-30 21:33:54 -07:00
throw new Error(
'failed to get token account balance: ' + res.error.message,
);
}
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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-07-30 21:33:54 -07:00
): Promise<
2020-08-06 08:47:22 -07:00
RpcResponseAndContext<
2021-03-14 20:01:35 -07:00
Array<{pubkey: PublicKey; account: AccountInfo<Buffer>}>
>
2020-07-30 21:33:54 -07:00
> {
2021-03-14 20:01:35 -07:00
let _args: any[] = [ownerAddress.toBase58()];
if ('mint' in filter) {
2020-08-06 08:47:22 -07:00
_args.push({mint: filter.mint.toBase58()});
} else {
_args.push({programId: filter.programId.toBase58()});
}
2020-07-30 21:33:54 -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 = create(unsafeRes, GetTokenAccountsByOwner);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-07-30 21:33:54 -07:00
throw new Error(
'failed to get token accounts owned by account ' +
ownerAddress.toBase58() +
': ' +
res.error.message,
);
}
return res.result;
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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-06 08:47:22 -07:00
): Promise<
RpcResponseAndContext<
2021-03-14 20:01:35 -07:00
Array<{pubkey: PublicKey; account: AccountInfo<ParsedAccountData>}>
>
2020-08-06 08:47:22 -07:00
> {
2021-03-14 20:01:35 -07:00
let _args: any[] = [ownerAddress.toBase58()];
if ('mint' in filter) {
2020-08-06 08:47:22 -07:00
_args.push({mint: filter.mint.toBase58()});
} else {
_args.push({programId: filter.programId.toBase58()});
}
const args = this._buildArgs(_args, commitment, 'jsonParsed');
2020-08-06 08:47:22 -07:00
const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
const res = create(unsafeRes, GetParsedTokenAccountsByOwner);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-08-06 08:47:22 -07:00
throw new Error(
'failed to get token accounts owned by account ' +
ownerAddress.toBase58() +
': ' +
res.error.message,
);
}
return res.result;
2020-08-06 08:47:22 -07:00
}
2020-05-22 10:23:29 -07:00
/**
* Fetch the 20 largest accounts with their current balances
*/
async getLargestAccounts(
2021-03-14 20:01:35 -07:00
config?: GetLargestAccountsConfig,
2020-05-22 10:23:29 -07:00
): 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 = create(unsafeRes, GetLargestAccountsRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-05-22 10:23:29 -07:00
throw new Error('failed to get largest accounts: ' + res.error.message);
}
return res.result;
}
/**
* Fetch the 20 largest token accounts with their current balances
* for a given mint.
*/
async getTokenLargestAccounts(
mintAddress: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<RpcResponseAndContext<Array<TokenAccountBalancePair>>> {
const args = this._buildArgs([mintAddress.toBase58()], commitment);
const unsafeRes = await this._rpcRequest('getTokenLargestAccounts', args);
const res = create(unsafeRes, GetTokenLargestAccountsResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get token largest accounts: ' + res.error.message,
);
}
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
*/
async getAccountInfoAndContext(
publicKey: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-06 08:47:22 -07:00
): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> {
const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64');
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
const res = create(
unsafeRes,
jsonRpcResultAndContext(nullable(AccountInfoResult)),
);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
return res.result;
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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-06 08:47:22 -07:00
): Promise<
2021-03-14 20:01:35 -07:00
RpcResponseAndContext<AccountInfo<Buffer | ParsedAccountData> | null>
2020-08-06 08:47:22 -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 = create(
unsafeRes,
jsonRpcResultAndContext(nullable(ParsedAccountInfoResult)),
);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-08-06 08:47:22 -07:00
throw new Error(
'failed to get info about account ' +
publicKey.toBase58() +
': ' +
res.error.message,
);
}
return res.result;
2020-08-06 08:47:22 -07:00
}
2020-01-08 13:44:50 -08:00
/**
* Fetch all the account info for the specified public key
*/
async getAccountInfo(
publicKey: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-06 08:47:22 -07:00
): Promise<AccountInfo<Buffer> | null> {
try {
const res = await this.getAccountInfoAndContext(publicKey, commitment);
return res.value;
} catch (e) {
throw new Error(
'failed to get info about account ' + publicKey.toBase58() + ': ' + e,
);
}
}
2018-09-20 15:08:52 -07:00
/**
* Returns epoch activation information for a stake account that has been delegated
*/
async getStakeActivation(
publicKey: PublicKey,
2021-03-14 20:01:35 -07:00
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 = create(unsafeRes, jsonRpcResult(StakeActivationResult));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
`failed to get Stake Activation ${publicKey.toBase58()}: ${
res.error.message
}`,
);
}
return res.result;
}
/**
* 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>}>>}
*/
async getProgramAccounts(
programId: PublicKey,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<Array<{pubkey: PublicKey; account: AccountInfo<Buffer>}>> {
const args = this._buildArgs([programId.toBase58()], commitment, 'base64');
const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
const res = create(unsafeRes, jsonRpcResult(array(KeyedAccountInfoResult)));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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,
);
}
return res.result;
}
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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-08-06 08:47:22 -07:00
): Promise<
Array<{
2021-03-14 20:01:35 -07:00
pubkey: PublicKey;
account: AccountInfo<Buffer | ParsedAccountData>;
}>
2020-08-06 08:47:22 -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 = create(
unsafeRes,
jsonRpcResult(array(KeyedParsedAccountInfoResult)),
);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-08-06 08:47:22 -07:00
throw new Error(
'failed to get accounts owned by program ' +
programId.toBase58() +
': ' +
res.error.message,
);
}
return res.result;
2020-08-06 08:47:22 -07:00
}
2020-01-08 13:44:50 -08:00
/**
* Confirm the transaction identified by the specified signature.
2020-01-08 13:44:50 -08:00
*/
async confirmTransaction(
signature: TransactionSignature,
2021-03-14 20:01:35 -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');
const start = Date.now();
const subscriptionCommitment = commitment || this.commitment;
let subscriptionId;
let response: RpcResponseAndContext<SignatureResult> | null = null;
const confirmPromise = new Promise((resolve, reject) => {
try {
subscriptionId = this.onSignature(
signature,
2021-03-14 20:01:35 -07:00
(result: SignatureResult, context: Context) => {
subscriptionId = undefined;
response = {
context,
value: result,
};
2021-03-14 20:01:35 -07:00
resolve(null);
},
subscriptionCommitment,
);
} catch (err) {
reject(err);
}
});
let timeoutMs = 60 * 1000;
switch (subscriptionCommitment) {
case 'processed':
case 'recent':
case 'single':
case 'confirmed':
case 'singleGossip': {
timeoutMs = 30 * 1000;
break;
}
// exhaust enums to ensure full coverage
case 'finalized':
case 'max':
case 'root':
}
try {
await promiseTimeout(confirmPromise, timeoutMs);
} finally {
if (subscriptionId) {
this.removeSignatureListener(subscriptionId);
}
}
if (response === null) {
const duration = (Date.now() - start) / 1000;
throw new Error(
`Transaction was not confirmed in ${duration.toFixed(
2,
)} seconds. It is unknown if it succeeded or failed. Check signature ${signature} using the Solana Explorer or CLI tools.`,
);
}
return response;
2018-08-23 10:52:48 -07:00
}
/**
* Return the list of nodes that are currently participating in the cluster
*/
async getClusterNodes(): Promise<Array<ContactInfo>> {
const unsafeRes = await this._rpcRequest('getClusterNodes', []);
const res = create(unsafeRes, jsonRpcResult(array(ContactInfoResult)));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-04-08 16:49:51 -07:00
throw new Error('failed to get cluster nodes: ' + res.error.message);
}
return res.result;
}
/**
2019-04-24 08:23:10 -07:00
* Return the list of nodes that are currently participating in the cluster
*/
2021-03-14 20:01:35 -07:00
async getVoteAccounts(commitment?: Commitment): Promise<VoteAccountStatus> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getVoteAccounts', args);
const res = create(unsafeRes, GetVoteAccounts);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-04-08 16:49:51 -07:00
throw new Error('failed to get vote accounts: ' + res.error.message);
}
return res.result;
}
/**
2019-08-02 16:06:54 -07:00
* Fetch the current slot that the node is processing
*/
2021-03-14 20:01:35 -07:00
async getSlot(commitment?: Commitment): Promise<number> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getSlot', args);
const res = create(unsafeRes, jsonRpcResult(number()));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
return res.result;
}
/**
* Fetch the current slot leader of the cluster
*/
2021-03-14 20:01:35 -07:00
async getSlotLeader(commitment?: Commitment): Promise<string> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getSlotLeader', args);
const res = create(unsafeRes, jsonRpcResult(string()));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-04-08 16:49:51 -07:00
throw new Error('failed to get slot leader: ' + res.error.message);
}
return res.result;
}
2018-09-26 19:16:17 -07: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,
2021-03-14 20:01:35 -07:00
config?: SignatureStatusConfig,
2020-03-26 06:37:45 -07:00
): Promise<RpcResponseAndContext<SignatureStatus | null>> {
const {context, value: values} = await this.getSignatureStatuses(
2020-03-26 06:37:45 -07:00
[signature],
config,
2020-03-26 06:37:45 -07:00
);
assert(values.length === 1);
const value = values[0];
return {context, value};
2020-03-23 08:01:12 -07:00
}
/**
* Fetch the current statuses of a batch of signatures
2020-03-23 08:01:12 -07:00
*/
async getSignatureStatuses(
2020-03-23 08:01:12 -07:00
signatures: Array<TransactionSignature>,
2021-03-14 20:01:35 -07:00
config?: SignatureStatusConfig,
2020-03-26 06:37:45 -07:00
): Promise<RpcResponseAndContext<Array<SignatureStatus | null>>> {
2021-03-14 20:01:35 -07:00
const params: any[] = [signatures];
if (config) {
params.push(config);
}
const unsafeRes = await this._rpcRequest('getSignatureStatuses', params);
const res = create(unsafeRes, GetSignatureStatusesRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
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
*/
2021-03-14 20:01:35 -07:00
async getTransactionCount(commitment?: Commitment): Promise<number> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getTransactionCount', args);
const res = create(unsafeRes, jsonRpcResult(number()));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
return res.result;
2018-08-23 10:52:48 -07:00
}
/**
* Fetch the current total currency supply of the cluster in lamports
*/
2021-03-14 20:01:35 -07:00
async getTotalSupply(commitment?: Commitment): Promise<number> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getTotalSupply', args);
const res = create(unsafeRes, jsonRpcResult(number()));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error('failed to get total supply: ' + res.error.message);
}
return res.result;
}
/**
* Fetch the cluster InflationGovernor parameters
*/
async getInflationGovernor(
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<InflationGovernor> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getInflationGovernor', args);
const res = create(unsafeRes, GetInflationGovernorRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-04-08 16:49:51 -07:00
throw new Error('failed to get inflation: ' + res.error.message);
}
return res.result;
}
/**
* Fetch the Epoch Info parameters
*/
2021-03-14 20:01:35 -07:00
async getEpochInfo(commitment?: Commitment): Promise<EpochInfo> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getEpochInfo', args);
const res = create(unsafeRes, GetEpochInfoRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-04-08 16:49:51 -07:00
throw new Error('failed to get epoch info: ' + res.error.message);
}
return 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 = create(unsafeRes, GetEpochScheduleRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
return res.result;
2019-10-23 06:48:24 -07:00
}
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 = create(unsafeRes, GetLeaderScheduleRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-07-17 08:16:44 -07:00
throw new Error('failed to get leader schedule: ' + res.error.message);
}
return res.result;
}
/**
* Fetch the minimum balance needed to exempt an account of `dataLength`
* size from rent
*/
async getMinimumBalanceForRentExemption(
dataLength: number,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<number> {
const args = this._buildArgs([dataLength], commitment);
const unsafeRes = await this._rpcRequest(
'getMinimumBalanceForRentExemption',
args,
);
const res = create(unsafeRes, GetMinimumBalanceForRentExemptionRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
console.warn('Unable to fetch minimum balance for rent exemption');
return 0;
}
return 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
*/
async getRecentBlockhashAndContext(
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2020-02-14 07:01:01 -08:00
): Promise<
2021-03-14 20:01:35 -07:00
RpcResponseAndContext<{blockhash: Blockhash; feeCalculator: FeeCalculator}>
2020-02-14 07:01:01 -08:00
> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getRecentBlockhash', args);
const res = create(unsafeRes, GetRecentBlockhashAndContextRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
2020-01-08 12:59:58 -08:00
return res.result;
}
2020-01-08 13:44:50 -08:00
/**
* Fetch recent performance samples
* @return {Promise<Array<PerfSample>>}
*/
async getRecentPerformanceSamples(
2021-03-14 20:01:35 -07:00
limit?: number,
): Promise<Array<PerfSample>> {
const args = this._buildArgs(limit ? [limit] : []);
const unsafeRes = await this._rpcRequest(
'getRecentPerformanceSamples',
args,
);
const res = create(unsafeRes, GetRecentPerformanceSamplesRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get recent performance samples: ' + res.error.message,
);
}
return res.result;
}
/**
* Fetch the fee calculator for a recent blockhash from the cluster, return with context
*/
async getFeeCalculatorForBlockhash(
blockhash: Blockhash,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<RpcResponseAndContext<FeeCalculator | null>> {
const args = this._buildArgs([blockhash], commitment);
const unsafeRes = await this._rpcRequest(
'getFeeCalculatorForBlockhash',
args,
);
const res = create(unsafeRes, GetFeeCalculatorRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error('failed to get fee calculator: ' + res.error.message);
}
const {context, value} = res.result;
return {
context,
2021-03-14 20:01:35 -07:00
value: value !== null ? value.feeCalculator : null,
};
}
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
*/
async getRecentBlockhash(
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<{blockhash: Blockhash; feeCalculator: FeeCalculator}> {
try {
const res = await this.getRecentBlockhashAndContext(commitment);
return res.value;
} catch (e) {
throw new Error('failed to get recent blockhash: ' + e);
}
}
2019-11-11 17:09:00 -08:00
/**
* Fetch the node version
*/
async getVersion(): Promise<Version> {
const unsafeRes = await this._rpcRequest('getVersion', []);
const res = create(unsafeRes, jsonRpcResult(VersionResult));
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
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
*/
async getConfirmedBlock(slot: number): Promise<ConfirmedBlock> {
2019-11-16 08:28:14 -08:00
const unsafeRes = await this._rpcRequest('getConfirmedBlock', [slot]);
const res = create(unsafeRes, GetConfirmedBlockRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error('failed to get confirmed block: ' + res.error.message);
2019-11-12 08:21:19 -08:00
}
const result = res.result;
if (!result) {
2020-02-03 00:59:39 -08:00
throw new Error('Confirmed block ' + slot + ' not found');
}
return result;
2019-11-12 08:21:19 -08:00
}
/**
* Fetch a transaction details for a confirmed transaction
*/
async getConfirmedTransaction(
signature: TransactionSignature,
): Promise<ConfirmedTransaction | null> {
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', [
signature,
]);
const res = create(unsafeRes, GetConfirmedTransactionRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get confirmed transaction: ' + res.error.message,
);
}
return res.result;
}
/**
* Fetch parsed transaction details for a confirmed transaction
*/
async getParsedConfirmedTransaction(
signature: TransactionSignature,
): Promise<ParsedConfirmedTransaction | null> {
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', [
signature,
'jsonParsed',
]);
const res = create(unsafeRes, GetParsedConfirmedTransactionRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get confirmed transaction: ' + res.error.message,
);
}
return res.result;
}
/**
* 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 res = create(unsafeRes, GetConfirmedSignaturesForAddressRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get confirmed signatures for address: ' + res.error.message,
);
}
return res.result;
}
/**
* 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,
2021-03-14 20:01:35 -07:00
options?: ConfirmedSignaturesForAddress2Options,
): Promise<Array<ConfirmedSignatureInfo>> {
const unsafeRes = await this._rpcRequest(
'getConfirmedSignaturesForAddress2',
[address.toBase58(), options],
);
const res = create(unsafeRes, GetConfirmedSignaturesForAddress2RpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
throw new Error(
'failed to get confirmed signatures for address: ' + res.error.message,
);
}
return res.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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): Promise<RpcResponseAndContext<NonceAccount | null>> {
const {context, value: accountInfo} = await this.getAccountInfoAndContext(
nonceAccount,
2020-01-02 17:54:43 -08:00
commitment,
);
let value = null;
if (accountInfo !== null) {
value = NonceAccount.fromAccountData(accountInfo.data);
}
2020-01-02 17:54:43 -08:00
return {
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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): 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> {
const unsafeRes = await this._rpcRequest('requestAirdrop', [
to.toBase58(),
amount,
]);
const res = create(unsafeRes, RequestAirdropRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
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
}
2018-09-12 17:41:20 -07:00
return res.result;
2018-08-23 10:52:48 -07:00
}
2021-03-14 20:01:35 -07:00
/**
* @internal
*/
2020-08-10 23:35:56 -07:00
async _recentBlockhash(disableCache: boolean): Promise<Blockhash> {
if (!disableCache) {
// Wait for polling to finish
while (this._pollingBlockhash) {
await sleep(100);
}
2021-03-14 20:01:35 -07:00
const timeSinceFetch = Date.now() - this._blockhashInfo.lastFetch;
const expired = timeSinceFetch >= BLOCKHASH_CACHE_TIMEOUT_MS;
2020-08-10 23:35:56 -07:00
if (this._blockhashInfo.recentBlockhash !== null && !expired) {
return this._blockhashInfo.recentBlockhash;
}
}
return await this._pollNewBlockhash();
}
2021-03-14 20:01:35 -07:00
/**
* @internal
*/
2020-08-10 23:35:56 -07:00
async _pollNewBlockhash(): Promise<Blockhash> {
this._pollingBlockhash = true;
try {
const startTime = Date.now();
for (let i = 0; i < 50; i++) {
const {blockhash} = await this.getRecentBlockhash('finalized');
if (this._blockhashInfo.recentBlockhash != blockhash) {
this._blockhashInfo = {
recentBlockhash: blockhash,
2021-03-14 20:01:35 -07:00
lastFetch: Date.now(),
transactionSignatures: [],
simulatedSignatures: [],
};
return blockhash;
}
// Sleep for approximately half a slot
await sleep(MS_PER_SLOT / 2);
2020-08-10 23:35: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);
const encodedTransaction = wireTransaction.toString('base64');
2020-12-24 10:43:45 -08:00
const config: any = {
encoding: 'base64',
commitment: this.commitment,
};
2020-08-10 23:35:56 -07:00
if (signers) {
config.sigVerify = true;
2020-08-10 23:35:56 -07:00
}
2021-03-14 20:01:35 -07:00
const args = [encodedTransaction, config];
2020-08-10 23:35:56 -07:00
const unsafeRes = await this._rpcRequest('simulateTransaction', args);
const res = create(unsafeRes, SimulatedTransactionResponseStruct);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
2020-08-10 23:35:56 -07:00
throw new Error('failed to simulate transaction: ' + res.error.message);
}
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,
signers: Array<Account>,
2020-06-03 04:55:42 -07:00
options?: SendOptions,
2018-11-04 11:41:21 -08:00
): Promise<TransactionSignature> {
if (transaction.nonceInfo) {
transaction.sign(...signers);
} else {
2020-08-10 23:35:56 -07:00
let disableCache = this._disableBlockhashCaching;
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;
}
}
}
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
}
/**
* Send a transaction that has already been signed and serialized into the
* wire format
*/
async sendRawTransaction(
rawTransaction: Buffer | Uint8Array | Array<number>,
2021-03-14 20:01:35 -07:00
options?: SendOptions,
): Promise<TransactionSignature> {
const encodedTransaction = toBuffer(rawTransaction).toString('base64');
2020-06-03 04:55:42 -07:00
const result = await this.sendEncodedTransaction(
encodedTransaction,
options,
);
return result;
}
/**
* Send a transaction that has already been signed, serialized into the
* wire format, and encoded as a base64 string
*/
async sendEncodedTransaction(
encodedTransaction: string,
2021-03-14 20:01:35 -07:00
options?: SendOptions,
2018-11-27 21:06:03 -08:00
): Promise<TransactionSignature> {
const config: any = {encoding: 'base64'};
2020-06-03 04:55:42 -07:00
const skipPreflight = options && options.skipPreflight;
const preflightCommitment =
(options && options.preflightCommitment) || this.commitment;
if (skipPreflight) {
config.skipPreflight = skipPreflight;
}
if (preflightCommitment) {
config.preflightCommitment = preflightCommitment;
}
2021-03-14 20:01:35 -07:00
const args = [encodedTransaction, config];
2020-06-03 04:55:42 -07:00
const unsafeRes = await this._rpcRequest('sendTransaction', args);
const res = create(unsafeRes, SendTransactionRpcResult);
2021-03-14 20:01:35 -07:00
if ('error' in res) {
if ('data' in res.error) {
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);
}
return res.result;
2018-08-23 10:52:48 -07:00
}
2018-10-26 21:37:39 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2018-10-26 21:37:39 -07:00
*/
_wsOnOpen() {
this._rpcWebSocketConnected = true;
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();
}
/**
2021-03-14 20:01:35 -07:00
* @internal
2018-10-26 21:37:39 -07:00
*/
_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
}
/**
2021-03-14 20:01:35 -07:00
* @internal
2018-10-26 21:37:39 -07:00
*/
_wsOnClose(code: number) {
2021-03-14 20:01:35 -07:00
if (this._rpcWebSocketHeartbeat) {
clearInterval(this._rpcWebSocketHeartbeat);
this._rpcWebSocketHeartbeat = null;
}
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
}
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
2021-03-14 20:01:35 -07:00
async _subscribe(
sub: {subscriptionId: SubscriptionId | null},
rpcMethod: string,
2021-03-14 20:01:35 -07:00
rpcArgs: IWSRequestParams,
) {
if (sub.subscriptionId == null) {
sub.subscriptionId = 'subscribing';
try {
const id = await this._rpcWebSocket.call(rpcMethod, rpcArgs);
2021-03-14 20:01:35 -07:00
if (typeof id === 'number' && 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);
}
}
}
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
2021-03-14 20:01:35 -07:00
async _unsubscribe(
sub: {subscriptionId: SubscriptionId | null},
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-04-09 04:34:33 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2020-04-09 04:34:33 -07:00
*/
_resetSubscriptions() {
2021-03-14 20:01:35 -07:00
Object.values(this._accountChangeSubscriptions).forEach(
2020-04-09 04:34:33 -07:00
s => (s.subscriptionId = null),
);
2021-03-14 20:01:35 -07:00
Object.values(this._programAccountChangeSubscriptions).forEach(
2020-04-09 04:34:33 -07:00
s => (s.subscriptionId = null),
);
2021-03-14 20:01:35 -07:00
Object.values(this._signatureSubscriptions).forEach(
2020-04-09 04:34:33 -07:00
s => (s.subscriptionId = null),
);
2021-03-14 20:01:35 -07:00
Object.values(this._slotSubscriptions).forEach(
2020-04-09 04:34:33 -07:00
s => (s.subscriptionId = null),
);
2021-03-14 20:01:35 -07:00
Object.values(this._rootSubscriptions).forEach(
2020-04-09 04:34:33 -07:00
s => (s.subscriptionId = null),
);
}
2018-10-26 21:37:39 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2018-10-26 21:37:39 -07:00
*/
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);
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);
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
) {
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;
}
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];
this._subscribe(
sub,
'accountSubscribe',
this._buildArgs([sub.publicKey], sub.commitment, 'base64'),
);
2018-10-26 21:37:39 -07:00
}
2019-03-08 16:02:39 -08:00
for (let id of programKeys) {
const sub = this._programAccountChangeSubscriptions[id];
this._subscribe(
sub,
'programSubscribe',
this._buildArgs([sub.programId], sub.commitment, 'base64'),
);
2019-03-08 16:02:39 -08:00
}
for (let id of slotKeys) {
const sub = this._slotSubscriptions[id];
2020-02-11 06:33:50 -08:00
this._subscribe(sub, 'slotSubscribe', []);
}
2020-02-03 07:22:11 -08:00
for (let id of signatureKeys) {
const sub = this._signatureSubscriptions[id];
const args: any[] = [sub.signature];
if (sub.options) args.push(sub.options);
this._subscribe(sub, 'signatureSubscribe', args);
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
}
/**
2021-03-14 20:01:35 -07:00
* @internal
2019-03-08 16:02:39 -08:00
*/
2021-03-14 20:01:35 -07:00
_wsOnAccountNotification(notification: object) {
const res = create(notification, AccountNotificationResult);
2021-03-14 20:01:35 -07:00
for (const sub of Object.values(this._accountChangeSubscriptions)) {
2019-03-08 16:02:39 -08:00
if (sub.subscriptionId === res.subscription) {
sub.callback(res.result.value, res.result.context);
2021-03-14 20:01:35 -07:00
return;
2019-03-08 16:02:39 -08:00
}
}
2018-10-26 21:37:39 -07:00
}
/**
* Register a callback to be invoked whenever the specified account changes
*
* @param publicKey Public key of the account to monitor
2018-10-26 21:37:39 -07:00
* @param callback Function to invoke whenever the account is changed
* @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,
2021-03-14 20:01:35 -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,
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]) {
const subInfo = this._accountChangeSubscriptions[id];
2018-10-26 21:37:39 -07:00
delete this._accountChangeSubscriptions[id];
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
/**
2021-03-14 20:01:35 -07:00
* @internal
2019-03-08 16:02:39 -08:00
*/
_wsOnProgramAccountNotification(notification: Object) {
const res = create(notification, ProgramAccountNotificationResult);
2021-03-14 20:01:35 -07:00
for (const sub of Object.values(this._programAccountChangeSubscriptions)) {
2019-03-08 16:02:39 -08:00
if (sub.subscriptionId === res.subscription) {
const {value, context} = res.result;
sub.callback(
{
accountId: value.pubkey,
accountInfo: value.account,
2019-03-08 16:02:39 -08:00
},
context,
);
2021-03-14 20:01:35 -07:00
return;
2019-03-08 16:02:39 -08:00
}
}
}
/**
* 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
* @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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
2019-03-08 16:02:39 -08:00
): number {
const id = ++this._programAccountChangeSubscriptionCounter;
this._programAccountChangeSubscriptions[id] = {
programId: programId.toBase58(),
callback,
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]) {
const subInfo = this._programAccountChangeSubscriptions[id];
2019-03-08 16:02:39 -08:00
delete this._programAccountChangeSubscriptions[id];
await this._unsubscribe(subInfo, 'programUnsubscribe');
2019-03-08 16:02:39 -08:00
this._updateSubscriptions();
} else {
throw new Error(`Unknown program account change id: ${id}`);
2019-03-08 16:02:39 -08:00
}
}
/**
2021-03-14 20:01:35 -07:00
* @internal
*/
_wsOnSlotNotification(notification: Object) {
const res = create(notification, SlotNotificationResult);
2021-03-14 20:01:35 -07:00
for (const sub of Object.values(this._slotSubscriptions)) {
if (sub.subscriptionId === res.subscription) {
sub.callback(res.result);
2021-03-14 20:01:35 -07:00
return;
}
}
}
/**
* Register a callback to be invoked upon slot changes
*
* @param callback Function to invoke whenever the slot changes
* @return subscription id
*/
onSlotChange(callback: SlotChangeCallback): number {
const id = ++this._slotSubscriptionCounter;
this._slotSubscriptions[id] = {
callback,
subscriptionId: null,
};
this._updateSubscriptions();
return id;
}
/**
* Deregister a slot notification callback
*
* @param id subscription id to deregister
*/
async removeSlotChangeListener(id: number): Promise<void> {
if (this._slotSubscriptions[id]) {
const subInfo = this._slotSubscriptions[id];
delete this._slotSubscriptions[id];
await this._unsubscribe(subInfo, 'slotUnsubscribe');
this._updateSubscriptions();
} else {
throw new Error(`Unknown slot change id: ${id}`);
}
}
2021-03-14 20:01:35 -07:00
/**
* @internal
*/
_buildArgs(
2020-08-06 08:47:22 -07:00
args: Array<any>,
2021-03-14 20:01:35 -07:00
override?: Commitment,
encoding?: 'jsonParsed' | 'base64',
extra?: any,
2020-08-06 08:47:22 -07:00
): Array<any> {
const commitment = override || this._commitment;
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;
}
if (extra) {
options = Object.assign(options, extra);
}
2020-08-06 08:47:22 -07:00
args.push(options);
}
return args;
}
2020-02-03 07:22:11 -08:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2020-02-03 07:22:11 -08:00
*/
_wsOnSignatureNotification(notification: Object) {
const res = create(notification, SignatureNotificationResult);
2021-03-14 20:01:35 -07:00
for (const [id, sub] of Object.entries(this._signatureSubscriptions)) {
2020-02-03 07:22:11 -08:00
if (sub.subscriptionId === res.subscription) {
if (res.result.value === 'receivedSignature') {
sub.callback(
{
type: 'received',
},
res.result.context,
);
} else {
// Signatures subscriptions are auto-removed by the RPC service so
// no need to explicitly send an unsubscribe message
delete this._signatureSubscriptions[Number(id)];
this._updateSubscriptions();
sub.callback(
{
type: 'status',
result: res.result.value,
},
res.result.context,
);
}
2020-02-03 07:22:11 -08:00
return;
}
}
}
/**
* Register a callback to be invoked upon signature updates
*
* @param signature Transaction signature string in base 58
2020-02-03 07:22:11 -08:00
* @param callback Function to invoke on signature notifications
* @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,
2021-03-14 20:01:35 -07:00
commitment?: Commitment,
): number {
const id = ++this._signatureSubscriptionCounter;
this._signatureSubscriptions[id] = {
signature,
callback: (notification, context) => {
if (notification.type === 'status') {
callback(notification.result, context);
}
},
options: {commitment},
subscriptionId: null,
};
this._updateSubscriptions();
return id;
}
/**
* Register a callback to be invoked when a transaction is
* received and/or processed.
*
* @param signature Transaction signature string in base 58
* @param callback Function to invoke on signature notifications
* @param options Enable received notifications and set the commitment
* level that signature must reach before notification
* @return subscription id
*/
onSignatureWithOptions(
signature: TransactionSignature,
callback: SignatureSubscriptionCallback,
options?: SignatureSubscriptionOptions,
2020-02-03 07:22:11 -08:00
): number {
const id = ++this._signatureSubscriptionCounter;
this._signatureSubscriptions[id] = {
signature,
callback,
options,
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]) {
const subInfo = this._signatureSubscriptions[id];
2020-02-03 07:22:11 -08:00
delete this._signatureSubscriptions[id];
await this._unsubscribe(subInfo, 'signatureUnsubscribe');
2020-02-03 07:22:11 -08:00
this._updateSubscriptions();
} else {
throw new Error(`Unknown signature result id: ${id}`);
2020-02-03 07:22:11 -08:00
}
}
2020-03-27 07:22:53 -07:00
/**
2021-03-14 20:01:35 -07:00
* @internal
2020-03-27 07:22:53 -07:00
*/
_wsOnRootNotification(notification: Object) {
const res = create(notification, RootNotificationResult);
2021-03-14 20:01:35 -07:00
for (const sub of Object.values(this._rootSubscriptions)) {
2020-03-27 07:22:53 -07:00
if (sub.subscriptionId === res.subscription) {
sub.callback(res.result);
2021-03-14 20:01:35 -07:00
return;
2020-03-27 07:22:53 -07:00
}
}
}
/**
* 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
}