Skip to content

Commit

Permalink
Merge pull request #17 from solana-developers/add-confirmtransaction
Browse files Browse the repository at this point in the history
Add confirmTransaction()
  • Loading branch information
nickfrosty authored Feb 5, 2024
2 parents 6b5bd52 + 7210da3 commit adf9573
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Eventually most of these will end up in `@solana/web3.js`.

[Get a Solana Explorer link for a transaction, address, or block](#getexplorerlinktype-identifier-clustername)

[Confirm a transaction (includes getting a recent blockhash)](#confirmTransaction)

[Get a keypair from a keypair file (like id.json)](#getkeypairfromfilefilename)

[Get a keypair from an environment variable](#getkeypairfromenvironmentenvironmentvariable)
Expand Down Expand Up @@ -155,6 +157,14 @@ getExplorerLink("block", "241889720", "mainnet-beta");

Will return `"https://explorer.solana.com/block/241889720"`

### confirmTransaction(connection, transaction)

Confirm a transaction, and also gets the recent blockhash required to confirm it.

```typescript
await confirmTransaction(connection, transaction);
```

## node.js specific helpers

### getKeypairFromFile(filename)
Expand Down
35 changes: 34 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import {
requestAndConfirmAirdrop,
requestAndConfirmAirdropIfRequired,
getExplorerLink,
confirmTransaction,
makeKeypairs,
} from "./index";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
import {
Connection,
Keypair,
LAMPORTS_PER_SOL,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import assert from "node:assert/strict";
import base58 from "bs58";
// See https://m.media-amazon.com/images/I/51TJeGHxyTL._SY445_SX342_.jpg
Expand Down Expand Up @@ -288,6 +295,32 @@ describe("getExplorerLink", () => {
});
});

describe.only("confirmTransaction", () => {
test("confirmTransaction works for a successful transaction", async () => {
const connection = new Connection(LOCALHOST);
const [sender, recipient] = [Keypair.generate(), Keypair.generate()];
const lamportsToAirdrop = 2 * LAMPORTS_PER_SOL;
await requestAndConfirmAirdrop(
connection,
sender.publicKey,
lamportsToAirdrop,
);

const transaction = await connection.sendTransaction(
new Transaction().add(
SystemProgram.transfer({
fromPubkey: sender.publicKey,
toPubkey: recipient.publicKey,
lamports: 1_000_000,
}),
),
[sender],
);

await confirmTransaction(connection, transaction);
});
});

describe("makeKeypairs", () => {
test("makeKeypairs() creates the correct number of keypairs", () => {
const keypairs = makeKeypairs(3);
Expand Down
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export const requestAndConfirmAirdrop = async (
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature: airdropTransactionSignature,
},
"confirmed",
"finalized",
);
return connection.getBalance(publicKey, "confirmed");
return connection.getBalance(publicKey, "finalized");
};

export const requestAndConfirmAirdropIfRequired = async (
Expand All @@ -183,6 +183,27 @@ export const requestAndConfirmAirdropIfRequired = async (
return balance;
};

export const confirmTransaction = async (
connection: Connection,
signature: string,
): Promise<string> => {
const block = await connection.getLatestBlockhash();
const res = await connection.confirmTransaction({
signature,
...block,
});

/**
* note: `confirmTransaction` does not throw an error if the confirmation does not succeed,
* but rather a `TransactionError` object. so we handle that here
*
* https://solana-labs.github.io/solana-web3.js/classes/Connection.html#confirmTransaction.confirmTransaction-1
*/
if (!!res.value.err) throw Error(res.value.err.toString());

return signature;
};

// Shout out to deanmlittle for this technique
export const makeKeypairs = (amount: number): Array<Keypair> => {
return Array.from({ length: amount }, () => Keypair.generate());
Expand Down

0 comments on commit adf9573

Please sign in to comment.