From 7c424ee58a9525567ffadb18161396ba23a987db Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Fri, 22 Mar 2024 01:51:26 +0100 Subject: [PATCH] ts: Remove `programId` parameter of the `Program` constructor (#2864) --- CHANGELOG.md | 1 + tests/auction-house/tests/auction-house.ts | 8 +------- tests/misc/tests/misc/misc.ts | 4 ++-- tests/pda-derivation/tests/typescript.spec.ts | 1 - tests/spl/token-proxy/tests/token-proxy.js | 5 +++-- ts/packages/anchor/src/native/system.ts | 6 ++++-- ts/packages/anchor/src/program/accounts-resolver.ts | 2 +- ts/packages/anchor/src/program/index.ts | 10 +++------- ts/packages/anchor/src/workspace.ts | 11 +++-------- .../spl-associated-token-account/src/program.ts | 5 ++--- ts/packages/spl-token/src/program.ts | 5 ++--- 11 files changed, 22 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4fd8d3841..73807f3835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tests/auction-house/tests/auction-house.ts b/tests/auction-house/tests/auction-house.ts index f6bb150740..dfc638a80e 100644 --- a/tests/auction-house/tests/auction-house.ts +++ b/tests/auction-house/tests/auction-house.ts @@ -129,14 +129,9 @@ describe("auction-house", () => { }); it("Creates auction house program clients representing the buyer and seller", async () => { - authorityClient = new Program( - IDL, - AUCTION_HOUSE_PROGRAM_ID, - getProvider() - ); + authorityClient = new Program(IDL, getProvider()); sellerClient = new Program( IDL, - AUCTION_HOUSE_PROGRAM_ID, new AnchorProvider( getProvider().connection, new Wallet(sellerWallet), @@ -145,7 +140,6 @@ describe("auction-house", () => { ); buyerClient = new Program( IDL, - AUCTION_HOUSE_PROGRAM_ID, new AnchorProvider( getProvider().connection, new Wallet(buyerWallet), diff --git a/tests/misc/tests/misc/misc.ts b/tests/misc/tests/misc/misc.ts index 413f637ed3..7947e82720 100644 --- a/tests/misc/tests/misc/misc.ts +++ b/tests/misc/tests/misc/misc.ts @@ -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. diff --git a/tests/pda-derivation/tests/typescript.spec.ts b/tests/pda-derivation/tests/typescript.spec.ts index 7e82343c2e..c7cd53d93b 100644 --- a/tests/pda-derivation/tests/typescript.spec.ts +++ b/tests/pda-derivation/tests/typescript.spec.ts @@ -81,7 +81,6 @@ describe("typescript", () => { let called = false; const customProgram = new Program( program.idl, - program.programId, program.provider, program.coder, (instruction) => { diff --git a/tests/spl/token-proxy/tests/token-proxy.js b/tests/spl/token-proxy/tests/token-proxy.js index c7f4e0fed4..c5826a00f9 100644 --- a/tests/spl/token-proxy/tests/token-proxy.js +++ b/tests/spl/token-proxy/tests/token-proxy.js @@ -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; diff --git a/ts/packages/anchor/src/native/system.ts b/ts/packages/anchor/src/native/system.ts index 76f1a49a71..c7b4a67744 100644 --- a/ts/packages/anchor/src/native/system.ts +++ b/ts/packages/anchor/src/native/system.ts @@ -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 { - return new Program(IDL, SYSTEM_PROGRAM_ID, provider, coder()); + return new Program(IDL, provider, coder()); } export function coder(): SystemCoder { diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 836af62e1a..842d4938e8 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -558,7 +558,7 @@ class AccountStore { 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; } } diff --git a/ts/packages/anchor/src/program/index.ts b/ts/packages/anchor/src/program/index.ts index e8c3398adc..3baa1ff73f 100644 --- a/ts/packages/anchor/src/program/index.ts +++ b/ts/packages/anchor/src/program/index.ts @@ -272,7 +272,6 @@ export class Program { /** * @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 @@ -281,22 +280,19 @@ export class Program { */ public constructor( idl: IDL, - programId: Address, provider: Provider = getProvider(), coder?: Coder, getCustomResolver?: ( instruction: IdlInstruction ) => CustomAccountResolver | 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); @@ -305,7 +301,7 @@ export class Program { NamespaceFactory.build( camelCasedIdl, this._coder, - programId, + this._programId, provider, getCustomResolver ); @@ -338,7 +334,7 @@ export class Program { throw new Error(`IDL not found for program: ${address.toString()}`); } - return new Program(idl, programId, provider); + return new Program(idl, provider); } /** diff --git a/ts/packages/anchor/src/workspace.ts b/ts/packages/anchor/src/workspace.ts index a0dd46a4cb..7ae3e8ed2f 100644 --- a/ts/packages/anchor/src/workspace.ts +++ b/ts/packages/anchor/src/workspace.ts @@ -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]; }, diff --git a/ts/packages/spl-associated-token-account/src/program.ts b/ts/packages/spl-associated-token-account/src/program.ts index b54e0a1d0b..ad12af5fe4 100644 --- a/ts/packages/spl-associated-token-account/src/program.ts +++ b/ts/packages/spl-associated-token-account/src/program.ts @@ -16,15 +16,14 @@ export function splAssociatedTokenAccountProgram( params?: GetProgramParams ): Program { return new Program( - 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"; diff --git a/ts/packages/spl-token/src/program.ts b/ts/packages/spl-token/src/program.ts index 9107bbb9c0..04642cae2b 100644 --- a/ts/packages/spl-token/src/program.ts +++ b/ts/packages/spl-token/src/program.ts @@ -14,15 +14,14 @@ interface GetProgramParams { export function splTokenProgram(params?: GetProgramParams): Program { return new Program( - 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";