Skip to content

Commit

Permalink
Simple Versioned Transaction Support (#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry-E authored Mar 6, 2023
1 parent 5623c7c commit 064dde4
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- client: Add support for multithreading to the rust client: use flag `--multithreaded` ([#2321](https://github.com/coral-xyz/anchor/pull/2321)).
- client: Add `async_rpc` a method which returns a nonblocking solana rpc client ([2322](https://github.com/coral-xyz/anchor/pull/2322)).
- avm, cli: Use the `rustls-tls` feature of `reqwest` so that users don't need OpenSSL installed ([#2385](https://github.com/coral-xyz/anchor/pull/2385)).
- ts: Add `VersionedTransaction` support. Methods in the `Provider` class and `Wallet` interface now use the argument `tx: Transaction | VersionedTransaction` ([2407](https://github.com/coral-xyz/anchor/pull/2407)).
- cli: Add `--arch sbf` option to compile programs using `cargo build-sbf` ([#2398](https://github.com/coral-xyz/anchor/pull/2398)).

### Fixes
Expand Down
100 changes: 100 additions & 0 deletions tests/misc/tests/misc/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
SystemProgram,
Message,
VersionedTransaction,
AddressLookupTableProgram,
TransactionMessage,
} from "@solana/web3.js";
import {
TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -61,6 +63,104 @@ const miscTest = (
assert.strictEqual(dataAccount.data, 99);
});

it("Can send VersionedTransaction", async () => {
// Create the lookup table
const recentSlot = await provider.connection.getSlot();
const [loookupTableInstruction, lookupTableAddress] =
AddressLookupTableProgram.createLookupTable({
authority: provider.publicKey,
payer: provider.publicKey,
recentSlot,
});
const extendInstruction = AddressLookupTableProgram.extendLookupTable({
payer: provider.publicKey,
authority: provider.publicKey,
lookupTable: lookupTableAddress,
addresses: [provider.publicKey, SystemProgram.programId],
});
let createLookupTableTx = new VersionedTransaction(
new TransactionMessage({
instructions: [loookupTableInstruction, extendInstruction],
payerKey: program.provider.publicKey,
recentBlockhash: (await provider.connection.getLatestBlockhash())
.blockhash,
}).compileToV0Message()
);
await provider.sendAndConfirm(createLookupTableTx, [], {
skipPreflight: true,
});

// Use the lookup table in a transaction
const transferAmount = 1_000_000;
const lookupTableAccount = await provider.connection
.getAddressLookupTable(lookupTableAddress)
.then((res) => res.value);
const target = anchor.web3.Keypair.generate();
let transferInstruction = SystemProgram.transfer({
fromPubkey: provider.publicKey,
lamports: transferAmount,
toPubkey: target.publicKey,
});
let transferUsingLookupTx = new VersionedTransaction(
new TransactionMessage({
instructions: [transferInstruction],
payerKey: program.provider.publicKey,
recentBlockhash: (await provider.connection.getLatestBlockhash())
.blockhash,
}).compileToV0Message([lookupTableAccount])
);
await provider.simulate(transferUsingLookupTx, [], "processed");
await provider.sendAndConfirm(transferUsingLookupTx, [], {
skipPreflight: true,
commitment: "confirmed",
});
let newBalance = await provider.connection.getBalance(
target.publicKey,
"confirmed"
);
assert.strictEqual(newBalance, transferAmount);

// Test sendAll with versioned transaction
let oneTransferUsingLookupTx = new VersionedTransaction(
new TransactionMessage({
instructions: [
SystemProgram.transfer({
fromPubkey: provider.publicKey,
// Needed to make the transactions distinct
lamports: transferAmount + 1,
toPubkey: target.publicKey,
}),
],
payerKey: program.provider.publicKey,
recentBlockhash: (await provider.connection.getLatestBlockhash())
.blockhash,
}).compileToV0Message([lookupTableAccount])
);
let twoTransferUsingLookupTx = new VersionedTransaction(
new TransactionMessage({
instructions: [
SystemProgram.transfer({
fromPubkey: provider.publicKey,
lamports: transferAmount,
toPubkey: target.publicKey,
}),
],
payerKey: program.provider.publicKey,
recentBlockhash: (await provider.connection.getLatestBlockhash())
.blockhash,
}).compileToV0Message([lookupTableAccount])
);
await provider.sendAll(
[{ tx: oneTransferUsingLookupTx }, { tx: twoTransferUsingLookupTx }],
{ skipPreflight: true, commitment: "confirmed" }
);
newBalance = await provider.connection.getBalance(
target.publicKey,
"confirmed"
);
assert.strictEqual(newBalance, transferAmount * 3 + 1);
});

it("Can embed programs into genesis from the Anchor.toml", async () => {
const pid = new anchor.web3.PublicKey(
"FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
Expand Down
27 changes: 22 additions & 5 deletions ts/packages/anchor/src/nodewallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Buffer } from "buffer";
import { Keypair, PublicKey, Transaction } from "@solana/web3.js";
import {
Keypair,
PublicKey,
Transaction,
VersionedTransaction,
} from "@solana/web3.js";
import { Wallet } from "./provider";

/**
Expand Down Expand Up @@ -30,14 +35,26 @@ export default class NodeWallet implements Wallet {
return new NodeWallet(payer);
}

async signTransaction(tx: Transaction): Promise<Transaction> {
tx.partialSign(this.payer);
async signTransaction<T extends Transaction | VersionedTransaction>(
tx: T
): Promise<T> {
if (tx instanceof VersionedTransaction) {
tx.sign([this.payer]);
} else {
tx.partialSign(this.payer);
}
return tx;
}

async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
async signAllTransactions<T extends Transaction | VersionedTransaction>(
txs: T[]
): Promise<T[]> {
return txs.map((t) => {
t.partialSign(this.payer);
if (t instanceof VersionedTransaction) {
t.sign([this.payer]);
} else {
t.partialSign(this.payer);
}
return t;
});
}
Expand Down
Loading

1 comment on commit 064dde4

@vercel
Copy link

@vercel vercel bot commented on 064dde4 Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
anchor-lang.com
www.anchor-lang.com

Please sign in to comment.