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,

source

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

source

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

source

pub fn wrap( runtime: &'st ActorRuntime<S, BS>, granularity: u64, state: &'st mut TokenState, ) -> Self

Wrap an existing token state

source

pub fn replace(&mut self, state: TokenState) -> TokenState

Replace the current state with another The previous state is returned and can be safely dropped

source

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

source

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

source

pub fn flush(&mut self) -> Result<Cid, TokenError>

Flush state and return Cid for root

source

pub fn state(&self) -> &TokenState

Get a reference to the wrapped state tree

source

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,

source

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

source

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

source

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

source

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

source

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

source

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.

source

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

source

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

source

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

source

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

source

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
source

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
source

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

source

pub fn transfer_return( &self, intermediate: TransferIntermediate, ) -> Result<TransferReturn, TokenError>

Generate TransferReturn from the intermediate data returned by a receiver hook call

source

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

source

pub fn transfer_from_return( &self, intermediate: TransferFromIntermediate, ) -> Result<TransferFromReturn, TokenError>

Generate TransferReturn from the intermediate data returned by a receiver hook call

source

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,

source

pub fn call_receiver_hook( &mut self, token_receiver: &Address, params: FRC46TokenReceived, ) -> Result<(), TokenError>

Calls the receiver hook, returning the result

source

pub fn assert_invariants( &self, ) -> Result<StateSummary, Vec<StateInvariantError>>

Checks the state invariants, throwing an error if they are not met

source

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>

§

impl<'st, S, BS> Send for Token<'st, S, BS>
where S: Sync, BS: Sync,

§

impl<'st, S, BS> Sync for Token<'st, S, BS>
where S: Sync, BS: Sync,

§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Conv for T

source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
source§

impl<T> FmtForward for T

source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
source§

impl<T> Pipe for T
where T: ?Sized,

source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows 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
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows 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
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> References<RawCodec> for T

source§

fn references<R, E>(_c: RawCodec, _r: &mut R, _set: &mut E) -> Result<(), Error>
where R: Read, E: Extend<Cid<64>>,

Scrape the references from an impl Read. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> Tap for T

source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .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
where Self: BorrowMut<B>, B: ?Sized,

Calls .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
where Self: AsRef<R>, R: ?Sized,

Calls .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
where Self: AsMut<R>, R: ?Sized,

Calls .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
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> TryConv for T

source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V