Skip to content

Commit

Permalink
Rename some confidential transfer instructions (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Oct 7, 2024
1 parent 6770ecb commit 99b7dfd
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ import {
import { TOKEN_2022_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const APPROVE_CONFIDENTIAL_ACCOUNT_DISCRIMINATOR = 27;
export const APPROVE_CONFIDENTIAL_TRANSFER_ACCOUNT_DISCRIMINATOR = 27;

export function getApproveConfidentialAccountDiscriminatorBytes() {
return getU8Encoder().encode(APPROVE_CONFIDENTIAL_ACCOUNT_DISCRIMINATOR);
export function getApproveConfidentialTransferAccountDiscriminatorBytes() {
return getU8Encoder().encode(
APPROVE_CONFIDENTIAL_TRANSFER_ACCOUNT_DISCRIMINATOR
);
}

export const APPROVE_CONFIDENTIAL_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR = 3;
export const APPROVE_CONFIDENTIAL_TRANSFER_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR = 3;

export function getApproveConfidentialAccountConfidentialTransferDiscriminatorBytes() {
export function getApproveConfidentialTransferAccountConfidentialTransferDiscriminatorBytes() {
return getU8Encoder().encode(
APPROVE_CONFIDENTIAL_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR
APPROVE_CONFIDENTIAL_TRANSFER_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR
);
}

export type ApproveConfidentialAccountInstruction<
export type ApproveConfidentialTransferAccountInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken extends string | IAccountMeta<string> = string,
TAccountMint extends string | IAccountMeta<string> = string,
Expand All @@ -68,46 +70,46 @@ export type ApproveConfidentialAccountInstruction<
]
>;

export type ApproveConfidentialAccountInstructionData = {
export type ApproveConfidentialTransferAccountInstructionData = {
discriminator: number;
confidentialTransferDiscriminator: number;
};

export type ApproveConfidentialAccountInstructionDataArgs = {};
export type ApproveConfidentialTransferAccountInstructionDataArgs = {};

export function getApproveConfidentialAccountInstructionDataEncoder(): Encoder<ApproveConfidentialAccountInstructionDataArgs> {
export function getApproveConfidentialTransferAccountInstructionDataEncoder(): Encoder<ApproveConfidentialTransferAccountInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['confidentialTransferDiscriminator', getU8Encoder()],
]),
(value) => ({
...value,
discriminator: APPROVE_CONFIDENTIAL_ACCOUNT_DISCRIMINATOR,
discriminator: APPROVE_CONFIDENTIAL_TRANSFER_ACCOUNT_DISCRIMINATOR,
confidentialTransferDiscriminator:
APPROVE_CONFIDENTIAL_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR,
APPROVE_CONFIDENTIAL_TRANSFER_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR,
})
);
}

export function getApproveConfidentialAccountInstructionDataDecoder(): Decoder<ApproveConfidentialAccountInstructionData> {
export function getApproveConfidentialTransferAccountInstructionDataDecoder(): Decoder<ApproveConfidentialTransferAccountInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['confidentialTransferDiscriminator', getU8Decoder()],
]);
}

export function getApproveConfidentialAccountInstructionDataCodec(): Codec<
ApproveConfidentialAccountInstructionDataArgs,
ApproveConfidentialAccountInstructionData
export function getApproveConfidentialTransferAccountInstructionDataCodec(): Codec<
ApproveConfidentialTransferAccountInstructionDataArgs,
ApproveConfidentialTransferAccountInstructionData
> {
return combineCodec(
getApproveConfidentialAccountInstructionDataEncoder(),
getApproveConfidentialAccountInstructionDataDecoder()
getApproveConfidentialTransferAccountInstructionDataEncoder(),
getApproveConfidentialTransferAccountInstructionDataDecoder()
);
}

export type ApproveConfidentialAccountInput<
export type ApproveConfidentialTransferAccountInput<
TAccountToken extends string = string,
TAccountMint extends string = string,
TAccountAuthority extends string = string,
Expand All @@ -120,17 +122,17 @@ export type ApproveConfidentialAccountInput<
authority: TransactionSigner<TAccountAuthority>;
};

export function getApproveConfidentialAccountInstruction<
export function getApproveConfidentialTransferAccountInstruction<
TAccountToken extends string,
TAccountMint extends string,
TAccountAuthority extends string,
>(
input: ApproveConfidentialAccountInput<
input: ApproveConfidentialTransferAccountInput<
TAccountToken,
TAccountMint,
TAccountAuthority
>
): ApproveConfidentialAccountInstruction<
): ApproveConfidentialTransferAccountInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken,
TAccountMint,
Expand Down Expand Up @@ -158,8 +160,10 @@ export function getApproveConfidentialAccountInstruction<
getAccountMeta(accounts.authority),
],
programAddress,
data: getApproveConfidentialAccountInstructionDataEncoder().encode({}),
} as ApproveConfidentialAccountInstruction<
data: getApproveConfidentialTransferAccountInstructionDataEncoder().encode(
{}
),
} as ApproveConfidentialTransferAccountInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken,
TAccountMint,
Expand All @@ -169,7 +173,7 @@ export function getApproveConfidentialAccountInstruction<
return instruction;
}

export type ParsedApproveConfidentialAccountInstruction<
export type ParsedApproveConfidentialTransferAccountInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
Expand All @@ -182,17 +186,20 @@ export type ParsedApproveConfidentialAccountInstruction<
/** Confidential transfer mint authority. */
authority: TAccountMetas[2];
};
data: ApproveConfidentialAccountInstructionData;
data: ApproveConfidentialTransferAccountInstructionData;
};

export function parseApproveConfidentialAccountInstruction<
export function parseApproveConfidentialTransferAccountInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedApproveConfidentialAccountInstruction<TProgram, TAccountMetas> {
): ParsedApproveConfidentialTransferAccountInstruction<
TProgram,
TAccountMetas
> {
if (instruction.accounts.length < 3) {
// TODO: Coded error.
throw new Error('Not enough accounts');
Expand All @@ -210,7 +217,7 @@ export function parseApproveConfidentialAccountInstruction<
mint: getNextAccount(),
authority: getNextAccount(),
},
data: getApproveConfidentialAccountInstructionDataDecoder().decode(
data: getApproveConfidentialTransferAccountInstructionDataDecoder().decode(
instruction.data
),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,23 @@ import {
type DecryptableBalanceArgs,
} from '../types';

export const CONFIGURE_CONFIDENTIAL_ACCOUNT_DISCRIMINATOR = 27;
export const CONFIGURE_CONFIDENTIAL_TRANSFER_ACCOUNT_DISCRIMINATOR = 27;

export function getConfigureConfidentialAccountDiscriminatorBytes() {
return getU8Encoder().encode(CONFIGURE_CONFIDENTIAL_ACCOUNT_DISCRIMINATOR);
export function getConfigureConfidentialTransferAccountDiscriminatorBytes() {
return getU8Encoder().encode(
CONFIGURE_CONFIDENTIAL_TRANSFER_ACCOUNT_DISCRIMINATOR
);
}

export const CONFIGURE_CONFIDENTIAL_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR = 2;
export const CONFIGURE_CONFIDENTIAL_TRANSFER_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR = 2;

export function getConfigureConfidentialAccountConfidentialTransferDiscriminatorBytes() {
export function getConfigureConfidentialTransferAccountConfidentialTransferDiscriminatorBytes() {
return getU8Encoder().encode(
CONFIGURE_CONFIDENTIAL_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR
CONFIGURE_CONFIDENTIAL_TRANSFER_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR
);
}

export type ConfigureConfidentialAccountInstruction<
export type ConfigureConfidentialTransferAccountInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken extends string | IAccountMeta<string> = string,
TAccountMint extends string | IAccountMeta<string> = string,
Expand Down Expand Up @@ -88,7 +90,7 @@ export type ConfigureConfidentialAccountInstruction<
]
>;

export type ConfigureConfidentialAccountInstructionData = {
export type ConfigureConfidentialTransferAccountInstructionData = {
discriminator: number;
confidentialTransferDiscriminator: number;
/** The decryptable balance (always 0) once the configure account succeeds. */
Expand All @@ -107,7 +109,7 @@ export type ConfigureConfidentialAccountInstructionData = {
proofInstructionOffset: number;
};

export type ConfigureConfidentialAccountInstructionDataArgs = {
export type ConfigureConfidentialTransferAccountInstructionDataArgs = {
/** The decryptable balance (always 0) once the configure account succeeds. */
decryptableZeroBalance: DecryptableBalanceArgs;
/**
Expand All @@ -124,7 +126,7 @@ export type ConfigureConfidentialAccountInstructionDataArgs = {
proofInstructionOffset: number;
};

export function getConfigureConfidentialAccountInstructionDataEncoder(): Encoder<ConfigureConfidentialAccountInstructionDataArgs> {
export function getConfigureConfidentialTransferAccountInstructionDataEncoder(): Encoder<ConfigureConfidentialTransferAccountInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
Expand All @@ -135,14 +137,14 @@ export function getConfigureConfidentialAccountInstructionDataEncoder(): Encoder
]),
(value) => ({
...value,
discriminator: CONFIGURE_CONFIDENTIAL_ACCOUNT_DISCRIMINATOR,
discriminator: CONFIGURE_CONFIDENTIAL_TRANSFER_ACCOUNT_DISCRIMINATOR,
confidentialTransferDiscriminator:
CONFIGURE_CONFIDENTIAL_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR,
CONFIGURE_CONFIDENTIAL_TRANSFER_ACCOUNT_CONFIDENTIAL_TRANSFER_DISCRIMINATOR,
})
);
}

export function getConfigureConfidentialAccountInstructionDataDecoder(): Decoder<ConfigureConfidentialAccountInstructionData> {
export function getConfigureConfidentialTransferAccountInstructionDataDecoder(): Decoder<ConfigureConfidentialTransferAccountInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['confidentialTransferDiscriminator', getU8Decoder()],
Expand All @@ -152,17 +154,17 @@ export function getConfigureConfidentialAccountInstructionDataDecoder(): Decoder
]);
}

export function getConfigureConfidentialAccountInstructionDataCodec(): Codec<
ConfigureConfidentialAccountInstructionDataArgs,
ConfigureConfidentialAccountInstructionData
export function getConfigureConfidentialTransferAccountInstructionDataCodec(): Codec<
ConfigureConfidentialTransferAccountInstructionDataArgs,
ConfigureConfidentialTransferAccountInstructionData
> {
return combineCodec(
getConfigureConfidentialAccountInstructionDataEncoder(),
getConfigureConfidentialAccountInstructionDataDecoder()
getConfigureConfidentialTransferAccountInstructionDataEncoder(),
getConfigureConfidentialTransferAccountInstructionDataDecoder()
);
}

export type ConfigureConfidentialAccountInput<
export type ConfigureConfidentialTransferAccountInput<
TAccountToken extends string = string,
TAccountMint extends string = string,
TAccountInstructionsSysvarOrContextState extends string = string,
Expand All @@ -184,27 +186,27 @@ export type ConfigureConfidentialAccountInput<
record?: Address<TAccountRecord>;
/** The source account's owner/delegate or its multisignature account. */
authority: Address<TAccountAuthority> | TransactionSigner<TAccountAuthority>;
decryptableZeroBalance: ConfigureConfidentialAccountInstructionDataArgs['decryptableZeroBalance'];
maximumPendingBalanceCreditCounter: ConfigureConfidentialAccountInstructionDataArgs['maximumPendingBalanceCreditCounter'];
proofInstructionOffset: ConfigureConfidentialAccountInstructionDataArgs['proofInstructionOffset'];
decryptableZeroBalance: ConfigureConfidentialTransferAccountInstructionDataArgs['decryptableZeroBalance'];
maximumPendingBalanceCreditCounter: ConfigureConfidentialTransferAccountInstructionDataArgs['maximumPendingBalanceCreditCounter'];
proofInstructionOffset: ConfigureConfidentialTransferAccountInstructionDataArgs['proofInstructionOffset'];
multiSigners?: Array<TransactionSigner>;
};

export function getConfigureConfidentialAccountInstruction<
export function getConfigureConfidentialTransferAccountInstruction<
TAccountToken extends string,
TAccountMint extends string,
TAccountInstructionsSysvarOrContextState extends string,
TAccountRecord extends string,
TAccountAuthority extends string,
>(
input: ConfigureConfidentialAccountInput<
input: ConfigureConfidentialTransferAccountInput<
TAccountToken,
TAccountMint,
TAccountInstructionsSysvarOrContextState,
TAccountRecord,
TAccountAuthority
>
): ConfigureConfidentialAccountInstruction<
): ConfigureConfidentialTransferAccountInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken,
TAccountMint,
Expand Down Expand Up @@ -263,10 +265,10 @@ export function getConfigureConfidentialAccountInstruction<
...remainingAccounts,
],
programAddress,
data: getConfigureConfidentialAccountInstructionDataEncoder().encode(
args as ConfigureConfidentialAccountInstructionDataArgs
data: getConfigureConfidentialTransferAccountInstructionDataEncoder().encode(
args as ConfigureConfidentialTransferAccountInstructionDataArgs
),
} as ConfigureConfidentialAccountInstruction<
} as ConfigureConfidentialTransferAccountInstruction<
typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountToken,
TAccountMint,
Expand All @@ -281,7 +283,7 @@ export function getConfigureConfidentialAccountInstruction<
return instruction;
}

export type ParsedConfigureConfidentialAccountInstruction<
export type ParsedConfigureConfidentialTransferAccountInstruction<
TProgram extends string = typeof TOKEN_2022_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
Expand All @@ -304,17 +306,20 @@ export type ParsedConfigureConfidentialAccountInstruction<
/** The source account's owner/delegate or its multisignature account. */
authority: TAccountMetas[4];
};
data: ConfigureConfidentialAccountInstructionData;
data: ConfigureConfidentialTransferAccountInstructionData;
};

export function parseConfigureConfidentialAccountInstruction<
export function parseConfigureConfidentialTransferAccountInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedConfigureConfidentialAccountInstruction<TProgram, TAccountMetas> {
): ParsedConfigureConfidentialTransferAccountInstruction<
TProgram,
TAccountMetas
> {
if (instruction.accounts.length < 5) {
// TODO: Coded error.
throw new Error('Not enough accounts');
Expand All @@ -340,7 +345,7 @@ export function parseConfigureConfidentialAccountInstruction<
record: getNextOptionalAccount(),
authority: getNextAccount(),
},
data: getConfigureConfidentialAccountInstructionDataDecoder().decode(
data: getConfigureConfidentialTransferAccountInstructionDataDecoder().decode(
instruction.data
),
};
Expand Down
Loading

0 comments on commit 99b7dfd

Please sign in to comment.