Skip to content

Commit

Permalink
migrate to coral, update wallet adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Mar 24, 2024
1 parent 95b5b7c commit 04f60f2
Show file tree
Hide file tree
Showing 27 changed files with 231 additions and 190 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"@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",
Expand Down
4 changes: 2 additions & 2 deletions packages/anchor-contrib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"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",
"@coral-xyz/anchor": "^0.29.0",
"@saberhq/tsconfig": "^3.1.1",
"@solana/web3.js": "^1.87.6",
"@types/lodash.camelcase": "^4.3.9",
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
4 changes: 2 additions & 2 deletions packages/chai-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
"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",
"@coral-xyz/anchor": "^0.29.0",
"@saberhq/tsconfig": "^3.1.1",
"@solana/web3.js": "^1.87.6",
"bn.js": "^5.2.1",
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
41 changes: 12 additions & 29 deletions packages/solana-contrib/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,45 @@ import type {
Signer,
SimulatedTransactionResponse,
Transaction,
TransactionVersion,
} from "@solana/web3.js";
import { VersionedTransaction } from "@solana/web3.js";

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

export type SupportedTransactionVersions = Set<TransactionVersion> | null;

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

export const ALL_TRANSACTION_VERSIONS: Set<TransactionVersion> =
new Set<TransactionVersion>(["legacy", 0]);

export type TransactionOrVersionedTransaction<
S extends SupportedTransactionVersions,
> = S extends null ? Transaction : Transaction | 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;

export interface MultiVersionWalletAdapter<
V extends SupportedTransactionVersions,
> {
readonly supportedTransactionVersions: V;
/**
* Signs a transaction with the wallet.
* @param tx
*/
signTransaction<T extends TransactionOrVersionedTransaction<V>>(
signTransaction<T extends Transaction | VersionedTransaction>(
tx: T,
): Promise<T>;

/**
* Signs all transactions with the wallet.
* @param txs
*/
signAllTransactions<T extends TransactionOrVersionedTransaction<V>>(
signAllTransactions<T extends Transaction | VersionedTransaction>(
txs: T[],
): Promise<T[]>;
}

/**
* Wallet interface for objects that can be used to sign provider transactions.
*
* This interface comes from Anchor.
*/
export interface Wallet<
V extends SupportedTransactionVersions = SupportedTransactionVersions,
> extends MultiVersionWalletAdapter<V> {
/**
* The PublicKey of the wallet.
*/
publicKey: PublicKey;
}

/**
* Request to send a transaction.
*/
Expand Down
2 changes: 0 additions & 2 deletions packages/solana-contrib/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { default as invariant } from "tiny-invariant";
import { SingleConnectionBroadcaster } from "./broadcaster/index.js";
import type { Broadcaster, ReadonlyProvider } from "./index.js";
import {
ALL_TRANSACTION_VERSIONS,
PendingTransaction,
SignerWallet,
TieredBroadcaster,
Expand Down Expand Up @@ -58,7 +57,6 @@ export class SolanaReadonlyProvider implements ReadonlyProvider {
}

wallet: Wallet = {
supportedTransactionVersions: ALL_TRANSACTION_VERSIONS,
signTransaction: Promise.resolve.bind(Promise),
signAllTransactions: Promise.resolve.bind(Promise),
publicKey: DEFAULT_READONLY_PUBLIC_KEY,
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
21 changes: 9 additions & 12 deletions packages/solana-contrib/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@ import type {
Connection,
PublicKey,
Signer,
Transaction,
VersionedTransaction,
} from "@solana/web3.js";

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

/**
* Wallet based on a Signer.
*/
export class SignerWallet implements Wallet<SupportedTransactionVersions> {
export class SignerWallet implements Wallet {
constructor(readonly signer: Signer) {}

get publicKey(): PublicKey {
return this.signer.publicKey;
}

readonly supportedTransactionVersions = ALL_TRANSACTION_VERSIONS;

signAllTransactions<
T extends TransactionOrVersionedTransaction<SupportedTransactionVersions>,
>(txs: T[]): Promise<T[]> {
signAllTransactions<T extends Transaction | VersionedTransaction>(
txs: T[],
): Promise<T[]> {
return Promise.resolve(
txs.map((tx) => {
if (isVersionedTransaction(tx)) {
Expand All @@ -42,9 +39,9 @@ export class SignerWallet implements Wallet<SupportedTransactionVersions> {
);
}

signTransaction<
T extends TransactionOrVersionedTransaction<SupportedTransactionVersions>,
>(transaction: T): Promise<T> {
signTransaction<T extends Transaction | VersionedTransaction>(
transaction: T,
): Promise<T> {
if (isVersionedTransaction(transaction)) {
transaction.sign([this.signer]);
} else {
Expand Down
Loading

0 comments on commit 04f60f2

Please sign in to comment.