Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add confirmTransaction() #17

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 @@ -145,6 +147,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,8 +7,15 @@ import {
requestAndConfirmAirdrop,
requestAndConfirmAirdropIfRequired,
getExplorerLink,
confirmTransaction,
} 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 @@ -286,3 +293,29 @@ describe("getExplorerLink", () => {
assert.equal(link, "https://explorer.solana.com/block/241889720");
});
});

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);
});
});
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 @@ -182,3 +182,24 @@ 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;
};