Skip to content

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
garikbesson committed Aug 7, 2024
1 parent b47301e commit fb271d0
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 90 deletions.
2 changes: 0 additions & 2 deletions contract-simple-rs/.cargo/config

This file was deleted.

10 changes: 5 additions & 5 deletions contract-simple-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "5.1.0"
near-sdk = "5.2.1"

[dev-dependencies]
near-sdk = { version = "5.0.0", features = ["unit-testing"] }
near-workspaces = { version = "0.10.0", features = ["unstable"] }
tokio = { version = "1.12.0", features = ["full"] }
serde_json = "1"
near-sdk = { version = "5.2.1", features = ["unit-testing"] }
near-workspaces = { version = "0.11.0", features = ["unstable"] }
tokio = { version = "1.39.2", features = ["full"] }
serde_json = "1.0.122"

[profile.release]
codegen-units = 1
Expand Down
12 changes: 0 additions & 12 deletions contract-simple-rs/src/external.rs

This file was deleted.

75 changes: 75 additions & 0 deletions contract-simple-rs/src/high_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Find all our documentation at https://docs.near.org
use near_sdk::ext_contract;
use near_sdk::{env, log, near, Promise, PromiseError};

use crate::{Contract, ContractExt, FIVE_TGAS};

// Validator interface, for cross-contract calls
#[ext_contract(hello_near)]
trait HelloNear {
fn get_greeting(&self) -> String;
fn set_greeting(&self, greeting: String);
}

#[near]
impl Contract {
// Public - query external greeting
pub fn hl_query_greeting(&self) -> Promise {
// Create a promise to call HelloNEAR.get_greeting()
let promise = hello_near::ext(self.hello_account.clone())
.with_static_gas(FIVE_TGAS)
.get_greeting();

promise.then(
// Create a promise to callback query_greeting_callback
Self::ext(env::current_account_id())
.with_static_gas(FIVE_TGAS)
.hl_query_greeting_callback(),
)
}

#[private] // Public - but only callable by env::current_account_id()
pub fn hl_query_greeting_callback(
&self,
#[callback_result] call_result: Result<String, PromiseError>,
) -> String {
// Check if the promise succeeded by calling the method outlined in external.rs
if call_result.is_err() {
log!("There was an error contacting Hello NEAR");
return "".to_string();
}

// Return the greeting
let greeting: String = call_result.unwrap();
greeting
}

// Public - change external greeting
pub fn hl_change_greeting(&mut self, new_greeting: String) -> Promise {
// Create a promise to call HelloNEAR.set_greeting(message:string)
hello_near::ext(self.hello_account.clone())
.with_static_gas(FIVE_TGAS)
.set_greeting(new_greeting)
.then(
// Create a callback change_greeting_callback
Self::ext(env::current_account_id())
.with_static_gas(FIVE_TGAS)
.hl_change_greeting_callback(),
)
}

#[private]
pub fn hl_change_greeting_callback(
&mut self,
#[callback_result] call_result: Result<(), PromiseError>,
) -> bool {
// Return whether or not the promise succeeded using the method outlined in external.rs
if call_result.is_err() {
env::log_str("set_greeting failed...");
false
} else {
env::log_str("set_greeting was successful!");
true
}
}
}
71 changes: 8 additions & 63 deletions contract-simple-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Find all our documentation at https://docs.near.org
use near_sdk::{env, log, near, AccountId, Gas, PanicOnDefault, Promise, PromiseError};
use near_sdk::{env, near, AccountId, Gas, PanicOnDefault, NearToken};

pub mod external;
pub use crate::external::*;
pub mod high_level;
pub mod low_level;

const NO_ARGS: Vec<u8> = vec![];
const NO_DEPOSIT: NearToken = NearToken::from_near(0);
const FIVE_TGAS: Gas = Gas::from_tgas(5);
const TEN_TGAS: Gas = Gas::from_tgas(10);

#[near(contract_state)]
#[derive(PanicOnDefault)]
Expand All @@ -18,66 +23,6 @@ impl Contract {
assert!(!env::state_exists(), "Already initialized");
Self { hello_account }
}

// Public - query external greeting
pub fn query_greeting(&self) -> Promise {
// Create a promise to call HelloNEAR.get_greeting()
let promise = hello_near::ext(self.hello_account.clone())
.with_static_gas(Gas::from_tgas(5))
.get_greeting();

return promise.then(
// Create a promise to callback query_greeting_callback
Self::ext(env::current_account_id())
.with_static_gas(Gas::from_tgas(5))
.query_greeting_callback(),
);
}

#[private] // Public - but only callable by env::current_account_id()
pub fn query_greeting_callback(
&self,
#[callback_result] call_result: Result<String, PromiseError>,
) -> String {
// Check if the promise succeeded by calling the method outlined in external.rs
if call_result.is_err() {
log!("There was an error contacting Hello NEAR");
return "".to_string();
}

// Return the greeting
let greeting: String = call_result.unwrap();
greeting
}

// Public - change external greeting
pub fn change_greeting(&mut self, new_greeting: String) -> Promise {
// Create a promise to call HelloNEAR.set_greeting(message:string)
hello_near::ext(self.hello_account.clone())
.with_static_gas(Gas::from_tgas(5))
.set_greeting(new_greeting)
.then(
// Create a callback change_greeting_callback
Self::ext(env::current_account_id())
.with_static_gas(Gas::from_tgas(5))
.change_greeting_callback(),
)
}

#[private]
pub fn change_greeting_callback(
&mut self,
#[callback_result] call_result: Result<(), PromiseError>,
) -> bool {
// Return whether or not the promise succeeded using the method outlined in external.rs
if call_result.is_err() {
env::log_str("set_greeting failed...");
return false;
} else {
env::log_str("set_greeting was successful!");
return true;
}
}
}

#[cfg(test)]
Expand Down
75 changes: 75 additions & 0 deletions contract-simple-rs/src/low_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use near_sdk::{env, log, near, serde_json::json, Promise, PromiseError};

use crate::{Contract, ContractExt, NO_ARGS, NO_DEPOSIT, TEN_TGAS};

#[near]
impl Contract {
// Public - query external greeting
pub fn ll_query_greeting(&self) -> Promise {
// Create a promise to call HelloNEAR.get_greeting()
let hello_promise = Promise::new(self.hello_account.clone()).function_call(
"get_greeting".to_owned(),
NO_ARGS,
NO_DEPOSIT,
TEN_TGAS
);

hello_promise.then(
// Create a promise to callback query_greeting_callback
Self::ext(env::current_account_id())
.with_static_gas(TEN_TGAS)
.ll_query_greeting_callback(),
)
}

#[private] // Public - but only callable by env::current_account_id()
pub fn ll_query_greeting_callback(
&self,
#[callback_result] call_result: Result<String, PromiseError>,
) -> String {
// Check if the promise succeeded by calling the method outlined in external.rs
if call_result.is_err() {
log!("There was an error contacting Hello NEAR");
return "".to_string();
}

// Return the greeting
let greeting: String = call_result.unwrap();
greeting
}

// Public - change external greeting
pub fn ll_change_greeting(&mut self, new_greeting: String) -> Promise {
let args = json!({ "greeting": new_greeting })
.to_string()
.into_bytes();
let hello_promise = Promise::new(self.hello_account.clone()).function_call(
"set_greeting".to_owned(),
args,
NO_DEPOSIT,
TEN_TGAS
);

hello_promise.then(
// Create a promise to callback query_greeting_callback
Self::ext(env::current_account_id())
.with_static_gas(TEN_TGAS)
.ll_change_greeting_callback(),
)
}

#[private]
pub fn ll_change_greeting_callback(
&mut self,
#[callback_result] call_result: Result<(), PromiseError>,
) -> bool {
// Return whether or not the promise succeeded using the method outlined in external.rs
if call_result.is_err() {
env::log_str("set_greeting failed...");
false
} else {
env::log_str("set_greeting was successful!");
true
}
}
}
43 changes: 35 additions & 8 deletions contract-simple-rs/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.into_result()?;

// Begin tests
test_default_greeting(&alice, &contract).await?;
test_change_greeting(&alice, &contract).await?;
test_hl_default_greeting(&alice, &contract).await?;
test_hl_change_greeting(&alice, &contract).await?;
test_ll_change_greeting(&alice, &contract).await?;
Ok(())
}

async fn test_default_greeting(
async fn test_hl_default_greeting(
user: &Account,
contract: &Contract,
) -> Result<(), Box<dyn std::error::Error>> {
let greeting: String = user
.call(contract.id(), "query_greeting")
.call(contract.id(), "hl_query_greeting")
.args_json(json!({}))
.max_gas()
.transact()
Expand All @@ -50,22 +51,22 @@ async fn test_default_greeting(
Ok(())
}

async fn test_change_greeting(
async fn test_hl_change_greeting(
user: &Account,
contract: &Contract,
) -> Result<(), Box<dyn std::error::Error>> {
let result: bool = user
.call(contract.id(), "change_greeting")
.call(contract.id(), "hl_change_greeting")
.args_json(json!({ "new_greeting": "Howdy" }))
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(result, true);
assert!(result);

let greeting: String = user
.call(contract.id(), "query_greeting")
.call(contract.id(), "hl_query_greeting")
.args_json(json!({}))
.max_gas()
.transact()
Expand All @@ -74,4 +75,30 @@ async fn test_change_greeting(

assert_eq!(greeting, "Howdy".to_string());
Ok(())
}

async fn test_ll_change_greeting(
user: &Account,
contract: &Contract,
) -> Result<(), Box<dyn std::error::Error>> {
let result: bool = user
.call(contract.id(), "ll_change_greeting")
.args_json(json!({ "new_greeting": "Hello" }))
.max_gas()
.transact()
.await?
.json()?;

assert!(result);

let greeting: String = user
.call(contract.id(), "ll_query_greeting")
.args_json(json!({}))
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(greeting, "Hello".to_string());
Ok(())
}

0 comments on commit fb271d0

Please sign in to comment.