Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit f30ad1d

Browse files
omertxyzjnwngjoncinque
authored
Add serde feature - token-2022 (#3291)
* Add serde to token-2022 * Add tests * Fix feature syntax * Formatting, enabling features * Make CI work, and some naming nit cleanup * Un-async tests Co-authored-by: jon wong <[email protected]> Co-authored-by: Jon Cinque <[email protected]>
1 parent b5cec79 commit f30ad1d

File tree

9 files changed

+240
-9
lines changed

9 files changed

+240
-9
lines changed

.github/workflows/pull-request-token.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ jobs:
6767
./token/twoxtx-setup.sh
6868
./ci/cargo-test-bpf.sh token/program-2022-test
6969
70+
- name: Build and test token-2022 with "serde" activated
71+
run: |
72+
cargo +"${{ env.RUST_STABLE }}" test \
73+
--manifest-path=token/program-2022/Cargo.toml \
74+
--features serde-traits \
75+
-- --nocapture
76+
exit 0
77+
7078
- name: Upload programs
7179
uses: actions/upload-artifact@v2
7280
with:

Cargo.lock

Lines changed: 81 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

token/program-2022/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ exclude = ["js/**"]
1111
[features]
1212
no-entrypoint = []
1313
test-bpf = []
14+
serde-traits = ["serde", "serde_with"]
1415
# Remove these features once the underlying syscalls are released on all networks
1516
default = ["zk-ops"]
1617
zk-ops = []
@@ -26,13 +27,16 @@ solana-zk-token-sdk = "1.10.29"
2627
spl-memo = { version = "3.0.1", path = "../../memo/program", features = [ "no-entrypoint" ] }
2728
spl-token = { version = "3.3", path = "../program", features = ["no-entrypoint"] }
2829
thiserror = "1.0"
30+
serde = { version = "1.0.136", optional = true }
31+
serde_with = { version = "1.14.0", optional = true }
2932

3033
[dev-dependencies]
3134
lazy_static = "1.4.0"
3235
proptest = "1.0"
3336
serial_test = "0.5.1"
3437
solana-program-test = "1.10.29"
3538
solana-sdk = "1.10.29"
39+
serde_json = "1.0.81"
3640

3741
[lib]
3842
crate-type = ["cdylib", "lib"]

token/program-2022/src/extension/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ use {
2828
},
2929
};
3030

31+
#[cfg(feature = "serde-traits")]
32+
use serde::{Deserialize, Serialize};
33+
3134
/// Confidential Transfer extension
3235
pub mod confidential_transfer;
3336
/// Default Account State extension
@@ -601,6 +604,7 @@ impl Default for AccountType {
601604
/// applied to mint accounts, and account extensions must only be applied to token holding
602605
/// accounts.
603606
#[repr(u16)]
607+
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
604608
#[derive(Clone, Copy, Debug, PartialEq, TryFromPrimitive, IntoPrimitive)]
605609
pub enum ExtensionType {
606610
/// Used as padding if the account size would otherwise be 355, same as a multisig

token/program-2022/src/extension/transfer_fee/instruction.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ use {
99
std::convert::TryFrom,
1010
};
1111

12+
#[cfg(feature = "serde-traits")]
13+
use {
14+
crate::serialization::coption_fromstr,
15+
serde::{Deserialize, Serialize},
16+
};
17+
1218
/// Transfer Fee extension instructions
19+
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
1320
#[derive(Clone, Copy, Debug, PartialEq)]
1421
#[repr(u8)]
1522
pub enum TransferFeeInstruction {
@@ -27,8 +34,10 @@ pub enum TransferFeeInstruction {
2734
/// 0. `[writable]` The mint to initialize.
2835
InitializeTransferFeeConfig {
2936
/// Pubkey that may update the fees
37+
#[cfg_attr(feature = "serde-traits", serde(with = "coption_fromstr"))]
3038
transfer_fee_config_authority: COption<Pubkey>,
3139
/// Withdraw instructions must be signed by this key
40+
#[cfg_attr(feature = "serde-traits", serde(with = "coption_fromstr"))]
3241
withdraw_withheld_authority: COption<Pubkey>,
3342
/// Amount of transfer collected as fees, expressed as basis points of the
3443
/// transfer amount

token/program-2022/src/instruction.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Instruction types
22
3+
#![allow(deprecated)] // needed to avoid deprecation warning when generating serde implementation for TokenInstruction
4+
35
use {
46
crate::{
57
check_program_account, check_spl_token_program_account,
@@ -21,6 +23,13 @@ use {
2123
},
2224
};
2325

26+
#[cfg(feature = "serde-traits")]
27+
use {
28+
crate::serialization::coption_fromstr,
29+
serde::{Deserialize, Serialize},
30+
serde_with::{As, DisplayFromStr},
31+
};
32+
2433
/// Minimum number of multisignature signers (min N)
2534
pub const MIN_SIGNERS: usize = 1;
2635
/// Maximum number of multisignature signers (max N)
@@ -32,6 +41,7 @@ const U64_BYTES: usize = 8;
3241

3342
/// Instructions supported by the token program.
3443
#[repr(C)]
44+
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
3545
#[derive(Clone, Debug, PartialEq)]
3646
pub enum TokenInstruction<'a> {
3747
/// Initializes a new mint and optionally deposits all the newly minted
@@ -54,8 +64,10 @@ pub enum TokenInstruction<'a> {
5464
/// Number of base 10 digits to the right of the decimal place.
5565
decimals: u8,
5666
/// The authority/multisignature to mint tokens.
67+
#[cfg_attr(feature = "serde-traits", serde(with = "As::<DisplayFromStr>"))]
5768
mint_authority: Pubkey,
5869
/// The freeze authority/multisignature of the mint.
70+
#[cfg_attr(feature = "serde-traits", serde(with = "coption_fromstr"))]
5971
freeze_authority: COption<Pubkey>,
6072
},
6173
/// Initializes a new account to hold tokens. If this account is associated
@@ -180,6 +192,7 @@ pub enum TokenInstruction<'a> {
180192
/// The type of authority to update.
181193
authority_type: AuthorityType,
182194
/// The new authority
195+
#[cfg_attr(feature = "serde-traits", serde(with = "coption_fromstr"))]
183196
new_authority: COption<Pubkey>,
184197
},
185198
/// Mints new tokens to an account. The native mint does not support
@@ -444,8 +457,10 @@ pub enum TokenInstruction<'a> {
444457
/// Number of base 10 digits to the right of the decimal place.
445458
decimals: u8,
446459
/// The authority/multisignature to mint tokens.
460+
#[cfg_attr(feature = "serde-traits", serde(with = "As::<DisplayFromStr>"))]
447461
mint_authority: Pubkey,
448462
/// The freeze authority/multisignature of the mint.
463+
#[cfg_attr(feature = "serde-traits", serde(with = "coption_fromstr"))]
449464
freeze_authority: COption<Pubkey>,
450465
},
451466
/// Gets the required size of an account for the given mint as a little-endian
@@ -514,6 +529,7 @@ pub enum TokenInstruction<'a> {
514529
/// 0. `[writable]` The mint to initialize.
515530
InitializeMintCloseAuthority {
516531
/// Authority that must sign the `CloseAccount` instruction on a mint
532+
#[cfg_attr(feature = "serde-traits", serde(with = "coption_fromstr"))]
517533
close_authority: COption<Pubkey>,
518534
},
519535
/// The common instruction prefix for Transfer Fee extension instructions.
@@ -933,6 +949,7 @@ impl<'a> TokenInstruction<'a> {
933949

934950
/// Specifies the authority type for SetAuthority instructions
935951
#[repr(u8)]
952+
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
936953
#[derive(Clone, Debug, PartialEq)]
937954
pub enum AuthorityType {
938955
/// Authority to mint new tokens

token/program-2022/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub mod instruction;
1010
pub mod native_mint;
1111
pub mod pod;
1212
pub mod processor;
13+
#[cfg(feature = "serde-traits")]
14+
pub mod serialization;
1315
pub mod state;
1416

1517
#[cfg(not(feature = "no-entrypoint"))]

0 commit comments

Comments
 (0)