Skip to content

Commit

Permalink
WIP fix: Update Code Snippets (#1964)
Browse files Browse the repository at this point in the history
* update xcc

* update rust/callbacks.md

* update crosscontract, count, donation

* update code snippets

* Update docs/2.build/2.smart-contracts/anatomy/actions.md

---------

Co-authored-by: Guille <gagdiez.c@gmail.com>
  • Loading branch information
flmel and gagdiez authored May 11, 2024
1 parent 1456a6f commit e84c6da
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Python and Rust output different hex RLP encoded transactions.
<Language value="Rust" language="rust">
<Github fname="test.rs"
url="https://github.com/near/multichain-relayer-server/blob/5b040611f2dc6c6b405b5ec00d5102e3cc27a65c/tests/tests.rs"
start="24" end="33" />
start="24" end="38" />
</Language>
</CodeTabs>

Expand Down
50 changes: 22 additions & 28 deletions docs/2.build/2.smart-contracts/anatomy/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ You can send $NEAR from your contract to any other account on the network. The G
<TabItem value="rust" label="🦀 Rust">

```rust
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, AccountId, Promise, Balance};
use near_sdk::{near, AccountId, Promise, NearToken};

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }

#[near_bindgen]
impl Contract {
pub fn transfer(&self, to: AccountId, amount: Balance){
pub fn transfer(&self, to: AccountId, amount: NearToken){
Promise::new(to).transfer(amount);
}
}
Expand Down Expand Up @@ -125,12 +124,11 @@ right in the callback.
<TabItem value="rust" label="🦀 Rust">

```rust
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, env, log, Promise, Gas, PromiseError};
use serde_json::json;

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }

const HELLO_NEAR: &str = "hello-nearverse.testnet";
Expand Down Expand Up @@ -205,16 +203,15 @@ Sub-accounts are simply useful for organizing your accounts (e.g. `dao.project.n
<TabItem value="rust" label="🦀 Rust">

```rust
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, env, Promise, Balance};
use near_sdk::{near, env, Promise, NearToken};

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }

const MIN_STORAGE: Balance = 1_000_000_000_000_000_000_000; //0.001Ⓝ

#[near_bindgen]
#[near]
impl Contract {
pub fn create(&self, prefix: String){
let account_id = prefix + "." + &env::current_account_id().to_string();
Expand Down Expand Up @@ -277,18 +274,17 @@ the `create_account` method of `near` or `testnet` root contracts.
<TabItem value="rust" label="🦀 Rust">

```rust
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, Promise, Gas, Balance };
use near_sdk::{near, Promise, Gas, NearToken };
use serde_json::json;

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }

const CALL_GAS: Gas = Gas(28_000_000_000_000);
const MIN_STORAGE: Balance = 1_820_000_000_000_000_000_000; //0.00182Ⓝ

#[near_bindgen]
#[near]
impl Contract {
pub fn create_account(&self, account_id: String, public_key: String){
let args = json!({
Expand Down Expand Up @@ -317,17 +313,16 @@ When creating an account you can also batch the action of deploying a contract t
<TabItem value="rust" label="🦀 Rust">

```rust
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, env, Promise, Balance};
use near_sdk::{near_bindgen, env, Promise, NearToken};

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }

const MIN_STORAGE: Balance = 1_100_000_000_000_000_000_000_000; //1.1Ⓝ
const HELLO_CODE: &[u8] = include_bytes!("./hello.wasm");

#[near_bindgen]
#[near]
impl Contract {
pub fn create_hello(&self, prefix: String){
let account_id = prefix + "." + &env::current_account_id().to_string();
Expand Down Expand Up @@ -462,16 +457,15 @@ There are two scenarios in which you can use the `delete_account` action:
<TabItem value="rust" label="🦀 Rust">

```rust
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, env, Promise, Balance, AccountId};
use near_sdk::{near, env, Promise, Neartoken, AccountId};

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract { }

const MIN_STORAGE: Balance = 1_000_000_000_000_000_000_000; //0.001Ⓝ

#[near_bindgen]
#[near]
impl Contract {
pub fn create_delete(&self, prefix: String, beneficiary: AccountId){
let account_id = prefix + "." + &env::current_account_id().to_string();
Expand Down
10 changes: 6 additions & 4 deletions docs/2.build/2.smart-contracts/anatomy/crosscontract.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ While making your contract, it is likely that you will want to query information
<Language value="rust" language="rust">
<Github fname="lib.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/lib.rs"
start="25" end="48" />
start="22" end="51" />
<Github fname="external.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs" />
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs"
start="2" end="12" />

</Language>

Expand All @@ -57,9 +58,10 @@ Calling another contract passing information is also a common scenario. Bellow y
<Language value="rust" language="rust">
<Github fname="lib.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/lib.rs"
start="52" end="75" />
start="53" end="80" />
<Github fname="external.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs" />
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs"
start="2" end="12" />

</Language>

Expand Down
2 changes: 1 addition & 1 deletion docs/2.build/2.smart-contracts/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Your new smart contract stores a `greeting: string` attribute in their state, an
<Language value="rs" language="rust">
<Github fname="lib.rs"
url="https://github.com/near-examples/hello-near-examples/blob/main/contract-rs/src/lib.rs"
start="6" end="32" />
start="4" end="32" />

</Language>

Expand Down
2 changes: 1 addition & 1 deletion docs/3.tutorials/examples/count-near.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ The contract presents 4 methods: `get_num`, `increment`, `decrement`, and `reset
<Language value="rust" language="rust">
<Github fname="lib.rs"
url="https://github.com/near-examples/counters/blob/main/contract-rs/src/lib.rs"
start="5" end="39" />
start="5" end="37" />
</Language>
</CodeTabs>

Expand Down
4 changes: 2 additions & 2 deletions docs/3.tutorials/examples/donation.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ NEAR wallet to accept a transaction.

## Smart Contract

The contract exposes methods to donate tokens (`donate`), and methods to retrieve the recorded donations (e.g. `get_donation_by_number`).
The contract exposes methods to donate tokens (`donate`), and methods to retrieve the recorded donations (e.g. `get_donation_for_account`).

<CodeTabs>
<Language value="js" language="ts">
Expand All @@ -128,7 +128,7 @@ The contract exposes methods to donate tokens (`donate`), and methods to retriev
<Language value="rust" language="rust">
<Github fname="lib.rs"
url="https://github.com/near-examples/donation-examples/blob/main/contract-rs/src/donation.rs"
start="22" end="65" />
start="17" end="74" />
</Language>
</CodeTabs>

Expand Down
5 changes: 3 additions & 2 deletions docs/3.tutorials/examples/xcc.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ The contract exposes methods to query the greeting and change it. These methods
<Language value="rust" language="rust">
<Github fname="lib.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/lib.rs"
start="25" end="50" />
start="22" end="51" />
<Github fname="external.rs"
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs" />
url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs"
start="2" end="12" />
</Language>
</CodeTabs>

Expand Down
27 changes: 13 additions & 14 deletions docs/sdk/rust/contract-structure/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ Example of `HashMap`:

```rust
/// Using Default initialization.
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, Default)]
#[near(contract_state)]
#[derive(Default)]
pub struct Contract {
pub status_updates: HashMap<AccountId, String>,
}

#[near_bindgen]
#[near]
impl Contract {
pub fn set_status(&mut self, status: String) {
self.status_updates.insert(env::predecessor_account_id(), status);
Expand All @@ -80,13 +80,13 @@ impl Contract {
Example of `UnorderedMap`:

```rust
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct Contract {
pub status_updates: UnorderedMap<AccountId, String>,
}

#[near_bindgen]
#[near]
impl Contract {
#[init]
pub fn new() -> Self {
Expand Down Expand Up @@ -194,13 +194,13 @@ This can be done using iterators with [`Skip`](https://doc.rust-lang.org/std/ite
Example of pagination for `UnorderedMap`:

```rust
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct Contract {
pub status_updates: UnorderedMap<AccountId, String>,
}

#[near_bindgen]
#[near]
impl Contract {
/// Retrieves multiple elements from the `UnorderedMap`.
/// - `from_index` is the index to start from.
Expand Down Expand Up @@ -252,21 +252,20 @@ like other persistent collections.
Compared to other collections, `LazyOption` only allows you to initialize the value during initialization.

```rust
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct Contract {
pub metadata: LazyOption<Metadata>,
}

#[derive(Serialize, Deserialize, BorshDeserialize, BorshSerialize)]
#[serde(crate = "near_sdk::serde")]
#[near(serializers=[borsh, json])]
pub struct Metadata {
data: String,
image: Base64Vec,
blobs: Vec<String>,
}

#[near_bindgen]
#[near]
impl Contract {
#[init]
pub fn new(metadata: Metadata) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/rust/cross-contract/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod ext_calculator {
Let's assume the calculator is deployed on `calc.near`, we can use the following:

```rust
#[near_bindgen]
#[near]
impl Contract {
pub fn sum_a_b(&mut self, a: U128, b: U128) -> Promise {
let calculator_account_id: AccountId = "calc.near".parse().unwrap();
Expand Down Expand Up @@ -94,7 +94,7 @@ fn get_account_to_check() -> AccountId {
```

```rust
#[near_bindgen]
#[near]
impl Contract {
pub fn xcc_use_promise_result() -> Promise {
// Call the method `is_allowlisted` on the allowlisted contract. Static GAS is only attached to the callback.
Expand Down

0 comments on commit e84c6da

Please sign in to comment.