Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
[zk-token-sdk] Refactor pod FeeParameters conversion and remove man…
Browse files Browse the repository at this point in the history
…ual byte conversion for the type (#32149)

* replace `decoded::TransferAmountCiphertext` to `DecodedTransferAmountCiphertext`

* refactor pod convert logic for fee parameters

* remove manual byte conversions for `FeeParameters`

* fix error from rebase
  • Loading branch information
samkim-crypto authored Jun 17, 2023
1 parent de024bf commit 428283c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
22 changes: 0 additions & 22 deletions zk-token-sdk/src/instruction/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use {
elgamal::ElGamalCiphertext,
pedersen::{PedersenCommitment, PedersenOpening},
},
arrayref::{array_ref, array_refs},
curve25519_dalek::scalar::Scalar,
};
#[cfg(not(target_os = "solana"))]
Expand Down Expand Up @@ -92,24 +91,3 @@ pub struct FeeParameters {
/// Maximum fee assessed on transfers, expressed as an amount of tokens
pub maximum_fee: u64,
}

#[cfg(not(target_os = "solana"))]
impl FeeParameters {
pub fn to_bytes(&self) -> [u8; 10] {
let mut bytes = [0u8; 10];
bytes[..2].copy_from_slice(&self.fee_rate_basis_points.to_le_bytes());
bytes[2..10].copy_from_slice(&self.maximum_fee.to_le_bytes());

bytes
}

pub fn from_bytes(bytes: &[u8]) -> Self {
let bytes = array_ref![bytes, 0, 10];
let (fee_rate_basis_points, maximum_fee) = array_refs![bytes, 2, 8];

Self {
fee_rate_basis_points: u16::from_le_bytes(*fee_rate_basis_points),
maximum_fee: u64::from_le_bytes(*maximum_fee),
}
}
}
20 changes: 1 addition & 19 deletions zk-token-sdk/src/zk_token_elgamal/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod target_arch {
crate::{
curve25519::scalar::PodScalar,
errors::ProofError,
instruction::transfer::{FeeParameters, TransferPubkeys, TransferWithFeePubkeys},
instruction::transfer::{TransferPubkeys, TransferWithFeePubkeys},
},
curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar},
std::convert::TryFrom,
Expand Down Expand Up @@ -132,24 +132,6 @@ mod target_arch {
})
}
}

impl From<FeeParameters> for pod::FeeParameters {
fn from(parameters: FeeParameters) -> Self {
Self {
fee_rate_basis_points: parameters.fee_rate_basis_points.into(),
maximum_fee: parameters.maximum_fee.into(),
}
}
}

impl From<pod::FeeParameters> for FeeParameters {
fn from(pod: pod::FeeParameters) -> Self {
Self {
fee_rate_basis_points: pod.fee_rate_basis_points.into(),
maximum_fee: pod.maximum_fee.into(),
}
}
}
}

#[cfg(target_os = "solana")]
Expand Down
20 changes: 20 additions & 0 deletions zk-token-sdk/src/zk_token_elgamal/pod/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,23 @@ pub struct FeeParameters {
/// Maximum fee assessed on transfers, expressed as an amount of tokens
pub maximum_fee: PodU64,
}

#[cfg(not(target_os = "solana"))]
impl From<decoded::FeeParameters> for FeeParameters {
fn from(decoded_fee_parameters: decoded::FeeParameters) -> Self {
FeeParameters {
fee_rate_basis_points: decoded_fee_parameters.fee_rate_basis_points.into(),
maximum_fee: decoded_fee_parameters.maximum_fee.into(),
}
}
}

#[cfg(not(target_os = "solana"))]
impl From<FeeParameters> for decoded::FeeParameters {
fn from(pod_fee_parameters: FeeParameters) -> Self {
decoded::FeeParameters {
fee_rate_basis_points: pod_fee_parameters.fee_rate_basis_points.into(),
maximum_fee: pod_fee_parameters.maximum_fee.into(),
}
}
}

0 comments on commit 428283c

Please sign in to comment.