Skip to content

Commit

Permalink
[token-2022] Add serde support for the rest of token-2022 instructions (
Browse files Browse the repository at this point in the history
#4772)

* token-2022: Add serde support for the rest of token-2022 instructions #3325

* token-2022: Add serde support for the rest of token-2022 instructions #3325

* revert ts file changes

* token-2022: Add serde support for the rest of token-2022 instructions #3325

* token-2022: Add serde support for the rest of token-2022 instructions #3325

* token-2022: Add serde support for the rest of token-2022 instructions #3325

* token-2022: Add serde support for the rest of token-2022 instructions #3325

---------

Co-authored-by: un <un@un>
Co-authored-by: Serban <@>
  • Loading branch information
serbangv and un authored Aug 16, 2023
1 parent ba40ffb commit 46aef02
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion token/program-2022/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exclude = ["js/**"]
[features]
no-entrypoint = []
test-sbf = []
serde-traits = ["serde", "serde_with"]
serde-traits = ["serde", "serde_with", "base64"]
# Remove these features once the underlying syscalls are released on all networks
default = ["zk-ops"]
zk-ops = []
Expand All @@ -33,6 +33,7 @@ spl-type-length-value = { version = "0.2.0", path = "../../libraries/type-length
thiserror = "1.0"
serde = { version = "1.0.183", optional = true }
serde_with = { version = "3.2.0", optional = true }
base64 = { version = "0.21.2", optional = true }

[dev-dependencies]
lazy_static = "1.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ use {
},
};

#[cfg(feature = "serde-traits")]
use {
crate::serialization::aeciphertext_fromstr,
serde::{Deserialize, Serialize},
};

/// Confidential Transfer extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum ConfidentialTransferInstruction {
Expand Down Expand Up @@ -352,6 +359,7 @@ pub enum ConfidentialTransferInstruction {
}

/// Data expected by `ConfidentialTransferInstruction::InitializeMint`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeMintData {
Expand All @@ -366,6 +374,7 @@ pub struct InitializeMintData {
}

/// Data expected by `ConfidentialTransferInstruction::UpdateMint`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct UpdateMintData {
Expand All @@ -377,10 +386,12 @@ pub struct UpdateMintData {
}

/// Data expected by `ConfidentialTransferInstruction::ConfigureAccount`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct ConfigureAccountInstructionData {
/// The decryptable balance (always 0) once the configure account succeeds
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub decryptable_zero_balance: DecryptableBalance,
/// The maximum number of despots and transfers that an account can receiver before the
/// `ApplyPendingBalance` is executed
Expand All @@ -392,6 +403,7 @@ pub struct ConfigureAccountInstructionData {
}

/// Data expected by `ConfidentialTransferInstruction::EmptyAccount`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct EmptyAccountInstructionData {
Expand All @@ -402,6 +414,7 @@ pub struct EmptyAccountInstructionData {
}

/// Data expected by `ConfidentialTransferInstruction::Deposit`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct DepositInstructionData {
Expand All @@ -412,6 +425,7 @@ pub struct DepositInstructionData {
}

/// Data expected by `ConfidentialTransferInstruction::Withdraw`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct WithdrawInstructionData {
Expand All @@ -420,6 +434,7 @@ pub struct WithdrawInstructionData {
/// Expected number of base 10 digits to the right of the decimal place
pub decimals: u8,
/// The new decryptable balance if the withdrawal succeeds
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub new_decryptable_available_balance: DecryptableBalance,
/// Relative location of the `ProofInstruction::VerifyWithdraw` instruction to the `Withdraw`
/// instruction in the transaction. If the offset is `0`, then use a context state account for
Expand All @@ -428,10 +443,12 @@ pub struct WithdrawInstructionData {
}

/// Data expected by `ConfidentialTransferInstruction::Transfer`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct TransferInstructionData {
/// The new source decryptable balance if the transfer succeeds
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub new_source_decryptable_available_balance: DecryptableBalance,
/// Relative location of the `ProofInstruction::VerifyTransfer` instruction to the
/// `Transfer` instruction in the transaction. If the offset is `0`, then use a context state
Expand All @@ -440,13 +457,15 @@ pub struct TransferInstructionData {
}

/// Data expected by `ConfidentialTransferInstruction::ApplyPendingBalance`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct ApplyPendingBalanceData {
/// The expected number of pending balance credits since the last successful
/// `ApplyPendingBalance` instruction
pub expected_pending_balance_credit_counter: PodU64,
/// The new decryptable balance if the pending balance is applied successfully
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub new_decryptable_available_balance: DecryptableBalance,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ use {
std::convert::TryFrom,
};

#[cfg(feature = "serde-traits")]
use {
crate::serialization::{aeciphertext_fromstr, elgamalpubkey_fromstr},
serde::{Deserialize, Serialize},
};

/// Confidential Transfer extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum ConfidentialTransferFeeInstruction {
Expand Down Expand Up @@ -190,17 +197,20 @@ pub enum ConfidentialTransferFeeInstruction {
}

/// Data expected by `InitializeConfidentialTransferFeeConfig`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeConfidentialTransferFeeConfigData {
/// confidential transfer fee authority
pub authority: OptionalNonZeroPubkey,

/// ElGamal public key used to encrypt withheld fees.
#[cfg_attr(feature = "serde-traits", serde(with = "elgamalpubkey_fromstr"))]
pub withdraw_withheld_authority_elgamal_pubkey: ElGamalPubkey,
}

/// Data expected by `ConfidentialTransferFeeInstruction::WithdrawWithheldTokensFromMint`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct WithdrawWithheldTokensFromMintData {
Expand All @@ -209,10 +219,12 @@ pub struct WithdrawWithheldTokensFromMintData {
/// use a context state account for the proof.
pub proof_instruction_offset: i8,
/// The new decryptable balance in the destination token account.
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub new_decryptable_available_balance: DecryptableBalance,
}

/// Data expected by `ConfidentialTransferFeeInstruction::WithdrawWithheldTokensFromAccounts`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct WithdrawWithheldTokensFromAccountsData {
Expand All @@ -223,6 +235,7 @@ pub struct WithdrawWithheldTokensFromAccountsData {
/// `0`, then use a context state account for the proof.
pub proof_instruction_offset: i8,
/// The new decryptable balance in the destination token account.
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub new_decryptable_available_balance: DecryptableBalance,
}

Expand Down
4 changes: 4 additions & 0 deletions token/program-2022/src/extension/cpi_guard/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use {
},
};

#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};

/// CPI Guard extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum CpiGuardInstruction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use {
std::convert::TryFrom,
};

#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};

/// Default Account State extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum DefaultAccountStateInstruction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ use {
std::convert::TryInto,
};

#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};

/// Interesting-bearing mint extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum InterestBearingMintInstruction {
Expand Down Expand Up @@ -57,6 +61,7 @@ pub enum InterestBearingMintInstruction {
}

/// Data expected by `InterestBearing::Initialize`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeInstructionData {
Expand Down
4 changes: 4 additions & 0 deletions token/program-2022/src/extension/memo_transfer/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use {
},
};

#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};

/// Required Memo Transfers extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum RequiredMemoTransfersInstruction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use {
std::convert::TryInto,
};

#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};

/// Metadata pointer extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum MetadataPointerInstruction {
Expand Down Expand Up @@ -56,6 +60,7 @@ pub enum MetadataPointerInstruction {
}

/// Data expected by `Initialize`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeInstructionData {
Expand All @@ -66,6 +71,7 @@ pub struct InitializeInstructionData {
}

/// Data expected by `Update`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct UpdateInstructionData {
Expand Down
6 changes: 6 additions & 0 deletions token/program-2022/src/extension/transfer_hook/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use {
std::convert::TryInto,
};

#[cfg(feature = "serde-traits")]
use serde::{Deserialize, Serialize};

/// Transfer hook extension instructions
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u8)]
pub enum TransferHookInstruction {
Expand Down Expand Up @@ -56,6 +60,7 @@ pub enum TransferHookInstruction {
}

/// Data expected by `Initialize`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct InitializeInstructionData {
Expand All @@ -66,6 +71,7 @@ pub struct InitializeInstructionData {
}

/// Data expected by `Update`
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct UpdateInstructionData {
Expand Down
3 changes: 3 additions & 0 deletions token/program-2022/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ pub enum TokenInstruction<'a> {
/// 2. `[]` Rent sysvar
InitializeAccount2 {
/// The new account's owner/multisignature.
#[cfg_attr(feature = "serde-traits", serde(with = "As::<DisplayFromStr>"))]
owner: Pubkey,
},
/// Given a wrapped / native token account (a token account containing SOL)
Expand All @@ -440,6 +441,7 @@ pub enum TokenInstruction<'a> {
/// 1. `[]` The mint this account will be associated with.
InitializeAccount3 {
/// The new account's owner/multisignature.
#[cfg_attr(feature = "serde-traits", serde(with = "As::<DisplayFromStr>"))]
owner: Pubkey,
},
/// Like InitializeMultisig, but does not require the Rent sysvar to be provided
Expand Down Expand Up @@ -635,6 +637,7 @@ pub enum TokenInstruction<'a> {
///
InitializePermanentDelegate {
/// Authority that may sign for `Transfer`s and `Burn`s on any account
#[cfg_attr(feature = "serde-traits", serde(with = "As::<DisplayFromStr>"))]
delegate: Pubkey,
},
/// The common instruction prefix for transfer hook extension instructions.
Expand Down
Loading

0 comments on commit 46aef02

Please sign in to comment.