Skip to content

Commit

Permalink
ts: Remove programId parameter of the Program constructor (#2864)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Mar 22, 2024
1 parent 62dccce commit 7c424ee
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- ts: Change `accounts` method to no longer accept resolvable accounts ([#2824](https://github.com/coral-xyz/anchor/pull/2824)).
- ts: `Program` instances use camelCase for everything ([#2824](https://github.com/coral-xyz/anchor/pull/2824)).
- ts: Remove discriminator functions ([#2824](https://github.com/coral-xyz/anchor/pull/2824)).
- ts: Remove `programId` parameter of the `Program` constructor ([#2864](https://github.com/coral-xyz/anchor/pull/2864)).

## [0.29.0] - 2023-10-16

Expand Down
8 changes: 1 addition & 7 deletions tests/auction-house/tests/auction-house.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,9 @@ describe("auction-house", () => {
});

it("Creates auction house program clients representing the buyer and seller", async () => {
authorityClient = new Program<AuctionHouse>(
IDL,
AUCTION_HOUSE_PROGRAM_ID,
getProvider()
);
authorityClient = new Program<AuctionHouse>(IDL, getProvider());
sellerClient = new Program<AuctionHouse>(
IDL,
AUCTION_HOUSE_PROGRAM_ID,
new AnchorProvider(
getProvider().connection,
new Wallet(sellerWallet),
Expand All @@ -145,7 +140,6 @@ describe("auction-house", () => {
);
buyerClient = new Program<AuctionHouse>(
IDL,
AUCTION_HOUSE_PROGRAM_ID,
new AnchorProvider(
getProvider().connection,
new Wallet(buyerWallet),
Expand Down
4 changes: 2 additions & 2 deletions tests/misc/tests/misc/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,9 @@ const miscTest = (
new anchor.Wallet(anchor.web3.Keypair.generate()),
{ commitment: program.provider.connection.commitment }
);

const anotherProgram = new anchor.Program(
miscIdl,
program.programId,
{ ...miscIdl, address: program.programId },
anotherProvider
);
// Request airdrop for secondary wallet.
Expand Down
1 change: 0 additions & 1 deletion tests/pda-derivation/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ describe("typescript", () => {
let called = false;
const customProgram = new Program<PdaDerivation>(
program.idl,
program.programId,
program.provider,
program.coder,
(instruction) => {
Expand Down
5 changes: 3 additions & 2 deletions tests/spl/token-proxy/tests/token-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ describe("program", () => {
const program = anchor.workspace.TokenProxy;

TOKEN_PROGRAMS.forEach((tokenProgram) => {
const name =
tokenProgram.programId === SPL_TOKEN_PROGRAM_ID ? "token" : "token-2022";
const name = tokenProgram.programId.equals(SPL_TOKEN_PROGRAM_ID)
? "token"
: "token-2022";
describe(name, () => {
let mint = null;
let from = null;
Expand Down
6 changes: 4 additions & 2 deletions ts/packages/anchor/src/native/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Program } from "../program/index.js";
import Provider from "../provider.js";
import { SystemCoder } from "../coder/system/index.js";

const SYSTEM_PROGRAM_ID = new PublicKey("11111111111111111111111111111111");
export const SYSTEM_PROGRAM_ID = new PublicKey(
"11111111111111111111111111111111"
);

export function program(provider?: Provider): Program<SystemProgram> {
return new Program<SystemProgram>(IDL, SYSTEM_PROGRAM_ID, provider, coder());
return new Program<SystemProgram>(IDL, provider, coder());
}

export function coder(): SystemCoder {
Expand Down
2 changes: 1 addition & 1 deletion ts/packages/anchor/src/program/accounts-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ class AccountStore<IDL extends Idl> {
if (!this._idls[programIdStr]) {
const idl = await Program.fetchIdl(programId, this._provider);
if (idl) {
const program = new Program(idl, programId, this._provider);
const program = new Program(idl, this._provider);
this._idls[programIdStr] = program.account;
}
}
Expand Down
10 changes: 3 additions & 7 deletions ts/packages/anchor/src/program/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ export class Program<IDL extends Idl = Idl> {

/**
* @param idl The interface definition.
* @param programId The on-chain address of the program.
* @param provider The network and wallet context to use. If not provided
* then uses [[getProvider]].
* @param getCustomResolver A function that returns a custom account resolver
Expand All @@ -281,22 +280,19 @@ export class Program<IDL extends Idl = Idl> {
*/
public constructor(
idl: IDL,
programId: Address,
provider: Provider = getProvider(),
coder?: Coder,
getCustomResolver?: (
instruction: IdlInstruction
) => CustomAccountResolver<IDL> | undefined
) {
programId = translateAddress(programId);

const camelCasedIdl = convertIdlToCamelCase(idl);

// Fields.
this._idl = camelCasedIdl;
this._rawIdl = idl;
this._provider = provider;
this._programId = programId;
this._programId = translateAddress(idl.address);
this._coder = coder ?? new BorshCoder(camelCasedIdl);
this._events = new EventManager(this._programId, provider, this._coder);

Expand All @@ -305,7 +301,7 @@ export class Program<IDL extends Idl = Idl> {
NamespaceFactory.build(
camelCasedIdl,
this._coder,
programId,
this._programId,
provider,
getCustomResolver
);
Expand Down Expand Up @@ -338,7 +334,7 @@ export class Program<IDL extends Idl = Idl> {
throw new Error(`IDL not found for program: ${address.toString()}`);
}

return new Program(idl, programId, provider);
return new Program(idl, provider);
}

/**
Expand Down
11 changes: 3 additions & 8 deletions ts/packages/anchor/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ const workspace = new Proxy(
}

const idl: Idl = JSON.parse(fs.readFileSync(idlPath));
if (!programId) {
if (!idl.address) {
throw new Error(
`IDL for program \`${programName}\` does not have \`address\` field.`
);
}
programId = idl.address;
if (programId) {
idl.address = programId;
}
workspaceCache[programName] = new Program(idl, programId);
workspaceCache[programName] = new Program(idl);

return workspaceCache[programName];
},
Expand Down
5 changes: 2 additions & 3 deletions ts/packages/spl-associated-token-account/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ export function splAssociatedTokenAccountProgram(
params?: GetProgramParams
): Program<SplAssociatedTokenAccount> {
return new Program<SplAssociatedTokenAccount>(
IDL,
params?.programId ?? SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID,
params?.programId ? { ...IDL, address: params.programId.toString() } : IDL,
params?.provider,
new SplAssociatedTokenAccountCoder(IDL)
);
}

type SplAssociatedTokenAccount = {
address: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
address: string;
metadata: {
name: "splAssociatedTokenAccount";
version: "1.1.1";
Expand Down
5 changes: 2 additions & 3 deletions ts/packages/spl-token/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ interface GetProgramParams {

export function splTokenProgram(params?: GetProgramParams): Program<SplToken> {
return new Program<SplToken>(
IDL,
params?.programId ?? SPL_TOKEN_PROGRAM_ID,
params?.programId ? { ...IDL, address: params.programId.toString() } : IDL,
params?.provider,
new SplTokenCoder(IDL)
);
}

type SplToken = {
address: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
address: string;
metadata: {
name: "splToken";
version: "3.3.0";
Expand Down

0 comments on commit 7c424ee

Please sign in to comment.