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 support for versioned transactions #773

Merged
merged 6 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"path": "packages/anchor-contrib/dist/esm/index.js",
"limit": "12 KB",
"ignore": [
"@project-serum/anchor",
"@coral-xyz/anchor",
"@solana/web3.js",
"@saberhq/solana-contrib"
],
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.23.5",
"@babel/preset-typescript": "^7.23.3",
"@coral-xyz/anchor": "^0.29.0",
"@jest/types": "^29.6.3",
"@project-serum/anchor": "^0.25.0",
"@rushstack/eslint-patch": "^1.6.0",
"@saberhq/eslint-config": "^3.1.1",
"@saberhq/eslint-config-react": "^3.1.1",
"@saberhq/tsconfig": "^3.1.1",
"@saberhq/eslint-config": "^3.2.1",
"@saberhq/eslint-config-react": "^3.2.1",
"@saberhq/tsconfig": "^3.2.1",
"@saberhq/use-solana": "workspace:*",
"@size-limit/file": "^11.0.0",
"@size-limit/webpack": "^11.0.0",
"@size-limit/webpack-why": "^11.0.0",
"@solana/web3.js": "^1.87.6",
"@solana/web3.js": "^1.91.1",
"@types/babel__core": "^7.20.5",
"@types/babel__preset-env": "^7.9.6",
"@types/bn.js": "^5.1.5",
Expand Down
8 changes: 4 additions & 4 deletions packages/anchor-contrib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"prepublishOnly": "npm run build"
},
"peerDependencies": {
"@project-serum/anchor": "^0.22 || ^0.23 || ^0.24",
"@coral-xyz/anchor": "^0.22 || ^0.23 || ^0.24 || ^0.28 || ^0.29",
"@solana/web3.js": "^1.42",
"bn.js": "^4 || ^5"
},
"devDependencies": {
"@project-serum/anchor": "^0.25.0",
"@saberhq/tsconfig": "^3.1.1",
"@solana/web3.js": "^1.87.6",
"@coral-xyz/anchor": "^0.29.0",
"@saberhq/tsconfig": "^3.2.1",
"@solana/web3.js": "^1.91.1",
"@types/lodash.camelcase": "^4.3.9",
"@types/lodash.mapvalues": "^4.6.9",
"bn.js": "^5.2.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/anchor-contrib/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Idl } from "@project-serum/anchor";
import type { IdlErrorCode } from "@project-serum/anchor/dist/esm/idl.js";
import type { Idl } from "@coral-xyz/anchor";
import type { IdlErrorCode } from "@coral-xyz/anchor/dist/esm/idl.js";

import type { AnchorError } from "./index.js";

Expand Down
4 changes: 2 additions & 2 deletions packages/anchor-contrib/src/generateAccountParsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AccountsCoder, Idl } from "@project-serum/anchor";
import { BorshAccountsCoder } from "@project-serum/anchor";
import type { AccountsCoder, Idl } from "@coral-xyz/anchor";
import { BorshAccountsCoder } from "@coral-xyz/anchor";
import camelCase from "lodash.camelcase";

/**
Expand Down
21 changes: 14 additions & 7 deletions packages/anchor-contrib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type {
Address,
BN,
Context as AnchorContext,
MethodsNamespace,
Program as AProgram,
ProgramAccount,
StateClient,
} from "@project-serum/anchor";
} from "@coral-xyz/anchor";
import type {
Idl,
IdlAccountItem,
Expand All @@ -18,7 +18,7 @@ import type {
IdlType,
IdlTypeDef,
IdlTypeDefTyStruct,
} from "@project-serum/anchor/dist/esm/idl.js";
} from "@coral-xyz/anchor/dist/esm/idl.js";
import type {
AccountMeta,
PublicKey,
Expand All @@ -27,6 +27,13 @@ import type {
TransactionSignature,
} from "@solana/web3.js";

export type IdlWithState = Idl & {
state?: {
methods: IdlInstruction[];
struct: IdlTypeDef;
};
};

type InstructionsParsed = Record<
string,
{
Expand Down Expand Up @@ -159,7 +166,7 @@ type MakeInstructions<I extends IdlInstruction[], Defined> = {
};

export type AnchorProgram<
IDL extends Idl,
IDL extends IdlWithState,
A,
Defined = AnchorDefined<IDL>,
RPCInstructions extends MakeInstructions<
Expand All @@ -175,7 +182,7 @@ export type AnchorProgram<
"rpc" | "state" | "account" | "transaction" | "instruction"
> & {
rpc: RpcNamespace<RPCInstructions>;
state: StateClient<IDL>;
state: MethodsNamespace<IDL>;
account: AccountsNamespace<A>;
transaction: TransactionNamespace<RPCInstructions & Methods>;
instruction: InstructionNamespace<RPCInstructions & Methods>;
Expand Down Expand Up @@ -210,13 +217,13 @@ export type AnchorAccounts<T extends Idl, Defined> = AnchorTypeDefs<
Defined
>;

export type AnchorState<T extends Idl, Defined> = AnchorTypeDef<
export type AnchorState<T extends IdlWithState, Defined> = AnchorTypeDef<
NonNullable<T["state"]>["struct"],
Defined
>;

export type AnchorTypes<
T extends Idl,
T extends IdlWithState,
AccountMap = Record<string, never>,
D = Record<string, never>,
DEF = AnchorDefined<T, D>,
Expand Down
6 changes: 3 additions & 3 deletions packages/anchor-contrib/src/utils/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AccountsCoder } from "@project-serum/anchor";
import { BorshAccountsCoder } from "@project-serum/anchor";
import type { IdlTypeDef } from "@project-serum/anchor/dist/esm/idl.js";
import type { AccountsCoder } from "@coral-xyz/anchor";
import { BorshAccountsCoder } from "@coral-xyz/anchor";
import type { IdlTypeDef } from "@coral-xyz/anchor/dist/esm/idl.js";
import type { ProgramAccountParser, PublicKey } from "@saberhq/solana-contrib";
import camelCase from "lodash.camelcase";

Expand Down
11 changes: 6 additions & 5 deletions packages/anchor-contrib/src/utils/coder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Accounts, Idl } from "@project-serum/anchor";
import { BorshCoder, EventParser, utils } from "@project-serum/anchor";
import { default as InstructionNamespaceFactory } from "@project-serum/anchor/dist/cjs/program/namespace/instruction.js";
import type { InstructionDisplay } from "@project-serum/anchor/dist/esm/coder/borsh/instruction.js";
import type { Accounts, Idl } from "@coral-xyz/anchor";
import { BorshCoder, EventParser, utils } from "@coral-xyz/anchor";
import { default as InstructionNamespaceFactory } from "@coral-xyz/anchor/dist/cjs/program/namespace/instruction.js";
import type { InstructionDisplay } from "@coral-xyz/anchor/dist/esm/coder/borsh/instruction.js";
import type {
IdlAccountItem,
IdlTypeDef,
} from "@project-serum/anchor/dist/esm/idl.js";
} from "@coral-xyz/anchor/dist/esm/idl.js";
import type { Provider as SaberProvider } from "@saberhq/solana-contrib";
import type { GetProgramAccountsFilter, PublicKey } from "@solana/web3.js";
import { TransactionInstruction } from "@solana/web3.js";
Expand Down Expand Up @@ -207,6 +207,7 @@ export class SuperCoder<T extends CoderAnchorTypes> {
const keys = InstructionNamespaceFactory.accountsArray(
accounts,
idlIx.accounts,
this.address,
name,
);
return new TransactionInstruction({
Expand Down
2 changes: 1 addition & 1 deletion packages/anchor-contrib/src/utils/idl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IdlType } from "@project-serum/anchor/dist/esm/idl.js";
import type { IdlType } from "@coral-xyz/anchor/dist/esm/idl.js";

/**
* Formats an IDL type as a string. This comes straight from the Anchor source.
Expand Down
4 changes: 2 additions & 2 deletions packages/anchor-contrib/src/utils/programs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Idl } from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import type { Idl } from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import type {
Provider as SaberProvider,
ReadonlyProvider as ReadonlySaberProvider,
Expand Down
4 changes: 2 additions & 2 deletions packages/anchor-contrib/src/utils/provider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {
AnchorProvider as AnchorProviderImpl,
Provider as IAnchorProvider,
} from "@project-serum/anchor";
import * as anchor from "@project-serum/anchor";
} from "@coral-xyz/anchor";
import * as anchor from "@coral-xyz/anchor";
import type {
Provider as SaberProvider,
ReadonlyProvider as ReadonlySaberProvider,
Expand Down
8 changes: 4 additions & 4 deletions packages/chai-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
"access": "public"
},
"peerDependencies": {
"@project-serum/anchor": ">=0.17",
"@coral-xyz/anchor": ">=0.17",
"@solana/web3.js": "^1.42",
"bn.js": "^5.2.0",
"jsbi": "*"
},
"devDependencies": {
"@project-serum/anchor": "^0.25.0",
"@saberhq/tsconfig": "^3.1.1",
"@solana/web3.js": "^1.87.6",
"@coral-xyz/anchor": "^0.29.0",
"@saberhq/tsconfig": "^3.2.1",
"@solana/web3.js": "^1.91.1",
"bn.js": "^5.2.1",
"jsbi": "^4.3.0",
"typescript": "^5.3.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/chai-solana/src/expectTXTable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IdlErrorCode } from "@project-serum/anchor/dist/esm/idl.js";
import type { IdlErrorCode } from "@coral-xyz/anchor/dist/esm/idl.js";
import { matchError, matchErrorCode } from "@saberhq/anchor-contrib";
import type { TransactionEnvelope } from "@saberhq/solana-contrib";
import { parseTransactionLogs, printTXTable } from "@saberhq/solana-contrib";
Expand Down
4 changes: 2 additions & 2 deletions packages/chai-solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import "./types.js";

import type { Address } from "@project-serum/anchor";
import { BN } from "@project-serum/anchor";
import type { Address } from "@coral-xyz/anchor";
import { BN } from "@coral-xyz/anchor";
import { TokenAmount } from "@saberhq/token-utils";
import { PublicKey } from "@solana/web3.js";
import chaiAsPromised from "chai-as-promised";
Expand Down
2 changes: 1 addition & 1 deletion packages/chai-solana/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import "chai-bn";
import "chai-as-promised";

import type { Address } from "@project-serum/anchor";
import type { Address } from "@coral-xyz/anchor";
import type { BigintIsh, TokenAmount } from "@saberhq/token-utils";

declare global {
Expand Down
2 changes: 1 addition & 1 deletion packages/chai-solana/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "chai-as-promised";

import type { Idl } from "@project-serum/anchor";
import type { Idl } from "@coral-xyz/anchor";
import type {
PromiseOrValue,
TransactionLike,
Expand Down
2 changes: 1 addition & 1 deletion packages/option-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"tslib": "^2.6.2"
},
"devDependencies": {
"@saberhq/tsconfig": "^3.1.1",
"@saberhq/tsconfig": "^3.2.1",
"typescript": "^5.3.2"
}
}
4 changes: 2 additions & 2 deletions packages/solana-contrib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"tslib": "^2.6.2"
},
"devDependencies": {
"@saberhq/tsconfig": "^3.1.1",
"@solana/web3.js": "^1.87.6",
"@saberhq/tsconfig": "^3.2.1",
"@solana/web3.js": "^1.91.1",
"@types/bn.js": "^5.1.5",
"@types/jest": "^29.5.10",
"@types/node": "^18.18.14",
Expand Down
25 changes: 18 additions & 7 deletions packages/solana-contrib/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,42 @@ import type {
SimulatedTransactionResponse,
Transaction,
} from "@solana/web3.js";
import { VersionedTransaction } from "@solana/web3.js";

import type { BroadcastOptions, PendingTransaction } from "./index.js";

export const isVersionedTransaction = (
tx: Transaction | VersionedTransaction,
): tx is VersionedTransaction => {
return "version" in tx || tx instanceof VersionedTransaction;
};

/**
* Wallet interface for objects that can be used to sign provider transactions.
*
* This interface comes from Anchor.
*/
export interface Wallet {
/**
* The PublicKey of the wallet.
*/
publicKey: PublicKey;

/**
* Signs a transaction with the wallet.
* @param tx
*/
signTransaction(tx: Transaction): Promise<Transaction>;
signTransaction<T extends Transaction | VersionedTransaction>(
tx: T,
): Promise<T>;

/**
* Signs all transactions with the wallet.
* @param txs
*/
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;

/**
* The PublicKey of the wallet.
*/
publicKey: PublicKey;
signAllTransactions<T extends Transaction | VersionedTransaction>(
txs: T[],
): Promise<T[]>;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/solana-contrib/src/utils/printAccountOwners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import { Provider as AnchorProvider } from "@project-serum/anchor";
// import { Provider as AnchorProvider } from "@coral-xyz/anchor";
import type { AccountInfo, Connection } from "@solana/web3.js";
import { PublicKey } from "@solana/web3.js";

Expand Down Expand Up @@ -213,12 +213,12 @@ const _transformAccountOwners = async (
if (Object.keys(base58ToResultKey).length > 0) {
const accountInfos = await gracefulGetMultipleAccountsInfo(
connection,
base58ToLookup
base58ToLookup,
);

for (const [base58, resultKey] of Object.entries(base58ToResultKey)) {
const lookupIndex = base58ToLookup.findIndex(
(p) => p.toBase58() === base58
(p) => p.toBase58() === base58,
);
if (lookupIndex >= 0) {
const accountInfo = accountInfos[lookupIndex];
Expand Down
29 changes: 23 additions & 6 deletions packages/solana-contrib/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import type {
PublicKey,
Signer,
Transaction,
VersionedTransaction,
} from "@solana/web3.js";

import type { Provider, Wallet } from "./interfaces.js";
import {
isVersionedTransaction,
type Provider,
type Wallet,
} from "./interfaces.js";
import { SolanaProvider } from "./provider.js";

/**
Expand All @@ -19,17 +24,29 @@ export class SignerWallet implements Wallet {
return this.signer.publicKey;
}

signAllTransactions(transactions: Transaction[]): Promise<Transaction[]> {
signAllTransactions<T extends Transaction | VersionedTransaction>(
txs: T[],
): Promise<T[]> {
return Promise.resolve(
transactions.map((tx) => {
tx.partialSign(this.signer);
txs.map((tx) => {
if (isVersionedTransaction(tx)) {
tx.sign([this.signer]);
} else {
tx.partialSign(this.signer);
}
return tx;
}),
);
}

signTransaction(transaction: Transaction): Promise<Transaction> {
transaction.partialSign(this.signer);
signTransaction<T extends Transaction | VersionedTransaction>(
transaction: T,
): Promise<T> {
if (isVersionedTransaction(transaction)) {
transaction.sign([this.signer]);
} else {
transaction.partialSign(this.signer);
}
return Promise.resolve(transaction);
}

Expand Down
Loading
Loading