Skip to content

Commit 272041b

Browse files
convert market actor
1 parent 75342a3 commit 272041b

File tree

5 files changed

+214
-344
lines changed

5 files changed

+214
-344
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actors/market/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ ahash = "0.7.6"
2424
serde = { version = "1.0.136", features = ["derive"] }
2525
cid = { version = "0.8.3", default-features = false, features = ["serde-codec"] }
2626
log = "0.4.14"
27-
anyhow = "1.0.56"
2827
fvm_ipld_blockstore = { version = "0.1" }
2928
fvm_ipld_encoding = "0.1.0"
3029
libipld-core = { version = "0.13.1", features = ["serde-codec"] }

actors/market/src/balance_table.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use fvm_shared::bigint::bigint_ser::BigIntDe;
99
use fvm_shared::econ::TokenAmount;
1010
use num_traits::{Signed, Zero};
1111

12-
use fil_actors_runtime::{make_empty_map, make_map_with_root_and_bitwidth, Map};
12+
use fil_actors_runtime::{
13+
actor_error, make_empty_map, make_map_with_root_and_bitwidth, ActorError, Map,
14+
};
1315

1416
pub const BALANCE_TABLE_BITWIDTH: u32 = 6;
1517

@@ -25,17 +27,17 @@ where
2527
}
2628

2729
/// Initializes a balance table from a root Cid
28-
pub fn from_root(bs: &'a BS, cid: &Cid) -> Result<Self, HamtError> {
30+
pub fn from_root(bs: &'a BS, cid: &Cid) -> Result<Self, HamtError<BS::Error>> {
2931
Ok(Self(make_map_with_root_and_bitwidth(cid, bs, BALANCE_TABLE_BITWIDTH)?))
3032
}
3133

3234
/// Retrieve root from balance table
33-
pub fn root(&mut self) -> Result<Cid, HamtError> {
35+
pub fn root(&mut self) -> Result<Cid, HamtError<BS::Error>> {
3436
self.0.flush()
3537
}
3638

3739
/// Gets token amount for given address in balance table
38-
pub fn get(&self, key: &Address) -> Result<TokenAmount, HamtError> {
40+
pub fn get(&self, key: &Address) -> Result<TokenAmount, HamtError<BS::Error>> {
3941
if let Some(v) = self.0.get(&key.to_bytes())? {
4042
Ok(v.0.clone())
4143
} else {
@@ -44,12 +46,17 @@ where
4446
}
4547

4648
/// Adds token amount to previously initialized account.
47-
pub fn add(&mut self, key: &Address, value: &TokenAmount) -> Result<(), HamtError> {
49+
pub fn add(&mut self, key: &Address, value: &TokenAmount) -> Result<(), ActorError> {
4850
let prev = self.get(key)?;
4951
let sum = &prev + value;
5052
if sum.is_negative() {
51-
Err(format!("New balance in table cannot be negative: {}", sum).into())
52-
} else if sum.is_zero() && !prev.is_zero() {
53+
return Err(actor_error!(
54+
illegal_argument,
55+
"new balance in table cannot be negative: {}",
56+
sum
57+
));
58+
}
59+
if sum.is_zero() && !prev.is_zero() {
5360
self.0.delete(&key.to_bytes())?;
5461
Ok(())
5562
} else {
@@ -66,7 +73,7 @@ where
6673
key: &Address,
6774
req: &TokenAmount,
6875
floor: &TokenAmount,
69-
) -> Result<TokenAmount, HamtError> {
76+
) -> Result<TokenAmount, ActorError> {
7077
let prev = self.get(key)?;
7178
let available = std::cmp::max(TokenAmount::zero(), prev - floor);
7279
let sub: TokenAmount = std::cmp::min(&available, req).clone();
@@ -79,24 +86,24 @@ where
7986
}
8087

8188
/// Subtracts value from a balance, and errors if full amount was not substracted.
82-
pub fn must_subtract(&mut self, key: &Address, req: &TokenAmount) -> Result<(), HamtError> {
89+
pub fn must_subtract(&mut self, key: &Address, req: &TokenAmount) -> Result<(), ActorError> {
8390
let prev = self.get(key)?;
8491

8592
if req > &prev {
86-
Err("couldn't subtract the requested amount".into())
87-
} else {
88-
self.add(key, &-req)
93+
return Err(actor_error!(illegal_argument, "couldn't subtract the requested amount"));
8994
}
95+
self.add(key, &-req)?;
96+
97+
Ok(())
9098
}
9199

92100
/// Returns total balance held by this balance table
93101
#[allow(dead_code)]
94-
pub fn total(&self) -> Result<TokenAmount, HamtError> {
102+
pub fn total(&self) -> Result<TokenAmount, HamtError<BS::Error>> {
95103
let mut total = TokenAmount::default();
96104

97105
self.0.for_each(|_, v: &BigIntDe| {
98106
total += &v.0;
99-
Ok(())
100107
})?;
101108

102109
Ok(total)

0 commit comments

Comments
 (0)