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
|
|
|
|
2018-09-14 08:27:40 -07:00
|
|
|
import {Transaction} from './transaction';
|
2018-09-30 18:42:45 -07:00
|
|
|
import {PublicKey} from './publickey';
|
2018-10-22 15:31:56 -07:00
|
|
|
import {sleep} from './util/sleep';
|
2018-09-30 18:42:45 -07:00
|
|
|
import type {Account} from './account';
|
2018-09-13 18:48:51 -07:00
|
|
|
import type {TransactionSignature, TransactionId} 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
|
|
|
loader: 'array',
|
|
|
|
owner: 'array',
|
2018-09-26 19:54:59 -07:00
|
|
|
tokens: 'number',
|
|
|
|
userdata: '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
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getLastId" message
|
|
|
|
*/
|
2018-09-26 19:54:59 -07:00
|
|
|
const GetLastId = jsonRpcResult('string');
|
2018-08-23 10:52:48 -07:00
|
|
|
|
2018-08-24 09:05:23 -07:00
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "getFinality" message
|
|
|
|
*/
|
2018-09-26 19:54:59 -07:00
|
|
|
const GetFinalityRpcResult = jsonRpcResult('number');
|
2018-08-24 09:05:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "requestAirdrop" message
|
|
|
|
*/
|
2018-09-26 19:54:59 -07:00
|
|
|
const RequestAirdropRpcResult = jsonRpcResult('string');
|
2018-08-24 09:05:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expected JSON RPC response for the "sendTransaction" message
|
|
|
|
*/
|
2018-09-26 19:54:59 -07:00
|
|
|
const SendTokensRpcResult = 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
|
|
|
|
* @property {number} tokens Number of tokens assigned to the account
|
2018-11-14 10:06:13 -08:00
|
|
|
* @property {PublicKey} owner Identifier of the program that owns the account
|
2018-09-20 15:35:41 -07:00
|
|
|
* @property {?Buffer} userdata Optional userdata assigned to the account
|
2018-11-14 10:06:13 -08:00
|
|
|
* @property {PublicKey} loader Identifier of the loader for this account
|
|
|
|
* @property {boolean} executable `true` if this account's userdata 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
|
|
|
loader: PublicKey,
|
|
|
|
owner: PublicKey,
|
2018-10-17 09:35:24 -07:00
|
|
|
tokens: number,
|
2018-09-28 22:51:52 -07:00
|
|
|
userdata: Buffer,
|
2018-11-04 11:41:21 -08:00
|
|
|
};
|
2018-09-20 15:08:52 -07:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
_lastIdInfo: {
|
|
|
|
lastId: TransactionId | null,
|
|
|
|
seconds: number,
|
|
|
|
transactionSignatures: Array<string>,
|
|
|
|
};
|
2018-11-04 11:41:21 -08:00
|
|
|
_disableLastIdCaching: boolean = false;
|
2018-10-26 21:37:39 -07:00
|
|
|
_accountChangeSubscriptions: {[number]: AccountSubscriptionInfo} = {};
|
|
|
|
_accountChangeSubscriptionCounter: 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);
|
2018-10-22 20:03:44 -07:00
|
|
|
this._lastIdInfo = {
|
|
|
|
lastId: null,
|
|
|
|
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),
|
|
|
|
);
|
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
|
|
|
loader: new PublicKey(result.loader),
|
|
|
|
owner: new PublicKey(result.owner),
|
2018-09-20 15:08:52 -07:00
|
|
|
tokens: result.tokens,
|
2018-09-28 22:51:52 -07:00
|
|
|
userdata: Buffer.from(result.userdata),
|
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
|
|
|
/**
|
|
|
|
* Fetch the current transaction count of the network
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Fetch the current transaction count of the network
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Fetch the identifier to the latest transaction on the network
|
|
|
|
*/
|
2018-08-23 10:52:48 -07:00
|
|
|
async getLastId(): Promise<TransactionId> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getLastId', []);
|
|
|
|
const res = GetLastId(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
|
|
|
/**
|
|
|
|
* Return the current network finality time in millliseconds
|
|
|
|
*/
|
2018-08-23 10:52:48 -07:00
|
|
|
async getFinality(): Promise<number> {
|
|
|
|
const unsafeRes = await this._rpcRequest('getFinality', []);
|
|
|
|
const res = GetFinalityRpcResult(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
|
|
|
/**
|
|
|
|
* Request an allocation of tokens to the specified account
|
|
|
|
*/
|
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 (;;) {
|
2018-10-22 20:03:44 -07:00
|
|
|
// Attempt to use the previous last id for up to 1 second
|
2018-11-04 11:41:21 -08:00
|
|
|
const seconds = new Date().getSeconds();
|
|
|
|
if (
|
|
|
|
this._lastIdInfo.lastId != null &&
|
|
|
|
this._lastIdInfo.seconds === seconds
|
|
|
|
) {
|
2018-10-22 20:03:44 -07:00
|
|
|
transaction.lastId = this._lastIdInfo.lastId;
|
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
|
|
|
|
// current lastId, all done.
|
|
|
|
const signature = transaction.signature.toString();
|
|
|
|
if (!this._lastIdInfo.transactionSignatures.includes(signature)) {
|
|
|
|
this._lastIdInfo.transactionSignatures.push(signature);
|
|
|
|
if (this._disableLastIdCaching) {
|
|
|
|
this._lastIdInfo.seconds = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-10-22 15:31:56 -07:00
|
|
|
}
|
2018-10-22 20:03:44 -07:00
|
|
|
|
|
|
|
// Fetch a new last id
|
|
|
|
let attempts = 0;
|
2018-11-01 20:34:48 -07:00
|
|
|
const startTime = Date.now();
|
2018-10-22 20:03:44 -07:00
|
|
|
for (;;) {
|
|
|
|
const lastId = await this.getLastId();
|
|
|
|
|
|
|
|
if (this._lastIdInfo.lastId != lastId) {
|
|
|
|
this._lastIdInfo = {
|
|
|
|
lastId,
|
2018-11-04 11:41:21 -08:00
|
|
|
seconds: new Date().getSeconds(),
|
2018-10-22 20:03:44 -07:00
|
|
|
transactionSignatures: [],
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (attempts === 8) {
|
2018-11-04 11:41:21 -08:00
|
|
|
throw new Error(
|
|
|
|
`Unable to obtain a new last id after ${Date.now() - startTime}ms`,
|
|
|
|
);
|
2018-10-22 20:03:44 -07:00
|
|
|
}
|
|
|
|
await sleep(250);
|
|
|
|
++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(
|
|
|
|
wireTransaction: Buffer,
|
|
|
|
): Promise<TransactionSignature> {
|
2018-11-04 11:41:21 -08:00
|
|
|
const unsafeRes = await this._rpcRequest('sendTransaction', [
|
|
|
|
[...wireTransaction],
|
|
|
|
]);
|
2018-08-23 20:10:30 -07:00
|
|
|
const res = SendTokensRpcResult(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
|
|
|
|
*/
|
|
|
|
_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,
|
2018-11-14 10:06:13 -08:00
|
|
|
loader: new PublicKey(result.loader),
|
|
|
|
owner: new PublicKey(result.owner),
|
2018-10-26 21:37:39 -07:00
|
|
|
tokens: result.tokens,
|
|
|
|
userdata: Buffer.from(result.userdata),
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async _updateSubscriptions() {
|
|
|
|
const keys = Object.keys(this._accountChangeSubscriptions).map(Number);
|
|
|
|
if (keys.length === 0) {
|
|
|
|
this._rpcWebSocket.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._rpcWebSocketConnected) {
|
|
|
|
for (let id of keys) {
|
|
|
|
this._accountChangeSubscriptions[id].subscriptionId = null;
|
|
|
|
}
|
|
|
|
this._rpcWebSocket.connect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let id of keys) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}`);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 10:52:48 -07:00
|
|
|
}
|