Struct frc46_token::token::Token
source · pub struct Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,{ /* private fields */ }
Expand description
Library functions that implement core FRC-??? standards
Holds injectable services to access/interface with IPLD/FVM layer.
Implementations§
source§impl<'st, S, BS> Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,
impl<'st, S, BS> Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,
sourcepub fn create_state(bs: &BS) -> Result<TokenState, TokenError>
pub fn create_state(bs: &BS) -> Result<TokenState, TokenError>
Creates a new clean token state instance
This should be wrapped in a Token handle for convenience. Must be flushed to the blockstore explicitly to persist changes
sourcepub fn create_state_with_bit_width(
bs: &BS,
hamt_bit_width: u32,
) -> Result<TokenState, TokenError>
pub fn create_state_with_bit_width( bs: &BS, hamt_bit_width: u32, ) -> Result<TokenState, TokenError>
Creates a new clean token state instance, specifying the underlying Hamt bit widths
This should be wrapped in a Token handle for convenience. Must be flushed to the blockstore explicitly to persist changes
sourcepub fn wrap(
runtime: &'st ActorRuntime<S, BS>,
granularity: u64,
state: &'st mut TokenState,
) -> Self
pub fn wrap( runtime: &'st ActorRuntime<S, BS>, granularity: u64, state: &'st mut TokenState, ) -> Self
Wrap an existing token state
sourcepub fn replace(&mut self, state: TokenState) -> TokenState
pub fn replace(&mut self, state: TokenState) -> TokenState
Replace the current state with another The previous state is returned and can be safely dropped
sourcepub fn load_state(bs: &BS, state_cid: &Cid) -> Result<TokenState, TokenError>
pub fn load_state(bs: &BS, state_cid: &Cid) -> Result<TokenState, TokenError>
For an already initialised state tree, loads the state tree from the blockstore at a Cid
sourcepub fn load_replace(&mut self, cid: &Cid) -> Result<TokenState, TokenError>
pub fn load_replace(&mut self, cid: &Cid) -> Result<TokenState, TokenError>
Loads a fresh copy of the state from a blockstore from a given cid, replacing existing state The old state is returned to enable comparisons and the like but can be safely dropped otherwise
sourcepub fn flush(&mut self) -> Result<Cid, TokenError>
pub fn flush(&mut self) -> Result<Cid, TokenError>
Flush state and return Cid for root
sourcepub fn state(&self) -> &TokenState
pub fn state(&self) -> &TokenState
Get a reference to the wrapped state tree
sourcepub fn runtime(&self) -> &ActorRuntime<S, BS>
pub fn runtime(&self) -> &ActorRuntime<S, BS>
Get a reference to the underlying runtime
source§impl<'st, S, BS> Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,
impl<'st, S, BS> Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,
sourcepub fn granularity(&self) -> u64
pub fn granularity(&self) -> u64
Returns the smallest amount of tokens which is indivisible
Transfers and balances must be in multiples of granularity but allowances need not be. Granularity never changes after it is initially set
sourcepub fn mint(
&mut self,
operator: &Address,
initial_owner: &Address,
amount: &TokenAmount,
operator_data: RawBytes,
token_data: RawBytes,
) -> Result<ReceiverHook<MintIntermediate>, TokenError>
pub fn mint( &mut self, operator: &Address, initial_owner: &Address, amount: &TokenAmount, operator_data: RawBytes, token_data: RawBytes, ) -> Result<ReceiverHook<MintIntermediate>, TokenError>
Mints the specified value of tokens into an account
The minter is implicitly defined as the caller of the actor, and must be an ID address. The mint amount must be non-negative or the method returns an error.
Returns a ReceiverHook to call the owner’s token receiver hook, and the owner’s new balance. ReceiverHook must be called or it will panic and abort the transaction.
The hook call will return a MintIntermediate struct which must be passed to mint_return to get the final return data
sourcepub fn mint_return(
&self,
intermediate: MintIntermediate,
) -> Result<MintReturn, TokenError>
pub fn mint_return( &self, intermediate: MintIntermediate, ) -> Result<MintReturn, TokenError>
Finalise return data from MintIntermediate data returned by calling receiver hook after minting This is done to allow reloading the state if it changed as a result of the hook call so we can return an accurate balance even if the receiver transferred or burned tokens upon receipt
sourcepub fn total_supply(&self) -> TokenAmount
pub fn total_supply(&self) -> TokenAmount
Gets the total number of tokens in existence
This equals the sum of balance_of
called on all addresses. This equals sum of all
successful mint
calls minus the sum of all successful burn
/burn_from
calls
sourcepub fn balance_of(&self, owner: &Address) -> Result<TokenAmount, TokenError>
pub fn balance_of(&self, owner: &Address) -> Result<TokenAmount, TokenError>
Returns the balance associated with a particular address
Accounts that have never received transfers implicitly have a zero-balance
sourcepub fn allowance(
&self,
owner: &Address,
operator: &Address,
) -> Result<TokenAmount, TokenError>
pub fn allowance( &self, owner: &Address, operator: &Address, ) -> Result<TokenAmount, TokenError>
Gets the allowance between owner and operator
An allowance is the amount that the operator can transfer or burn out of the owner’s account
via the transfer
and burn
methods.
sourcepub fn increase_allowance(
&mut self,
owner: &Address,
operator: &Address,
delta: &TokenAmount,
) -> Result<TokenAmount, TokenError>
pub fn increase_allowance( &mut self, owner: &Address, operator: &Address, delta: &TokenAmount, ) -> Result<TokenAmount, TokenError>
Increase the allowance that an operator can control of an owner’s balance by the requested delta
Returns an error if requested delta is negative or there are errors in (de)serialization of state.If either owner or operator addresses are not resolvable and cannot be initialised, this method returns MessagingError::AddressNotInitialized.
Else returns the new allowance
sourcepub fn decrease_allowance(
&mut self,
owner: &Address,
operator: &Address,
delta: &TokenAmount,
) -> Result<TokenAmount, TokenError>
pub fn decrease_allowance( &mut self, owner: &Address, operator: &Address, delta: &TokenAmount, ) -> Result<TokenAmount, TokenError>
Decrease the allowance that an operator controls of the owner’s balance by the requested delta
Returns an error if requested delta is negative or there are errors in (de)serialization of of state. If the resulting allowance would be negative, the allowance between owner and operator is set to zero.Returns an error if either the operator or owner addresses are not resolvable and cannot be initialized.
Else returns the new allowance
sourcepub fn revoke_allowance(
&mut self,
owner: &Address,
operator: &Address,
) -> Result<TokenAmount, TokenError>
pub fn revoke_allowance( &mut self, owner: &Address, operator: &Address, ) -> Result<TokenAmount, TokenError>
Sets the allowance between owner and operator to zero, returning the old allowance
sourcepub fn set_allowance(
&mut self,
owner: &Address,
operator: &Address,
amount: &TokenAmount,
) -> Result<TokenAmount, TokenError>
pub fn set_allowance( &mut self, owner: &Address, operator: &Address, amount: &TokenAmount, ) -> Result<TokenAmount, TokenError>
Sets the allowance to a specified amount, returning the old allowance
sourcepub fn burn(
&mut self,
owner: &Address,
amount: &TokenAmount,
) -> Result<BurnReturn, TokenError>
pub fn burn( &mut self, owner: &Address, amount: &TokenAmount, ) -> Result<BurnReturn, TokenError>
Burns an amount of token from the specified address, decreasing total token supply
- The requested value MUST be non-negative
- The requested value MUST NOT exceed the target’s balance
- If the burn operation would result in a negative balance for the owner, the burn is discarded and this method returns an error
Upon successful burn
- The target’s balance decreases by the requested value
- The total_supply decreases by the requested value
sourcepub fn burn_from(
&mut self,
operator: &Address,
owner: &Address,
amount: &TokenAmount,
) -> Result<BurnFromReturn, TokenError>
pub fn burn_from( &mut self, operator: &Address, owner: &Address, amount: &TokenAmount, ) -> Result<BurnFromReturn, TokenError>
Burns an amount of token from the specified address, decreasing total token supply
If operator and owner are the same address, this method returns an InvalidOperator error.
- The requested value MUST be non-negative
- The requested value MUST NOT exceed the target’s balance
- If the burn operation would result in a negative balance for the owner, the burn is discarded and this method returns an error
- The operator MUST have an allowance not less than the requested value
Upon successful burn
- The target’s balance decreases by the requested value
- The total_supply decreases by the requested value
- The operator’s allowance is decreased by the requested value
sourcepub fn transfer(
&mut self,
from: &Address,
to: &Address,
amount: &TokenAmount,
operator_data: RawBytes,
token_data: RawBytes,
) -> Result<ReceiverHook<TransferIntermediate>, TokenError>
pub fn transfer( &mut self, from: &Address, to: &Address, amount: &TokenAmount, operator_data: RawBytes, token_data: RawBytes, ) -> Result<ReceiverHook<TransferIntermediate>, TokenError>
Transfers an amount from the caller to another address
- The requested value MUST be non-negative
- The requested value MUST NOT exceed the sender’s balance
- The receiving actor MUST implement a method called
tokens_received
, corresponding to the interface specified for FRC-0046 token receiver. If the receiving hook aborts, when called, the transfer is discarded and this method returns an error
Upon successful transfer:
- The from balance decreases by the requested value
- The to balance increases by the requested value
Returns a ReceiverHook to call the recipient’s token receiver hook, and a TransferIntermediate struct ReceiverHook must be called or it will panic and abort the transaction.
Return data from the hook should be passed to transfer_return which will generate the Transfereturn struct
sourcepub fn transfer_return(
&self,
intermediate: TransferIntermediate,
) -> Result<TransferReturn, TokenError>
pub fn transfer_return( &self, intermediate: TransferIntermediate, ) -> Result<TransferReturn, TokenError>
Generate TransferReturn from the intermediate data returned by a receiver hook call
sourcepub fn transfer_from(
&mut self,
operator: &Address,
from: &Address,
to: &Address,
amount: &TokenAmount,
operator_data: RawBytes,
token_data: RawBytes,
) -> Result<ReceiverHook<TransferFromIntermediate>, TokenError>
pub fn transfer_from( &mut self, operator: &Address, from: &Address, to: &Address, amount: &TokenAmount, operator_data: RawBytes, token_data: RawBytes, ) -> Result<ReceiverHook<TransferFromIntermediate>, TokenError>
Transfers an amount from one address to another
- The requested value MUST be non-negative
- The requested value MUST NOT exceed the sender’s balance
- The receiving actor MUST implement a method called
tokens_received
, corresponding to the interface specified for FRC-0046 token receiver. If the receiving hook aborts, when called, the transfer is discarded and this method returns an error - The operator MUST be initialised AND have an allowance not less than the requested value
Upon successful transfer:
- The from balance decreases by the requested value
- The to balance increases by the requested value
- The owner-operator allowance decreases by the requested value
Returns a ReceiverHook to call the recipient’s token receiver hook, and a TransferFromIntermediate struct. ReceiverHook must be called or it will panic and abort the transaction.
Return data from the hook should be passed to transfer_from_return which will generate the TransferFromReturn struct
sourcepub fn transfer_from_return(
&self,
intermediate: TransferFromIntermediate,
) -> Result<TransferFromReturn, TokenError>
pub fn transfer_from_return( &self, intermediate: TransferFromIntermediate, ) -> Result<TransferFromReturn, TokenError>
Generate TransferReturn from the intermediate data returned by a receiver hook call
sourcepub fn set_balance(
&mut self,
owner: &Address,
amount: &TokenAmount,
) -> Result<TokenAmount, TokenError>
pub fn set_balance( &mut self, owner: &Address, amount: &TokenAmount, ) -> Result<TokenAmount, TokenError>
Sets the balance of an account to a specific amount
Using this library method obeys internal invariants (changing total supply etc.) but does not invoke the receiver hook on recipient accounts. Returns the old balance.
source§impl<'st, S, BS> Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,
impl<'st, S, BS> Token<'st, S, BS>where
S: Syscalls,
BS: Blockstore,
sourcepub fn call_receiver_hook(
&mut self,
token_receiver: &Address,
params: FRC46TokenReceived,
) -> Result<(), TokenError>
pub fn call_receiver_hook( &mut self, token_receiver: &Address, params: FRC46TokenReceived, ) -> Result<(), TokenError>
Calls the receiver hook, returning the result
sourcepub fn assert_invariants(
&self,
) -> Result<StateSummary, Vec<StateInvariantError>>
pub fn assert_invariants( &self, ) -> Result<StateSummary, Vec<StateInvariantError>>
Checks the state invariants, throwing an error if they are not met
sourcepub fn check_invariants(&self) -> (StateSummary, Vec<StateInvariantError>)
pub fn check_invariants(&self) -> (StateSummary, Vec<StateInvariantError>)
Checks the state invariants, returning a state summary and list of errors
Auto Trait Implementations§
impl<'st, S, BS> Freeze for Token<'st, S, BS>
impl<'st, S, BS> RefUnwindSafe for Token<'st, S, BS>where
S: RefUnwindSafe,
BS: RefUnwindSafe,
impl<'st, S, BS> Send for Token<'st, S, BS>
impl<'st, S, BS> Sync for Token<'st, S, BS>
impl<'st, S, BS> Unpin for Token<'st, S, BS>
impl<'st, S, BS> !UnwindSafe for Token<'st, S, BS>
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> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> References<RawCodec> for T
impl<T> References<RawCodec> for T
source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.