forked from kklas/anchor-client-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add support for optional accounts with
@solana/web3.js:2
Option
…
… types - Do not mark none optional accounts as mutable
- Loading branch information
1 parent
e41f6cf
commit 0e419ce
Showing
16 changed files
with
662 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
examples/tic-tac-toe/generated-client/instructions/setupGame.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
tests/example-program-gen/exp/accounts/OptionalState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { | ||
address, | ||
Address, | ||
fetchEncodedAccount, | ||
fetchEncodedAccounts, | ||
GetAccountInfoApi, | ||
GetMultipleAccountsApi, | ||
Rpc, | ||
} from "@solana/web3.js" | ||
import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import * as borsh from "@coral-xyz/borsh" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import { borshAddress } from "../utils" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import { PROGRAM_ID } from "../programId" | ||
|
||
export interface OptionalStateFields { | ||
readonlySignerOption: boolean | ||
mutableSignerOption: boolean | ||
readonlyOption: boolean | ||
mutableOption: boolean | ||
} | ||
|
||
export interface OptionalStateJSON { | ||
readonlySignerOption: boolean | ||
mutableSignerOption: boolean | ||
readonlyOption: boolean | ||
mutableOption: boolean | ||
} | ||
|
||
export class OptionalState { | ||
readonly readonlySignerOption: boolean | ||
readonly mutableSignerOption: boolean | ||
readonly readonlyOption: boolean | ||
readonly mutableOption: boolean | ||
|
||
static readonly discriminator = Buffer.from([ | ||
182, 31, 131, 174, 98, 39, 6, 20, | ||
]) | ||
|
||
static readonly layout = borsh.struct<OptionalState>([ | ||
borsh.bool("readonlySignerOption"), | ||
borsh.bool("mutableSignerOption"), | ||
borsh.bool("readonlyOption"), | ||
borsh.bool("mutableOption"), | ||
]) | ||
|
||
constructor(fields: OptionalStateFields) { | ||
this.readonlySignerOption = fields.readonlySignerOption | ||
this.mutableSignerOption = fields.mutableSignerOption | ||
this.readonlyOption = fields.readonlyOption | ||
this.mutableOption = fields.mutableOption | ||
} | ||
|
||
static async fetch( | ||
rpc: Rpc<GetAccountInfoApi>, | ||
address: Address, | ||
programId: Address = PROGRAM_ID | ||
): Promise<OptionalState | null> { | ||
const info = await fetchEncodedAccount(rpc, address) | ||
|
||
if (!info.exists) { | ||
return null | ||
} | ||
if (info.programAddress !== programId) { | ||
throw new Error("account doesn't belong to this program") | ||
} | ||
|
||
return this.decode(Buffer.from(info.data)) | ||
} | ||
|
||
static async fetchMultiple( | ||
rpc: Rpc<GetMultipleAccountsApi>, | ||
addresses: Address[], | ||
programId: Address = PROGRAM_ID | ||
): Promise<Array<OptionalState | null>> { | ||
const infos = await fetchEncodedAccounts(rpc, addresses) | ||
|
||
return infos.map((info) => { | ||
if (!info.exists) { | ||
return null | ||
} | ||
if (info.programAddress !== programId) { | ||
throw new Error("account doesn't belong to this program") | ||
} | ||
|
||
return this.decode(Buffer.from(info.data)) | ||
}) | ||
} | ||
|
||
static decode(data: Buffer): OptionalState { | ||
if (!data.slice(0, 8).equals(OptionalState.discriminator)) { | ||
throw new Error("invalid account discriminator") | ||
} | ||
|
||
const dec = OptionalState.layout.decode(data.slice(8)) | ||
|
||
return new OptionalState({ | ||
readonlySignerOption: dec.readonlySignerOption, | ||
mutableSignerOption: dec.mutableSignerOption, | ||
readonlyOption: dec.readonlyOption, | ||
mutableOption: dec.mutableOption, | ||
}) | ||
} | ||
|
||
toJSON(): OptionalStateJSON { | ||
return { | ||
readonlySignerOption: this.readonlySignerOption, | ||
mutableSignerOption: this.mutableSignerOption, | ||
readonlyOption: this.readonlyOption, | ||
mutableOption: this.mutableOption, | ||
} | ||
} | ||
|
||
static fromJSON(obj: OptionalStateJSON): OptionalState { | ||
return new OptionalState({ | ||
readonlySignerOption: obj.readonlySignerOption, | ||
mutableSignerOption: obj.mutableSignerOption, | ||
readonlyOption: obj.readonlyOption, | ||
mutableOption: obj.mutableOption, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/example-program-gen/exp/instructions/initializeWithValues.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/example-program-gen/exp/instructions/initializeWithValues2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
Address, | ||
isSome, | ||
IAccountMeta, | ||
IAccountSignerMeta, | ||
IInstruction, | ||
Option, | ||
TransactionSigner, | ||
} from "@solana/web3.js" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import * as borsh from "@coral-xyz/borsh" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import { borshAddress } from "../utils" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import { PROGRAM_ID } from "../programId" | ||
|
||
export interface OptionalAccounts { | ||
optionalState: TransactionSigner | ||
readonlySignerOption: Option<TransactionSigner> | ||
mutableSignerOption: Option<TransactionSigner> | ||
readonlyOption: Option<Address> | ||
mutableOption: Option<Address> | ||
payer: TransactionSigner | ||
systemProgram: Address | ||
} | ||
|
||
export function optional( | ||
accounts: OptionalAccounts, | ||
programAddress: Address = PROGRAM_ID | ||
) { | ||
const keys: Array<IAccountMeta | IAccountSignerMeta> = [ | ||
{ | ||
address: accounts.optionalState.address, | ||
role: 3, | ||
signer: accounts.optionalState, | ||
}, | ||
isSome(accounts.readonlySignerOption) | ||
? { | ||
address: accounts.readonlySignerOption.value.address, | ||
role: 2, | ||
signer: accounts.readonlySignerOption.value, | ||
} | ||
: { address: programAddress, role: 0 }, | ||
isSome(accounts.mutableSignerOption) | ||
? { | ||
address: accounts.mutableSignerOption.value.address, | ||
role: 3, | ||
signer: accounts.mutableSignerOption.value, | ||
} | ||
: { address: programAddress, role: 0 }, | ||
isSome(accounts.readonlyOption) | ||
? { address: accounts.readonlyOption.value, role: 0 } | ||
: { address: programAddress, role: 0 }, | ||
isSome(accounts.mutableOption) | ||
? { address: accounts.mutableOption.value, role: 1 } | ||
: { address: programAddress, role: 0 }, | ||
{ address: accounts.payer.address, role: 3, signer: accounts.payer }, | ||
{ address: accounts.systemProgram, role: 0 }, | ||
] | ||
const identifier = Buffer.from([199, 182, 147, 252, 17, 246, 54, 225]) | ||
const data = identifier | ||
const ix: IInstruction = { accounts: keys, programAddress, data } | ||
return ix | ||
} |
Oops, something went wrong.