Struct frc46_token::token::state::TokenState
source · pub struct TokenState {
pub supply: TokenAmount,
pub balances: Cid,
pub allowances: Cid,
/* private fields */
}
Expand description
Token state IPLD structure
Fields§
§supply: TokenAmount
Total supply of token
balances: Cid
Map<ActorId, TokenAmount> of balances as a Hamt
allowances: Cid
Map<ActorId, Map<ActorId, TokenAmount>> as a Hamt. Allowances are stored balances[owner][operator]
Implementations§
source§impl TokenState
impl TokenState
An abstraction over the IPLD layer to get and modify token state without dealing with HAMTs etc.
This is a simple wrapper of state and in general does not account for token protocol level checks such as ensuring necessary approvals are enforced during transfers. This is left for the caller to handle. However, some invariants such as non-negative balances, allowances and total supply are enforced.
sourcepub fn new<BS: Blockstore>(store: &BS) -> Result<Self, StateError>
pub fn new<BS: Blockstore>(store: &BS) -> Result<Self, StateError>
Create a new token state-tree, without committing it (the root cid) to a blockstore
sourcepub fn new_with_bit_width<BS: Blockstore>(
store: &BS,
hamt_bit_width: u32,
) -> Result<Self, StateError>
pub fn new_with_bit_width<BS: Blockstore>( store: &BS, hamt_bit_width: u32, ) -> Result<Self, StateError>
Create a new token state-tree, without committing it (the root cid) to a blockstore
Explicitly sets the bit width of underlying Hamt structures. Caller must ensure 1 <= hamt_bit_width <= 8.
sourcepub fn load<BS: Blockstore>(bs: &BS, cid: &Cid) -> Result<Self, StateError>
pub fn load<BS: Blockstore>(bs: &BS, cid: &Cid) -> Result<Self, StateError>
Loads a fresh copy of the state from a blockstore from a given cid
sourcepub fn save<BS: Blockstore>(&self, bs: &BS) -> Result<Cid, StateError>
pub fn save<BS: Blockstore>(&self, bs: &BS) -> Result<Cid, StateError>
Saves the current state to the blockstore, returning the cid
sourcepub fn get_balance<BS: Blockstore>(
&self,
bs: &BS,
owner: ActorID,
) -> Result<TokenAmount, StateError>
pub fn get_balance<BS: Blockstore>( &self, bs: &BS, owner: ActorID, ) -> Result<TokenAmount, StateError>
Get the balance of an ActorID from the currently stored state
sourcepub fn change_balance_by<BS: Blockstore>(
&mut self,
bs: &BS,
owner: ActorID,
delta: &TokenAmount,
) -> Result<TokenAmount, StateError>
pub fn change_balance_by<BS: Blockstore>( &mut self, bs: &BS, owner: ActorID, delta: &TokenAmount, ) -> Result<TokenAmount, StateError>
Changes the balance of the specified account by the delta
Caller must ensure that the sign of of the delta is consistent with token rules (i.e. negative transfers, burns etc. are not allowed). Returns the new balance of the account.
sourcepub fn set_balance<BS: Blockstore>(
&mut self,
bs: &BS,
owner: ActorID,
new_balance: &TokenAmount,
) -> Result<TokenAmount, StateError>
pub fn set_balance<BS: Blockstore>( &mut self, bs: &BS, owner: ActorID, new_balance: &TokenAmount, ) -> Result<TokenAmount, StateError>
Set the balance of the account returning the old balance
Consistent with change_balance_by
, this method does not change the total supply. Business
logic to reconcile the total supply with changes in balancesis the responsibility of the
caller.
sourcepub fn get_balance_map<'bs, BS: Blockstore>(
&self,
bs: &'bs BS,
) -> Result<Hamt<&'bs BS, TokenAmount, BytesKey>, StateError>
pub fn get_balance_map<'bs, BS: Blockstore>( &self, bs: &'bs BS, ) -> Result<Hamt<&'bs BS, TokenAmount, BytesKey>, StateError>
Retrieve the balance map as a HAMT
sourcepub fn make_transfer<BS: Blockstore>(
&mut self,
bs: &BS,
from: ActorID,
to: ActorID,
amount: &TokenAmount,
) -> Result<(), StateError>
pub fn make_transfer<BS: Blockstore>( &mut self, bs: &BS, from: ActorID, to: ActorID, amount: &TokenAmount, ) -> Result<(), StateError>
Record a transfer of an amount between two accounts
It is the caller’s responsibility to ensure that allowance invariants are upheld. The caller should check that the amount is non-negative and complies with the token granularity.
sourcepub fn count_balances<BS: Blockstore>(
&self,
bs: &BS,
) -> Result<usize, StateError>
pub fn count_balances<BS: Blockstore>( &self, bs: &BS, ) -> Result<usize, StateError>
Retrieve the number of token holders
This involves iterating through the entire HAMT
sourcepub fn change_supply_by(
&mut self,
delta: &TokenAmount,
) -> Result<&TokenAmount, StateError>
pub fn change_supply_by( &mut self, delta: &TokenAmount, ) -> Result<&TokenAmount, StateError>
Increase/decrease the total supply by the specified value
Returns the new total supply
sourcepub fn get_allowance_between<BS: Blockstore>(
&self,
bs: &BS,
owner: ActorID,
operator: ActorID,
) -> Result<TokenAmount, StateError>
pub fn get_allowance_between<BS: Blockstore>( &self, bs: &BS, owner: ActorID, operator: ActorID, ) -> Result<TokenAmount, StateError>
Get the allowance that an owner has approved for a operator
If an existing allowance cannot be found, it is implicitly assumed to be zero
sourcepub fn change_allowance_by<BS: Blockstore>(
&mut self,
bs: &BS,
owner: ActorID,
operator: ActorID,
delta: &TokenAmount,
) -> Result<TokenAmount, StateError>
pub fn change_allowance_by<BS: Blockstore>( &mut self, bs: &BS, owner: ActorID, operator: ActorID, delta: &TokenAmount, ) -> Result<TokenAmount, StateError>
Change the allowance between owner and operator by the specified delta
sourcepub fn revoke_allowance<BS: Blockstore>(
&mut self,
bs: &BS,
owner: ActorID,
operator: ActorID,
) -> Result<TokenAmount, StateError>
pub fn revoke_allowance<BS: Blockstore>( &mut self, bs: &BS, owner: ActorID, operator: ActorID, ) -> Result<TokenAmount, StateError>
Revokes an approved allowance by removing the entry from the owner-operator map
If that map becomes empty, it is removed from the root map. Returns the old allowance
sourcepub fn set_allowance<BS: Blockstore>(
&mut self,
bs: &BS,
owner: ActorID,
operator: ActorID,
amount: &TokenAmount,
) -> Result<TokenAmount, StateError>
pub fn set_allowance<BS: Blockstore>( &mut self, bs: &BS, owner: ActorID, operator: ActorID, amount: &TokenAmount, ) -> Result<TokenAmount, StateError>
Set the allowance between owner and operator to a specific amount, returning the old allowance
sourcepub fn attempt_use_allowance<BS: Blockstore>(
&mut self,
bs: &BS,
operator: u64,
owner: u64,
amount: &TokenAmount,
) -> Result<TokenAmount, StateError>
pub fn attempt_use_allowance<BS: Blockstore>( &mut self, bs: &BS, operator: u64, owner: u64, amount: &TokenAmount, ) -> Result<TokenAmount, StateError>
Atomically checks if value is less than the allowance and deducts it if so
Returns new allowance if successful, else returns an error and the allowance is unchanged
sourcepub fn get_owner_allowance_map<'bs, BS: Blockstore>(
&self,
bs: &'bs BS,
owner: ActorID,
) -> Result<Option<Hamt<&'bs BS, TokenAmount, BytesKey>>, StateError>
pub fn get_owner_allowance_map<'bs, BS: Blockstore>( &self, bs: &'bs BS, owner: ActorID, ) -> Result<Option<Hamt<&'bs BS, TokenAmount, BytesKey>>, StateError>
Get the allowances map of a specific actor, resolving the CID link to a Hamt
Ok(Some) if the owner has allocated allowances to other actors Ok(None) if the owner has no current non-zero allowances to other actors Err if operations on the underlying Hamt failed
sourcepub fn get_allowances_map<'bs, BS: Blockstore>(
&self,
bs: &'bs BS,
) -> Result<Hamt<&'bs BS, Cid, BytesKey>, StateError>
pub fn get_allowances_map<'bs, BS: Blockstore>( &self, bs: &'bs BS, ) -> Result<Hamt<&'bs BS, Cid, BytesKey>, StateError>
Get the global allowances map
Gets a HAMT with CIDs linking to other HAMTs
source§impl TokenState
impl TokenState
sourcepub fn check_invariants<BS: Blockstore>(
&self,
bs: &BS,
granularity: u64,
) -> (StateSummary, Vec<StateInvariantError>)
pub fn check_invariants<BS: Blockstore>( &self, bs: &BS, granularity: u64, ) -> (StateSummary, Vec<StateInvariantError>)
Checks that the current state obeys all system invariants
Checks that there are no zero balances, zero allowances or empty allowance maps explicitly stored in the blockstore. Checks that balances, total supply, allowances are never negative. Checks that sum of all balances matches total_supply. Checks that no allowances are stored where operator == owner. Checks that all balances are a multiple of the granularity.
Returns a state summary that can be used to check application specific invariants and a list of errors that were found.
Trait Implementations§
source§impl Clone for TokenState
impl Clone for TokenState
source§fn clone(&self) -> TokenState
fn clone(&self) -> TokenState
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for TokenState
impl Debug for TokenState
source§impl<'de> Deserialize<'de> for TokenState
impl<'de> Deserialize<'de> for TokenState
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl PartialEq for TokenState
impl PartialEq for TokenState
source§fn eq(&self, other: &TokenState) -> bool
fn eq(&self, other: &TokenState) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl Serialize for TokenState
impl Serialize for TokenState
impl Eq for TokenState
impl StructuralPartialEq for TokenState
Auto Trait Implementations§
impl Freeze for TokenState
impl RefUnwindSafe for TokenState
impl Send for TokenState
impl Sync for TokenState
impl Unpin for TokenState
impl UnwindSafe for TokenState
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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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.