From 50a986a23429a4fa232ff1e4283325d871c4e52a Mon Sep 17 00:00:00 2001 From: Xun Li Date: Fri, 3 Jan 2025 10:38:20 -0800 Subject: [PATCH] [nit] Improve Object::new_coin --- crates/sui-json-rpc/src/coin_api.rs | 2 +- crates/sui-types/src/base_types.rs | 4 ++++ crates/sui-types/src/coin.rs | 6 +++--- crates/sui-types/src/gas_coin.rs | 3 +-- crates/sui-types/src/object.rs | 11 +++-------- .../src/programmable_transactions/execution.rs | 4 ++-- .../src/programmable_transactions/execution.rs | 4 ++-- .../src/programmable_transactions/execution.rs | 4 ++-- .../src/programmable_transactions/execution.rs | 4 ++-- 9 files changed, 20 insertions(+), 22 deletions(-) diff --git a/crates/sui-json-rpc/src/coin_api.rs b/crates/sui-json-rpc/src/coin_api.rs index 3ca4cda4f5124..6d5a9c37fe284 100644 --- a/crates/sui-json-rpc/src/coin_api.rs +++ b/crates/sui-json-rpc/src/coin_api.rs @@ -550,7 +550,7 @@ mod tests { let previous_transaction = TransactionDigest::from(arr); let object = Object::new_move( MoveObject::new_coin( - coin_type_string.parse::().unwrap().into(), + coin_type_string.parse::().unwrap(), 1.into(), object_id, balance, diff --git a/crates/sui-types/src/base_types.rs b/crates/sui-types/src/base_types.rs index 9c28ad12d1766..0d8fed32bf4db 100644 --- a/crates/sui-types/src/base_types.rs +++ b/crates/sui-types/src/base_types.rs @@ -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) } diff --git a/crates/sui-types/src/coin.rs b/crates/sui-types/src/coin.rs index 6c2fc3abd6ece..3c07fd5da2a68 100644 --- a/crates/sui-types/src/coin.rs +++ b/crates/sui-types/src/coin.rs @@ -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), } } @@ -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 { + pub fn split(&mut self, amount: u64, new_coin_id: ObjectID) -> Result { self.balance.withdraw(amount)?; Ok(Coin::new(new_coin_id, amount)) } diff --git a/crates/sui-types/src/gas_coin.rs b/crates/sui-types/src/gas_coin.rs index 838016c8beffc..859fd05c8aa3f 100644 --- a/crates/sui-types/src/gas_coin.rs +++ b/crates/sui-types/src/gas_coin.rs @@ -16,7 +16,6 @@ use crate::{ base_types::{ObjectID, SequenceNumber}, coin::Coin, error::{ExecutionError, ExecutionErrorKind}, - id::UID, object::{Data, MoveObject, Object}, SUI_FRAMEWORK_ADDRESS, }; @@ -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 { diff --git a/crates/sui-types/src/object.rs b/crates/sui-types/src/object.rs index db9971c1192bd..bf21c86d8e305 100644 --- a/crates/sui-types/src/object.rs +++ b/crates/sui-types/src/object.rs @@ -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() diff --git a/sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs b/sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs index 2b72db7222021..e78b49d0a04bc 100644 --- a/sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs @@ -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, @@ -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 diff --git a/sui-execution/v0/sui-adapter/src/programmable_transactions/execution.rs b/sui-execution/v0/sui-adapter/src/programmable_transactions/execution.rs index 18b20ada7b3dc..dc21c1122263b 100644 --- a/sui-execution/v0/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/v0/sui-adapter/src/programmable_transactions/execution.rs @@ -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, @@ -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 diff --git a/sui-execution/v1/sui-adapter/src/programmable_transactions/execution.rs b/sui-execution/v1/sui-adapter/src/programmable_transactions/execution.rs index e0ec4766306f7..61f1b775c619a 100644 --- a/sui-execution/v1/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/v1/sui-adapter/src/programmable_transactions/execution.rs @@ -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, @@ -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 diff --git a/sui-execution/v2/sui-adapter/src/programmable_transactions/execution.rs b/sui-execution/v2/sui-adapter/src/programmable_transactions/execution.rs index ac751d9d4a72c..d4fb4328a3c5f 100644 --- a/sui-execution/v2/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/v2/sui-adapter/src/programmable_transactions/execution.rs @@ -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, @@ -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