Skip to content

Commit

Permalink
Encode/Decode PaymentId as u64
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Feb 14, 2024
1 parent 4e1c8f2 commit 8cb01dc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
51 changes: 42 additions & 9 deletions common/src/payment_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ use wasm_bindgen::prelude::*;
/// A compact identifier for payment
#[cfg_attr(feature = "js", wasm_bindgen)]
#[derive(Debug, Default, Clone, Eq, Copy, PartialEq)]
#[cfg_attr(
feature = "runtime",
derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
parity_scale_codec::MaxEncodedLen,
scale_info::TypeInfo,
)
)]
#[repr(C)]
pub struct PaymentId {
prefix: [u8; 2],
Expand Down Expand Up @@ -135,6 +126,48 @@ impl core::fmt::Display for PaymentId {
}
}

// Manual implementation of Encode/Decode/TypeInfo to treat PaymentId like a u64
#[cfg(feature = "runtime")]
mod runtime {
use super::PaymentId;
use core::mem;
use parity_scale_codec::{Decode, Encode, EncodeLike, Error, Input, MaxEncodedLen};
use scale_info::{TypeDefPrimitive, TypeInfo};

impl EncodeLike for PaymentId {}

impl MaxEncodedLen for PaymentId {
fn max_encoded_len() -> usize {
mem::size_of::<u64>()
}
}
impl Encode for PaymentId {
fn size_hint(&self) -> usize {
mem::size_of::<u64>()
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let buf = self.to_number().to_le_bytes();
f(&buf[..])
}
}
impl Decode for PaymentId {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut buf = [0u8; mem::size_of::<u64>()];
input.read(&mut buf)?;
Ok(<u64>::from_le_bytes(buf).into())
}
fn encoded_fixed_size() -> Option<usize> {
Some(mem::size_of::<u64>())
}
}
impl TypeInfo for PaymentId {
type Identity = u64;
fn type_info() -> scale_info::Type {
TypeDefPrimitive::U64.into()
}
}
}

#[cfg(all(test, feature = "nightly"))]
mod tests {
extern crate alloc;
Expand Down
4 changes: 2 additions & 2 deletions pallets/payments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ impl<T: Config> Pallet<T> {
.map_err(|_| Error::<T>::ReleaseFailed)?;

let beneficiary = &payment.beneficiary;
T::Assets::release(payment.asset.clone(), reason, &beneficiary, payment.amount, Exact)
T::Assets::release(payment.asset.clone(), reason, beneficiary, payment.amount, Exact)
.map_err(|_| Error::<T>::ReleaseFailed)?;

T::Assets::transfer(payment.asset, &beneficiary, sender, payment.amount, Expendable)
T::Assets::transfer(payment.asset, beneficiary, sender, payment.amount, Expendable)
.map_err(|_| Error::<T>::TransferFailed)?;

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions pallets/payments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn build_payment(assert_payment_creation: bool) -> Fees<Test> {

assert_ok!(Payments::pay(
RuntimeOrigin::signed(SENDER_ACCOUNT),
PAYMENT_BENEFICIARY.into(),
PAYMENT_BENEFICIARY,
ASSET_ID,
PAYMENT_AMOUNT,
Some(remark.clone()),
Expand Down Expand Up @@ -47,7 +47,7 @@ fn build_payment(assert_payment_creation: bool) -> Fees<Test> {
incentive_amount: INCENTIVE_AMOUNT,
state: PaymentState::Created,
fees: fees_details.clone(),
beneficiary: PAYMENT_BENEFICIARY.into()
beneficiary: PAYMENT_BENEFICIARY
}
);

Expand Down Expand Up @@ -125,7 +125,7 @@ fn test_pay_and_release_works() {
incentive_amount: INCENTIVE_AMOUNT,
state: PaymentState::Finished,
fees,
beneficiary: PAYMENT_BENEFICIARY.into()
beneficiary: PAYMENT_BENEFICIARY
}
);

Expand Down Expand Up @@ -214,7 +214,7 @@ fn payment_refunded_request() {
incentive_amount: INCENTIVE_AMOUNT,
state: PaymentState::RefundRequested { cancel_block: 11 },
fees,
beneficiary: PAYMENT_BENEFICIARY.into()
beneficiary: PAYMENT_BENEFICIARY
}
);

Expand Down Expand Up @@ -306,7 +306,7 @@ fn payment_disputed_beneficiary_wins() {
incentive_amount: INCENTIVE_AMOUNT,
state: PaymentState::NeedsReview,
fees,
beneficiary: PAYMENT_BENEFICIARY.into()
beneficiary: PAYMENT_BENEFICIARY
}
);

Expand Down Expand Up @@ -421,7 +421,7 @@ fn payment_disputed_sender_wins() {
incentive_amount: INCENTIVE_AMOUNT,
state: PaymentState::NeedsReview,
fees,
beneficiary: PAYMENT_BENEFICIARY.into()
beneficiary: PAYMENT_BENEFICIARY
}
);

Expand Down Expand Up @@ -496,7 +496,7 @@ fn request_payment() {
incentive_amount: INCENTIVE_AMOUNT,
state: PaymentState::PaymentRequested,
fees,
beneficiary: PAYMENT_BENEFICIARY.into()
beneficiary: PAYMENT_BENEFICIARY
}
);

Expand Down

0 comments on commit 8cb01dc

Please sign in to comment.