Skip to content

Commit

Permalink
Merge pull request #5 from flmel/update
Browse files Browse the repository at this point in the history
chore: update rust sdk to 5.1, format
  • Loading branch information
gagdiez committed May 8, 2024
2 parents aa69a8a + 7047908 commit 5c36000
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 118 deletions.
2 changes: 1 addition & 1 deletion contract-advanced-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib", "rlib"]

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

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions contract-advanced-rs/src/batch_actions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use near_sdk::{env, log, near_bindgen, serde_json::json, Promise, PromiseError};
// Find all our documentation at https://docs.near.org
use near_sdk::{env, log, near, serde_json::json, Promise, PromiseError};

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

#[near_bindgen]
#[near]
impl Contract {
pub fn batch_actions(&mut self) -> Promise {
let hi = json!({ "greeting": "hi" }).to_string().into_bytes();
Expand Down
10 changes: 4 additions & 6 deletions contract-advanced-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, AccountId, NearToken, Gas};
// Find all our documentation at https://docs.near.org
use near_sdk::{near, AccountId, Gas, NearToken};

mod batch_actions;
mod multiple_contracts;
Expand All @@ -13,9 +13,7 @@ const COUNTER_CONTRACT: &str = "counter.near-examples.testnet";
const GUESTBOOK_CONTRACT: &str = "guestbook.near-examples.testnet";

// Define the contract structure
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
#[near(contract_state)]
pub struct Contract {
pub hello_account: AccountId,
pub counter_account: AccountId,
Expand All @@ -32,7 +30,7 @@ impl Default for Contract {
}
}

#[near_bindgen]
#[near]
impl Contract {
#[init]
#[private]
Expand Down
11 changes: 5 additions & 6 deletions contract-advanced-rs/src/multiple_contracts.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use schemars::JsonSchema;
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, log, near_bindgen, serde_json::json, Promise, PromiseError};
// Find all our documentation at https://docs.near.org
use near_sdk::{env, log, near, serde_json::json, Promise, PromiseError};

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

#[derive(Deserialize, Serialize, Debug, JsonSchema)]
#[serde(crate = "near_sdk::serde")]
#[near(serializers=[borsh, json])]
#[derive(Debug)]
pub struct PostedMessage {
pub premium: bool,
pub sender: String,
pub text: String,
}

#[near_bindgen]
#[near]
impl Contract {
/// A method which calls different contracts via cross contract function calls.
pub fn multiple_contracts(&mut self) -> Promise {
Expand Down
5 changes: 3 additions & 2 deletions contract-advanced-rs/src/similar_contracts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use near_sdk::{env, log, near_bindgen, serde_json::json, Promise, PromiseResult};
// Find all our documentation at https://docs.near.org
use near_sdk::{env, log, near, serde_json::json, Promise, PromiseResult};

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

#[near_bindgen]
#[near]
impl Contract {
fn promise_set_get(&self, message: &str) -> Promise {
// Aux method to create a batch transaction calling
Expand Down
13 changes: 9 additions & 4 deletions contract-advanced-ts/src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NearBindgen, initialize, call, near, NearPromise } from "near-sdk-js";
// Find all our documentation at https://docs.near.org
import { call, initialize, near, NearBindgen, NearPromise } from "near-sdk-js";
import { AccountId } from "near-sdk-js/lib/types";
import {
batch_actions as internal_batch_actions,
Expand Down Expand Up @@ -50,7 +51,9 @@ export class CrossContractCall {
}

@call({ privateFunction: true })
multiple_contracts_callback({ number_promises }: { number_promises: number;}): string[] {
multiple_contracts_callback(
{ number_promises }: { number_promises: number },
): string[] {
return internal_multiple_contracts_callback(number_promises);
}

Expand All @@ -60,7 +63,9 @@ export class CrossContractCall {
}

@call({ privateFunction: true })
similar_contracts_callback({ number_promises }: { number_promises: number;}): string[] {
similar_contracts_callback(
{ number_promises }: { number_promises: number },
): string[] {
return internal_similar_contracts_callback(number_promises);
}
}
}
2 changes: 1 addition & 1 deletion contract-simple-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib", "rlib"]

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

[dev-dependencies]
near-sdk = { version = "5.0.0", features = ["unit-testing"] }
Expand Down
7 changes: 4 additions & 3 deletions contract-simple-rs/src/external.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Find all our documentation at https://docs.near.org
use near_sdk::ext_contract;

pub const NO_DEPOSIT: u128 = 0;
Expand All @@ -6,6 +7,6 @@ pub const XCC_SUCCESS: u64 = 1;
// Validator interface, for cross-contract calls
#[ext_contract(hello_near)]
trait HelloNear {
fn get_greeting(&self) -> String;
fn set_greeting(&self, greeting: String);
}
fn get_greeting(&self) -> String;
fn set_greeting(&self, greeting: String);
}
126 changes: 65 additions & 61 deletions contract-simple-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,84 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{env, log, near_bindgen, AccountId, Gas, Promise, PromiseError, PanicOnDefault};
// Find all our documentation at https://docs.near.org
use near_sdk::{env, log, near, AccountId, Gas, PanicOnDefault, Promise, PromiseError};

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

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
#[borsh(crate = "near_sdk::borsh")]
#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct Contract {
pub hello_account: AccountId
pub hello_account: AccountId,
}

#[near_bindgen]
#[near]
impl Contract {
#[init]
#[private] // Public - but only callable by env::current_account_id()
pub fn init(hello_account: AccountId) -> Self {
assert!(!env::state_exists(), "Already initialized");
Self {
hello_account,
#[init]
#[private] // Public - but only callable by env::current_account_id()
pub fn init(hello_account: AccountId) -> Self {
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()
)
}
// 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();

#[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 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(),
);
}

// Return the greeting
let greeting: String = call_result.unwrap();
greeting
}
#[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();
}

// 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()
)
}
// Return the greeting
let greeting: String = call_result.unwrap();
greeting
}

#[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;
// 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)]
mod tests {
Expand All @@ -88,4 +92,4 @@ mod tests {
let contract = Contract::init(beneficiary);
assert_eq!(contract.hello_account, HELLO_NEAR)
}
}
}
2 changes: 1 addition & 1 deletion contract-simple-ts/ava.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ module.exports = {
"nodeArguments": [
"--import=tsimp"
]
};
};

0 comments on commit 5c36000

Please sign in to comment.