Skip to content

Commit

Permalink
[Rosetta] - serialize total_coin_value to string instead of number to…
Browse files Browse the repository at this point in the history
… prevent precision lost (#19580)

## Description

As titled

## Test plan

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol:
- [ ] Nodes (Validators and Full nodes):
- [ ] Indexer:
- [ ] JSON-RPC:
- [ ] GraphQL:
- [ ] CLI:
- [ ] Rust SDK:
- [ ] REST API:

---------

Co-authored-by: nikos.kitmeridis <[email protected]>

(cherry picked from commit df56cb0)
  • Loading branch information
patrickkuo authored and nikos-kitmeridis committed Oct 18, 2024
1 parent 2d6c4ba commit 1bf7d7e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/sui-rosetta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ tempfile.workspace = true
rand.workspace = true
reqwest.workspace = true
move-cli.workspace = true
quick-js = "0.4.1"
2 changes: 1 addition & 1 deletion crates/sui-rosetta/src/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub async fn metadata(
sender,
coins,
objects,
total_coin_value,
total_coin_value: total_coin_value.into(),
gas_price,
budget,
currency,
Expand Down
6 changes: 4 additions & 2 deletions crates/sui-rosetta/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ pub struct ConstructionMetadata {
pub sender: SuiAddress,
pub coins: Vec<ObjectRef>,
pub objects: Vec<ObjectRef>,
pub total_coin_value: u64,
#[serde(with = "str_format")]
pub total_coin_value: i128,
pub gas_price: u64,
pub budget: u64,
pub currency: Option<Currency>,
Expand Down Expand Up @@ -987,7 +988,8 @@ impl InternalOperation {
let state = builder.input(CallArg::SUI_SYSTEM_MUT)?;
(validator, state, amount)
} else {
let amount = builder.pure(metadata.total_coin_value - metadata.budget)?;
let amount =
builder.pure(metadata.total_coin_value as u64 - metadata.budget)?;
let state = builder.input(CallArg::SUI_SYSTEM_MUT)?;
let validator = builder.input(CallArg::Pure(bcs::to_bytes(&validator)?))?;
(validator, state, amount)
Expand Down
60 changes: 59 additions & 1 deletion crates/sui-rosetta/src/unit_tests/types_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::types::{AccountBalanceRequest, Amount, Currency, CurrencyMetadata};
use crate::types::{
AccountBalanceRequest, Amount, ConstructionMetadata, Currency, CurrencyMetadata,
};
use quick_js::Context;
use serde::{Deserialize, Serialize};
use serde_json::json;
use sui_types::base_types::{ObjectRef, SuiAddress};

#[tokio::test]
async fn test_currency_defaults() {
Expand Down Expand Up @@ -65,3 +70,56 @@ async fn test_currency_defaults() {
account_balance_request.currencies.0.clone().pop().unwrap()
);
}

#[tokio::test]
async fn test_metadata_total_coin_value_js_conversion_for_large_balance() {
#[derive(Serialize, Deserialize, Debug)]
pub struct TestConstructionMetadata {
pub sender: SuiAddress,
pub coins: Vec<ObjectRef>,
pub objects: Vec<ObjectRef>,
pub total_coin_value: u64,
pub gas_price: u64,
pub budget: u64,
pub currency: Option<Currency>,
}

let test_metadata = TestConstructionMetadata {
sender: Default::default(),
coins: vec![],
objects: vec![],
total_coin_value: 65_000_004_233_578_496,
gas_price: 0,
budget: 0,
currency: None,
};
let test_metadata_json = serde_json::to_string(&test_metadata).unwrap();

let prod_metadata = ConstructionMetadata {
sender: Default::default(),
coins: vec![],
objects: vec![],
total_coin_value: 65_000_004_233_578_496,
gas_price: 0,
budget: 0,
currency: None,
};
let prod_metadata_json = serde_json::to_string(&prod_metadata).unwrap();

let context = Context::new().unwrap();

let test_total_coin_value = format!(
"JSON.parse({:?}).total_coin_value.toString()",
test_metadata_json
);
let js_test_total_coin_value = context.eval_as::<String>(&test_total_coin_value).unwrap();

let prod_total_coin_value = format!(
"JSON.parse({:?}).total_coin_value.toString()",
prod_metadata_json
);
let js_prod_total_coin_value = context.eval_as::<String>(&prod_total_coin_value).unwrap();

assert_eq!("65000004233578500", js_test_total_coin_value);
assert_eq!("65000004233578496", js_prod_total_coin_value);
}

0 comments on commit 1bf7d7e

Please sign in to comment.