Skip to content

Commit

Permalink
[Rosetta] fix: Handle a case when all the balances do not exactly mat…
Browse files Browse the repository at this point in the history
…ch out
  • Loading branch information
nonvis committed Jan 24, 2025
1 parent c566c47 commit 4004396
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
6 changes: 2 additions & 4 deletions crates/sui-rosetta/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,7 @@ impl Operations {
/// If GasCoin is transferred as a part of transferObjects, operations need to be
/// updated such that:
/// 1) gas owner needs to be assigned back to the previous owner
/// 2) SuiBalanceChange type needs to be converted to PaySui for
/// previous and new gas owners and their balances need to be adjusted for the gas
/// 2) balances of previous and new gas owners need to be adjusted for the gas
fn process_gascoin_transfer(
coin_change_operations: &mut impl Iterator<Item = Operation>,
tx: SuiTransactionBlockKind,
Expand Down Expand Up @@ -638,11 +637,10 @@ impl Operations {
.amount
.as_mut()
.ok_or_else(|| anyhow!("Missing amount for a balance-change"))?;
// adjust the balances for previous and new gas_owners
if account.address == prev_gas_owner && amount.currency == *SUI {
// previous owner's balance needs to be adjusted for gas
amount.value -= gas_used;
} else if account.address == new_gas_owner && amount.currency == *SUI {
// new owner's balance needs to be adjusted for gas
amount.value += gas_used;
}
}
Expand Down
19 changes: 9 additions & 10 deletions crates/sui-rosetta/tests/end_to_end_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use anyhow::anyhow;
use rand::rngs::OsRng;
use rand::seq::IteratorRandom;
use rosetta_client::start_rosetta_test_server;
use serde_json::json;
use shared_crypto::intent::Intent;
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -673,8 +673,7 @@ async fn test_balance_from_obj_merge_to_gas() {
let test_cluster = TestClusterBuilder::new().build().await;
const SENDER: &str = "0x6293e2b4434265fa60ac8ed96342b7a288c0e43ffe737ba40feb24f06fed305d";
const RECIPIENT: &str = "0x0e3225553e3b945b4cde5621a980297c45b96002f33c95d3306e58013129ee7c";
// let sender = test_cluster.get_address_0();
// let recipient = test_cluster.get_address_1();
const VAULT_BALANCE: i128 = 200000000000;
let client = test_cluster.wallet.get_client().await.unwrap();
let response: SuiTransactionBlockResponse = serde_json::from_value(json!({
"digest": "GL2e3C8M1XGXfbyTbNmWcEhXg3kKF1fJViYwEzFdd8iu",
Expand Down Expand Up @@ -827,30 +826,30 @@ async fn test_balance_from_obj_merge_to_gas() {
"operations: {}",
serde_json::to_string_pretty(&operations).unwrap()
);
let pay_sui_sender = operations
let sui_sender = operations
.clone()
.into_iter()
.find(|op| {
op.type_ == OperationType::PaySui
op.type_ == OperationType::SuiBalanceChange
&& op.account
== Some(AccountIdentifier::from(
SuiAddress::from_str(SENDER).unwrap(),
))
})
.unwrap();
let pay_sui_recipient = operations
let sui_recipient = operations
.into_iter()
.find(|op| {
op.type_ == OperationType::PaySui
op.type_ == OperationType::SuiBalanceChange
&& op.account
== Some(AccountIdentifier::from(
SuiAddress::from_str(RECIPIENT).unwrap(),
))
})
.unwrap();
assert_eq!(
-pay_sui_sender.amount.unwrap().value,
pay_sui_recipient.amount.unwrap().value
-sui_sender.amount.unwrap().value + VAULT_BALANCE,
sui_recipient.amount.unwrap().value
);
}

Expand All @@ -859,7 +858,7 @@ async fn test_balance_from_obj_merge_to_gas() {
/// returned to sender.
/// This checks to see when GAS_COST is transferred back to the sender, which is an edge case.
/// In this case `process_gascoin_transfer` should be not processed.
///
///
/// ```move
/// public fun amount_to_coin(self: &mut Vault, amount: u64, ctx: &mut TxContext): Coin<SUI> {
/// self.balance.split(amount).into_coin(ctx)
Expand Down

0 comments on commit 4004396

Please sign in to comment.