Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decode_contract_data #61

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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