feat: update getSignatureStatus methods
This commit is contained in:
parent
ae10f1ecab
commit
3a2fe7fdbc
|
@ -48,6 +48,10 @@ declare module '@solana/web3.js' {
|
|||
|
||||
export type Commitment = 'max' | 'recent';
|
||||
|
||||
export type SignatureStatusConfig = {
|
||||
searchTransactionHistory: boolean;
|
||||
};
|
||||
|
||||
export type SignatureStatus = {
|
||||
slot: number;
|
||||
err: TransactionError | null;
|
||||
|
@ -160,6 +164,7 @@ declare module '@solana/web3.js' {
|
|||
|
||||
export class Connection {
|
||||
constructor(endpoint: string, commitment?: Commitment);
|
||||
commitment?: Commitment;
|
||||
getAccountInfoAndContext(
|
||||
publicKey: PublicKey,
|
||||
commitment?: Commitment,
|
||||
|
@ -192,11 +197,11 @@ declare module '@solana/web3.js' {
|
|||
getSlotLeader(commitment?: Commitment): Promise<string>;
|
||||
getSignatureStatus(
|
||||
signature: TransactionSignature,
|
||||
commitment?: Commitment,
|
||||
config?: SignatureStatusConfig,
|
||||
): Promise<RpcResponseAndContext<SignatureStatus | null>>;
|
||||
getSignatureStatuses(
|
||||
signatures: Array<TransactionSignature>,
|
||||
commitment?: Commitment,
|
||||
config?: SignatureStatusConfig,
|
||||
): Promise<RpcResponseAndContext<Array<SignatureStatus | null>>>;
|
||||
getTransactionCount(commitment?: Commitment): Promise<number>;
|
||||
getTotalSupply(commitment?: Commitment): Promise<number>;
|
||||
|
|
|
@ -61,6 +61,10 @@ declare module '@solana/web3.js' {
|
|||
|
||||
declare export type Commitment = 'max' | 'recent';
|
||||
|
||||
declare export type SignatureStatusConfig = {
|
||||
searchTransactionHistory: boolean,
|
||||
};
|
||||
|
||||
declare export type SignatureStatus = {
|
||||
slot: number,
|
||||
err: TransactionError | null,
|
||||
|
@ -173,6 +177,7 @@ declare module '@solana/web3.js' {
|
|||
|
||||
declare export class Connection {
|
||||
constructor(endpoint: string, commitment: ?Commitment): Connection;
|
||||
commitment: ?Commitment;
|
||||
getAccountInfoAndContext(
|
||||
publicKey: PublicKey,
|
||||
commitment: ?Commitment,
|
||||
|
@ -205,11 +210,11 @@ declare module '@solana/web3.js' {
|
|||
getSlotLeader(commitment: ?Commitment): Promise<string>;
|
||||
getSignatureStatus(
|
||||
signature: TransactionSignature,
|
||||
commitment: ?Commitment,
|
||||
config: ?SignatureStatusConfig,
|
||||
): Promise<RpcResponseAndContext<SignatureStatus | null>>;
|
||||
getSignatureStatuses(
|
||||
signatures: Array<TransactionSignature>,
|
||||
commitment: ?Commitment,
|
||||
config: ?SignatureStatusConfig,
|
||||
): Promise<RpcResponseAndContext<Array<SignatureStatus | null>>>;
|
||||
getTransactionCount(commitment: ?Commitment): Promise<number>;
|
||||
getTotalSupply(commitment: ?Commitment): Promise<number>;
|
||||
|
|
|
@ -96,6 +96,16 @@ function notificationResultAndContext(resultDescription: any) {
|
|||
*/
|
||||
export type Commitment = 'max' | 'recent';
|
||||
|
||||
/**
|
||||
* Configuration object for changing query behavior
|
||||
*
|
||||
* @typedef {Object} SignatureStatusConfig
|
||||
* @property {boolean} searchTransactionHistory enable searching status history, not needed for recent transactions
|
||||
*/
|
||||
export type SignatureStatusConfig = {
|
||||
searchTransactionHistory: boolean,
|
||||
};
|
||||
|
||||
/**
|
||||
* Information describing a cluster node
|
||||
*
|
||||
|
@ -829,6 +839,13 @@ export class Connection {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The default commitment used for requests
|
||||
*/
|
||||
get commitment(): ?Commitment {
|
||||
return this._commitment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the balance for the specified public key, return with context
|
||||
*/
|
||||
|
@ -1033,25 +1050,28 @@ export class Connection {
|
|||
*/
|
||||
async getSignatureStatus(
|
||||
signature: TransactionSignature,
|
||||
commitment: ?Commitment,
|
||||
config: ?SignatureStatusConfig,
|
||||
): Promise<RpcResponseAndContext<SignatureStatus | null>> {
|
||||
const {context, value} = await this.getSignatureStatuses(
|
||||
[signature],
|
||||
commitment,
|
||||
config,
|
||||
);
|
||||
assert(value.length === 1);
|
||||
return {context, value: value[0]};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the current status of a signature
|
||||
* Fetch the current statuses of a batch of signatures
|
||||
*/
|
||||
async getSignatureStatuses(
|
||||
signatures: Array<TransactionSignature>,
|
||||
commitment: ?Commitment,
|
||||
config: ?SignatureStatusConfig,
|
||||
): Promise<RpcResponseAndContext<Array<SignatureStatus | null>>> {
|
||||
const args = this._argsWithCommitment([signatures], commitment);
|
||||
const unsafeRes = await this._rpcRequest('getSignatureStatuses', args);
|
||||
const params = [signatures];
|
||||
if (config) {
|
||||
params.push(config);
|
||||
}
|
||||
const unsafeRes = await this._rpcRequest('getSignatureStatuses', params);
|
||||
const res = GetSignatureStatusesRpcResult(unsafeRes);
|
||||
if (res.error) {
|
||||
throw new Error(res.error.message);
|
||||
|
|
|
@ -15,15 +15,20 @@ export async function sendAndConfirmRawTransaction(
|
|||
commitment: ?Commitment,
|
||||
): Promise<TransactionSignature> {
|
||||
const start = Date.now();
|
||||
const statusCommitment = commitment || connection.commitment || 'max';
|
||||
let signature = await connection.sendRawTransaction(rawTransaction);
|
||||
|
||||
// Wait up to a couple slots for a confirmation
|
||||
let status = null;
|
||||
let statusRetries = 6;
|
||||
for (;;) {
|
||||
status = (await connection.getSignatureStatus(signature, commitment)).value;
|
||||
status = (await connection.getSignatureStatus(signature)).value;
|
||||
if (status) {
|
||||
break;
|
||||
if (statusCommitment === 'max' && status.confirmations === null) {
|
||||
break;
|
||||
} else if (statusCommitment === 'recent') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep for approximately half a slot
|
||||
|
|
|
@ -43,8 +43,11 @@ async function _sendAndConfirmTransaction(
|
|||
signers: Array<Account>,
|
||||
commitment: ?Commitment,
|
||||
): Promise<TransactionSignature> {
|
||||
const statusCommitment = commitment || connection.commitment || 'max';
|
||||
|
||||
let sendRetries = 10;
|
||||
let signature;
|
||||
|
||||
for (;;) {
|
||||
const start = Date.now();
|
||||
signature = await connection.sendTransaction(transaction, ...signers);
|
||||
|
@ -53,10 +56,13 @@ async function _sendAndConfirmTransaction(
|
|||
let status = null;
|
||||
let statusRetries = 6;
|
||||
for (;;) {
|
||||
status = (await connection.getSignatureStatus(signature, commitment))
|
||||
.value;
|
||||
status = (await connection.getSignatureStatus(signature)).value;
|
||||
if (status) {
|
||||
break;
|
||||
if (statusCommitment === 'max' && status.confirmations === null) {
|
||||
break;
|
||||
} else if (statusCommitment === 'recent') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (--statusRetries <= 0) {
|
||||
|
|
|
@ -109,7 +109,6 @@ test('get program accounts', async () => {
|
|||
[
|
||||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -154,7 +153,6 @@ test('get program accounts', async () => {
|
|||
[
|
||||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -905,7 +903,6 @@ test('request airdrop - max commitment', async () => {
|
|||
[
|
||||
'1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -926,7 +923,7 @@ test('request airdrop - max commitment', async () => {
|
|||
},
|
||||
]);
|
||||
|
||||
const {value} = await connection.getSignatureStatus(signature, 'recent');
|
||||
const {value} = await connection.getSignatureStatus(signature);
|
||||
if (value === null) {
|
||||
expect(value).not.toBeNull();
|
||||
return;
|
||||
|
@ -1050,7 +1047,6 @@ test('transaction failure', async () => {
|
|||
[
|
||||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -1249,7 +1245,6 @@ test('transaction', async () => {
|
|||
[
|
||||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -1297,7 +1292,6 @@ test('transaction', async () => {
|
|||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
unprocessedSignature,
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
|
@ -151,7 +151,6 @@ test('transaction-payer', async () => {
|
|||
[
|
||||
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
|
||||
],
|
||||
{commitment: 'recent'},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue