pub struct Payment<M: ManagedTypeApi> {
pub token_identifier: TokenId<M>,
pub token_nonce: u64,
pub amount: NonZeroBigUint<M>,
}Expand description
Represents a payment in a specific token with a guaranteed non-zero amount.
A Payment encapsulates all the information needed to represent a token transfer:
- The token identifier (native EGLD or ESDT token)
- The token nonce (0 for fungible tokens, > 0 for NFTs/SFTs)
- The amount (guaranteed to be non-zero)
This type is commonly used in smart contracts for handling incoming payments, storing payment information, and ensuring payment validity through the type system.
§Key Features
- Type Safety: The amount is guaranteed to be non-zero through
NonZeroBigUint - Universal Token Support: Works with both native EGLD and ESDT tokens
- NFT/SFT Support: Handles both fungible (nonce=0) and non-fungible tokens (nonce>0)
- Serialization: Supports encoding/decoding for blockchain storage and communication
§Examples
// Create a fungible token payment
let usdc_payment = Payment::new(
TokenId::from("USDC-123456"),
0,
NonZeroBigUint::try_from(1000u64)?,
);
// Create an NFT payment
let nft_payment = Payment::try_new(
"NFT-456789",
42,
1u64
)?;
// Check if payment is fungible
assert!(usdc_payment.is_fungible());
assert!(!nft_payment.is_fungible());§See Also
FungiblePaymentfor fungible-only paymentsNonZeroBigUintfor the amount type constraints
Fields§
§token_identifier: TokenId<M>The token identifier (native EGLD or ESDT token)
token_nonce: u64The token nonce (0 for fungible tokens, > 0 for NFTs/SFTs)
amount: NonZeroBigUint<M>The payment amount (guaranteed to be non-zero)
Implementations§
Source§impl<M: ManagedTypeApi> Payment<M>
impl<M: ManagedTypeApi> Payment<M>
Sourcepub fn new<T, A>(token_identifier: T, token_nonce: u64, amount: A) -> Self
pub fn new<T, A>(token_identifier: T, token_nonce: u64, amount: A) -> Self
Creates a new Payment with the specified token identifier, nonce, and amount.
This constructor accepts any type that can be converted into a TokenId<M> and any type that can be converted into a NonZeroBigUint<M>, for ergonomic usage.
§Arguments
token_identifier- Any type convertible toTokenId<M>(e.g.,&str,String,ManagedBuffer<M>, etc.)token_nonce- The token nonce (0 for fungible tokens, > 0 for NFTs/SFTs)amount- Any type convertible toNonZeroBigUint<M>(e.g.,NonZeroU64)
§Examples
let token_id = TokenId::from("USDC-123456");
let amount = NonZero::new(1000u32).unwrap();
// Create a fungible token payment
let payment = Payment::new(token_id, 0, amount);
// Create an NFT payment
let nft_payment = Payment::new("NFT-456789", 42, NonZeroBigUint::one());Sourcepub fn try_new<T, A, E>(
token_identifier: T,
token_nonce: u64,
amount: A,
) -> Result<Self, E>
pub fn try_new<T, A, E>( token_identifier: T, token_nonce: u64, amount: A, ) -> Result<Self, E>
Attempts to create a new Payment from flexible input types.
This is a more flexible version of Payment::new that accepts arguments
implementing Into and TryInto traits, allowing for convenient conversion
from various compatible types.
§Arguments
token_identifier- Any type that can be converted into aTokenId<M>(e.g.,ManagedBuffer<M>,&str,String, etc.)token_nonce- The token nonce (0 for fungible tokens, > 0 for NFTs/SFTs)amount- Any type that can be converted into aNonZeroBigUint<M>(e.g.,BigUint<M>,u128,ManagedBuffer<M>, etc.)
§Returns
Ok(Payment)- If all conversions succeed and the amount is non-zeroErr(NonZeroError)- If the amount converts to zero
§Examples
let token_id = TokenId::<M>::from("TOKEN-123456");
let amount = BigUint::from(1000u64);
// Create payment from BigUint (might be zero)
let payment = Payment::<M>::try_new(token_id, 0, amount)?;
// Create payment from u128
let payment = Payment::<M>::try_new("USDC-123456", 0, 500u128)?;pub fn is_fungible(&self) -> bool
pub fn fungible_or_panic(self) -> FungiblePayment<M>
pub fn into_tuple(self) -> (TokenId<M>, u64, NonZeroBigUint<M>)
pub fn as_tuple(&self) -> (&TokenId<M>, u64, &NonZeroBigUint<M>)
Sourcepub fn into_egld_or_esdt_payment(self) -> EgldOrEsdtTokenPayment<M>
pub fn into_egld_or_esdt_payment(self) -> EgldOrEsdtTokenPayment<M>
Zero-cost conversion to the legacy payment type, EgldOrEsdtTokenPayment.
Sourcepub fn map_ref_egld_or_esdt<Context, D, F, U>(
&self,
context: Context,
for_egld: D,
for_esdt: F,
) -> Uwhere
D: FnOnce(Context, Egld<&BigUint<M>>) -> U,
F: FnOnce(Context, EsdtTokenPaymentRefs<'_, M>) -> U,
pub fn map_ref_egld_or_esdt<Context, D, F, U>(
&self,
context: Context,
for_egld: D,
for_esdt: F,
) -> Uwhere
D: FnOnce(Context, Egld<&BigUint<M>>) -> U,
F: FnOnce(Context, EsdtTokenPaymentRefs<'_, M>) -> U,
Same as map_egld_or_esdt, but only takes a reference,
and consequently, the closures also only get references.
pub fn map_egld_or_esdt<Context, D, F, U>( self, context: Context, for_egld: D, for_esdt: F, ) -> U
pub fn as_refs(&self) -> PaymentRefs<'_, M>
Trait Implementations§
Source§impl<M: ManagedTypeApi> From<Payment<M>> for PaymentVec<M>
impl<M: ManagedTypeApi> From<Payment<M>> for PaymentVec<M>
Source§impl<M: ManagedTypeApi> From<Payment<M>> for PaymentMultiValue<M>
impl<M: ManagedTypeApi> From<Payment<M>> for PaymentMultiValue<M>
Source§impl<M: ManagedTypeApi> IntoMultiValue for Payment<M>
impl<M: ManagedTypeApi> IntoMultiValue for Payment<M>
type MultiValue = PaymentMultiValue<M>
fn into_multi_value(self) -> Self::MultiValue
Source§impl<M: ManagedTypeApi> ManagedVecItem for Payment<M>
impl<M: ManagedTypeApi> ManagedVecItem for Payment<M>
Source§const SKIPS_RESERIALIZATION: bool = false
const SKIPS_RESERIALIZATION: bool = false
u32).Source§type PAYLOAD = ManagedVecItemPayloadBuffer<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>>
type PAYLOAD = ManagedVecItemPayloadBuffer<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>>
Source§fn read_from_payload(payload: &Self::PAYLOAD) -> Self
fn read_from_payload(payload: &Self::PAYLOAD) -> Self
Source§unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a>
unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a>
Source§fn save_to_payload(self, payload: &mut Self::PAYLOAD)
fn save_to_payload(self, payload: &mut Self::PAYLOAD)
fn payload_size() -> usize
Source§impl<M: ManagedTypeApi> NestedDecode for Payment<M>
impl<M: ManagedTypeApi> NestedDecode for Payment<M>
Source§fn dep_decode_or_handle_err<I, H>(
input: &mut I,
h: H,
) -> Result<Self, H::HandledErr>where
I: NestedDecodeInput,
H: DecodeErrorHandler,
fn dep_decode_or_handle_err<I, H>(
input: &mut I,
h: H,
) -> Result<Self, H::HandledErr>where
I: NestedDecodeInput,
H: DecodeErrorHandler,
dep_decode that can handle errors as soon as they occur.
For instance in can exit immediately and make sure that if it returns, it is a success.
By not deferring error handling, this can lead to somewhat smaller bytecode.Source§fn dep_decode<I>(input: &mut I) -> Result<Self, DecodeError>where
I: NestedDecodeInput,
fn dep_decode<I>(input: &mut I) -> Result<Self, DecodeError>where
I: NestedDecodeInput,
Source§impl<M: ManagedTypeApi> NestedEncode for Payment<M>
impl<M: ManagedTypeApi> NestedEncode for Payment<M>
Source§fn dep_encode_or_handle_err<O, H>(
&self,
__dest__: &mut O,
__h__: H,
) -> Result<(), H::HandledErr>where
O: NestedEncodeOutput,
H: EncodeErrorHandler,
fn dep_encode_or_handle_err<O, H>(
&self,
__dest__: &mut O,
__h__: H,
) -> Result<(), H::HandledErr>where
O: NestedEncodeOutput,
H: EncodeErrorHandler,
dep_encode that can handle errors as soon as they occur.
For instance in can exit immediately and make sure that if it returns, it is a success.
By not deferring error handling, this can lead to somewhat smaller bytecode.Source§fn dep_encode<O>(&self, dest: &mut O) -> Result<(), EncodeError>where
O: NestedEncodeOutput,
fn dep_encode<O>(&self, dest: &mut O) -> Result<(), EncodeError>where
O: NestedEncodeOutput,
Source§impl<M: ManagedTypeApi> TopDecode for Payment<M>
impl<M: ManagedTypeApi> TopDecode for Payment<M>
Source§fn top_decode_or_handle_err<I, H>(
top_input: I,
h: H,
) -> Result<Self, H::HandledErr>where
I: TopDecodeInput,
H: DecodeErrorHandler,
fn top_decode_or_handle_err<I, H>(
top_input: I,
h: H,
) -> Result<Self, H::HandledErr>where
I: TopDecodeInput,
H: DecodeErrorHandler,
top_decode that can handle errors as soon as they occur.
For instance it can exit immediately and make sure that if it returns, it is a success.
By not deferring error handling, this can lead to somewhat smaller bytecode.Source§fn top_decode<I>(input: I) -> Result<Self, DecodeError>where
I: TopDecodeInput,
fn top_decode<I>(input: I) -> Result<Self, DecodeError>where
I: TopDecodeInput,
Source§impl<M: ManagedTypeApi> TopEncode for Payment<M>
impl<M: ManagedTypeApi> TopEncode for Payment<M>
Source§fn top_encode_or_handle_err<O, H>(
&self,
output: O,
__h__: H,
) -> Result<(), H::HandledErr>where
O: TopEncodeOutput,
H: EncodeErrorHandler,
fn top_encode_or_handle_err<O, H>(
&self,
output: O,
__h__: H,
) -> Result<(), H::HandledErr>where
O: TopEncodeOutput,
H: EncodeErrorHandler,
top_encode that can handle errors as soon as they occur.
For instance in can exit immediately and make sure that if it returns, it is a success.
By not deferring error handling, this can lead to somewhat smaller bytecode.Source§fn top_encode<O>(&self, output: O) -> Result<(), EncodeError>where
O: TopEncodeOutput,
fn top_encode<O>(&self, output: O) -> Result<(), EncodeError>where
O: TopEncodeOutput,
Source§impl<Env> TxPayment<Env> for &Payment<Env::Api>where
Env: TxEnv,
impl<Env> TxPayment<Env> for &Payment<Env::Api>where
Env: TxEnv,
Source§fn is_no_payment(&self, _env: &Env) -> bool
fn is_no_payment(&self, _env: &Env) -> bool
Source§fn perform_transfer_execute_fallible(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) -> Result<(), TransferExecuteFailed>
fn perform_transfer_execute_fallible( self, env: &Env, to: &ManagedAddress<Env::Api>, gas_limit: u64, fc: FunctionCall<Env::Api>, ) -> Result<(), TransferExecuteFailed>
Source§fn perform_transfer_execute_legacy(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
)
fn perform_transfer_execute_legacy( self, env: &Env, to: &ManagedAddress<Env::Api>, gas_limit: u64, fc: FunctionCall<Env::Api>, )
Source§fn with_normalized<From, To, F, R>(
self,
env: &Env,
from: &From,
to: To,
fc: FunctionCall<Env::Api>,
f: F,
) -> Rwhere
From: TxFrom<Env>,
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, FunctionCall<Env::Api>) -> R,
fn with_normalized<From, To, F, R>(
self,
env: &Env,
from: &From,
to: To,
fc: FunctionCall<Env::Api>,
f: F,
) -> Rwhere
From: TxFrom<Env>,
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, FunctionCall<Env::Api>) -> R,
Source§fn into_scenario_payments(self, env: &Env) -> ScenarioPayments<Env::Api>
fn into_scenario_payments(self, env: &Env) -> ScenarioPayments<Env::Api>
Source§fn perform_transfer_fallible(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
) -> Result<(), TransferExecuteFailed>
fn perform_transfer_fallible( self, env: &Env, to: &ManagedAddress<Env::Api>, ) -> Result<(), TransferExecuteFailed>
Source§impl<Env> TxPayment<Env> for Payment<Env::Api>where
Env: TxEnv,
impl<Env> TxPayment<Env> for Payment<Env::Api>where
Env: TxEnv,
Source§fn is_no_payment(&self, env: &Env) -> bool
fn is_no_payment(&self, env: &Env) -> bool
Source§fn perform_transfer_execute_fallible(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) -> Result<(), TransferExecuteFailed>
fn perform_transfer_execute_fallible( self, env: &Env, to: &ManagedAddress<Env::Api>, gas_limit: u64, fc: FunctionCall<Env::Api>, ) -> Result<(), TransferExecuteFailed>
Source§fn perform_transfer_execute_legacy(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
)
fn perform_transfer_execute_legacy( self, env: &Env, to: &ManagedAddress<Env::Api>, gas_limit: u64, fc: FunctionCall<Env::Api>, )
Source§fn with_normalized<From, To, F, R>(
self,
env: &Env,
from: &From,
to: To,
fc: FunctionCall<Env::Api>,
f: F,
) -> Rwhere
From: TxFrom<Env>,
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, FunctionCall<Env::Api>) -> R,
fn with_normalized<From, To, F, R>(
self,
env: &Env,
from: &From,
to: To,
fc: FunctionCall<Env::Api>,
f: F,
) -> Rwhere
From: TxFrom<Env>,
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, FunctionCall<Env::Api>) -> R,
Source§fn into_scenario_payments(self, env: &Env) -> ScenarioPayments<Env::Api>
fn into_scenario_payments(self, env: &Env) -> ScenarioPayments<Env::Api>
Source§fn perform_transfer_fallible(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
) -> Result<(), TransferExecuteFailed>
fn perform_transfer_fallible( self, env: &Env, to: &ManagedAddress<Env::Api>, ) -> Result<(), TransferExecuteFailed>
Source§impl<Env> TxPaymentCompose<Env, Payment<<Env as TxEnv>::Api>> for PaymentVec<Env::Api>where
Env: TxEnv,
impl<Env> TxPaymentCompose<Env, Payment<<Env as TxEnv>::Api>> for PaymentVec<Env::Api>where
Env: TxEnv,
Source§impl<Env> TxPaymentCompose<Env, Payment<<Env as TxEnv>::Api>> for Payment<Env::Api>where
Env: TxEnv,
impl<Env> TxPaymentCompose<Env, Payment<<Env as TxEnv>::Api>> for Payment<Env::Api>where
Env: TxEnv,
Source§impl<M: ManagedTypeApi> TypeAbi for Payment<M>
impl<M: ManagedTypeApi> TypeAbi for Payment<M>
type Unmanaged = Payment<M>
Source§fn type_name_rust() -> TypeName
fn type_name_rust() -> TypeName
fn type_names() -> TypeNames
Source§fn type_name_specific() -> Option<TypeName>
fn type_name_specific() -> Option<TypeName>
Source§fn provide_type_descriptions<TDC: TypeDescriptionContainer>(
accumulator: &mut TDC,
)
fn provide_type_descriptions<TDC: TypeDescriptionContainer>( accumulator: &mut TDC, )
impl<M: Eq + ManagedTypeApi> Eq for Payment<M>
impl<M: ManagedTypeApi> StructuralPartialEq for Payment<M>
impl<M: ManagedTypeApi> TypeAbiFrom<&Payment<M>> for Payment<M>
impl<M: ManagedTypeApi> TypeAbiFrom<Payment<M>> for Payment<M>
Auto Trait Implementations§
impl<M> Freeze for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: Freeze,
<M as HandleTypeInfo>::BigIntHandle: Freeze,
impl<M> RefUnwindSafe for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: RefUnwindSafe,
<M as HandleTypeInfo>::BigIntHandle: RefUnwindSafe,
M: RefUnwindSafe,
impl<M> Send for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: Send,
<M as HandleTypeInfo>::BigIntHandle: Send,
M: Send,
impl<M> Sync for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: Sync,
<M as HandleTypeInfo>::BigIntHandle: Sync,
M: Sync,
impl<M> Unpin for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: Unpin,
<M as HandleTypeInfo>::BigIntHandle: Unpin,
M: Unpin,
impl<M> UnsafeUnpin for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: UnsafeUnpin,
<M as HandleTypeInfo>::BigIntHandle: UnsafeUnpin,
impl<M> UnwindSafe for Payment<M>where
<M as HandleTypeInfo>::ManagedBufferHandle: UnwindSafe,
<M as HandleTypeInfo>::BigIntHandle: UnwindSafe,
M: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> MultiValueConstLength for T
impl<T> MultiValueConstLength for T
Source§const MULTI_VALUE_CONST_LEN: usize = 1
const MULTI_VALUE_CONST_LEN: usize = 1
Source§impl<T> MultiValueLength for T
impl<T> MultiValueLength for T
Source§fn multi_value_len(&self) -> usize
fn multi_value_len(&self) -> usize
Source§impl<T> SCCodec for Twhere
T: TopEncode,
impl<T> SCCodec for Twhere
T: TopEncode,
fn fmt<F>(&self, f: &mut F)where
F: FormatByteReceiver,
Source§impl<T> TopDecodeMulti for Twhere
T: TopDecode,
impl<T> TopDecodeMulti for Twhere
T: TopDecode,
Source§const IS_SINGLE_VALUE: bool = true
const IS_SINGLE_VALUE: bool = true
fn multi_decode_or_handle_err<I, H>(
input: &mut I,
h: H,
) -> Result<T, <H as DecodeErrorHandler>::HandledErr>where
I: TopDecodeMultiInput,
H: DecodeErrorHandler,
fn multi_decode<I>(input: &mut I) -> Result<Self, DecodeError>where
I: TopDecodeMultiInput,
Source§impl<T> TopEncodeMulti for Twhere
T: TopEncode,
impl<T> TopEncodeMulti for Twhere
T: TopEncode,
Source§fn multi_encode_or_handle_err<O, H>(
&self,
output: &mut O,
h: H,
) -> Result<(), <H as EncodeErrorHandler>::HandledErr>where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
fn multi_encode_or_handle_err<O, H>(
&self,
output: &mut O,
h: H,
) -> Result<(), <H as EncodeErrorHandler>::HandledErr>where
O: TopEncodeMultiOutput,
H: EncodeErrorHandler,
top_encode that can handle errors as soon as they occur.
For instance in can exit immediately and make sure that if it returns, it is a success.
By not deferring error handling, this can lead to somewhat smaller bytecode.