Skip to content

Commit

Permalink
[nit] Improve Object::new_coin
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Jan 3, 2025
1 parent 02ce40e commit f8aa38c
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/sui-json-rpc/src/coin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ mod tests {
let previous_transaction = TransactionDigest::from(arr);
let object = Object::new_move(
MoveObject::new_coin(
coin_type_string.parse::<StructTag>().unwrap().into(),
coin_type_string.parse::<TypeTag>().unwrap(),
1.into(),
object_id,
balance,
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-types/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ impl MoveObjectType {
Self(MoveObjectType_::GasCoin)
}

pub fn coin(coin_type: TypeTag) -> Self {
Self(MoveObjectType_::Coin(coin_type))
}

pub fn staked_sui() -> Self {
Self(MoveObjectType_::StakedSui)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-types/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ pub struct Coin {
}

impl Coin {
pub fn new(id: UID, value: u64) -> Self {
pub fn new(id: ObjectID, value: u64) -> Self {
Self {
id,
id: UID::new(id),
balance: Balance::new(value),
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Coin {
// Split amount out of this coin to a new coin.
// Related coin objects need to be updated in temporary_store to persist the changes,
// including creating the coin object related to the newly created coin.
pub fn split(&mut self, amount: u64, new_coin_id: UID) -> Result<Coin, ExecutionError> {
pub fn split(&mut self, amount: u64, new_coin_id: ObjectID) -> Result<Coin, ExecutionError> {
self.balance.withdraw(amount)?;
Ok(Coin::new(new_coin_id, amount))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-types/src/gas_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod checked {

impl GasCoin {
pub fn new(id: ObjectID, value: u64) -> Self {
Self(Coin::new(UID::new(id), value))
Self(Coin::new(id, value))
}

pub fn value(&self) -> u64 {
Expand Down
11 changes: 3 additions & 8 deletions crates/sui-types/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,14 @@ impl MoveObject {
}
}

pub fn new_coin(
coin_type: MoveObjectType,
version: SequenceNumber,
id: ObjectID,
value: u64,
) -> Self {
pub fn new_coin(coin_type: TypeTag, version: SequenceNumber, id: ObjectID, value: u64) -> Self {
// unwrap safe because coins are always smaller than the max object size
unsafe {
Self::new_from_execution_with_limit(
coin_type,
MoveObjectType::coin(coin_type),
true,
version,
GasCoin::new(id, value).to_bcs_bytes(),
Coin::new(id, value).to_bcs_bytes(),
256,
)
.unwrap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod checked {
let amount: u64 =
context.by_value_arg(CommandKind::SplitCoins, 1, amount_arg)?;
let new_coin_id = context.fresh_id()?;
let new_coin = coin.split(amount, UID::new(new_coin_id))?;
let new_coin = coin.split(amount, new_coin_id)?;
let coin_type = obj.type_.clone();
// safe because we are propagating the coin type, and relying on the internal
// invariant that coin values have a coin type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ mod checked {
let amount: u64 =
context.by_value_arg(CommandKind::SplitCoins, 1, amount_arg)?;
let new_coin_id = context.fresh_id()?;
let new_coin = coin.split(amount, UID::new(new_coin_id))?;
let new_coin = coin.split(amount, new_coin_id)?;
let coin_type = obj.type_.clone();
// safe because we are propagating the coin type, and relying on the internal
// invariant that coin values have a coin type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ mod checked {
let amount: u64 =
context.by_value_arg(CommandKind::SplitCoins, 1, amount_arg)?;
let new_coin_id = context.fresh_id()?;
let new_coin = coin.split(amount, UID::new(new_coin_id))?;
let new_coin = coin.split(amount, new_coin_id)?;
let coin_type = obj.type_.clone();
// safe because we are propagating the coin type, and relying on the internal
// invariant that coin values have a coin type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mod checked {
let amount: u64 =
context.by_value_arg(CommandKind::SplitCoins, 1, amount_arg)?;
let new_coin_id = context.fresh_id()?;
let new_coin = coin.split(amount, UID::new(new_coin_id))?;
let new_coin = coin.split(amount, new_coin_id)?;
let coin_type = obj.type_.clone();
// safe because we are propagating the coin type, and relying on the internal
// invariant that coin values have a coin type
Expand Down

0 comments on commit f8aa38c

Please sign in to comment.