chore: update Connection to non-deprecated endpoints (#22800)

* chore: remove usage of `getConfirmedTransaction`

* chore: use `getBlock` instead of `getConfirmedBlock`

* chore: add `getLatestBlockhash` and test
This commit is contained in:
Jon Cinque
2022-01-28 00:43:01 +01:00
committed by GitHub
parent 350207b05f
commit fa51e5b704
3 changed files with 310 additions and 52 deletions

View File

@@ -504,8 +504,15 @@ export type TokenBalance = {
/**
* Metadata for a parsed confirmed transaction on the ledger
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link ParsedTransactionMeta} instead.
*/
export type ParsedConfirmedTransactionMeta = {
export type ParsedConfirmedTransactionMeta = ParsedTransactionMeta;
/**
* Metadata for a parsed transaction on the ledger
*/
export type ParsedTransactionMeta = {
/** The fee charged for processing the transaction */
fee: number;
/** An array of cross program invoked parsed instructions */
@@ -644,14 +651,21 @@ export type ParsedTransaction = {
/**
* A parsed and confirmed transaction on the ledger
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link ParsedTransactionWithMeta} instead.
*/
export type ParsedConfirmedTransaction = {
export type ParsedConfirmedTransaction = ParsedTransactionWithMeta;
/**
* A parsed transaction on the ledger with meta
*/
export type ParsedTransactionWithMeta = {
/** The slot during which the transaction was processed */
slot: number;
/** The details of the transaction */
transaction: ParsedTransaction;
/** Metadata produced from the transaction */
meta: ParsedConfirmedTransactionMeta | null;
meta: ParsedTransactionMeta | null;
/** The unix timestamp of when the transaction was processed */
blockTime?: number | null;
};
@@ -720,9 +734,9 @@ export type ConfirmedBlock = {
};
/**
* A ConfirmedBlock on the ledger with signatures only
* A Block on the ledger with signatures only
*/
export type ConfirmedBlockSignatures = {
export type BlockSignatures = {
/** Blockhash of this block */
blockhash: Blockhash;
/** Blockhash of this block's parent */
@@ -1494,8 +1508,41 @@ const ParsedConfirmedTransactionMetaResult = pick({
postTokenBalances: optional(nullable(array(TokenBalanceResult))),
});
/**
* Expected JSON RPC response for the "getBlock" message
*/
const GetBlockRpcResult = jsonRpcResult(
nullable(
pick({
blockhash: string(),
previousBlockhash: string(),
parentSlot: number(),
transactions: array(
pick({
transaction: ConfirmedTransactionResult,
meta: nullable(ConfirmedTransactionMetaResult),
}),
),
rewards: optional(
array(
pick({
pubkey: string(),
lamports: number(),
postBalance: nullable(number()),
rewardType: nullable(string()),
}),
),
),
blockTime: nullable(number()),
blockHeight: nullable(number()),
}),
),
);
/**
* Expected JSON RPC response for the "getConfirmedBlock" message
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link GetBlockRpcResult} instead.
*/
const GetConfirmedBlockRpcResult = jsonRpcResult(
nullable(
@@ -1525,9 +1572,9 @@ const GetConfirmedBlockRpcResult = jsonRpcResult(
);
/**
* Expected JSON RPC response for the "getConfirmedBlockSignatures" message
* Expected JSON RPC response for the "getBlock" message
*/
const GetConfirmedBlockSignaturesRpcResult = jsonRpcResult(
const GetBlockSignaturesRpcResult = jsonRpcResult(
nullable(
pick({
blockhash: string(),
@@ -1540,9 +1587,9 @@ const GetConfirmedBlockSignaturesRpcResult = jsonRpcResult(
);
/**
* Expected JSON RPC response for the "getConfirmedTransaction" message
* Expected JSON RPC response for the "getTransaction" message
*/
const GetConfirmedTransactionRpcResult = jsonRpcResult(
const GetTransactionRpcResult = jsonRpcResult(
nullable(
pick({
slot: number(),
@@ -1554,9 +1601,9 @@ const GetConfirmedTransactionRpcResult = jsonRpcResult(
);
/**
* Expected JSON RPC response for the "getConfirmedTransaction" message
* Expected parsed JSON RPC response for the "getTransaction" message
*/
const GetParsedConfirmedTransactionRpcResult = jsonRpcResult(
const GetParsedTransactionRpcResult = jsonRpcResult(
nullable(
pick({
slot: number(),
@@ -1569,6 +1616,8 @@ const GetParsedConfirmedTransactionRpcResult = jsonRpcResult(
/**
* Expected JSON RPC response for the "getRecentBlockhash" message
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link GetLatestBlockhashRpcResult} instead.
*/
const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
pick({
@@ -1579,6 +1628,16 @@ const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext(
}),
);
/**
* Expected JSON RPC response for the "getLatestBlockhash" message
*/
const GetLatestBlockhashRpcResult = jsonRpcResultAndContext(
pick({
blockhash: string(),
lastValidBlockHeight: number(),
}),
);
const PerfSampleResult = pick({
slot: number(),
numTransactions: number(),
@@ -2962,6 +3021,8 @@ export class Connection {
/**
* Fetch a recent blockhash from the cluster, return with context
* @return {Promise<RpcResponseAndContext<{blockhash: Blockhash, feeCalculator: FeeCalculator}>>}
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getLatestBlockhash} instead.
*/
async getRecentBlockhashAndContext(
commitment?: Commitment,
@@ -3001,6 +3062,8 @@ export class Connection {
/**
* Fetch the fee calculator for a recent blockhash from the cluster, return with context
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getFeeForMessage} instead.
*/
async getFeeCalculatorForBlockhash(
blockhash: Blockhash,
@@ -3047,6 +3110,8 @@ export class Connection {
/**
* Fetch a recent blockhash from the cluster
* @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>}
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getLatestBlockhash} instead.
*/
async getRecentBlockhash(
commitment?: Commitment,
@@ -3059,6 +3124,39 @@ export class Connection {
}
}
/**
* Fetch the latest blockhash from the cluster
* @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
*/
async getLatestBlockhash(
commitment?: Commitment,
): Promise<{blockhash: Blockhash; lastValidBlockHeight: number}> {
try {
const res = await this.getLatestBlockhashAndContext(commitment);
return res.value;
} catch (e) {
throw new Error('failed to get recent blockhash: ' + e);
}
}
/**
* Fetch the latest blockhash from the cluster
* @return {Promise<{blockhash: Blockhash, lastValidBlockHeight: number}>}
*/
async getLatestBlockhashAndContext(
commitment?: Commitment,
): Promise<
RpcResponseAndContext<{blockhash: Blockhash; lastValidBlockHeight: number}>
> {
const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getLatestBlockhash', args);
const res = create(unsafeRes, GetLatestBlockhashRpcResult);
if ('error' in res) {
throw new Error('failed to get latest blockhash: ' + res.error.message);
}
return res.result;
}
/**
* Fetch the node version
*/
@@ -3094,8 +3192,8 @@ export class Connection {
[slot],
opts && opts.commitment,
);
const unsafeRes = await this._rpcRequest('getConfirmedBlock', args);
const res = create(unsafeRes, GetConfirmedBlockRpcResult);
const unsafeRes = await this._rpcRequest('getBlock', args);
const res = create(unsafeRes, GetBlockRpcResult);
if ('error' in res) {
throw new Error('failed to get confirmed block: ' + res.error.message);
@@ -3120,7 +3218,7 @@ export class Connection {
}
/**
* Fetch a processed transaction from the cluster.
* Fetch a confirmed or finalized transaction from the cluster.
*/
async getTransaction(
signature: string,
@@ -3130,12 +3228,10 @@ export class Connection {
[signature],
opts && opts.commitment,
);
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
const res = create(unsafeRes, GetConfirmedTransactionRpcResult);
const unsafeRes = await this._rpcRequest('getTransaction', args);
const res = create(unsafeRes, GetTransactionRpcResult);
if ('error' in res) {
throw new Error(
'failed to get confirmed transaction: ' + res.error.message,
);
throw new Error('failed to get transaction: ' + res.error.message);
}
const result = res.result;
@@ -3150,6 +3246,57 @@ export class Connection {
};
}
/**
* Fetch parsed transaction details for a confirmed or finalized transaction
*/
async getParsedTransaction(
signature: TransactionSignature,
commitment?: Finality,
): Promise<ParsedConfirmedTransaction | null> {
const args = this._buildArgsAtLeastConfirmed(
[signature],
commitment,
'jsonParsed',
);
const unsafeRes = await this._rpcRequest('getTransaction', args);
const res = create(unsafeRes, GetParsedTransactionRpcResult);
if ('error' in res) {
throw new Error('failed to get transaction: ' + res.error.message);
}
return res.result;
}
/**
* Fetch parsed transaction details for a batch of confirmed transactions
*/
async getParsedTransactions(
signatures: TransactionSignature[],
commitment?: Finality,
): Promise<(ParsedConfirmedTransaction | null)[]> {
const batch = signatures.map(signature => {
const args = this._buildArgsAtLeastConfirmed(
[signature],
commitment,
'jsonParsed',
);
return {
methodName: 'getTransaction',
args,
};
});
const unsafeRes = await this._rpcBatchRequest(batch);
const res = unsafeRes.map((unsafeRes: any) => {
const res = create(unsafeRes, GetParsedTransactionRpcResult);
if ('error' in res) {
throw new Error('failed to get transactions: ' + res.error.message);
}
return res.result;
});
return res;
}
/**
* Fetch a list of Transactions and transaction statuses from the cluster
* for a confirmed block.
@@ -3160,14 +3307,36 @@ export class Connection {
slot: number,
commitment?: Finality,
): Promise<ConfirmedBlock> {
const result = await this.getBlock(slot, {commitment});
const args = this._buildArgsAtLeastConfirmed([slot], commitment);
const unsafeRes = await this._rpcRequest('getConfirmedBlock', args);
const res = create(unsafeRes, GetConfirmedBlockRpcResult);
if ('error' in res) {
throw new Error('failed to get confirmed block: ' + res.error.message);
}
const result = res.result;
if (!result) {
throw new Error('Confirmed block ' + slot + ' not found');
}
return {
const block = {
...result,
transactions: result.transactions.map(({transaction, meta}) => {
const message = new Message(transaction.message);
return {
meta,
transaction: {
...transaction,
message,
},
};
}),
};
return {
...block,
transactions: block.transactions.map(({transaction, meta}) => {
return {
meta,
transaction: Transaction.populate(
@@ -3191,7 +3360,7 @@ export class Connection {
endSlot !== undefined ? [startSlot, endSlot] : [startSlot],
commitment,
);
const unsafeRes = await this._rpcRequest('getConfirmedBlocks', args);
const unsafeRes = await this._rpcRequest('getBlocks', args);
const res = create(unsafeRes, jsonRpcResult(array(number())));
if ('error' in res) {
throw new Error('failed to get blocks: ' + res.error.message);
@@ -3199,13 +3368,43 @@ export class Connection {
return res.result;
}
/**
* Fetch a list of Signatures from the cluster for a block, excluding rewards
*/
async getBlockSignatures(
slot: number,
commitment?: Finality,
): Promise<BlockSignatures> {
const args = this._buildArgsAtLeastConfirmed(
[slot],
commitment,
undefined,
{
transactionDetails: 'signatures',
rewards: false,
},
);
const unsafeRes = await this._rpcRequest('getBlock', args);
const res = create(unsafeRes, GetBlockSignaturesRpcResult);
if ('error' in res) {
throw new Error('failed to get block: ' + res.error.message);
}
const result = res.result;
if (!result) {
throw new Error('Block ' + slot + ' not found');
}
return result;
}
/**
* Fetch a list of Signatures from the cluster for a confirmed block, excluding rewards
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getBlockSignatures} instead.
*/
async getConfirmedBlockSignatures(
slot: number,
commitment?: Finality,
): Promise<ConfirmedBlockSignatures> {
): Promise<BlockSignatures> {
const args = this._buildArgsAtLeastConfirmed(
[slot],
commitment,
@@ -3216,7 +3415,7 @@ export class Connection {
},
);
const unsafeRes = await this._rpcRequest('getConfirmedBlock', args);
const res = create(unsafeRes, GetConfirmedBlockSignaturesRpcResult);
const res = create(unsafeRes, GetBlockSignaturesRpcResult);
if ('error' in res) {
throw new Error('failed to get confirmed block: ' + res.error.message);
}
@@ -3229,14 +3428,25 @@ export class Connection {
/**
* Fetch a transaction details for a confirmed transaction
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getTransaction} instead.
*/
async getConfirmedTransaction(
signature: TransactionSignature,
commitment?: Finality,
): Promise<ConfirmedTransaction | null> {
const result = await this.getTransaction(signature, {commitment});
const args = this._buildArgsAtLeastConfirmed([signature], commitment);
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
const res = create(unsafeRes, GetTransactionRpcResult);
if ('error' in res) {
throw new Error('failed to get transaction: ' + res.error.message);
}
const result = res.result;
if (!result) return result;
const {message, signatures} = result.transaction;
const message = new Message(result.transaction.message);
const signatures = result.transaction.signatures;
return {
...result,
transaction: Transaction.populate(message, signatures),
@@ -3245,6 +3455,8 @@ export class Connection {
/**
* Fetch parsed transaction details for a confirmed transaction
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getParsedTransaction} instead.
*/
async getParsedConfirmedTransaction(
signature: TransactionSignature,
@@ -3256,7 +3468,7 @@ export class Connection {
'jsonParsed',
);
const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
const res = create(unsafeRes, GetParsedConfirmedTransactionRpcResult);
const res = create(unsafeRes, GetParsedTransactionRpcResult);
if ('error' in res) {
throw new Error(
'failed to get confirmed transaction: ' + res.error.message,
@@ -3267,6 +3479,8 @@ export class Connection {
/**
* Fetch parsed transaction details for a batch of confirmed transactions
*
* @deprecated Deprecated since Solana v1.8.0. Please use {@link getParsedTransactions} instead.
*/
async getParsedConfirmedTransactions(
signatures: TransactionSignature[],
@@ -3286,7 +3500,7 @@ export class Connection {
const unsafeRes = await this._rpcBatchRequest(batch);
const res = unsafeRes.map((unsafeRes: any) => {
const res = create(unsafeRes, GetParsedConfirmedTransactionRpcResult);
const res = create(unsafeRes, GetParsedTransactionRpcResult);
if ('error' in res) {
throw new Error(
'failed to get confirmed transactions: ' + res.error.message,