From 14167f8db9902b07cc9a42220615f940533d1e31 Mon Sep 17 00:00:00 2001 From: Yolley Date: Tue, 26 Mar 2024 12:24:06 +0900 Subject: [PATCH 1/2] STREAM-1403: optimise tx confirmation --- packages/common/solana/utils.ts | 8 ++++- packages/distributor/solana/client.ts | 6 ++-- packages/stream/solana/StreamClient.ts | 42 +++++--------------------- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/packages/common/solana/utils.ts b/packages/common/solana/utils.ts index d64684b4..f60fdf3c 100644 --- a/packages/common/solana/utils.ts +++ b/packages/common/solana/utils.ts @@ -65,7 +65,13 @@ export function isSignerKeypair(walletOrKeypair: Keypair | SignerWalletAdapter): } /** - * Creates a Transaction with given instructions and optionally signs it + * Creates a Transaction with given instructions and optionally signs it. + * Be careful when passing `commitment` as for `confirmed` blockhash it always returns blockheight + 300 in `lastValidBlockHeight` + * And if you use this blockheight to confirm the transaction it could happen so that transaction is successfully executed + * But because `confirmTransaction` waits for only a minute it considers tx as expired as it could be that 300 blocks won't pass in a minute + * https://solana.stackexchange.com/questions/6238/why-is-lastvalidblockheight-300-blocks-ahead-than-current-blockheight-if-hashes + * https://solana.com/docs/core/transactions/retry + * It might be better to rely on `commitment` level that you pass to `Connection` instance of Solana client as it will be used when fetching blockheight on transaction confirmation * @param connection - Solana client connection * @param ixs - Instructions to add to the Transaction * @param payer - PublicKey of payer diff --git a/packages/distributor/solana/client.ts b/packages/distributor/solana/client.ts index 6d9b120b..60e6cf68 100644 --- a/packages/distributor/solana/client.ts +++ b/packages/distributor/solana/client.ts @@ -129,7 +129,7 @@ export default class SolanaDistributorClient { ), ); - const { tx, hash } = await prepareTransaction(this.connection, ixs, invoker.publicKey, this.getCommitment()); + const { tx, hash } = await prepareTransaction(this.connection, ixs, invoker.publicKey); const signature = await wrappedSignAndExecuteTransaction(this.connection, invoker, tx, hash); return { @@ -182,7 +182,7 @@ export default class SolanaDistributorClient { ixs.push(claimLocked(accounts, this.programId)); - const { tx, hash } = await prepareTransaction(this.connection, ixs, invoker.publicKey, this.getCommitment()); + const { tx, hash } = await prepareTransaction(this.connection, ixs, invoker.publicKey); const signature = await wrappedSignAndExecuteTransaction(this.connection, invoker, tx, hash); return { ixs, txId: signature }; @@ -217,7 +217,7 @@ export default class SolanaDistributorClient { ixs.push(clawback(accounts, this.programId)); - const { tx, hash } = await prepareTransaction(this.connection, ixs, invoker.publicKey, this.getCommitment()); + const { tx, hash } = await prepareTransaction(this.connection, ixs, invoker.publicKey); const signature = await wrappedSignAndExecuteTransaction(this.connection, invoker, tx, hash); return { ixs, txId: signature }; diff --git a/packages/stream/solana/StreamClient.ts b/packages/stream/solana/StreamClient.ts index bb5b2a62..f3fe804e 100644 --- a/packages/stream/solana/StreamClient.ts +++ b/packages/stream/solana/StreamClient.ts @@ -139,7 +139,7 @@ export default class SolanaStreamClient extends BaseStreamClient { this.connection, ixs, extParams.sender.publicKey, - this.getCommitment(), + undefined, metadata, ); const signature = await signAndExecuteTransaction(this.connection, extParams.sender, tx, hash); @@ -271,7 +271,7 @@ export default class SolanaStreamClient extends BaseStreamClient { this.connection, ixs, extParams.sender.publicKey, - this.getCommitment(), + undefined, metadata, ); const signature = await signAndExecuteTransaction(this.connection, extParams.sender, tx, hash); @@ -428,8 +428,7 @@ export default class SolanaStreamClient extends BaseStreamClient { }); } - const commitment = typeof this.commitment == "string" ? this.commitment : this.commitment.commitment; - const hash = await this.connection.getLatestBlockhash(commitment); + const hash = await this.connection.getLatestBlockhash(); for (const { ixs, metadata, recipient } of instructionsBatch) { const tx = new Transaction({ @@ -508,12 +507,7 @@ export default class SolanaStreamClient extends BaseStreamClient { extParams: IInteractStreamSolanaExt, ): Promise { const ixs: TransactionInstruction[] = await this.prepareWithdrawInstructions({ id, amount }, extParams); - const { tx, hash } = await prepareTransaction( - this.connection, - ixs, - extParams.invoker.publicKey, - this.getCommitment(), - ); + const { tx, hash } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey); const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, hash); return { ixs, txId: signature }; @@ -570,12 +564,7 @@ export default class SolanaStreamClient extends BaseStreamClient { */ public async cancel({ id }: ICancelData, extParams: IInteractStreamSolanaExt): Promise { const ixs = await this.prepareCancelInstructions({ id }, extParams); - const { tx, hash } = await prepareTransaction( - this.connection, - ixs, - extParams.invoker.publicKey, - this.getCommitment(), - ); + const { tx, hash } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey); const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, hash); return { ixs, txId: signature }; @@ -639,12 +628,7 @@ export default class SolanaStreamClient extends BaseStreamClient { extParams: IInteractStreamSolanaExt, ): Promise { const ixs: TransactionInstruction[] = await this.prepareTransferInstructions({ id, newRecipient }, extParams); - const { tx, hash } = await prepareTransaction( - this.connection, - ixs, - extParams.invoker.publicKey, - this.getCommitment(), - ); + const { tx, hash } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey); const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, hash); return { ixs, txId: signature }; @@ -698,12 +682,7 @@ export default class SolanaStreamClient extends BaseStreamClient { */ public async topup({ id, amount }: ITopUpData, extParams: ITopUpStreamSolanaExt): Promise { const ixs: TransactionInstruction[] = await this.prepareTopupInstructions({ id, amount }, extParams); - const { tx, hash } = await prepareTransaction( - this.connection, - ixs, - extParams.invoker.publicKey, - this.getCommitment(), - ); + const { tx, hash } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey); const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, hash); return { ixs, txId: signature }; @@ -820,12 +799,7 @@ export default class SolanaStreamClient extends BaseStreamClient { */ public async update(data: IUpdateData, extParams: IInteractStreamSolanaExt): Promise { const ixs = await this.prepareUpdateInstructions(data, extParams); - const { tx, hash } = await prepareTransaction( - this.connection, - ixs, - extParams.invoker.publicKey, - this.getCommitment(), - ); + const { tx, hash } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey); const signature = await signAndExecuteTransaction(this.connection, extParams.invoker, tx, hash); return { From 42db76372c37597f1e9a5bfb60ad2f29bdeaa349 Mon Sep 17 00:00:00 2001 From: Yolley Date: Tue, 26 Mar 2024 12:25:32 +0900 Subject: [PATCH 2/2] bump --- lerna.json | 2 +- packages/common/package.json | 2 +- packages/distributor/package.json | 2 +- packages/eslint-config/package.json | 2 +- packages/stream/package.json | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lerna.json b/lerna.json index c9177ade..4cbe9498 100644 --- a/lerna.json +++ b/lerna.json @@ -2,6 +2,6 @@ "packages": [ "packages/*" ], - "version": "6.0.0-alpha.6", + "version": "6.0.0-alpha.7", "$schema": "node_modules/lerna/schemas/lerna-schema.json" } diff --git a/packages/common/package.json b/packages/common/package.json index af78c117..afb3b4c5 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -1,6 +1,6 @@ { "name": "@streamflow/common", - "version": "6.0.0-alpha.6", + "version": "6.0.0-alpha.7", "description": "Common utilities and types used by streamflow packages.", "homepage": "https://github.com/streamflow-finance/js-sdk/", "main": "dist/index.js", diff --git a/packages/distributor/package.json b/packages/distributor/package.json index 4badb36d..a7a0bcfd 100644 --- a/packages/distributor/package.json +++ b/packages/distributor/package.json @@ -1,6 +1,6 @@ { "name": "@streamflow/distributor", - "version": "6.0.0-alpha.6", + "version": "6.0.0-alpha.7", "description": "JavaScript SDK to interact with Streamflow Airdrop protocol.", "homepage": "https://github.com/streamflow-finance/js-sdk/", "main": "dist/index.js", diff --git a/packages/eslint-config/package.json b/packages/eslint-config/package.json index b01fa424..2f291ecd 100644 --- a/packages/eslint-config/package.json +++ b/packages/eslint-config/package.json @@ -1,6 +1,6 @@ { "name": "@streamflow/eslint-config", - "version": "6.0.0-alpha.6", + "version": "6.0.0-alpha.7", "license": "ISC", "main": "index.js", "files": [ diff --git a/packages/stream/package.json b/packages/stream/package.json index 1ad1ee22..6f6d56d8 100644 --- a/packages/stream/package.json +++ b/packages/stream/package.json @@ -1,6 +1,6 @@ { "name": "@streamflow/stream", - "version": "6.0.0-alpha.6", + "version": "6.0.0-alpha.7", "description": "JavaScript SDK to interact with Streamflow protocol.", "homepage": "https://github.com/streamflow-finance/js-sdk/", "main": "dist/index.js",