From c6163c29924b651ac705cba16d2f377023564957 Mon Sep 17 00:00:00 2001 From: aldenhu Date: Tue, 26 Apr 2022 16:35:06 -0700 Subject: [PATCH] [tutorial] update rust tutorial with get_resource API --- .../rust/first_transaction/src/lib.rs | 42 ++++++++----------- .../examples/rust/hello_blockchain/src/lib.rs | 20 ++------- 2 files changed, 20 insertions(+), 42 deletions(-) diff --git a/developer-docs-site/static/examples/rust/first_transaction/src/lib.rs b/developer-docs-site/static/examples/rust/first_transaction/src/lib.rs index 9a741f90d8e99..2f5dea265fd1b 100644 --- a/developer-docs-site/static/examples/rust/first_transaction/src/lib.rs +++ b/developer-docs-site/static/examples/rust/first_transaction/src/lib.rs @@ -85,14 +85,20 @@ impl RestClient { } /// Returns all resources associated with the account - pub fn account_resources(&self, account_address: &str) -> serde_json::Value { + pub fn account_resource( + &self, + account_address: &str, + resource_type: &str, + ) -> Option { let res = reqwest::blocking::get(format!( - "{}/accounts/{}/resources", - self.url, account_address + "{}/accounts/{}/resource/{}", + self.url, account_address, resource_type, )) .unwrap(); - if res.status() != 200 { + if res.status() == 404 { + None + } else if res.status() != 200 { assert_eq!( res.status(), 200, @@ -100,9 +106,10 @@ impl RestClient { res.text().unwrap_or("".to_string()), account_address, ); + unreachable!() + } else { + Some(res.json().unwrap()) } - - res.json().unwrap() } //<:!:section_3 //:!:>section_4 @@ -241,25 +248,10 @@ impl RestClient { //:!:>section_5 /// Returns the test coin balance associated with the account pub fn account_balance(&self, account_address: &str) -> Option { - self.account_resources(account_address) - .as_array() - .unwrap() - .iter() - .find(|x| { - x.get("type") - .map(|v| v.as_str().unwrap() == "0x1::TestCoin::Balance".to_string()) - .unwrap_or(false) - }) - .and_then(|coin| { - coin.get("data") - .unwrap() - .get("coin") - .unwrap() - .get("value") - .unwrap() - .as_str() - .and_then(|s| s.parse::().ok()) - }) + self.account_resource(account_address, "0x1::TestCoin::Balance") + .unwrap()["data"]["coin"]["value"] + .as_str() + .and_then(|s| s.parse::().ok()) } /// Transfer a given coin amount from a given Account to the recipient's account address. diff --git a/developer-docs-site/static/examples/rust/hello_blockchain/src/lib.rs b/developer-docs-site/static/examples/rust/hello_blockchain/src/lib.rs index 2cc746c6f95ad..cff0239196d4b 100644 --- a/developer-docs-site/static/examples/rust/hello_blockchain/src/lib.rs +++ b/developer-docs-site/static/examples/rust/hello_blockchain/src/lib.rs @@ -35,24 +35,10 @@ impl HelloBlockchainClient { pub fn get_message(&self, contract_address: &str, account_address: &str) -> Option { let module_type = format!("0x{}::Message::MessageHolder", contract_address); self.rest_client - .account_resources(account_address) - .as_array() - .unwrap() - .iter() - .find(|x| { - x.get("type") - .map(|v| v.as_str().unwrap() == module_type) - .unwrap_or(false) - }) - .and_then(|coin| { - coin.get("data") - .unwrap() - .get("message") - .unwrap() - .as_str() - .and_then(|s| Some(s.to_string())) - }) + .account_resource(account_address, &module_type) + .map(|value| value["data"]["message"].as_str().unwrap().to_string()) } + //<:!:section_2 //:!:>section_3 /// Potentially initialize and set the resource Message::MessageHolder::message