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
|
|
|
|
2019-03-05 09:53:56 -08:00
|
|
|
import {DEFAULT_TICKS_PER_SLOT, NUM_TICKS_PER_SECOND} from './timing';
|
2018-09-30 18:42:45 -07:00
|
|
|
import {PublicKey} from './publickey';
|
2019-03-05 09:53:56 -08:00
|
|
|
import {Transaction} from './transaction';
|
2018-10-22 15:31:56 -07:00
|
|
|
import {sleep} from './util/sleep';
|
2019-03-04 08:06:33 -08:00
|
|
|
import type {Blockhash} from './blockhash';
|
2018-09-30 18:42:45 -07:00
|
|
|
import type {Account} from './account';
|
2019-03-04 08:06:33 -08:00
|
|
|
import type {TransactionSignature} from './transaction';
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-08-23 20:10:30 -07:00
|
|
|
type RpcRequest = (methodName: string, args: Array<any>) => any;
|
2018-08-23 10:52:48 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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({
|
2018-10-17 09:35:24 -07:00
|
|
|
executable: 'boolean',
|
2018-11-14 10:06:13 -08:00
|
|
|
owner: 'array',
|
2019-03-05 17:52:13 -08:00
|
|
|
lamports: 'number',
|
2019-03-14 13:27:47 -07:00
|
|
|
data: 'array',
|
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,
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
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.enum([
|
|
|
|
'AccountInUse',
|
|
|
|
'Confirmed',
|
|
|
|
'GenericFailure',
|
|
|
|
'ProgramRuntimeError',
|
|
|
|
'SignatureNotFound',
|
|
|
|
]),
|
|
|
|
);
|
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
|
|
|
|
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-03-04 08:06:33 -08:00
|
|
|
const GetRecentBlockhash = jsonRpcResult('string');
|
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
|
|
|
|
*/
|
2018-11-28 11:56:50 -08:00
|
|
|
const SendTransactionRpcResult = jsonRpcResult('string');
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-09-20 15:08:52 -07:00
|
|
|
/**
|
|
|
|
* Information describing an account
|
2018-09-20 15:35:41 -07:00
|
|
|
*
|
|
|
|
* @typedef {Object} AccountInfo
|
2019-03-05 17:52:13 -08:00
|
|
|
* @property {number} lamports Number of lamports assigned to the account
|
2018-11-14 10:06:13 -08:00
|
|
|
* @property {PublicKey} owner Identifier of the program that owns the account
|
2019-03-14 13:27:47 -07:00
|
|
|
* @property {?Buffer} data Optional data assigned to the account
|
|
|
|
* @property {boolean} executable `true` if this account's data contains a loaded program
|
2018-09-20 15:08:52 -07:00
|
|
|
*/
|
|
|
|
type AccountInfo = {
|
2018-11-04 11:41:21 -08:00
|
|
|
executable: boolean,
|
2018-11-14 10:06:13 -08:00
|
|
|
owner: PublicKey,
|
2019-03-05 17:52:13 -08:00
|
|
|
lamports: number,
|
2019-03-14 13:27:47 -07:00
|
|
|
data: Buffer,
|
2018-11-04 11:41:21 -08:00
|
|
|
};
|
2018-09-20 15:08:52 -07:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/**
|
|
|
|
* Account information identified by pubkey
|
|
|
|
*
|
|
|
|
* @typedef {Object} KeyedAccountInfo
|
|
|
|
* @property {PublicKey} accountId
|
|
|
|
* @property {AccountInfo} accountInfo
|
|
|
|
*/
|
|
|
|
type KeyedAccountInfo = {
|
|
|
|
accountId: PublicKey,
|
|
|
|
accountInfo: AccountInfo,
|
|
|
|
};
|
|
|
|
|
2018-10-26 21:37:39 -07:00
|
|
|
/**
|
|
|
|
* Callback function for account change notifications
|
|
|
|
*/
|
|
|
|
export type AccountChangeCallback = (accountInfo: AccountInfo) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type AccountSubscriptionInfo = {
|
2018-11-04 11:41:21 -08:00
|
|
|
publicKey: string, // PublicKey of the account as a base 58 string
|
2018-10-26 21:37:39 -07:00
|
|
|
callback: AccountChangeCallback,
|
2018-11-04 11:41:21 -08:00
|
|
|
subscriptionId: null | number, // null when there's no current server subscription id
|
|
|
|
};
|
2018-10-26 21:37:39 -07:00
|
|
|
|
2019-03-08 16:02:39 -08:00
|
|
|
/**
|
|
|
|
* Callback function for program account change notifications
|
|
|
|
*/
|
|
|
|
export type ProgramAccountChangeCallback = (
|
|
|
|
keyedAccountInfo: KeyedAccountInfo,
|
|
|
|
) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type ProgramAccountSubscriptionInfo = {
|
|
|
|
programId: string, // PublicKey of the program as a base 58 string
|
|
|
|
callback: ProgramAccountChangeCallback,
|
|
|
|
subscriptionId: null | number, // null when there's no current server subscription id
|
|
|
|
};
|
|
|
|
|
2018-09-26 19:16:17 -07:00
|
|
|
/**
|
|
|
|
* Possible signature status values
|
|
|
|
*
|
|
|
|
* @typedef {string} SignatureStatus
|
|
|
|
*/
|
2018-11-04 11:41:21 -08:00
|
|
|
export type SignatureStatus =
|
|
|
|
| 'Confirmed'
|
2018-10-23 13:10:08 -07:00
|
|
|
| 'AccountInUse'
|
|
|
|
| 'SignatureNotFound'
|
|
|
|
| 'ProgramRuntimeError'
|
|
|
|
| 'GenericFailure';
|
2018-09-26 19:16:17 -07:00
|
|
|
|
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
|
|
|
|
2018-11-02 16:09:34 -07:00
|
|
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
2018-10-26 21:37:39 -07:00
|
|
|
url.host = '';
|
|
|
|
url.port = String(Number(url.port) + 1);
|
2018-11-01 20:42:14 -07:00
|
|
|
if (url.port === '1') {
|
2018-11-02 16:09:34 -07:00
|
|
|
url.port = url.protocol === 'wss:' ? '8901' : '8900';
|
2018-11-01 20:42:14 -07:00
|
|
|
}
|
2018-11-04 11:41:21 -08:00
|
|
|
this._rpcWebSocket = new RpcWebSocketClient(urlFormat(url), {
|
|
|
|
autoconnect: false,
|
|
|
|
max_reconnects: Infinity,
|
|
|
|
});
|
2018-10-26 21:37:39 -07:00
|
|
|
this._rpcWebSocket.on('open', this._wsOnOpen.bind(this));
|
|
|
|
this._rpcWebSocket.on('error', this._wsOnError.bind(this));
|
|
|
|
this._rpcWebSocket.on('close', this._wsOnClose.bind(this));
|
2018-11-04 11:41:21 -08:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'accountNotification',
|
|
|
|
this._wsOnAccountNotification.bind(this),
|
|
|
|
);
|
2019-03-08 16:02:39 -08:00
|
|
|
this._rpcWebSocket.on(
|
|
|
|
'programNotification',
|
|
|
|
this._wsOnProgramAccountNotification.bind(this),
|
|
|
|
);
|
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 {
|
2018-10-17 09:35:24 -07:00
|
|
|
executable: result.executable,
|
2018-11-14 10:06:13 -08:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
): Promise<SignatureStatus> {
|
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);
|
|
|
|
}
|
|
|
|
|
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-03-04 08:06:33 -08:00
|
|
|
async getRecentBlockhash(): Promise<Blockhash> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getRecentBlockhash', []);
|
|
|
|
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');
|
|
|
|
return res.result;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-11-18 08:48:14 -08:00
|
|
|
...signers: Array<Account>
|
2018-11-04 11:41:21 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2018-10-22 15:31:56 -07:00
|
|
|
for (;;) {
|
2019-03-05 09:53:56 -08:00
|
|
|
// 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 &&
|
2019-03-05 09:53:56 -08:00
|
|
|
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;
|
2018-11-18 08:48:14 -08:00
|
|
|
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 15:31:56 -07:00
|
|
|
}
|
2018-10-22 20:03:44 -07:00
|
|
|
|
2019-03-05 09:53:56 -08:00
|
|
|
// Fetch a new blockhash
|
2018-10-22 20:03:44 -07:00
|
|
|
let attempts = 0;
|
2018-11-01 20:34:48 -07:00
|
|
|
const startTime = Date.now();
|
2018-10-22 20:03:44 -07:00
|
|
|
for (;;) {
|
2019-03-04 08:06:33 -08:00
|
|
|
const recentBlockhash = 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-02-14 22:04:45 -08:00
|
|
|
if (attempts === 16) {
|
2018-11-04 11:41:21 -08:00
|
|
|
throw new Error(
|
2019-03-05 09:53:56 -08:00
|
|
|
`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
|
|
|
}
|
2019-03-05 09:53:56 -08: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;
|
2018-10-22 15:31:56 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 17:14:58 -07:00
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
const wireTransaction = transaction.serialize();
|
2018-11-27 21:06:03 -08:00
|
|
|
return await this.sendRawTransaction(wireTransaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a transaction that has already been signed and serialized into the
|
|
|
|
* wire format
|
|
|
|
*/
|
|
|
|
async sendRawTransaction(
|
2018-11-28 11:56:50 -08:00
|
|
|
rawTransaction: Buffer,
|
2018-11-27 21:06:03 -08:00
|
|
|
): Promise<TransactionSignature> {
|
2018-11-04 11:41:21 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('sendTransaction', [
|
2019-04-10 12:33:42 -07:00
|
|
|
[...rawTransaction],
|
2018-11-04 11:41:21 -08:00
|
|
|
]);
|
2018-11-28 11:56:50 -08:00
|
|
|
const res = SendTransactionRpcResult(unsafeRes);
|
2018-08-23 20:10:30 -07:00
|
|
|
if (res.error) {
|
|
|
|
throw new Error(res.error.message);
|
|
|
|
}
|
|
|
|
assert(typeof res.result !== 'undefined');
|
|
|
|
assert(res.result);
|
|
|
|
return res.result;
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|
2018-10-26 21:37:39 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnOpen() {
|
|
|
|
this._rpcWebSocketConnected = true;
|
|
|
|
this._updateSubscriptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnError(err: Error) {
|
|
|
|
console.log('ws error:', err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_wsOnClose(code: number, message: string) {
|
|
|
|
// 1000 means _rpcWebSocket.close() was called explicitly
|
|
|
|
if (code !== 1000) {
|
|
|
|
console.log('ws close:', code, message);
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
console.log('program-id: ' + programId);
|
|
|
|
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
|
|
|
}
|