Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nit] Minor improvements to Object::new_coin #20771

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 1 addition & 2 deletions crates/sui-types/src/gas_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::{
base_types::{ObjectID, SequenceNumber},
coin::Coin,
error::{ExecutionError, ExecutionErrorKind},
id::UID,
object::{Data, MoveObject, Object},
SUI_FRAMEWORK_ADDRESS,
};
Expand Down Expand Up @@ -73,7 +72,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 @@ -47,7 +47,7 @@ mod checked {
},
coin::Coin,
error::{command_argument_error, ExecutionError, ExecutionErrorKind},
id::{RESOLVED_SUI_ID, UID},
id::RESOLVED_SUI_ID,
metrics::LimitsMetrics,
move_package::{
normalize_deserialized_modules, MovePackage, UpgradeCap, UpgradePolicy, UpgradeReceipt,
Expand Down 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 @@ -48,7 +48,7 @@ mod checked {
},
coin::Coin,
error::{command_argument_error, ExecutionError, ExecutionErrorKind},
id::{RESOLVED_SUI_ID, UID},
id::RESOLVED_SUI_ID,
metrics::LimitsMetrics,
move_package::{
normalize_deserialized_modules, MovePackage, TypeOrigin, UpgradeCap, UpgradePolicy,
Expand Down 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 @@ -46,7 +46,7 @@ mod checked {
},
coin::Coin,
error::{command_argument_error, ExecutionError, ExecutionErrorKind},
id::{RESOLVED_SUI_ID, UID},
id::RESOLVED_SUI_ID,
metrics::LimitsMetrics,
move_package::{
normalize_deserialized_modules, MovePackage, UpgradeCap, UpgradePolicy, UpgradeReceipt,
Expand Down 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 @@ -46,7 +46,7 @@ mod checked {
},
coin::Coin,
error::{command_argument_error, ExecutionError, ExecutionErrorKind},
id::{RESOLVED_SUI_ID, UID},
id::RESOLVED_SUI_ID,
metrics::LimitsMetrics,
move_package::{
normalize_deserialized_modules, MovePackage, UpgradeCap, UpgradePolicy, UpgradeReceipt,
Expand Down 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
Loading