From f8aa38cda39cbdc4c37b26058c9dd7c0b03ecd9f 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 | 2 +- crates/sui-types/src/object.rs | 11 +++-------- .../src/programmable_transactions/execution.rs | 2 +- .../src/programmable_transactions/execution.rs | 2 +- .../src/programmable_transactions/execution.rs | 2 +- .../src/programmable_transactions/execution.rs | 2 +- 9 files changed, 16 insertions(+), 17 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..df85f22e8359c 100644 --- a/crates/sui-types/src/gas_coin.rs +++ b/crates/sui-types/src/gas_coin.rs @@ -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 { 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..f8419b50cc1ee 100644 --- a/sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/latest/sui-adapter/src/programmable_transactions/execution.rs @@ -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..3e0c0838d2a62 100644 --- a/sui-execution/v0/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/v0/sui-adapter/src/programmable_transactions/execution.rs @@ -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..f0b123e039b68 100644 --- a/sui-execution/v1/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/v1/sui-adapter/src/programmable_transactions/execution.rs @@ -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..89759743586bf 100644 --- a/sui-execution/v2/sui-adapter/src/programmable_transactions/execution.rs +++ b/sui-execution/v2/sui-adapter/src/programmable_transactions/execution.rs @@ -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