Skip to content

Commit 9aa6720

Browse files
committed
make to_json() return string instead of JsValue;
add `to_json_dapp` for `ErgoBox` and `Token`;
1 parent e8d53f7 commit 9aa6720

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

bindings/ergo-lib-wasm/src/ergo_box.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use wasm_bindgen::prelude::*;
2424

2525
use crate::ast::Constant;
2626
use crate::ergo_tree::ErgoTree;
27+
use crate::json::ErgoBoxJsonDapp;
2728
use crate::token::Tokens;
2829
use crate::utils::I64;
2930
use crate::{contract::Contract, transaction::TxId};
@@ -163,12 +164,21 @@ impl ErgoBox {
163164
.map(Constant::from)
164165
}
165166

166-
/// JSON representation
167-
pub fn to_json(&self) -> Result<JsValue, JsValue> {
168-
JsValue::from_serde(&self.0.clone()).map_err(|e| JsValue::from_str(&format!("{}", e)))
167+
/// JSON representation as text (compatible with Ergo Node/Explorer API, numbers are encoded as numbers)
168+
pub fn to_json(&self) -> Result<String, JsValue> {
169+
serde_json::to_string_pretty(&self.0.clone())
170+
.map_err(|e| JsValue::from_str(&format!("{}", e)))
171+
}
172+
173+
/// JSON representation (same as [`Self::to_json`],
174+
/// but with box value encoding as string)
175+
pub fn to_json_dapp(&self) -> Result<JsValue, JsValue> {
176+
let box_dapp: ErgoBoxJsonDapp = self.0.clone().into();
177+
JsValue::from_serde(&box_dapp).map_err(|e| JsValue::from_str(&format!("{}", e)))
169178
}
170179

171-
/// JSON representation
180+
/// parse from JSON
181+
/// supports Ergo Node/Explorer API and box values and token amount encoded as strings
172182
pub fn from_json(json: &str) -> Result<ErgoBox, JsValue> {
173183
serde_json::from_str(json)
174184
.map(Self)

bindings/ergo-lib-wasm/src/token.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ergo_lib::chain::Digest32;
88
use wasm_bindgen::prelude::*;
99

1010
use crate::ergo_box::BoxId;
11+
use crate::json::TokenJsonDapp;
1112
use crate::utils::I64;
1213

1314
/// Token id (32 byte digest)
@@ -100,9 +101,17 @@ impl Token {
100101
TokenAmount(self.0.amount)
101102
}
102103

103-
/// JSON representation
104-
pub fn to_json(&self) -> Result<JsValue, JsValue> {
105-
JsValue::from_serde(&self.0.clone()).map_err(|e| JsValue::from_str(&format!("{}", e)))
104+
/// JSON representation as text (compatible with Ergo Node/Explorer API, numbers are encoded as numbers)
105+
pub fn to_json(&self) -> Result<String, JsValue> {
106+
serde_json::to_string_pretty(&self.0.clone())
107+
.map_err(|e| JsValue::from_str(&format!("{}", e)))
108+
}
109+
110+
/// JSON representation (same as [`Self::to_json`],
111+
/// but with box value and token amount encoding as strings)
112+
pub fn to_json_dapp(&self) -> Result<JsValue, JsValue> {
113+
let t_dapp: TokenJsonDapp = self.0.clone().into();
114+
JsValue::from_serde(&t_dapp).map_err(|e| JsValue::from_str(&format!("{}", e)))
106115
}
107116
}
108117

bindings/ergo-lib-wasm/src/transaction.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,21 @@ impl Transaction {
7171
self.0.id().into()
7272
}
7373

74-
/// JSON representation as text (compatible with Ergo Node/Explorer API)
74+
/// JSON representation as text (compatible with Ergo Node/Explorer API, numbers are encoded as numbers)
7575
pub fn to_json(&self) -> Result<String, JsValue> {
7676
serde_json::to_string_pretty(&self.0.clone())
7777
.map_err(|e| JsValue::from_str(&format!("{}", e)))
7878
}
7979

80-
/// JSON representation (with box value and token amount encoding as strings)
80+
/// JSON representation (same as [`Self::to_json`],
81+
/// but with box value and token amount encoding as strings)
8182
pub fn to_json_dapp(&self) -> Result<JsValue, JsValue> {
8283
let tx_dapp: TransactionJsonDapp = self.0.clone().into();
8384
JsValue::from_serde(&tx_dapp).map_err(|e| JsValue::from_str(&format!("{}", e)))
8485
}
8586

86-
/// JSON representation
87+
/// parse from JSON
88+
/// supports Ergo Node/Explorer API and box values and token amount encoded as strings
8789
pub fn from_json(json: &str) -> Result<Transaction, JsValue> {
8890
serde_json::from_str(json)
8991
.map(Self)
@@ -144,19 +146,21 @@ impl UnsignedTransaction {
144146
self.0.output_candidates.clone().into()
145147
}
146148

147-
/// JSON representation as text (compatible with Ergo Node/Explorer API)
149+
/// JSON representation as text (compatible with Ergo Node/Explorer API, numbers are encoded as numbers)
148150
pub fn to_json(&self) -> Result<String, JsValue> {
149151
serde_json::to_string_pretty(&self.0.clone())
150152
.map_err(|e| JsValue::from_str(&format!("{}", e)))
151153
}
152154

153-
/// JSON representation (with box value and token amount encoding as strings)
155+
/// JSON representation (same as [`Self::to_json`],
156+
/// but with box value and token amount encoding as strings)
154157
pub fn to_json_dapp(&self) -> Result<JsValue, JsValue> {
155158
let tx_dapp: UnsignedTransactionJsonDapp = self.0.clone().into();
156159
JsValue::from_serde(&tx_dapp).map_err(|e| JsValue::from_str(&format!("{}", e)))
157160
}
158161

159-
/// JSON representation
162+
/// parse from JSON
163+
/// supports Ergo Node/Explorer API and box values and token amount encoded as strings
160164
pub fn from_json(json: &str) -> Result<UnsignedTransaction, JsValue> {
161165
serde_json::from_str(json)
162166
.map(Self)

ergo-lib/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased] - ReleaseDate
99

1010
### Added
11+
- WASM `to_json_dapp()` (along `to_json_dapp()`) that encodes the same JSON as `to_json()` but box value and token amount encodes as strings to avoid accuracy loss in JS [#346](https://github.com/ergoplatform/sigma-rust/pull/346);
1112
- `Coll.slice` [#309](https://github.com/ergoplatform/sigma-rust/pull/309);
1213
- Byte-wise XOR for byte arrays [#310](https://github.com/ergoplatform/sigma-rust/pull/310);
1314
- `Constant::from_i64_str_array` and `to_i64_str_array` for `Coll[Long]` encoding [#311](https://github.com/ergoplatform/sigma-rust/pull/311);
@@ -17,14 +18,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1718
- Better Debug print for EC point [#319](https://github.com/ergoplatform/sigma-rust/pull/319);
1819
- `Constant::from_ecpoint_bytes` to Wasm API [#324](https://github.com/ergoplatform/sigma-rust/pull/324);
1920

20-
### Changed
21-
- `SigmaSerializable:sigma_serialize` errors are extended beyond `io::Error` [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
21+
### Changed(BREAKING)
22+
- **WASM `to_json()` returns string (instead of `JsValue`) to avoid silent accuracy loss on JS object -> text conversion on JS side [#346](https://github.com/ergoplatform/sigma-rust/pull/346);**
2223
- `SigmaSerializable:sigma_serialize_bytes` made failible (returns `Result`) [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
2324
- `ErgoBox::new`, `from_box_candidate` made failible (returns `Result`) [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
2425
- `ErgoTree::new`, `template_bytes`, `to_base16_bytes` made failible (returns `Result`) [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
2526
- `Transaction::new` made failible (returns `Result`) [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
2627
- WASM `ErgoBox::new`, `byte_to_sign` made failible (returns `Result`) [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
2728
- WASM `ErgoTree::to_bytes`, `to_base16_bytes` made failible (returns `Result`) [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
29+
30+
### Changed
31+
- `SigmaSerializable:sigma_serialize` errors are extended beyond `io::Error` [#328](https://github.com/ergoplatform/sigma-rust/pull/328);
2832
- `ergotree-ir::mir::constant::constant_placeholder` module is public;
2933
- `ErgoTree::set_constant` is removed in favor of `ErgoTree::with_constant` with an added check for matching constant type[#323](https://github.com/ergoplatform/sigma-rust/pull/323);
3034

0 commit comments

Comments
 (0)