Skip to content

Commit

Permalink
Merge pull request #61 from tonlabs/decode-init-data
Browse files Browse the repository at this point in the history
decode_contract_data
  • Loading branch information
d3p authored Oct 1, 2021
2 parents b42c367 + 968c3c3 commit 5f26334
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ton_abi"
version = "2.1.1"
version = "2.1.2"

[dependencies]
ton_types = { git = "https://github.com/tonlabs/ton-labs-types.git" }
Expand Down
19 changes: 19 additions & 0 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,25 @@ impl Contract {
Ok(map.write_to_new_cell()?.into())
}

/// Decode initial values of public contract variables
pub fn decode_data(&self, data: SliceData) -> Result<Vec<Token>> {
let map = HashmapE::with_hashmap(
Self::DATA_MAP_KEYLEN,
data.reference_opt(0),
);

let mut tokens = vec![];
for (_, item) in &self.data {
if let Some(value) = map.get(item.key.write_to_new_cell().unwrap().into())? {
tokens.append(
&mut TokenValue::decode_params(&vec![item.value.clone()], value, &self.abi_version)?
);
}
}

Ok(tokens)
}

// Gets public key from contract data
pub fn get_pubkey(data: &SliceData) -> Result<Option<Vec<u8>>> {
let map = HashmapE::with_hashmap(
Expand Down
7 changes: 7 additions & 0 deletions src/json_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ pub fn update_contract_data(abi: &str, parameters: &str, data: SliceData) -> Res
contract.update_data(data, &tokens)
}

/// Decode initial values of public contract variables
pub fn decode_contract_data(abi: &str, data: SliceData) -> Result<String> {
let contract = Contract::load(abi.as_bytes())?;

Detokenizer::detokenize(&contract.decode_data(data)?)
}

/// Decode account storage fields
pub fn decode_storage_fields(abi: &str, data: SliceData) -> Result<String> {
let contract = Contract::load(abi.as_bytes())?;
Expand Down
8 changes: 7 additions & 1 deletion src/tests/v1/full_stack_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ fn test_store_pubkey() {
}

#[test]
fn test_update_contract_data() {
fn test_update_decode_contract_data() {
let mut test_map = HashmapE::with_bit_len(Contract::DATA_MAP_KEYLEN);
test_map.set_builder(
0u64.write_to_new_cell().unwrap().into(),
Expand Down Expand Up @@ -401,4 +401,10 @@ fn test_update_contract_data() {
.unwrap();

assert_eq!(owner_slice.get_bytestring(0), vec![0x22; 32]);

let decoded = decode_contract_data(WALLET_ABI, new_data).unwrap();
assert_eq!(
serde_json::from_str::<Value>(params).unwrap(),
serde_json::from_str::<Value>(&decoded).unwrap()
);
}
8 changes: 7 additions & 1 deletion src/tests/v2/full_stack_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ fn test_store_pubkey() {
}

#[test]
fn test_update_contract_data() {
fn test_update_decode_contract_data() {
let mut test_map = HashmapE::with_bit_len(Contract::DATA_MAP_KEYLEN);
test_map.set_builder(
0u64.write_to_new_cell().unwrap().into(),
Expand Down Expand Up @@ -418,6 +418,12 @@ fn test_update_contract_data() {
.unwrap();

assert_eq!(owner_slice.get_bytestring(0), vec![0x22; 32]);

let decoded = decode_contract_data(WALLET_ABI, new_data).unwrap();
assert_eq!(
serde_json::from_str::<Value>(params).unwrap(),
serde_json::from_str::<Value>(&decoded).unwrap()
);
}

const ABI_WITH_FIELDS: &str = r#"{
Expand Down

0 comments on commit 5f26334

Please sign in to comment.