Skip to content

Commit

Permalink
feat: dependency inject programId with default
Browse files Browse the repository at this point in the history
  • Loading branch information
sol-mocha authored and kklas committed Jun 17, 2023
1 parent fa6f749 commit 405ace1
Show file tree
Hide file tree
Showing 18 changed files with 107 additions and 43 deletions.
10 changes: 6 additions & 4 deletions examples/basic-2/generated-client/accounts/Counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ export class Counter {

static async fetch(
c: Connection,
address: PublicKey
address: PublicKey,
programId: PublicKey = PROGRAM_ID
): Promise<Counter | null> {
const info = await c.getAccountInfo(address)

if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand All @@ -49,15 +50,16 @@ export class Counter {

static async fetchMultiple(
c: Connection,
addresses: PublicKey[]
addresses: PublicKey[],
programId: PublicKey = PROGRAM_ID
): Promise<Array<Counter | null>> {
const infos = await c.getMultipleAccountsInfo(addresses)

return infos.map((info) => {
if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand Down
8 changes: 6 additions & 2 deletions examples/basic-2/generated-client/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicKey } from "@solana/web3.js"
import { PROGRAM_ID } from "../programId"
import * as anchor from "./anchor"

Expand All @@ -17,7 +18,10 @@ function hasOwnProperty<X extends object, Y extends PropertyKey>(

const errorRe = /Program (\w+) failed: custom program error: (\w+)/

export function fromTxError(err: unknown): anchor.AnchorError | null {
export function fromTxError(
err: unknown,
programId: PublicKey = PROGRAM_ID
): anchor.AnchorError | null {
if (
typeof err !== "object" ||
err === null ||
Expand All @@ -40,7 +44,7 @@ export function fromTxError(err: unknown): anchor.AnchorError | null {
}

const [programIdRaw, codeRaw] = firstMatch.slice(1)
if (programIdRaw !== PROGRAM_ID.toString()) {
if (programIdRaw !== programId.toString()) {
return null
}

Expand Down
8 changes: 6 additions & 2 deletions examples/basic-2/generated-client/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export interface CreateAccounts {

export const layout = borsh.struct([borsh.publicKey("authority")])

export function create(args: CreateArgs, accounts: CreateAccounts) {
export function create(
args: CreateArgs,
accounts: CreateAccounts,
programId: PublicKey = PROGRAM_ID
) {
const keys: Array<AccountMeta> = [
{ pubkey: accounts.counter, isSigner: true, isWritable: true },
{ pubkey: accounts.user, isSigner: true, isWritable: true },
Expand All @@ -30,6 +34,6 @@ export function create(args: CreateArgs, accounts: CreateAccounts) {
buffer
)
const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len)
const ix = new TransactionInstruction({ keys, programId: PROGRAM_ID, data })
const ix = new TransactionInstruction({ keys, programId, data })
return ix
}
7 changes: 5 additions & 2 deletions examples/basic-2/generated-client/instructions/increment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export interface IncrementAccounts {
authority: PublicKey
}

export function increment(accounts: IncrementAccounts) {
export function increment(
accounts: IncrementAccounts,
programId: PublicKey = PROGRAM_ID
) {
const keys: Array<AccountMeta> = [
{ pubkey: accounts.counter, isSigner: false, isWritable: true },
{ pubkey: accounts.authority, isSigner: true, isWritable: false },
]
const identifier = Buffer.from([11, 18, 104, 9, 104, 174, 59, 33])
const data = identifier
const ix = new TransactionInstruction({ keys, programId: PROGRAM_ID, data })
const ix = new TransactionInstruction({ keys, programId, data })
return ix
}
13 changes: 9 additions & 4 deletions examples/tic-tac-toe/generated-client/accounts/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ export class Game {
this.state = fields.state
}

static async fetch(c: Connection, address: PublicKey): Promise<Game | null> {
static async fetch(
c: Connection,
address: PublicKey,
programId: PublicKey = PROGRAM_ID
): Promise<Game | null> {
const info = await c.getAccountInfo(address)

if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand All @@ -57,15 +61,16 @@ export class Game {

static async fetchMultiple(
c: Connection,
addresses: PublicKey[]
addresses: PublicKey[],
programId: PublicKey = PROGRAM_ID
): Promise<Array<Game | null>> {
const infos = await c.getMultipleAccountsInfo(addresses)

return infos.map((info) => {
if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand Down
6 changes: 4 additions & 2 deletions examples/tic-tac-toe/generated-client/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicKey } from "@solana/web3.js"
import { PROGRAM_ID } from "../programId"
import * as anchor from "./anchor"
import * as custom from "./custom"
Expand All @@ -21,7 +22,8 @@ function hasOwnProperty<X extends object, Y extends PropertyKey>(
const errorRe = /Program (\w+) failed: custom program error: (\w+)/

export function fromTxError(
err: unknown
err: unknown,
programId: PublicKey = PROGRAM_ID
): custom.CustomError | anchor.AnchorError | null {
if (
typeof err !== "object" ||
Expand All @@ -45,7 +47,7 @@ export function fromTxError(
}

const [programIdRaw, codeRaw] = firstMatch.slice(1)
if (programIdRaw !== PROGRAM_ID.toString()) {
if (programIdRaw !== programId.toString()) {
return null
}

Expand Down
8 changes: 6 additions & 2 deletions examples/tic-tac-toe/generated-client/instructions/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export interface PlayAccounts {

export const layout = borsh.struct([types.Tile.layout("tile")])

export function play(args: PlayArgs, accounts: PlayAccounts) {
export function play(
args: PlayArgs,
accounts: PlayAccounts,
programId: PublicKey = PROGRAM_ID
) {
const keys: Array<AccountMeta> = [
{ pubkey: accounts.game, isSigner: false, isWritable: true },
{ pubkey: accounts.player, isSigner: true, isWritable: false },
Expand All @@ -29,6 +33,6 @@ export function play(args: PlayArgs, accounts: PlayAccounts) {
buffer
)
const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len)
const ix = new TransactionInstruction({ keys, programId: PROGRAM_ID, data })
const ix = new TransactionInstruction({ keys, programId, data })
return ix
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export interface SetupGameAccounts {

export const layout = borsh.struct([borsh.publicKey("playerTwo")])

export function setupGame(args: SetupGameArgs, accounts: SetupGameAccounts) {
export function setupGame(
args: SetupGameArgs,
accounts: SetupGameAccounts,
programId: PublicKey = PROGRAM_ID
) {
const keys: Array<AccountMeta> = [
{ pubkey: accounts.game, isSigner: true, isWritable: true },
{ pubkey: accounts.playerOne, isSigner: true, isWritable: true },
Expand All @@ -31,6 +35,6 @@ export function setupGame(args: SetupGameArgs, accounts: SetupGameAccounts) {
buffer
)
const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len)
const ix = new TransactionInstruction({ keys, programId: PROGRAM_ID, data })
const ix = new TransactionInstruction({ keys, programId, data })
return ix
}
14 changes: 12 additions & 2 deletions src/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ function genAccountFiles(
name: "address",
type: "PublicKey",
},
{
name: "programId",
type: "PublicKey",
initializer: "PROGRAM_ID"
}
],
returnType: `Promise<${name} | null>`,
statements: [
Expand All @@ -187,7 +192,7 @@ function genAccountFiles(
writer.inlineBlock(() => {
writer.writeLine("return null")
})
writer.write("if (!info.owner.equals(PROGRAM_ID))")
writer.write("if (!info.owner.equals(programId))")
writer.inlineBlock(() => {
writer.writeLine(
`throw new Error("account doesn't belong to this program")`
Expand All @@ -213,6 +218,11 @@ function genAccountFiles(
name: "addresses",
type: "PublicKey[]",
},
{
name: "programId",
type: "PublicKey",
initializer: "PROGRAM_ID"
}
],
returnType: `Promise<Array<${name} | null>>`,
statements: [
Expand All @@ -229,7 +239,7 @@ function genAccountFiles(
})
writer.write("")

writer.write("if (!info.owner.equals(PROGRAM_ID))")
writer.write("if (!info.owner.equals(programId))")
writer.inlineBlock(() => {
writer.writeLine(
`throw new Error("account doesn't belong to this program")`
Expand Down
11 changes: 9 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function genIndex(
})

const hasCustomErrors = idl.errors && idl.errors.length > 0

src.addStatements([
`import { PublicKey } from "@solana/web3.js"`,
]);
src.addImportDeclaration({
namedImports: ["PROGRAM_ID"],
moduleSpecifier: "../programId",
Expand Down Expand Up @@ -112,6 +114,11 @@ export function genIndex(
name: "err",
type: "unknown",
},
{
name: "programId",
type: "PublicKey",
initializer: "PROGRAM_ID"
}
],
returnType: hasCustomErrors
? "custom.CustomError | anchor.AnchorError | null"
Expand Down Expand Up @@ -139,7 +146,7 @@ if (firstMatch === null) {
}
const [programIdRaw, codeRaw] = firstMatch.slice(1)
if (programIdRaw !== PROGRAM_ID.toString()) {
if (programIdRaw !== programId.toString()) {
return null
}
Expand Down
7 changes: 6 additions & 1 deletion src/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ function genInstructionFiles(
type: accountsInterfaceName(ix.name),
})
}
ixFn.addParameter({
name: "programId",
type: "PublicKey",
initializer: "PROGRAM_ID"
});

// keys
ixFn.addVariableStatement({
Expand Down Expand Up @@ -289,7 +294,7 @@ function genInstructionFiles(
{
name: "ix",
initializer:
"new TransactionInstruction({ keys, programId: PROGRAM_ID, data })",
"new TransactionInstruction({ keys, programId, data })",
},
],
})
Expand Down
13 changes: 9 additions & 4 deletions tests/example-program-gen/exp/accounts/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,17 @@ export class State {
this.enumField4 = fields.enumField4
}

static async fetch(c: Connection, address: PublicKey): Promise<State | null> {
static async fetch(
c: Connection,
address: PublicKey,
programId: PublicKey = PROGRAM_ID
): Promise<State | null> {
const info = await c.getAccountInfo(address)

if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand All @@ -176,15 +180,16 @@ export class State {

static async fetchMultiple(
c: Connection,
addresses: PublicKey[]
addresses: PublicKey[],
programId: PublicKey = PROGRAM_ID
): Promise<Array<State | null>> {
const infos = await c.getMultipleAccountsInfo(addresses)

return infos.map((info) => {
if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand Down
10 changes: 6 additions & 4 deletions tests/example-program-gen/exp/accounts/State2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ export class State2 {

static async fetch(
c: Connection,
address: PublicKey
address: PublicKey,
programId: PublicKey = PROGRAM_ID
): Promise<State2 | null> {
const info = await c.getAccountInfo(address)

if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand All @@ -45,15 +46,16 @@ export class State2 {

static async fetchMultiple(
c: Connection,
addresses: PublicKey[]
addresses: PublicKey[],
programId: PublicKey = PROGRAM_ID
): Promise<Array<State2 | null>> {
const infos = await c.getMultipleAccountsInfo(addresses)

return infos.map((info) => {
if (info === null) {
return null
}
if (!info.owner.equals(PROGRAM_ID)) {
if (!info.owner.equals(programId)) {
throw new Error("account doesn't belong to this program")
}

Expand Down
Loading

0 comments on commit 405ace1

Please sign in to comment.