Просмотр исходного кода

update tto versioned transactions; ntermediate commit not done yet

henrye 2 лет назад
Родитель
Сommit
681a97ba3f

+ 10 - 10
ts/packages/anchor/src/program/namespace/transaction.ts

@@ -1,4 +1,4 @@
-import { Transaction } from "@solana/web3.js";
+import { Transaction, TransactionInstruction } from "@solana/web3.js";
 import { Idl, IdlInstruction } from "../../idl.js";
 import { splitArgsAndCtx } from "../context.js";
 import { InstructionFn } from "./instruction.js";
@@ -13,16 +13,16 @@ export default class TransactionFactory {
     idlIx: I,
     ixFn: InstructionFn<IDL, I>
   ): TransactionFn<IDL, I> {
-    const txFn: TransactionFn<IDL, I> = (...args): Transaction => {
+    const txFn: TransactionFn<IDL, I> = (...args): TransactionInstruction[] => {
       const [, ctx] = splitArgsAndCtx(idlIx, [...args]);
-      const tx = new Transaction();
+      const tx: TransactionInstruction[] = [];
       if (ctx.preInstructions && ctx.instructions) {
         throw new Error("instructions is deprecated, use preInstructions");
       }
-      ctx.preInstructions?.forEach((ix) => tx.add(ix));
-      ctx.instructions?.forEach((ix) => tx.add(ix));
-      tx.add(ixFn(...args));
-      ctx.postInstructions?.forEach((ix) => tx.add(ix));
+      ctx.preInstructions?.forEach((ix) => tx.push(ix));
+      ctx.instructions?.forEach((ix) => tx.push(ix));
+      tx.push(ixFn(...args));
+      ctx.postInstructions?.forEach((ix) => tx.push(ix));
       return tx;
     };
 
@@ -62,12 +62,12 @@ export default class TransactionFactory {
 export type TransactionNamespace<
   IDL extends Idl = Idl,
   I extends AllInstructions<IDL> = AllInstructions<IDL>
-> = MakeInstructionsNamespace<IDL, I, Transaction>;
+> = MakeInstructionsNamespace<IDL, I, TransactionInstruction[]>;
 
 /**
- * Tx is a function to create a `Transaction` for a given program instruction.
+ * Tx is a function to create a `TransactionInstruction[]` for a given program instruction.
  */
 export type TransactionFn<
   IDL extends Idl = Idl,
   I extends AllInstructions<IDL> = AllInstructions<IDL>
-> = InstructionContextFn<IDL, I, Transaction>;
+> = InstructionContextFn<IDL, I, TransactionInstruction[]>;

+ 33 - 22
ts/packages/anchor/src/provider.ts

@@ -2,13 +2,16 @@ import {
   Connection,
   Signer,
   PublicKey,
-  Transaction,
   TransactionSignature,
   ConfirmOptions,
   SimulatedTransactionResponse,
   Commitment,
   SendTransactionError,
   SendOptions,
+  TransactionInstruction,
+  VersionedTransaction,
+  AddressLookupTableAccount,
+  TransactionMessage,
 } from "@solana/web3.js";
 import { bs58 } from "./utils/bytes/index.js";
 import { isBrowser } from "./utils/common.js";
@@ -22,21 +25,21 @@ export default interface Provider {
   readonly publicKey?: PublicKey;
 
   send?(
-    tx: Transaction,
+    tx: TransactionInstruction[],
     signers?: Signer[],
     opts?: SendOptions
   ): Promise<TransactionSignature>;
   sendAndConfirm?(
-    tx: Transaction,
+    tx: TransactionInstruction[],
     signers?: Signer[],
     opts?: ConfirmOptions
   ): Promise<TransactionSignature>;
   sendAll?(
-    txWithSigners: { tx: Transaction; signers?: Signer[] }[],
+    txWithSigners: { tx: TransactionInstruction[]; signers?: Signer[] }[],
     opts?: ConfirmOptions
   ): Promise<Array<TransactionSignature>>;
   simulate?(
-    tx: Transaction,
+    tx: TransactionInstruction[],
     signers?: Signer[],
     commitment?: Commitment,
     includeAccounts?: boolean | PublicKey[]
@@ -124,24 +127,30 @@ export class AnchorProvider implements Provider {
    * @param opts    Transaction confirmation options.
    */
   async sendAndConfirm(
-    tx: Transaction,
+    instructions: TransactionInstruction[],
     signers?: Signer[],
-    opts?: ConfirmOptions
+    opts?: ConfirmOptions,
+    feePayer?: PublicKey,
+    lookupTable?: AddressLookupTableAccount[]
   ): Promise<TransactionSignature> {
     if (opts === undefined) {
       opts = this.opts;
     }
 
-    tx.feePayer = tx.feePayer || this.wallet.publicKey;
-
-    tx.recentBlockhash = (
-      await this.connection.getLatestBlockhash(opts.preflightCommitment)
-    ).blockhash;
+    let tx = new VersionedTransaction(
+      new TransactionMessage({
+        instructions,
+        payerKey: feePayer ?? this.wallet.publicKey,
+        recentBlockhash: (
+          await this.connection.getLatestBlockhash(opts.preflightCommitment)
+        ).blockhash,
+      }).compileToV0Message(lookupTable)
+    );
 
     tx = await this.wallet.signTransaction(tx);
-    (signers ?? []).forEach((kp) => {
-      tx.partialSign(kp);
-    });
+    if (signers) {
+      tx.sign(signers);
+    }
 
     const rawTx = tx.serialize();
 
@@ -156,7 +165,7 @@ export class AnchorProvider implements Provider {
         // because that will see the tx sent with `sendAndConfirmRawTransaction` no matter which
         // commitment `sendAndConfirmRawTransaction` used
         const failedTx = await this.connection.getTransaction(
-          bs58.encode(tx.signature!),
+          bs58.encode(tx.signatures[0]!),
           { commitment: "confirmed" }
         );
         if (!failedTx) {
@@ -178,7 +187,7 @@ export class AnchorProvider implements Provider {
    * @param opts          Transaction confirmation options.
    */
   async sendAll(
-    txWithSigners: { tx: Transaction; signers?: Signer[] }[],
+    txWithSigners: { tx: TransactionInstruction[]; signers?: Signer[] }[],
     opts?: ConfirmOptions
   ): Promise<Array<TransactionSignature>> {
     if (opts === undefined) {
@@ -253,7 +262,7 @@ export class AnchorProvider implements Provider {
    * @param opts    Transaction confirmation options.
    */
   async simulate(
-    tx: Transaction,
+    tx: TransactionInstruction[],
     signers?: Signer[],
     commitment?: Commitment,
     includeAccounts?: boolean | PublicKey[]
@@ -295,7 +304,7 @@ class SimulateError extends Error {
 }
 
 export type SendTxRequest = {
-  tx: Transaction;
+  tx: TransactionInstruction[];
   signers: Array<Signer | undefined>;
 };
 
@@ -303,8 +312,10 @@ export type SendTxRequest = {
  * Wallet interface for objects that can be used to sign provider transactions.
  */
 export interface Wallet {
-  signTransaction(tx: Transaction): Promise<Transaction>;
-  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
+  signTransaction(tx: VersionedTransaction): Promise<VersionedTransaction>;
+  signAllTransactions(
+    txs: VersionedTransaction[]
+  ): Promise<VersionedTransaction[]>;
   publicKey: PublicKey;
 }
 
@@ -312,7 +323,7 @@ export interface Wallet {
 // a better error if 'confirmTransaction` returns an error status
 async function sendAndConfirmRawTransaction(
   connection: Connection,
-  rawTransaction: Buffer,
+  rawTransaction: Uint8Array,
   options?: ConfirmOptions
 ): Promise<TransactionSignature> {
   const sendOptions = options && {