diff --git a/common/src/payment_id.rs b/common/src/payment_id.rs index c5b3db5b..471a430e 100644 --- a/common/src/payment_id.rs +++ b/common/src/payment_id.rs @@ -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], @@ -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::() + } + } + impl Encode for PaymentId { + fn size_hint(&self) -> usize { + mem::size_of::() + } + fn using_encoded R>(&self, f: F) -> R { + let buf = self.to_number().to_le_bytes(); + f(&buf[..]) + } + } + impl Decode for PaymentId { + fn decode(input: &mut I) -> Result { + let mut buf = [0u8; mem::size_of::()]; + input.read(&mut buf)?; + Ok(::from_le_bytes(buf).into()) + } + fn encoded_fixed_size() -> Option { + Some(mem::size_of::()) + } + } + 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; diff --git a/pallets/payments/src/lib.rs b/pallets/payments/src/lib.rs index 2c991fe3..ee64c5de 100644 --- a/pallets/payments/src/lib.rs +++ b/pallets/payments/src/lib.rs @@ -585,10 +585,10 @@ impl Pallet { .map_err(|_| Error::::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::::ReleaseFailed)?; - T::Assets::transfer(payment.asset, &beneficiary, sender, payment.amount, Expendable) + T::Assets::transfer(payment.asset, beneficiary, sender, payment.amount, Expendable) .map_err(|_| Error::::TransferFailed)?; Ok(()) diff --git a/pallets/payments/src/tests.rs b/pallets/payments/src/tests.rs index ec2e73d9..82a24365 100644 --- a/pallets/payments/src/tests.rs +++ b/pallets/payments/src/tests.rs @@ -17,7 +17,7 @@ fn build_payment(assert_payment_creation: bool) -> Fees { assert_ok!(Payments::pay( RuntimeOrigin::signed(SENDER_ACCOUNT), - PAYMENT_BENEFICIARY.into(), + PAYMENT_BENEFICIARY, ASSET_ID, PAYMENT_AMOUNT, Some(remark.clone()), @@ -47,7 +47,7 @@ fn build_payment(assert_payment_creation: bool) -> Fees { incentive_amount: INCENTIVE_AMOUNT, state: PaymentState::Created, fees: fees_details.clone(), - beneficiary: PAYMENT_BENEFICIARY.into() + beneficiary: PAYMENT_BENEFICIARY } ); @@ -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 } ); @@ -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 } ); @@ -306,7 +306,7 @@ fn payment_disputed_beneficiary_wins() { incentive_amount: INCENTIVE_AMOUNT, state: PaymentState::NeedsReview, fees, - beneficiary: PAYMENT_BENEFICIARY.into() + beneficiary: PAYMENT_BENEFICIARY } ); @@ -421,7 +421,7 @@ fn payment_disputed_sender_wins() { incentive_amount: INCENTIVE_AMOUNT, state: PaymentState::NeedsReview, fees, - beneficiary: PAYMENT_BENEFICIARY.into() + beneficiary: PAYMENT_BENEFICIARY } ); @@ -496,7 +496,7 @@ fn request_payment() { incentive_amount: INCENTIVE_AMOUNT, state: PaymentState::PaymentRequested, fees, - beneficiary: PAYMENT_BENEFICIARY.into() + beneficiary: PAYMENT_BENEFICIARY } );