-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
263 additions
and
0 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
155 changes: 155 additions & 0 deletions
155
clients/js/src/generated/instructions/initializeNonTransferableMint.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,155 @@ | ||
/** | ||
* This code was AUTOGENERATED using the codama library. | ||
* Please DO NOT EDIT THIS FILE, instead use visitors | ||
* to add features, then rerun codama to update it. | ||
* | ||
* @see https://github.com/codama-idl/codama | ||
*/ | ||
|
||
import { | ||
combineCodec, | ||
getStructDecoder, | ||
getStructEncoder, | ||
getU8Decoder, | ||
getU8Encoder, | ||
transformEncoder, | ||
type Address, | ||
type Codec, | ||
type Decoder, | ||
type Encoder, | ||
type IAccountMeta, | ||
type IInstruction, | ||
type IInstructionWithAccounts, | ||
type IInstructionWithData, | ||
type WritableAccount, | ||
} from '@solana/web3.js'; | ||
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs'; | ||
import { getAccountMetaFactory, type ResolvedAccount } from '../shared'; | ||
|
||
export const INITIALIZE_NON_TRANSFERABLE_MINT_DISCRIMINATOR = 32; | ||
|
||
export function getInitializeNonTransferableMintDiscriminatorBytes() { | ||
return getU8Encoder().encode(INITIALIZE_NON_TRANSFERABLE_MINT_DISCRIMINATOR); | ||
} | ||
|
||
export type InitializeNonTransferableMintInstruction< | ||
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS, | ||
TAccountMint extends string | IAccountMeta<string> = string, | ||
TRemainingAccounts extends readonly IAccountMeta<string>[] = [], | ||
> = IInstruction<TProgram> & | ||
IInstructionWithData<Uint8Array> & | ||
IInstructionWithAccounts< | ||
[ | ||
TAccountMint extends string | ||
? WritableAccount<TAccountMint> | ||
: TAccountMint, | ||
...TRemainingAccounts, | ||
] | ||
>; | ||
|
||
export type InitializeNonTransferableMintInstructionData = { | ||
discriminator: number; | ||
}; | ||
|
||
export type InitializeNonTransferableMintInstructionDataArgs = {}; | ||
|
||
export function getInitializeNonTransferableMintInstructionDataEncoder(): Encoder<InitializeNonTransferableMintInstructionDataArgs> { | ||
return transformEncoder( | ||
getStructEncoder([['discriminator', getU8Encoder()]]), | ||
(value) => ({ | ||
...value, | ||
discriminator: INITIALIZE_NON_TRANSFERABLE_MINT_DISCRIMINATOR, | ||
}) | ||
); | ||
} | ||
|
||
export function getInitializeNonTransferableMintInstructionDataDecoder(): Decoder<InitializeNonTransferableMintInstructionData> { | ||
return getStructDecoder([['discriminator', getU8Decoder()]]); | ||
} | ||
|
||
export function getInitializeNonTransferableMintInstructionDataCodec(): Codec< | ||
InitializeNonTransferableMintInstructionDataArgs, | ||
InitializeNonTransferableMintInstructionData | ||
> { | ||
return combineCodec( | ||
getInitializeNonTransferableMintInstructionDataEncoder(), | ||
getInitializeNonTransferableMintInstructionDataDecoder() | ||
); | ||
} | ||
|
||
export type InitializeNonTransferableMintInput< | ||
TAccountMint extends string = string, | ||
> = { | ||
/** The mint account to initialize. */ | ||
mint: Address<TAccountMint>; | ||
}; | ||
|
||
export function getInitializeNonTransferableMintInstruction< | ||
TAccountMint extends string, | ||
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS, | ||
>( | ||
input: InitializeNonTransferableMintInput<TAccountMint>, | ||
config?: { programAddress?: TProgramAddress } | ||
): InitializeNonTransferableMintInstruction<TProgramAddress, TAccountMint> { | ||
// Program address. | ||
const programAddress = config?.programAddress ?? TOKEN_2022_PROGRAM_ADDRESS; | ||
|
||
// Original accounts. | ||
const originalAccounts = { | ||
mint: { value: input.mint ?? null, isWritable: true }, | ||
}; | ||
const accounts = originalAccounts as Record< | ||
keyof typeof originalAccounts, | ||
ResolvedAccount | ||
>; | ||
|
||
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); | ||
const instruction = { | ||
accounts: [getAccountMeta(accounts.mint)], | ||
programAddress, | ||
data: getInitializeNonTransferableMintInstructionDataEncoder().encode({}), | ||
} as InitializeNonTransferableMintInstruction<TProgramAddress, TAccountMint>; | ||
|
||
return instruction; | ||
} | ||
|
||
export type ParsedInitializeNonTransferableMintInstruction< | ||
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS, | ||
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], | ||
> = { | ||
programAddress: Address<TProgram>; | ||
accounts: { | ||
/** The mint account to initialize. */ | ||
mint: TAccountMetas[0]; | ||
}; | ||
data: InitializeNonTransferableMintInstructionData; | ||
}; | ||
|
||
export function parseInitializeNonTransferableMintInstruction< | ||
TProgram extends string, | ||
TAccountMetas extends readonly IAccountMeta[], | ||
>( | ||
instruction: IInstruction<TProgram> & | ||
IInstructionWithAccounts<TAccountMetas> & | ||
IInstructionWithData<Uint8Array> | ||
): ParsedInitializeNonTransferableMintInstruction<TProgram, TAccountMetas> { | ||
if (instruction.accounts.length < 1) { | ||
// TODO: Coded error. | ||
throw new Error('Not enough accounts'); | ||
} | ||
let accountIndex = 0; | ||
const getNextAccount = () => { | ||
const accountMeta = instruction.accounts![accountIndex]!; | ||
accountIndex += 1; | ||
return accountMeta; | ||
}; | ||
return { | ||
programAddress: instruction.programAddress, | ||
accounts: { | ||
mint: getNextAccount(), | ||
}, | ||
data: getInitializeNonTransferableMintInstructionDataDecoder().decode( | ||
instruction.data | ||
), | ||
}; | ||
} |
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
52 changes: 52 additions & 0 deletions
52
clients/js/test/extensions/nonTransfer/initializeNonTransferableMint.test.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,52 @@ | ||
import { Account, generateKeyPairSigner, some } from '@solana/web3.js'; | ||
import test from 'ava'; | ||
import { | ||
Mint, | ||
extension, | ||
fetchMint, | ||
getInitializeNonTransferableMintInstruction, | ||
} from '../../../src'; | ||
import { | ||
createDefaultSolanaClient, | ||
generateKeyPairSignerWithSol, | ||
getCreateMintInstructions, | ||
sendAndConfirmInstructions, | ||
} from '../../_setup'; | ||
|
||
test('it initializes a non-transferable mint', async (t) => { | ||
// Given an authority and a mint account. | ||
const client = createDefaultSolanaClient(); | ||
const [authority, mint] = await Promise.all([ | ||
generateKeyPairSignerWithSol(client), | ||
generateKeyPairSigner(), | ||
]); | ||
|
||
// When we create and initialize a mint account as non-transferable | ||
const [createMintInstruction, initMintInstruction] = | ||
await getCreateMintInstructions({ | ||
authority: authority.address, | ||
client, | ||
extensions: [extension('NonTransferable', {})], | ||
mint, | ||
payer: authority, | ||
}); | ||
|
||
await sendAndConfirmInstructions(client, authority, [ | ||
createMintInstruction, | ||
getInitializeNonTransferableMintInstruction({ | ||
mint: mint.address, | ||
}), | ||
initMintInstruction, | ||
]); | ||
|
||
// Then we expect the mint to be initialized with the non-transferable extension | ||
const mintAccount = await fetchMint(client.rpc, mint.address); | ||
t.like(mintAccount, <Account<Mint>>{ | ||
address: mint.address, | ||
data: { | ||
mintAuthority: some(authority.address), | ||
isInitialized: true, | ||
extensions: some([extension('NonTransferable', {})]), | ||
}, | ||
}); | ||
}); |
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