Skip to content

Commit

Permalink
Merge pull request #11 from gear-foundation/lm-non-zero-U256
Browse files Browse the repository at this point in the history
NonZeroU256 to U256
  • Loading branch information
LouiseMedova authored Jul 29, 2024
2 parents 1767c1e + 2d147a0 commit d705a98
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion extended-vft/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions extended-vft/app/src/services/extended_vft/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ pub fn mint(
.checked_add(value)
.ok_or(Error::NumericOverflow)?;

let Some(non_zero_new_to) = NonZeroU256::new(new_to) else {
unreachable!("Infallible since fn is noop on zero value; qed");
};

balances.insert(to, non_zero_new_to);
balances.insert(to, new_to);
*total_supply = new_total_supply;

Ok(true)
Expand All @@ -47,11 +43,12 @@ pub fn burn(
.checked_sub(value)
.ok_or(Error::InsufficientBalance)?;

if let Some(non_zero_new_from) = NonZeroU256::new(new_from) {
balances.insert(from, non_zero_new_from);
if !new_from.is_zero() {
balances.insert(from, new_from);
} else {
balances.remove(&from);
}

*total_supply = new_total_supply;
Ok(true)
}
2 changes: 1 addition & 1 deletion extended-vft/wasm/.binpath
Original file line number Diff line number Diff line change
@@ -1 +1 @@
../target/wasm32-unknown-unknown/debug/extended_vft_wasm
../target/wasm32-unknown-unknown/release/extended_vft_wasm
32 changes: 14 additions & 18 deletions vft-service/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub fn allowance(allowances: &AllowancesMap, owner: ActorId, spender: ActorId) -
allowances
.get(&(owner, spender))
.cloned()
.map(Into::into)
.unwrap_or_default()
}

Expand All @@ -15,26 +14,26 @@ pub fn approve(
spender: ActorId,
value: U256,
) -> bool {

if owner == spender {
return false;
}

let key = (owner, spender);

let Some(non_zero_value) = NonZeroU256::new(value) else {
if value.is_zero() {
return allowances.remove(&key).is_some();
};

let prev = allowances.insert(key, non_zero_value);
}
let prev = allowances.insert(key, value);

prev.map(|v| v != non_zero_value).unwrap_or(true)
prev.map(|v| v != value).unwrap_or(true)
}

pub fn balance_of(balances: &BalancesMap, owner: ActorId) -> U256 {
balances
.get(&owner)
.cloned()
.map(Into::into)
.unwrap_or_default()
}

Expand All @@ -56,17 +55,14 @@ pub fn transfer(
.checked_add(value)
.ok_or(Error::NumericOverflow)?;

let Some(non_zero_new_to) = NonZeroU256::new(new_to) else {
unreachable!("Infallible since fn is noop on zero value; qed");
};

if let Some(non_zero_new_from) = NonZeroU256::new(new_from) {
balances.insert(from, non_zero_new_from);
if !new_from.is_zero() {
balances.insert(from, new_from);
} else {
balances.remove(&from);
}

balances.insert(to, non_zero_new_to);
balances.insert(to, new_to);

Ok(true)
}
Expand Down Expand Up @@ -96,8 +92,8 @@ pub fn transfer_from(

let key = (from, spender);

if let Some(non_zero_new_allowance) = NonZeroU256::new(new_allowance) {
allowances.insert(key, non_zero_new_allowance);
if !new_allowance.is_zero() {
allowances.insert(key, new_allowance);
} else {
allowances.remove(&key);
}
Expand Down Expand Up @@ -605,14 +601,14 @@ mod tests {
) -> AllowancesMap {
content
.into_iter()
.map(|(k1, k2, v)| ((k1, k2), NonZeroU256::new(v).unwrap()))
.map(|(k1, k2, v)| ((k1, k2), v))
.collect()
}

pub fn balances_map<const N: usize>(content: [(ActorId, U256); N]) -> BalancesMap {
content
.into_iter()
.map(|(k, v)| (k, NonZeroU256::new(v).unwrap()))
.map(|(k, v)| (k, v))
.collect()
}

Expand Down
6 changes: 3 additions & 3 deletions vft-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ static mut STORAGE: Option<Storage> = None;

#[derive(Debug, Default)]
pub struct Storage {
balances: HashMap<ActorId, NonZeroU256>,
allowances: HashMap<(ActorId, ActorId), NonZeroU256>,
balances: HashMap<ActorId, U256>,
allowances: HashMap<(ActorId, ActorId), U256>,
meta: Metadata,
total_supply: U256,
}
Expand All @@ -24,7 +24,7 @@ impl Storage {
pub fn get() -> &'static Self {
unsafe { STORAGE.as_ref().expect("Storage is not initialized") }
}
pub fn balances() -> &'static mut HashMap<ActorId, NonZeroU256> {
pub fn balances() -> &'static mut HashMap<ActorId, U256> {
let storage = unsafe { STORAGE.as_mut().expect("Storage is not initialized") };
&mut storage.balances
}
Expand Down
4 changes: 2 additions & 2 deletions vft-service/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::fmt::Debug;
use gstd::{collections::HashMap, ext, format, Decode, Encode, TypeInfo};
use sails_rs::prelude::*;
pub type AllowancesMap = HashMap<(ActorId, ActorId), NonZeroU256>;
pub type BalancesMap = HashMap<ActorId, NonZeroU256>;
pub type AllowancesMap = HashMap<(ActorId, ActorId), U256>;
pub type BalancesMap = HashMap<ActorId, U256>;
pub type Result<T, E = Error> = core::result::Result<T, E>;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, TypeInfo)]
Expand Down

0 comments on commit d705a98

Please sign in to comment.