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

GIB💎WORK Bounty - Pull Request Made To anchor #2751

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Remove deprecated literal constraint which has been replaced by `#[account(constraint = {})]` ([#2379](https://github.com/coral-xyz/anchor/pull/2379)).
- lang: `account(zero_copy)` and `zero_copy` attributes now derive the `bytemuck::Pod` and `bytemuck::Zeroable` traits instead of using `unsafe impl` ([#2330](https://github.com/coral-xyz/anchor/pull/2330)). This imposes useful restrictions on the type, like not having padding bytes and all fields being `Pod` themselves. See [bytemuck::Pod](https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html) for details. This change requires adding `bytemuck = { version = "1.4.0", features = ["derive", "min_const_generics"]}` to your `cargo.toml`. Legacy applications can still use `#[account(zero_copy(unsafe))]` and `#[zero_copy(unsafe)]` for the old behavior.
- ts: Remove `createProgramAddressSync`, `findProgramAddressSync` (now available in `@solana/web3.js`) and update `associatedAddress` to be synchronous ([#2357](https://github.com/coral-xyz/anchor/pull/2357)).
- ts: Add support for `VersionedTransaction` to `AnchorProvider`, `Transaction` is no longer accepted as an argument ([#2382](https://github.com/coral-xyz/anchor/pull/2382)).

## [0.26.0] - 2022-12-15

Expand Down
2 changes: 1 addition & 1 deletion ts/packages/anchor/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export type IdlPda = {
programId?: IdlSeed;
};

export type IdlSeed = any; // TODO
export type IdlSeed = string | number[] | string[];

// A nested/recursive version of IdlAccount.
export type IdlAccounts = {
Expand Down
14 changes: 9 additions & 5 deletions ts/packages/anchor/src/nodewallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Buffer } from "buffer";
import { Keypair, PublicKey, Transaction } from "@solana/web3.js";
import { Keypair, PublicKey, VersionedTransaction } from "@solana/web3.js";
import { Wallet } from "./provider";

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

async signTransaction(tx: Transaction): Promise<Transaction> {
tx.partialSign(this.payer);
async signTransaction(
tx: VersionedTransaction
): Promise<VersionedTransaction> {
tx.sign([this.payer]);
return tx;
}

async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
async signAllTransactions(
txs: VersionedTransaction[]
): Promise<VersionedTransaction[]> {
return txs.map((t) => {
t.partialSign(this.payer);
t.sign([this.payer]);
return t;
});
}
Expand Down
35 changes: 21 additions & 14 deletions ts/packages/anchor/src/program/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Coder, BorshCoder } from "../coder/index.js";
import NamespaceFactory, {
RpcNamespace,
InstructionNamespace,
TransactionNamespace,
TransactionInstructionsNamespace,
AccountNamespace,
SimulateNamespace,
MethodsNamespace,
Expand Down Expand Up @@ -138,13 +138,13 @@ export class Program<IDL extends Idl = Idl> {
readonly instruction: InstructionNamespace<IDL>;

/**
* The namespace provides functions to build [[Transaction]] objects for each
* The namespace provides functions to build [[TransactionInstruction\[\]]] objects for each
* method of a program.
*
* ## Usage
*
* ```javascript
* program.transaction.<method>(...args, ctx);
* program.transactionInstructions.<method>(...args, ctx);
* ```
*
* ## Parameters
Expand All @@ -159,15 +159,15 @@ export class Program<IDL extends Idl = Idl> {
* To create an instruction for the `increment` method above,
*
* ```javascript
* const tx = await program.transaction.increment({
* const tx = await program.transactionInstructions.increment({
* accounts: {
* counter,
* },
* });
* ```
* @deprecated
*/
readonly transaction: TransactionNamespace<IDL>;
readonly transactionInstructions: TransactionInstructionsNamespace<IDL>;

/**
* The namespace provides functions to simulate transactions for each method
Expand Down Expand Up @@ -283,17 +283,24 @@ export class Program<IDL extends Idl = Idl> {
this._events = new EventManager(this._programId, provider, this._coder);

// Dynamic namespaces.
const [rpc, instruction, transaction, account, simulate, methods, views] =
NamespaceFactory.build(
idl,
this._coder,
programId,
provider,
getCustomResolver ?? (() => undefined)
);
const [
rpc,
instruction,
transactionInstructions,
account,
simulate,
methods,
views,
] = NamespaceFactory.build(
idl,
this._coder,
programId,
provider,
getCustomResolver ?? (() => undefined)
);
this.rpc = rpc;
this.instruction = instruction;
this.transaction = transaction;
this.transactionInstructions = transactionInstructions;
this.account = account;
this.simulate = simulate;
this.methods = methods;
Expand Down
25 changes: 15 additions & 10 deletions ts/packages/anchor/src/program/namespace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { Coder } from "../../coder/index.js";
import Provider from "../../provider.js";
import { Idl, IdlInstruction } from "../../idl.js";
import InstructionFactory, { InstructionNamespace } from "./instruction.js";
import TransactionFactory, { TransactionNamespace } from "./transaction.js";
import TransactionInstructionsFactory, {
TransactionInstructionsNamespace,
} from "./transaction-instructions.js";
import RpcFactory, { RpcNamespace } from "./rpc.js";
import AccountFactory, { AccountNamespace } from "./account.js";
import SimulateFactory, { SimulateNamespace } from "./simulate.js";
Expand All @@ -15,7 +17,10 @@ import { CustomAccountResolver } from "../accounts-resolver.js";

// Re-exports.
export { InstructionNamespace, InstructionFn } from "./instruction.js";
export { TransactionNamespace, TransactionFn } from "./transaction.js";
export {
TransactionInstructionsNamespace,
TransactionInstructionsFn,
} from "./transaction-instructions.js";
export { RpcNamespace, RpcFn } from "./rpc.js";
export { AccountNamespace, AccountClient, ProgramAccount } from "./account.js";
export { SimulateNamespace, SimulateFn } from "./simulate.js";
Expand All @@ -38,15 +43,15 @@ export default class NamespaceFactory {
): [
RpcNamespace<IDL>,
InstructionNamespace<IDL>,
TransactionNamespace<IDL>,
TransactionInstructionsNamespace<IDL>,
AccountNamespace<IDL>,
SimulateNamespace<IDL>,
MethodsNamespace<IDL>,
ViewNamespace<IDL> | undefined
] {
const rpc: RpcNamespace = {};
const instruction: InstructionNamespace = {};
const transaction: TransactionNamespace = {};
const transactionInstructions: TransactionInstructionsNamespace = {};
const simulate: SimulateNamespace = {};
const methods: MethodsNamespace = {};
const view: ViewNamespace = {};
Expand All @@ -63,11 +68,11 @@ export default class NamespaceFactory {
(ixName, ix) => coder.instruction.encode(ixName, ix),
programId
);
const txItem = TransactionFactory.build(idlIx, ixItem);
const rpcItem = RpcFactory.build(idlIx, txItem, idlErrors, provider);
const txIxsItem = TransactionInstructionsFactory.build(idlIx, ixItem);
const rpcItem = RpcFactory.build(idlIx, txIxsItem, idlErrors, provider);
const simulateItem = SimulateFactory.build(
idlIx,
txItem,
txIxsItem,
idlErrors,
provider,
coder,
Expand All @@ -80,7 +85,7 @@ export default class NamespaceFactory {
programId,
idlIx,
ixItem,
txItem,
txIxsItem,
rpcItem,
simulateItem,
viewItem,
Expand All @@ -91,7 +96,7 @@ export default class NamespaceFactory {
const name = camelCase(idlIx.name);

instruction[name] = ixItem;
transaction[name] = txItem;
transactionInstructions[name] = txIxsItem;
rpc[name] = rpcItem;
simulate[name] = simulateItem;
methods[name] = methodItem;
Expand All @@ -103,7 +108,7 @@ export default class NamespaceFactory {
return [
rpc as RpcNamespace<IDL>,
instruction as InstructionNamespace<IDL>,
transaction as TransactionNamespace<IDL>,
transactionInstructions as TransactionInstructionsNamespace<IDL>,
account,
simulate as SimulateNamespace<IDL>,
methods as MethodsNamespace<IDL>,
Expand Down
13 changes: 6 additions & 7 deletions ts/packages/anchor/src/program/namespace/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ConfirmOptions,
PublicKey,
Signer,
Transaction,
TransactionInstruction,
TransactionSignature,
} from "@solana/web3.js";
Expand All @@ -20,7 +19,7 @@ import { AccountNamespace } from "./account.js";
import { InstructionFn } from "./instruction.js";
import { RpcFn } from "./rpc.js";
import { SimulateFn, SimulateResponse } from "./simulate.js";
import { TransactionFn } from "./transaction.js";
import { TransactionInstructionsFn } from "./transaction-instructions.js";
import {
AllInstructions,
InstructionAccountAddresses,
Expand All @@ -40,7 +39,7 @@ export class MethodsBuilderFactory {
programId: PublicKey,
idlIx: AllInstructions<IDL>,
ixFn: InstructionFn<IDL>,
txFn: TransactionFn<IDL>,
txIxsFn: TransactionInstructionsFn<IDL>,
rpcFn: RpcFn<IDL>,
simulateFn: SimulateFn<IDL>,
viewFn: ViewFn<IDL> | undefined,
Expand All @@ -52,7 +51,7 @@ export class MethodsBuilderFactory {
new MethodsBuilder(
args,
ixFn,
txFn,
txIxsFn,
rpcFn,
simulateFn,
viewFn,
Expand Down Expand Up @@ -121,7 +120,7 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
constructor(
_args: Array<any>,
private _ixFn: InstructionFn<IDL>,
private _txFn: TransactionFn<IDL>,
private _txIxsFn: TransactionInstructionsFn<IDL>,
private _rpcFn: RpcFn<IDL>,
private _simulateFn: SimulateFn<IDL>,
private _viewFn: ViewFn<IDL> | undefined,
Expand Down Expand Up @@ -299,13 +298,13 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
};
}

public async transaction(): Promise<Transaction> {
public async transactionInstructions(): Promise<TransactionInstruction[]> {
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}

// @ts-ignore
return this._txFn(...this._args, {
return this._txIxsFn(...this._args, {
accounts: this._accounts,
signers: this._signers,
remainingAccounts: this._remainingAccounts,
Expand Down
8 changes: 4 additions & 4 deletions ts/packages/anchor/src/program/namespace/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TransactionSignature } from "@solana/web3.js";
import Provider from "../../provider.js";
import { Idl } from "../../idl.js";
import { splitArgsAndCtx } from "../context.js";
import { TransactionFn } from "./transaction.js";
import { TransactionInstructionsFn } from "./transaction-instructions.js";
import { translateError } from "../../error.js";
import {
AllInstructions,
Expand All @@ -13,12 +13,12 @@ import {
export default class RpcFactory {
public static build<IDL extends Idl, I extends AllInstructions<IDL>>(
idlIx: I,
txFn: TransactionFn<IDL, I>,
txIxsFn: TransactionInstructionsFn<IDL, I>,
idlErrors: Map<number, string>,
provider: Provider
): RpcFn {
const rpc: RpcFn<IDL, I> = async (...args) => {
const tx = txFn(...args);
const txIxs = txIxsFn(...args);
const [, ctx] = splitArgsAndCtx(idlIx, [...args]);
if (provider.sendAndConfirm === undefined) {
throw new Error(
Expand All @@ -27,7 +27,7 @@ export default class RpcFactory {
}
try {
return await provider.sendAndConfirm(
tx,
txIxs,
ctx.signers ?? [],
ctx.options
);
Expand Down
6 changes: 3 additions & 3 deletions ts/packages/anchor/src/program/namespace/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PublicKey } from "@solana/web3.js";
import Provider from "../../provider.js";
import { SuccessfulTxSimulationResponse } from "src/utils/rpc.js";
import { splitArgsAndCtx } from "../context.js";
import { TransactionFn } from "./transaction.js";
import { TransactionInstructionsFn } from "./transaction-instructions.js";
import { EventParser, Event } from "../event.js";
import { Coder } from "../../coder/index.js";
import { Idl, IdlEvent } from "../../idl.js";
Expand All @@ -17,15 +17,15 @@ import {
export default class SimulateFactory {
public static build<IDL extends Idl, I extends AllInstructions<IDL>>(
idlIx: AllInstructions<IDL>,
txFn: TransactionFn<IDL>,
txIxsFn: TransactionInstructionsFn<IDL>,
idlErrors: Map<number, string>,
provider: Provider,
coder: Coder,
programId: PublicKey,
idl: IDL
): SimulateFn<IDL, I> {
const simulate: SimulateFn<IDL> = async (...args) => {
const tx = txFn(...args);
const tx = txIxsFn(...args);
const [, ctx] = splitArgsAndCtx(idlIx, [...args]);
let resp: SuccessfulTxSimulationResponse | undefined = undefined;
if (provider.simulate === undefined) {
Expand Down
Loading