Skip to content

Commit 18759e2

Browse files
committed
cleanup
1 parent 788f80d commit 18759e2

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

fil_token/src/token/mod.rs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use fvm_shared::bigint::bigint_ser::BigIntDe;
99
use fvm_shared::bigint::BigInt;
1010
use fvm_shared::bigint::Zero;
1111
use fvm_shared::econ::TokenAmount;
12-
use fvm_shared::ActorID;
1312

1413
use self::errors::ActorError;
1514
use self::state::TokenState;
@@ -83,15 +82,7 @@ where
8382
/// Injected blockstore
8483
bs: BS,
8584
/// Access to the runtime
86-
fvm: FVM,
87-
}
88-
89-
/// Resolve an address to an ActorID
90-
fn resolve_address(address: &Address) -> Result<ActorID> {
91-
match fvm_sdk::actor::resolve_address(address) {
92-
Some(addr) => Ok(addr),
93-
None => Err(ActorError::AddrNotFound(*address)),
94-
}
85+
_fvm: FVM,
9586
}
9687

9788
impl<BS, FVM> StandardToken<BS, FVM>
@@ -114,7 +105,7 @@ where
114105
init_state.save(&self.bs);
115106

116107
let mint_params = params.mint_params;
117-
self.mint(mint_params);
108+
self.mint(mint_params)?;
118109
Ok(())
119110
}
120111

@@ -157,21 +148,16 @@ where
157148
let balance = balances
158149
.delete(&holder)?
159150
.map(|de| de.1 .0)
160-
.unwrap_or(TokenAmount::zero());
151+
.unwrap_or_else(TokenAmount::zero);
161152
let new_balance = balance
162153
.checked_add(&params.value)
163-
.ok_or(ActorError::Arithmetic(String::from(
164-
"Minting into caused overflow",
165-
)))?;
154+
.ok_or_else(|| ActorError::Arithmetic(String::from("Minting into caused overflow")))?;
166155
balances.set(holder, BigIntDe(new_balance))?;
167156

168157
// set the global supply of the contract
169-
let new_supply = state
170-
.supply
171-
.checked_add(&params.value)
172-
.ok_or(ActorError::Arithmetic(String::from(
173-
"Minting caused total supply to overflow",
174-
)))?;
158+
let new_supply = state.supply.checked_add(&params.value).ok_or_else(|| {
159+
ActorError::Arithmetic(String::from("Minting caused total supply to overflow"))
160+
})?;
175161
state.supply = new_supply;
176162

177163
// commit the state if supply and balance increased
@@ -230,7 +216,7 @@ where
230216
},
231217
// No allowance recorded previously
232218
None => {
233-
caller_allowances_map.set(spender, BigIntDe(params.value.clone()));
219+
caller_allowances_map.set(spender, BigIntDe(params.value.clone()))?;
234220
params.value
235221
}
236222
};
@@ -263,7 +249,7 @@ where
263249
.checked_sub(&params.value)
264250
.unwrap() // Unwrap should be safe as allowance always > 0
265251
.max(BigInt::zero());
266-
caller_allowances_map.set(spender, BigIntDe(new_allowance.clone()));
252+
caller_allowances_map.set(spender, BigIntDe(new_allowance.clone()))?;
267253
new_allowance
268254
}
269255
None => {
@@ -298,7 +284,7 @@ where
298284
};
299285

300286
let new_allowance = TokenAmount::zero();
301-
caller_allowances_map.set(spender, BigIntDe(new_allowance.clone()));
287+
caller_allowances_map.set(spender, BigIntDe(new_allowance.clone()))?;
302288
state.save(&self.bs);
303289

304290
Ok(AllowanceReturn {
@@ -336,11 +322,11 @@ where
336322
})
337323
}
338324

339-
fn burn(&self, params: BurnParams) -> Result<BurnReturn> {
325+
fn burn(&self, _params: BurnParams) -> Result<BurnReturn> {
340326
todo!()
341327
}
342328

343-
fn transfer_from(&self, params: TransferParams) -> Result<TransferReturn> {
329+
fn transfer_from(&self, _params: TransferParams) -> Result<TransferReturn> {
344330
todo!()
345331
}
346332
}

fil_token/src/token/state.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use std::ops::Add;
2-
use std::ops::Sub;
3-
41
use anyhow::anyhow;
52
use cid::multihash::Code;
63
use cid::Cid;
@@ -16,13 +13,10 @@ use fvm_sdk::sself;
1613
use fvm_shared::address::Address;
1714
use fvm_shared::bigint::bigint_ser;
1815
use fvm_shared::bigint::bigint_ser::BigIntDe;
19-
use fvm_shared::bigint::Zero;
2016
use fvm_shared::econ::TokenAmount;
2117
use fvm_shared::ActorID;
2218
use fvm_shared::HAMT_BIT_WIDTH;
2319

24-
use crate::blockstore::Blockstore;
25-
2620
/// A macro to abort concisely.
2721
macro_rules! abort {
2822
($code:ident, $msg:literal $(, $ex:expr)*) => {
@@ -96,7 +90,7 @@ impl TokenState {
9690
codec: DAG_CBOR,
9791
data: serialized,
9892
};
99-
let cid = match bs.put(Code::Blake2b256.into(), &block) {
93+
let cid = match bs.put(Code::Blake2b256, &block) {
10094
Ok(cid) => cid,
10195
Err(err) => abort!(USR_SERIALIZATION, "failed to store initial state: {:}", err),
10296
};
@@ -107,26 +101,24 @@ impl TokenState {
107101
}
108102

109103
pub fn get_balance_map<BS: IpldStore + Copy>(&self, bs: &BS) -> Hamt<BS, BigIntDe, ActorID> {
110-
let balances = match Hamt::<BS, BigIntDe, ActorID>::load(&self.balances, *bs) {
104+
match Hamt::<BS, BigIntDe, ActorID>::load(&self.balances, *bs) {
111105
Ok(map) => map,
112106
Err(err) => abort!(USR_ILLEGAL_STATE, "Failed to load balances hamt: {:?}", err),
113-
};
114-
balances
107+
}
115108
}
116109

117110
/// Get the global allowances map
118111
///
119112
/// Gets a HAMT with CIDs linking to other HAMTs
120113
pub fn get_allowances_map<BS: IpldStore + Copy>(&self, bs: &BS) -> Hamt<BS, Cid, ActorID> {
121-
let allowances = match Hamt::<BS, Cid, ActorID>::load(&self.allowances, *bs) {
114+
match Hamt::<BS, Cid, ActorID>::load(&self.allowances, *bs) {
122115
Ok(map) => map,
123116
Err(err) => abort!(
124117
USR_ILLEGAL_STATE,
125118
"Failed to load allowances hamt: {:?}",
126119
err
127120
),
128-
};
129-
allowances
121+
}
130122
}
131123

132124
/// Get the allowances map of a specific actor, lazily creating one if it didn't exist
@@ -259,8 +251,6 @@ pub struct TokenAmountDiff {
259251
pub actual: TokenAmount,
260252
}
261253

262-
type TransferResult<T> = std::result::Result<T, TransferError>;
263-
264254
pub enum TransferError {
265255
NoRecrHook,
266256
InsufficientAllowance(TokenAmountDiff),

fvm_dispatch/src/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<T: Hasher> MethodHasher<T> {
5252
fn as_u32(bytes: &[u8]) -> u32 {
5353
((bytes[0] as u32) << (8 * 3)) +
5454
((bytes[1] as u32) << (8 * 2)) +
55-
((bytes[2] as u32) << (8 * 1)) +
55+
((bytes[2] as u32) << 8) +
5656
(bytes[3] as u32)
5757
}
5858

0 commit comments

Comments
 (0)