-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define IDL for initialize permanent delegate instruction #44
Merged
lorisleiva
merged 3 commits into
solana-program:main
from
0xCipherCoder:gib-bounty-tok052717
Nov 13, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
174 changes: 174 additions & 0 deletions
174
clients/js/src/generated/instructions/initializePermanentDelegate.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,174 @@ | ||
/** | ||
* 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, | ||
getAddressDecoder, | ||
getAddressEncoder, | ||
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_PERMANENT_DELEGATE_DISCRIMINATOR = 35; | ||
|
||
export function getInitializePermanentDelegateDiscriminatorBytes() { | ||
return getU8Encoder().encode(INITIALIZE_PERMANENT_DELEGATE_DISCRIMINATOR); | ||
} | ||
|
||
export type InitializePermanentDelegateInstruction< | ||
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 InitializePermanentDelegateInstructionData = { | ||
discriminator: number; | ||
/** Authority that may sign for `Transfer`s and `Burn`s on any account */ | ||
delegate: Address; | ||
}; | ||
|
||
export type InitializePermanentDelegateInstructionDataArgs = { | ||
/** Authority that may sign for `Transfer`s and `Burn`s on any account */ | ||
delegate: Address; | ||
}; | ||
|
||
export function getInitializePermanentDelegateInstructionDataEncoder(): Encoder<InitializePermanentDelegateInstructionDataArgs> { | ||
return transformEncoder( | ||
getStructEncoder([ | ||
['discriminator', getU8Encoder()], | ||
['delegate', getAddressEncoder()], | ||
]), | ||
(value) => ({ | ||
...value, | ||
discriminator: INITIALIZE_PERMANENT_DELEGATE_DISCRIMINATOR, | ||
}) | ||
); | ||
} | ||
|
||
export function getInitializePermanentDelegateInstructionDataDecoder(): Decoder<InitializePermanentDelegateInstructionData> { | ||
return getStructDecoder([ | ||
['discriminator', getU8Decoder()], | ||
['delegate', getAddressDecoder()], | ||
]); | ||
} | ||
|
||
export function getInitializePermanentDelegateInstructionDataCodec(): Codec< | ||
InitializePermanentDelegateInstructionDataArgs, | ||
InitializePermanentDelegateInstructionData | ||
> { | ||
return combineCodec( | ||
getInitializePermanentDelegateInstructionDataEncoder(), | ||
getInitializePermanentDelegateInstructionDataDecoder() | ||
); | ||
} | ||
|
||
export type InitializePermanentDelegateInput< | ||
TAccountMint extends string = string, | ||
> = { | ||
/** The mint to initialize. */ | ||
mint: Address<TAccountMint>; | ||
delegate: InitializePermanentDelegateInstructionDataArgs['delegate']; | ||
}; | ||
|
||
export function getInitializePermanentDelegateInstruction< | ||
TAccountMint extends string, | ||
TProgramAddress extends Address = typeof TOKEN_2022_PROGRAM_ADDRESS, | ||
>( | ||
input: InitializePermanentDelegateInput<TAccountMint>, | ||
config?: { programAddress?: TProgramAddress } | ||
): InitializePermanentDelegateInstruction<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 | ||
>; | ||
|
||
// Original args. | ||
const args = { ...input }; | ||
|
||
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); | ||
const instruction = { | ||
accounts: [getAccountMeta(accounts.mint)], | ||
programAddress, | ||
data: getInitializePermanentDelegateInstructionDataEncoder().encode( | ||
args as InitializePermanentDelegateInstructionDataArgs | ||
), | ||
} as InitializePermanentDelegateInstruction<TProgramAddress, TAccountMint>; | ||
|
||
return instruction; | ||
} | ||
|
||
export type ParsedInitializePermanentDelegateInstruction< | ||
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS, | ||
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], | ||
> = { | ||
programAddress: Address<TProgram>; | ||
accounts: { | ||
/** The mint to initialize. */ | ||
mint: TAccountMetas[0]; | ||
}; | ||
data: InitializePermanentDelegateInstructionData; | ||
}; | ||
|
||
export function parseInitializePermanentDelegateInstruction< | ||
TProgram extends string, | ||
TAccountMetas extends readonly IAccountMeta[], | ||
>( | ||
instruction: IInstruction<TProgram> & | ||
IInstructionWithAccounts<TAccountMetas> & | ||
IInstructionWithData<Uint8Array> | ||
): ParsedInitializePermanentDelegateInstruction<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: getInitializePermanentDelegateInstructionDataDecoder().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
41 changes: 41 additions & 0 deletions
41
clients/js/test/extensions/permanentDelegate/initializePermanentDelegate.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,41 @@ | ||
import { Account, address, some } from '@solana/web3.js'; | ||
import test from 'ava'; | ||
import { Mint, extension, fetchMint } from '../../../src'; | ||
import { | ||
createDefaultSolanaClient, | ||
createMint, | ||
generateKeyPairSignerWithSol, | ||
} from '../../_setup'; | ||
|
||
test('it initializes a mint with permanent delegate', async (t) => { | ||
// Given some signer accounts | ||
const client = createDefaultSolanaClient(); | ||
const [authority] = await Promise.all([generateKeyPairSignerWithSol(client)]); | ||
|
||
// And a permanent delegate extension | ||
const permanentDelegate = address( | ||
'6sPR6MzvjMMP5LSZzEtTe4ZBVX9rhBmtM1dmfFtkNTbW' | ||
); | ||
const permanentDelegateExtension = extension('PermanentDelegate', { | ||
delegate: permanentDelegate, | ||
}); | ||
|
||
// When we create a mint with this extension | ||
const mintAddress = await createMint({ | ||
authority, | ||
client, | ||
extensions: [permanentDelegateExtension], | ||
payer: authority, | ||
}); | ||
|
||
// Then we expect the mint account to exist with the permanent delegate | ||
const mintAccount = await fetchMint(client.rpc, mintAddress); | ||
t.like(mintAccount, <Account<Mint>>{ | ||
address: mintAddress, | ||
data: { | ||
mintAuthority: some(authority.address), | ||
isInitialized: true, | ||
extensions: some([permanentDelegateExtension]), | ||
}, | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because here, we're testing the initialisation, would you mind using the longer version instead of the
createMint
helper?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure will update this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lorisleiva I have updated code with a longer version of mint and rebased it with the main branch. Please review.