solana/web3.js/src/connection.js

1090 lines
29 KiB
JavaScript
Raw Normal View History

2018-08-23 10:52:48 -07:00
// @flow
import assert from 'assert';
2018-11-04 11:41:21 -08:00
import {parse as urlParse, format as urlFormat} from 'url';
2018-08-23 10:52:48 -07:00
import fetch from 'node-fetch';
import jayson from 'jayson/lib/client/browser';
import {struct} from 'superstruct';
2018-10-26 21:37:39 -07:00
import {Client as RpcWebSocketClient} from 'rpc-websockets';
2018-08-23 10:52:48 -07:00
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from './timing';
2018-09-30 18:42:45 -07:00
import {PublicKey} from './publickey';
import {Transaction} from './transaction';
import {sleep} from './util/sleep';
2019-03-04 08:06:33 -08:00
import type {Blockhash} from './blockhash';
2019-06-12 14:36:05 -07:00
import type {FeeCalculator} from './fee-calculator';
2018-09-30 18:42:45 -07:00
import type {Account} from './account';
2019-03-04 08:06:33 -08:00
import type {TransactionSignature} from './transaction';
2018-08-23 10:52:48 -07:00
type RpcRequest = (methodName: string, args: Array<any>) => any;
2018-08-23 10:52:48 -07:00
/**
* Information describing a cluster node
*
* @typedef {Object} ContactInfo
* @property {string} pubkey Identity public key of the node
* @property {string} gossip Gossip network address for the node
* @property {string} tpu TPU network address for the node (null if not available)
* @property {string|null} rpc JSON RPC network address for the node (null if not available)
*/
type ContactInfo = {
pubkey: string,
gossip: string,
tpu: string | null,
rpc: 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 {number} commission A 8-bit unsigned integer used as a fraction (commission/0xFF) for rewards payout
* @property {number} lastVote Most recent slot voted on by this vote account
*/
type VoteAccountInfo = {
votePubkey: string,
nodePubkey: string,
activatedStake: number,
epochVoteAccount: boolean,
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
*/
type VoteAccountStatus = {
current: Array<VoteAccountInfo>,
delinquent: Array<VoteAccountInfo>,
};
/**
* Network Inflation parameters
*
* @typedef {Object} Inflation TODO - link to book terminology?
* @property {number} foundation TODO - link to book terminology?
* @property {number} foundation_term TODO - link to book terminology?
* @property {number} grant TODO - link to book terminology?
* @property {number} grant_term TODO - link to book terminology?
* @property {number} initial TODO - link to book terminology?
* @property {number} storage TODO - link to book terminology?
* @property {number} taper TODO - link to book terminology?
* @property {number} terminal TODO - link to book terminology?
*/
const GetInflationResult = struct({
foundation: 'number',
foundation_term: 'number',
initial: 'number',
storage: 'number',
taper: 'number',
terminal: 'number',
});
2018-08-23 10:52:48 -07:00
function createRpcRequest(url): RpcRequest {
2018-11-04 11:41:21 -08:00
const server = jayson(async (request, callback) => {
const options = {
method: 'POST',
body: request,
headers: {
'Content-Type': 'application/json',
},
};
try {
const res = await fetch(url, options);
const text = await res.text();
callback(null, text);
} catch (err) {
callback(err);
2018-08-23 10:52:48 -07:00
}
2018-11-04 11:41:21 -08:00
});
2018-08-23 10:52:48 -07:00
return (method, args) => {
return new Promise((resolve, reject) => {
server.request(method, args, (err, response) => {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
};
}
/**
* Expected JSON RPC response for the "getInflation" message
*/
const GetInflationRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: GetInflationResult,
});
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "getBalance" message
*/
2018-08-23 10:52:48 -07:00
const GetBalanceRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'number?',
});
2018-09-26 19:54:59 -07:00
/**
* @private
*/
function jsonRpcResult(resultDescription: any) {
const jsonRpcVersion = struct.literal('2.0');
return struct.union([
struct({
jsonrpc: jsonRpcVersion,
id: 'string',
2018-11-04 11:41:21 -08:00
error: 'any',
2018-09-26 19:54:59 -07:00
}),
struct({
jsonrpc: jsonRpcVersion,
id: 'string',
error: 'null?',
result: resultDescription,
}),
]);
}
2018-09-20 15:08:52 -07:00
/**
2018-10-26 21:37:39 -07:00
* @private
2018-09-20 15:08:52 -07:00
*/
2018-10-26 21:37:39 -07:00
const AccountInfoResult = struct({
executable: 'boolean',
owner: 'array',
2019-03-05 17:52:13 -08:00
lamports: 'number',
2019-03-14 13:27:47 -07:00
data: 'array',
rent_epoch: 'number?',
2018-09-20 15:08:52 -07:00
});
2018-10-26 21:37:39 -07:00
/**
* Expected JSON RPC response for the "getAccountInfo" message
*/
const GetAccountInfoRpcResult = jsonRpcResult(AccountInfoResult);
/***
* Expected JSON RPC response for the "accountNotification" message
*/
const AccountNotificationResult = struct({
subscription: 'number',
result: AccountInfoResult,
});
2018-09-20 15:08:52 -07:00
2019-03-08 16:02:39 -08:00
/**
* @private
*/
const ProgramAccountInfoResult = struct(['string', AccountInfoResult]);
/***
* Expected JSON RPC response for the "programNotification" message
*/
const ProgramAccountNotificationResult = struct({
subscription: 'number',
result: ProgramAccountInfoResult,
});
/**
* Expected JSON RPC response for the "getProgramAccounts" message
*/
const GetProgramAccountsRpcResult = jsonRpcResult(
struct.list([ProgramAccountInfoResult]),
);
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "confirmTransaction" message
*/
2018-09-26 19:54:59 -07:00
const ConfirmTransactionRpcResult = jsonRpcResult('boolean');
2018-08-23 10:52:48 -07:00
2019-08-02 16:06:54 -07:00
/**
* Expected JSON RPC response for the "getSlot" message
*/
const GetSlot = jsonRpcResult('number');
/**
* Expected JSON RPC response for the "getSlotLeader" message
*/
const GetSlotLeader = jsonRpcResult('string');
/**
* Expected JSON RPC response for the "getClusterNodes" message
*/
const GetClusterNodes = jsonRpcResult(
struct.list([
struct({
pubkey: 'string',
gossip: 'string',
tpu: struct.union(['null', 'string']),
rpc: struct.union(['null', 'string']),
}),
]),
);
/**
* @ignore
*/
const GetClusterNodes_015 = jsonRpcResult(
struct.list([
struct({
id: 'string',
gossip: 'string',
tpu: struct.union(['null', 'string']),
rpc: struct.union(['null', 'string']),
}),
]),
);
/**
* Expected JSON RPC response for the "getVoteAccounts" message
*/
const GetVoteAccounts = jsonRpcResult(
struct({
current: struct.list([
struct({
votePubkey: 'string',
nodePubkey: 'string',
activatedStake: 'number',
epochVoteAccount: 'boolean',
commission: 'number',
lastVote: 'number',
}),
]),
delinquent: struct.list([
struct({
votePubkey: 'string',
nodePubkey: 'string',
activatedStake: 'number',
epochVoteAccount: 'boolean',
commission: 'number',
lastVote: 'number',
}),
]),
}),
);
2018-09-26 19:16:17 -07:00
/**
* Expected JSON RPC response for the "getSignatureStatus" message
*/
2018-11-04 11:41:21 -08:00
const GetSignatureStatusRpcResult = jsonRpcResult(
struct.union([
'null',
2019-04-10 14:40:49 -07:00
struct.union([struct({Ok: 'null'}), struct({Err: 'object'})]),
]),
2018-11-04 11:41:21 -08:00
);
2018-09-26 19:16:17 -07:00
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "getTransactionCount" message
*/
2018-09-26 19:54:59 -07:00
const GetTransactionCountRpcResult = jsonRpcResult('number');
2018-08-23 10:52:48 -07:00
/**
* Expected JSON RPC response for the "getTotalSupply" message
*/
const GetTotalSupplyRpcResult = jsonRpcResult('number');
/**
* Expected JSON RPC response for the "getMinimumBalanceForRentExemption" message
*/
const GetMinimumBalanceForRentExemptionRpcResult = jsonRpcResult('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
*/
2019-06-12 14:36:05 -07:00
const GetRecentBlockhash = jsonRpcResult([
'string',
struct({
burnPercent: 'number',
2019-06-12 14:36:05 -07:00
lamportsPerSignature: 'number',
maxLamportsPerSignature: 'number',
minLamportsPerSignature: 'number',
2019-06-12 14:36:05 -07:00
targetLamportsPerSignature: 'number',
targetSignaturesPerSlot: 'number',
}),
]);
/**
* @ignore
*/
const GetRecentBlockhash_016 = jsonRpcResult([
2019-06-12 14:36:05 -07:00
'string',
struct({
lamportsPerSignature: 'number',
maxLamportsPerSignature: 'number',
minLamportsPerSignature: 'number',
targetLamportsPerSignature: 'number',
targetSignaturesPerSlot: 'number',
2019-06-12 14:36:05 -07:00
}),
]);
2018-08-23 10:52:48 -07:00
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "requestAirdrop" message
*/
2018-09-26 19:54:59 -07:00
const RequestAirdropRpcResult = jsonRpcResult('string');
2018-08-24 09:05:23 -07:00
/**
* Expected JSON RPC response for the "sendTransaction" message
*/
const SendTransactionRpcResult = jsonRpcResult('string');
2018-08-23 10:52:48 -07:00
2018-09-20 15:08:52 -07:00
/**
* Information describing an account
2018-09-20 15:35:41 -07:00
*
* @typedef {Object} AccountInfo
2019-03-05 17:52:13 -08:00
* @property {number} lamports Number of lamports assigned to the account
* @property {PublicKey} owner Identifier of the program that owns the account
2019-03-14 13:27:47 -07:00
* @property {?Buffer} data Optional data assigned to the account
* @property {boolean} executable `true` if this account's data contains a loaded program
2018-09-20 15:08:52 -07:00
*/
type AccountInfo = {
2018-11-04 11:41:21 -08:00
executable: boolean,
owner: PublicKey,
2019-03-05 17:52:13 -08:00
lamports: number,
2019-03-14 13:27:47 -07:00
data: Buffer,
2018-11-04 11:41:21 -08:00
};
2018-09-20 15:08:52 -07:00
2019-03-08 16:02:39 -08:00
/**
* Account information identified by pubkey
*
* @typedef {Object} KeyedAccountInfo
* @property {PublicKey} accountId
* @property {AccountInfo} accountInfo
*/
type KeyedAccountInfo = {
accountId: PublicKey,
accountInfo: AccountInfo,
};
2018-10-26 21:37:39 -07:00
/**
* Callback function for account change notifications
*/
export type AccountChangeCallback = (accountInfo: AccountInfo) => void;
/**
* @private
*/
type AccountSubscriptionInfo = {
2018-11-04 11:41:21 -08:00
publicKey: string, // PublicKey of the account as a base 58 string
2018-10-26 21:37:39 -07:00
callback: AccountChangeCallback,
2018-11-04 11:41:21 -08:00
subscriptionId: null | number, // null when there's no current server subscription id
};
2018-10-26 21:37:39 -07:00
2019-03-08 16:02:39 -08:00
/**
* Callback function for program account change notifications
*/
export type ProgramAccountChangeCallback = (
keyedAccountInfo: KeyedAccountInfo,
) => void;
/**
* @private
*/
type ProgramAccountSubscriptionInfo = {
programId: string, // PublicKey of the program as a base 58 string
callback: ProgramAccountChangeCallback,
subscriptionId: null | number, // null when there's no current server subscription id
};
2018-09-26 19:16:17 -07:00
/**
2019-04-10 14:40:49 -07:00
* Signature status: Success
2018-09-26 19:16:17 -07:00
*
2019-04-10 14:40:49 -07:00
* @typedef {Object} SignatureSuccess
2018-09-26 19:16:17 -07:00
*/
2019-04-10 14:40:49 -07:00
export type SignatureSuccess = {|
Ok: null,
|};
/**
* Signature status: TransactionError
*
* @typedef {Object} TransactionError
*/
export type TransactionError = {|
Err: Object,
|};
2018-09-26 19:16:17 -07:00
2019-06-12 14:36:05 -07:00
/**
* @ignore
*/
type BlockhashAndFeeCalculator = [Blockhash, FeeCalculator]; // This type exists to workaround an esdoc parse error
/**
* @ignore
*/
type PublicKeyAndAccount = [PublicKey, AccountInfo]; // This type exists to workaround an esdoc parse error
2018-08-24 09:05:23 -07:00
/**
* A connection to a fullnode JSON RPC endpoint
*/
2018-08-23 10:52:48 -07:00
export class Connection {
_rpcRequest: RpcRequest;
2018-10-26 21:37:39 -07:00
_rpcWebSocket: RpcWebSocketClient;
_rpcWebSocketConnected: boolean = false;
2018-10-22 20:03:44 -07:00
2019-03-04 08:06:33 -08:00
_blockhashInfo: {
recentBlockhash: Blockhash | null,
2018-10-22 20:03:44 -07:00
seconds: number,
transactionSignatures: Array<string>,
};
2019-03-04 08:06:33 -08:00
_disableBlockhashCaching: boolean = false;
2018-10-26 21:37:39 -07:00
_accountChangeSubscriptions: {[number]: AccountSubscriptionInfo} = {};
_accountChangeSubscriptionCounter: number = 0;
2019-03-08 16:02:39 -08:00
_programAccountChangeSubscriptions: {
[number]: ProgramAccountSubscriptionInfo,
} = {};
_programAccountChangeSubscriptionCounter: number = 0;
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
*/
2018-08-23 10:52:48 -07:00
constructor(endpoint: string) {
2018-10-26 21:37:39 -07:00
let url = urlParse(endpoint);
this._rpcRequest = createRpcRequest(url.href);
2019-03-04 08:06:33 -08:00
this._blockhashInfo = {
recentBlockhash: null,
2018-10-22 20:03:44 -07:00
seconds: -1,
transactionSignatures: [],
};
2018-10-26 21:37:39 -07:00
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
2018-10-26 21:37:39 -07:00
url.host = '';
url.port = String(Number(url.port) + 1);
2018-11-01 20:42:14 -07:00
if (url.port === '1') {
url.port = url.protocol === 'wss:' ? '8901' : '8900';
2018-11-01 20:42:14 -07:00
}
2018-11-04 11:41:21 -08:00
this._rpcWebSocket = new RpcWebSocketClient(urlFormat(url), {
autoconnect: false,
max_reconnects: Infinity,
});
2018-10-26 21:37:39 -07:00
this._rpcWebSocket.on('open', this._wsOnOpen.bind(this));
this._rpcWebSocket.on('error', this._wsOnError.bind(this));
this._rpcWebSocket.on('close', this._wsOnClose.bind(this));
2018-11-04 11:41:21 -08:00
this._rpcWebSocket.on(
'accountNotification',
this._wsOnAccountNotification.bind(this),
);
2019-03-08 16:02:39 -08:00
this._rpcWebSocket.on(
'programNotification',
this._wsOnProgramAccountNotification.bind(this),
);
2018-08-23 10:52:48 -07:00
}
2018-08-24 09:05:23 -07:00
/**
* Fetch the balance for the specified public key
*/
async getBalance(publicKey: PublicKey): Promise<number> {
2018-11-04 11:41:21 -08:00
const unsafeRes = await this._rpcRequest('getBalance', [
publicKey.toBase58(),
]);
2018-08-23 10:52:48 -07:00
const res = GetBalanceRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-09-20 15:08:52 -07:00
/**
* Fetch all the account info for the specified public key
*/
async getAccountInfo(publicKey: PublicKey): Promise<AccountInfo> {
2018-11-04 11:41:21 -08:00
const unsafeRes = await this._rpcRequest('getAccountInfo', [
publicKey.toBase58(),
]);
2018-09-20 15:08:52 -07:00
const res = GetAccountInfoRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
const {result} = res;
assert(typeof result !== 'undefined');
return {
executable: result.executable,
owner: new PublicKey(result.owner),
2019-03-05 17:52:13 -08:00
lamports: result.lamports,
2019-03-14 13:27:47 -07:00
data: Buffer.from(result.data),
2018-09-20 15:08:52 -07:00
};
}
/**
* Fetch all the accounts owned by the specified program id
*/
async getProgramAccounts(
programId: PublicKey,
): Promise<Array<PublicKeyAndAccount>> {
const unsafeRes = await this._rpcRequest('getProgramAccounts', [
programId.toBase58(),
]);
const res = GetProgramAccountsRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
const {result} = res;
assert(typeof result !== 'undefined');
return result.map(result => {
return [
result[0],
{
executable: result[1].executable,
owner: new PublicKey(result[1].owner),
lamports: result[1].lamports,
data: Buffer.from(result[1].data),
},
];
});
}
2018-08-24 09:05:23 -07:00
/**
* Confirm the transaction identified by the specified signature
*/
2018-08-23 10:52:48 -07:00
async confirmTransaction(signature: TransactionSignature): Promise<boolean> {
2018-11-04 11:41:21 -08:00
const unsafeRes = await this._rpcRequest('confirmTransaction', [signature]);
2018-08-23 10:52:48 -07:00
const res = ConfirmTransactionRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/**
* Return the list of nodes that are currently participating in the cluster
*/
async getClusterNodes(): Promise<Array<ContactInfo>> {
const unsafeRes = await this._rpcRequest('getClusterNodes', []);
// Legacy v0.15 response. TODO: Remove in August 2019
try {
const res_015 = GetClusterNodes_015(unsafeRes);
if (res_015.error) {
console.log('no', res_015.error);
throw new Error(res_015.error.message);
}
return res_015.result.map(node => {
node.pubkey = node.id;
node.id = undefined;
return node;
});
} catch (e) {
// Not legacy format
}
// End Legacy v0.15 response
const res = GetClusterNodes(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/**
2019-04-24 08:23:10 -07:00
* Return the list of nodes that are currently participating in the cluster
*/
async getVoteAccounts(): Promise<VoteAccountStatus> {
const unsafeRes = await this._rpcRequest('getVoteAccounts', []);
const res = GetVoteAccounts(unsafeRes);
//const res = unsafeRes;
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/**
2019-08-02 16:06:54 -07:00
* Fetch the current slot that the node is processing
*/
async getSlot(): Promise<number> {
const unsafeRes = await this._rpcRequest('getSlot', []);
const res = GetSlot(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
/**
* Fetch the current slot leader of the cluster
*/
async getSlotLeader(): Promise<string> {
const unsafeRes = await this._rpcRequest('getSlotLeader', []);
const res = GetSlotLeader(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-09-26 19:16:17 -07:00
/**
2019-03-04 08:06:33 -08:00
* Fetch the current transaction count of the cluster
2018-09-26 19:16:17 -07:00
*/
2018-11-04 11:41:21 -08:00
async getSignatureStatus(
signature: TransactionSignature,
2019-04-10 14:40:49 -07:00
): Promise<SignatureSuccess | TransactionError | null> {
2018-09-26 19:16:17 -07:00
const unsafeRes = await this._rpcRequest('getSignatureStatus', [signature]);
const res = GetSignatureStatusRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-08-24 09:05:23 -07:00
/**
2019-03-04 08:06:33 -08:00
* Fetch the current transaction count of the cluster
2018-08-24 09:05:23 -07:00
*/
2018-08-23 10:52:48 -07:00
async getTransactionCount(): Promise<number> {
const unsafeRes = await this._rpcRequest('getTransactionCount', []);
const res = GetTransactionCountRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return Number(res.result);
}
/**
* Fetch the current total currency supply of the cluster in lamports
*/
async getTotalSupply(): Promise<number> {
const unsafeRes = await this._rpcRequest('getTotalSupply', []);
const res = GetTotalSupplyRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return Number(res.result);
}
/**
* Fetch the cluster Inflation parameters (TODO - book link/terminology?)
*/
async getInflation(): Promise<GetInflationRpcResult> {
const unsafeRes = await this._rpcRequest('getInflation', []);
const res = GetInflationRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return GetInflationResult(res.result);
}
/**
* Fetch the minimum balance needed to exempt an account of `dataLength`
* size from rent
*/
async getMinimumBalanceForRentExemption(dataLength: number): Promise<number> {
const unsafeRes = await this._rpcRequest(
'getMinimumBalanceForRentExemption',
[dataLength],
);
const res = GetMinimumBalanceForRentExemptionRpcResult(unsafeRes);
if (res.error) {
console.warn("Unable to fetch minimum balance for rent exemption");
return 0;
}
assert(typeof res.result !== 'undefined');
return Number(res.result);
}
2018-08-24 09:05:23 -07:00
/**
2019-03-04 08:06:33 -08:00
* Fetch a recent blockhash from the cluster
2018-08-24 09:05:23 -07:00
*/
2019-06-12 14:36:05 -07:00
async getRecentBlockhash(): Promise<BlockhashAndFeeCalculator> {
2019-03-04 08:06:33 -08:00
const unsafeRes = await this._rpcRequest('getRecentBlockhash', []);
// Legacy v0.16 response. TODO: Remove in September 2019
try {
const res_016 = GetRecentBlockhash_016(unsafeRes);
if (res_016.error) {
throw new Error(res_016.error.message);
}
const [blockhash, feeCalculator] = res_016.result;
feeCalculator.burnPercent = 0;
2019-06-12 14:36:05 -07:00
return [blockhash, feeCalculator];
} catch (e) {
// Not legacy format
}
// End Legacy v0.16 response
2019-03-04 08:06:33 -08:00
const res = GetRecentBlockhash(unsafeRes);
2018-08-23 10:52:48 -07:00
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
2019-06-12 14:36:05 -07:00
return res.result;
2018-08-23 10:52:48 -07:00
}
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,
]);
2018-08-23 16:39:52 -07:00
const res = RequestAirdropRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
2018-09-12 17:41:20 -07:00
return res.result;
2018-08-23 10:52:48 -07:00
}
2018-08-24 09:05:23 -07:00
/**
2018-09-14 08:27:40 -07:00
* Sign and send a transaction
2018-08-24 09:05:23 -07:00
*/
2018-11-04 11:41:21 -08:00
async sendTransaction(
transaction: Transaction,
...signers: Array<Account>
2018-11-04 11:41:21 -08:00
): Promise<TransactionSignature> {
for (;;) {
// Attempt to use a recent blockhash for up to 30 seconds
2018-11-04 11:41:21 -08:00
const seconds = new Date().getSeconds();
if (
2019-03-04 08:06:33 -08:00
this._blockhashInfo.recentBlockhash != null &&
this._blockhashInfo.seconds < seconds + 30
2018-11-04 11:41:21 -08:00
) {
2019-03-04 08:06:33 -08:00
transaction.recentBlockhash = this._blockhashInfo.recentBlockhash;
transaction.sign(...signers);
2018-10-22 20:03:44 -07:00
if (!transaction.signature) {
throw new Error('!signature'); // should never happen
}
// If the signature of this transaction has not been seen before with the
2019-03-04 08:06:33 -08:00
// current recentBlockhash, all done.
2018-10-22 20:03:44 -07:00
const signature = transaction.signature.toString();
2019-03-04 08:06:33 -08:00
if (!this._blockhashInfo.transactionSignatures.includes(signature)) {
this._blockhashInfo.transactionSignatures.push(signature);
if (this._disableBlockhashCaching) {
this._blockhashInfo.seconds = -1;
2018-10-22 20:03:44 -07:00
}
break;
}
}
2018-10-22 20:03:44 -07:00
// Fetch a new blockhash
2018-10-22 20:03:44 -07:00
let attempts = 0;
const startTime = Date.now();
2018-10-22 20:03:44 -07:00
for (;;) {
2019-06-12 14:36:05 -07:00
const [
recentBlockhash,
//feeCalculator,
] = await this.getRecentBlockhash();
2018-10-22 20:03:44 -07:00
2019-03-04 08:06:33 -08:00
if (this._blockhashInfo.recentBlockhash != recentBlockhash) {
this._blockhashInfo = {
recentBlockhash,
2018-11-04 11:41:21 -08:00
seconds: new Date().getSeconds(),
2018-10-22 20:03:44 -07:00
transactionSignatures: [],
};
break;
}
2019-04-23 18:03:31 -07:00
if (attempts === 50) {
2018-11-04 11:41:21 -08:00
throw new Error(
`Unable to obtain a new blockhash after ${Date.now() -
startTime}ms`,
2018-11-04 11:41:21 -08:00
);
2018-10-22 20:03:44 -07:00
}
// Sleep for approximately half a slot
await sleep((500 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
2018-10-22 20:03:44 -07:00
++attempts;
}
}
const wireTransaction = transaction.serialize();
2018-11-27 21:06:03 -08:00
return await this.sendRawTransaction(wireTransaction);
}
2019-04-24 15:22:50 -07:00
/**
* @private
*/
async validatorExit(): Promise<boolean> {
const unsafeRes = await this._rpcRequest('validatorExit', []);
2019-04-24 15:22:50 -07:00
const res = jsonRpcResult('boolean')(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
return res.result;
}
2018-11-27 21:06:03 -08:00
/**
* Send a transaction that has already been signed and serialized into the
* wire format
*/
async sendRawTransaction(
rawTransaction: Buffer,
2018-11-27 21:06:03 -08:00
): Promise<TransactionSignature> {
2018-11-04 11:41:21 -08:00
const unsafeRes = await this._rpcRequest('sendTransaction', [
2019-04-10 12:33:42 -07:00
[...rawTransaction],
2018-11-04 11:41:21 -08:00
]);
const res = SendTransactionRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
assert(res.result);
return res.result;
2018-08-23 10:52:48 -07:00
}
2018-10-26 21:37:39 -07:00
/**
* @private
*/
_wsOnOpen() {
this._rpcWebSocketConnected = true;
this._updateSubscriptions();
}
/**
* @private
*/
_wsOnError(err: Error) {
console.log('ws error:', err.message);
}
/**
* @private
*/
_wsOnClose(code: number, message: string) {
// 1000 means _rpcWebSocket.close() was called explicitly
if (code !== 1000) {
console.log('ws close:', code, message);
}
this._rpcWebSocketConnected = false;
}
/**
* @private
*/
async _updateSubscriptions() {
2019-03-08 16:02:39 -08:00
const accountKeys = Object.keys(this._accountChangeSubscriptions).map(
Number,
);
const programKeys = Object.keys(
this._programAccountChangeSubscriptions,
).map(Number);
if (accountKeys.length === 0 && programKeys.length === 0) {
2018-10-26 21:37:39 -07:00
this._rpcWebSocket.close();
return;
}
if (!this._rpcWebSocketConnected) {
2019-03-08 16:02:39 -08:00
for (let id of accountKeys) {
2018-10-26 21:37:39 -07:00
this._accountChangeSubscriptions[id].subscriptionId = null;
}
2019-03-08 16:02:39 -08:00
for (let id of programKeys) {
this._programAccountChangeSubscriptions[id].subscriptionId = null;
}
2018-10-26 21:37:39 -07:00
this._rpcWebSocket.connect();
return;
}
2019-03-08 16:02:39 -08:00
for (let id of accountKeys) {
2018-10-26 21:37:39 -07:00
const {subscriptionId, publicKey} = this._accountChangeSubscriptions[id];
if (subscriptionId === null) {
try {
2018-11-04 11:41:21 -08:00
this._accountChangeSubscriptions[
id
].subscriptionId = await this._rpcWebSocket.call('accountSubscribe', [
publicKey,
]);
2018-10-26 21:37:39 -07:00
} catch (err) {
2018-11-04 11:41:21 -08:00
console.log(
`accountSubscribe error for ${publicKey}: ${err.message}`,
);
2018-10-26 21:37:39 -07:00
}
}
}
2019-03-08 16:02:39 -08:00
for (let id of programKeys) {
const {
subscriptionId,
programId,
} = this._programAccountChangeSubscriptions[id];
if (subscriptionId === null) {
try {
this._programAccountChangeSubscriptions[
id
].subscriptionId = await this._rpcWebSocket.call('programSubscribe', [
programId,
]);
} catch (err) {
console.log(
`programSubscribe error for ${programId}: ${err.message}`,
);
}
}
}
}
/**
* @private
*/
_wsOnAccountNotification(notification: Object) {
const res = AccountNotificationResult(notification);
if (res.error) {
throw new Error(res.error.message);
}
const keys = Object.keys(this._accountChangeSubscriptions).map(Number);
for (let id of keys) {
const sub = this._accountChangeSubscriptions[id];
if (sub.subscriptionId === res.subscription) {
const {result} = res;
assert(typeof result !== 'undefined');
sub.callback({
executable: result.executable,
owner: new PublicKey(result.owner),
lamports: result.lamports,
2019-03-14 13:27:47 -07:00
data: Buffer.from(result.data),
2019-03-08 16:02:39 -08:00
});
return true;
}
}
2018-10-26 21:37:39 -07:00
}
/**
* Register a callback to be invoked whenever the specified account changes
*
* @param publickey Public key of the account to monitor
* @param callback Function to invoke whenever the account is changed
* @return subscription id
*/
2018-11-04 11:41:21 -08:00
onAccountChange(
publicKey: PublicKey,
callback: AccountChangeCallback,
): number {
2018-10-26 21:37:39 -07:00
const id = ++this._accountChangeSubscriptionCounter;
this._accountChangeSubscriptions[id] = {
publicKey: publicKey.toBase58(),
callback,
2018-11-04 11:41:21 -08:00
subscriptionId: null,
2018-10-26 21:37:39 -07:00
};
this._updateSubscriptions();
return id;
}
/**
* Deregister an account notification callback
*
* @param id subscription id to deregister
*/
async removeAccountChangeListener(id: number): Promise<void> {
if (this._accountChangeSubscriptions[id]) {
const {subscriptionId} = this._accountChangeSubscriptions[id];
delete this._accountChangeSubscriptions[id];
if (subscriptionId !== null) {
try {
await this._rpcWebSocket.call('accountUnsubscribe', [subscriptionId]);
} catch (err) {
console.log('accountUnsubscribe error:', err.message);
}
}
this._updateSubscriptions();
} else {
throw new Error(`Unknown account change id: ${id}`);
}
}
2019-03-08 16:02:39 -08:00
/**
* @private
*/
_wsOnProgramAccountNotification(notification: Object) {
const res = ProgramAccountNotificationResult(notification);
if (res.error) {
throw new Error(res.error.message);
}
const keys = Object.keys(this._programAccountChangeSubscriptions).map(
Number,
);
for (let id of keys) {
const sub = this._programAccountChangeSubscriptions[id];
if (sub.subscriptionId === res.subscription) {
const {result} = res;
assert(typeof result !== 'undefined');
sub.callback({
accountId: result[0],
accountInfo: {
executable: result[1].executable,
owner: new PublicKey(result[1].owner),
lamports: result[1].lamports,
2019-03-14 13:27:47 -07:00
data: Buffer.from(result[1].data),
2019-03-08 16:02:39 -08:00
},
});
return true;
}
}
}
/**
* Register a callback to be invoked whenever accounts owned by the
* specified program change
*
* @param programId Public key of the program to monitor
* @param callback Function to invoke whenever the account is changed
* @return subscription id
*/
onProgramAccountChange(
programId: PublicKey,
callback: ProgramAccountChangeCallback,
): number {
const id = ++this._programAccountChangeSubscriptionCounter;
this._programAccountChangeSubscriptions[id] = {
programId: programId.toBase58(),
callback,
subscriptionId: null,
};
this._updateSubscriptions();
return id;
}
/**
* Deregister an account notification callback
*
* @param id subscription id to deregister
*/
async removeProgramAccountChangeListener(id: number): Promise<void> {
if (this._programAccountChangeSubscriptions[id]) {
const {subscriptionId} = this._programAccountChangeSubscriptions[id];
delete this._programAccountChangeSubscriptions[id];
if (subscriptionId !== null) {
try {
await this._rpcWebSocket.call('programUnsubscribe', [subscriptionId]);
} catch (err) {
console.log('programUnsubscribe error:', err.message);
}
}
this._updateSubscriptions();
} else {
throw new Error(`Unknown account change id: ${id}`);
}
}
2018-08-23 10:52:48 -07:00
}