### Build All Contracts in Examples Directory Source: https://github.com/near/near-sdk-rs/blob/master/contract-builder/README.md Navigate to the examples directory and execute this script to build all contracts within it. ```bash cd examples ./build_all.sh ``` -------------------------------- ### Install cargo-near (Windows) Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Installs prebuilt binaries for cargo-near using a PowerShell script on Windows. ```powershell irm https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.ps1 | iex ``` -------------------------------- ### Install cargo-near from source (Cargo) Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Compiles and installs cargo-near from its source code using Cargo. ```bash cargo install cargo-near ``` -------------------------------- ### Install Dependencies and Run Comparison Script Source: https://github.com/near/near-sdk-rs/blob/master/ci/compare_sizes/README.md Install the required Python package and execute the comparison script from the project's root directory. ```bash pip install GitPython ci/compare_sizes/compare_sizes.py ``` -------------------------------- ### Start Local NEAR Sandbox Network Source: https://github.com/near/near-sdk-rs/blob/master/examples/mpc-contract/README.md Initialize and start a local NEAR sandbox network. This requires three terminals for the full demonstration. ```bash npm i -g near-sandbox near-sandbox localnet near-sandbox init near-sandbox run --rpc-addr 127.0.0.1:2323 ``` -------------------------------- ### Install Rustup Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Installs Rustup, the toolchain manager for Rust, which is a prerequisite for developing Rust contracts. ```sh curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ``` -------------------------------- ### Start Docker Instance Source: https://github.com/near/near-sdk-rs/blob/master/contract-builder/README.md Launch a Docker instance that mounts the current directory under /host. This is the default behavior. ```bash ./run.sh ``` -------------------------------- ### Install cargo-near (Linux/macOS) Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Installs prebuilt binaries for cargo-near using a shell script on Linux or macOS. ```shell curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh ``` -------------------------------- ### Install cargo-near from Git repository Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Clones the cargo-near repository and installs the latest version from the git repository. ```bash $ git clone https://github.com/near/cargo-near $ cargo install --path cargo-near ``` -------------------------------- ### Install cargo-near (Node.js) Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Installs cargo-near as a dependency within a Node.js application. ```bash npm install cargo-near ``` -------------------------------- ### Create NEAR development account Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Guides the user through the process of creating a new NEAR account on testnet. ```sh cargo near create-dev-account ``` -------------------------------- ### Start cargo-near interactive mode Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Launches cargo-near in interactive mode, allowing exploration of available commands. ```sh cargo near ``` -------------------------------- ### Rust Default Implementation with Panic Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Provides an example of implementing the `Default` trait for a NEAR smart contract to prevent default initialization and instead panic, ensuring the contract is explicitly initialized. ```rust impl Default for StatusMessage { fn default() -> Self { near_sdk::env::panic_str("Contract should be initialized before the usage.") } } ``` -------------------------------- ### NEAR Smart Contract Example with #[near] Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Defines a simple NEAR smart contract for storing and retrieving status messages. Use this to create stateful smart contracts on the NEAR blockchain. ```rust use near_sdk::{near, env}; use std::collections::HashMap; #[near(contract_state)] #[derive(Default)] pub struct StatusMessage { records: HashMap, } #[near] impl StatusMessage { pub fn set_status(&mut self, message: String) { let account_id = env::signer_account_id(); self.records.insert(account_id, message); } pub fn get_status(&self, account_id: AccountId) -> Option { self.records.get(&account_id).cloned() } } ``` -------------------------------- ### Define a NEAR Smart Contract with #[near] Source: https://github.com/near/near-sdk-rs/blob/master/README.md Use the `#[near]` attribute macro to define a struct that will be compiled into a NEAR-compatible smart contract. This example shows a basic contract for storing and retrieving status messages. ```rust use near_sdk::{near, env}; #[near(contract_state)] #[derive(Default)] pub struct StatusMessage { records: HashMap, } #[near] impl StatusMessage { pub fn set_status(&mut self, message: String) { let account_id = env::signer_account_id(); self.records.insert(account_id, message); } pub fn get_status(&self, account_id: AccountId) -> Option { self.records.get(&account_id).cloned() } } ``` -------------------------------- ### Rust Unit Test for StatusMessage Contract Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Demonstrates how to write a unit test for a StatusMessage contract using the NEAR SDK. This test verifies the functionality of setting and getting a status message. ```rust #[test] fn set_get_message() { let mut contract = StatusMessage::default(); contract.set_status("hello".to_string()); assert_eq!("hello".to_string(), contract.get_status("bob_near".to_string()).unwrap()); } ``` -------------------------------- ### Configure Network and Deploy Contract Source: https://github.com/near/near-sdk-rs/blob/master/examples/mpc-contract/README.md Set up the network configuration for the local sandbox and deploy the MPC contract. This involves importing a validator account and then deploying the contract. ```bash # preparing network config. Skip if you already configured your account. near config add-connection --network-name localnet --connection-name sandbox --rpc-url http://127.0.0.1:2323/ --wallet-url https://app.mynearwallet.com/ --explorer-transaction-url https://explorer.near.org/transactions/ VALIDATOR_KEY=$(cat ~/.near/validator_key.json | jq .secret_key) # adding validator account near account import-account using-private-key $VALIDATOR_KEY network-config sandbox # deploying contract cargo near deploy build-non-reproducible-wasm test.near without-init-call network-config sandbox sign-with-keychain send ``` -------------------------------- ### Run Tests Source: https://github.com/near/near-sdk-rs/blob/master/examples/status-message/README.md Command to execute the project's tests. ```bash cargo test ``` -------------------------------- ### Build with cargo-near Source: https://github.com/near/near-sdk-rs/blob/master/examples/factory-contract-global/README.md Build the project using the cargo-near tool. ```bash cargo near build ``` -------------------------------- ### Deploy to Dev Account Source: https://github.com/near/near-sdk-rs/blob/master/examples/factory-contract-global/README.md Deploy the contract to your development account on the NEAR testnet. ```bash cargo near deploy ``` -------------------------------- ### Demo Reproducible Build Source: https://github.com/near/near-sdk-rs/blob/master/examples/fungible-token/README.md Builds the FT contract for a reproducible WASM output within a Docker container. Use '--no-locked' for demo purposes. ```bash pushd ft cargo near build reproducible-wasm --no-locked popd pushd test-contract-defi cargo near build reproducible-wasm --no-locked popd ``` -------------------------------- ### Build Contract Container Source: https://github.com/near/near-sdk-rs/blob/master/contract-builder/README.md Execute this script to build the Docker container for contract compilation. ```bash ./build.sh ``` -------------------------------- ### Create Testnet Developer Account Source: https://github.com/near/near-sdk-rs/blob/master/examples/callback-results/README.md Execute this command to create a new developer account on the NEAR testnet. This account can be used for deploying and testing your smart contracts. ```bash cargo near create-dev-account ``` -------------------------------- ### Deploy NFT Contract (Dev Account) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Deploys the built NFT contract to the current dev account. Ensure you are in the 'nft' directory. ```bash pushd nft cargo near deploy build-non-reproducible-wasm popd ``` -------------------------------- ### Create Testnet Dev Account Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Creates a new testnet developer account. Run this command multiple times as needed. ```bash cargo near create-dev-account # 3 times ``` -------------------------------- ### Rust Initialization Method for StatusMessage Contract Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Shows how to define an initialization method for a NEAR smart contract using the `#[init]` decorator. This method ensures the contract state is initialized only once. ```rust #[near] impl StatusMessage { #[init] pub fn new(user: String, status: String) -> Self { let mut res = Self::default(); res.records.insert(user, status); res } } ``` -------------------------------- ### Run Integration Tests Source: https://github.com/near/near-sdk-rs/blob/master/examples/factory-contract-global/README.md Execute integration tests using near-workspaces or realistic test environments. ```bash cargo test --test workspaces ``` ```bash cargo test --test realistic ``` -------------------------------- ### Deploy FT Contract to Dev Account Source: https://github.com/near/near-sdk-rs/blob/master/examples/fungible-token/README.md Deploys the built FT contract to your NEAR testnet development account. ```bash pushd ft cargo near deploy build-non-reproducible-wasm popd pushd test-contract-defi cargo near deploy build-non-reproducible-wasm popd ``` -------------------------------- ### Shell Command to Run NEAR SDK Unit Tests Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Provides the command to run unit tests for a NEAR SDK Rust project using Cargo. ```sh cargo test --package status-message ``` -------------------------------- ### Deploy Token Receiver Contract (Dev Account) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Deploys the built test-token-receiver contract to the current dev account. Ensure you are in the 'test-token-receiver' directory. ```bash pushd test-token-receiver cargo near deploy build-non-reproducible-wasm popd ``` -------------------------------- ### Build NFT Contract (Reproducible) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Builds the NFT contract using cargo-near for a reproducible WASM output within a Docker container. Use '--no-locked' for demo purposes. ```bash pushd nft cargo near build reproducible-wasm --no-locked popd ``` -------------------------------- ### Demo Reproducible Build with cargo-near Source: https://github.com/near/near-sdk-rs/blob/master/examples/cross-contract-calls/README.md Demonstrates reproducible WASM builds within a Docker container using `cargo near build reproducible-wasm --no-locked`. This is useful for CI/CD pipelines. ```bash pushd high-level cargo near build reproducible-wasm --no-locked popd pushd low-level cargo near build reproducible-wasm --no-locked popd ``` -------------------------------- ### Initialize StatusMessage Contract Source: https://github.com/near/near-sdk-rs/blob/master/README.md This snippet shows how to define an initialization method for a NEAR smart contract using the `#[init]` attribute. It ensures the contract is initialized only once and sets initial state. The contract must also derive the `Default` trait. ```rust impl StatusMessage { #[init] pub fn new(user: String, status: String) -> Self { let mut res = Self::default(); res.records.insert(user, status); res } } ``` -------------------------------- ### Deploy Contract to Dev Account Source: https://github.com/near/near-sdk-rs/blob/master/examples/callback-results/README.md This command deploys a previously built WASM contract to your developer account on the NEAR testnet. Ensure the contract name matches the build output. ```bash cargo near deploy build-non-reproducible-wasm ``` -------------------------------- ### Implement Smart Contract Methods Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Implements public methods for a NEAR smart contract. These methods are exposed as smart contract functions and can interact with the blockchain environment using `env::*`. ```rust #[near] impl MyContract { pub fn insert_data(&mut self, key: u64, value: u64) -> Option { self.data.insert(key) } pub fn get_data(&self, key: u64) -> Option { self.data.get(&key).cloned() } } ``` -------------------------------- ### Add near-sdk-core Dependency Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk-core/README.md Add the near-sdk-core crate to your Cargo.toml file to include core NEAR types in your project. ```toml [dependencies] near-sdk-core = "0.1" ``` -------------------------------- ### Build Contracts with cargo-near Source: https://github.com/near/near-sdk-rs/blob/master/examples/factory-contract/README.md Builds WASM binaries for smart contracts using `cargo near build`. Use `non-reproducible-wasm` for standard builds and `reproducible-wasm` for reproducible builds. ```bash pushd high-level cargo near build non-reproducible-wasm popd pushd low-level cargo near build non-reproducible-wasm popd ``` ```bash pushd high-level cargo near build reproducible-wasm --no-locked popd pushd low-level cargo near build reproducible-wasm --no-locked popd ``` -------------------------------- ### Set Host Directory for Contracts Source: https://github.com/near/near-sdk-rs/blob/master/contract-builder/README.md Export the HOST_DIR environment variable to specify a different directory for contracts if needed. ```bash export HOST_DIR=/root/contracts/ ``` -------------------------------- ### Build FT Contract Source: https://github.com/near/near-sdk-rs/blob/master/examples/fungible-token/README.md Builds the FT contract using cargo-near. Use 'non-reproducible-wasm' for general builds. ```bash pushd ft cargo near build non-reproducible-wasm popd pushd test-contract-defi cargo near build non-reproducible-wasm popd ``` -------------------------------- ### Build and deploy NEAR Rust contract Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Builds the smart contract (equivalent to `cargo near build`) and initiates the deployment process to the blockchain. ```sh cargo near deploy ``` -------------------------------- ### Build Token Receiver Contract (Reproducible) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Builds the test-token-receiver contract using cargo-near for a reproducible WASM output within a Docker container. Use '--no-locked' for demo purposes. ```bash pushd test-token-receiver cargo near build reproducible-wasm --no-locked popd ``` -------------------------------- ### Build NFT Contract (Non-Reproducible) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Builds the NFT contract using cargo-near for a non-reproducible WASM output. Ensure you are in the 'nft' directory. ```bash pushd nft cargo near build non-reproducible-wasm popd ``` -------------------------------- ### Create Testnet Dev Account Source: https://github.com/near/near-sdk-rs/blob/master/examples/factory-contract/README.md Creates a new developer account on the NEAR testnet using `cargo near create-dev-account`. It is recommended to run this command twice to ensure a unique account is generated. ```bash cargo near create-dev-account # twice ``` -------------------------------- ### Navigate to Mounted Directory Source: https://github.com/near/near-sdk-rs/blob/master/contract-builder/README.md Change the current directory to /host within the Docker container to access mounted files. ```bash cd /host ``` -------------------------------- ### Build NEAR Contracts with cargo-near Source: https://github.com/near/near-sdk-rs/blob/master/examples/cross-contract-calls/README.md Builds NEAR smart contracts using `cargo near build`. Use `non-reproducible-wasm` for standard builds. ```bash pushd high-level cargo near build non-reproducible-wasm popd pushd low-level cargo near build non-reproducible-wasm popd ``` -------------------------------- ### Deploy Contracts with cargo-near Source: https://github.com/near/near-sdk-rs/blob/master/examples/factory-contract/README.md Deploys the built WASM contract to a developer account using `cargo near deploy`. Ensure you are in the contract's directory before running the deploy command. ```bash pushd high-level cargo near deploy build-non-reproducible-wasm popd pushd low-level cargo near deploy build-non-reproducible-wasm popd ``` -------------------------------- ### Retrieve Data ID and Respond to Signing Request Source: https://github.com/near/near-sdk-rs/blob/master/examples/mpc-contract/README.md First, retrieve the data ID from pending requests. Then, use this data ID to send the signature and resume the waiting transaction. This must be done before the transaction times out. ```bash # This should return a non-null value near --quiet contract call-function as-read-only test.near get_requests json-args {} network-config sandbox now | jq first.data_id DATA_ID=$(near --quiet contract call-function as-read-only test.near get_requests json-args {} network-config sandbox now | jq first.data_id) near contract call-function as-transaction test.near 'sign_respond' json-args '{ "data_id": '$DATA_ID', "signature": "signature" }' prepaid-gas '200.0 Tgas' attached-deposit '0 NEAR' sign-as test.near network-config sandbox sign-with-keychain send ``` -------------------------------- ### Rust Payable Method Declaration Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Illustrates how to declare a payable method in a NEAR smart contract using the `#[payable]` decorator, allowing the method to accept token transfers. ```rust #[payable] pub fn my_method(&mut self) { ... ``` -------------------------------- ### Build NEAR Rust contract Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Builds a NEAR smart contract and its ABI. Assumes you are in the directory containing the contract's Cargo.toml. ```sh cargo near build ``` -------------------------------- ### Build Token Receiver Contract (Non-Reproducible) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Builds the test-token-receiver contract using cargo-near for a non-reproducible WASM output. Ensure you are in the 'test-token-receiver' directory. ```bash pushd test-token-receiver cargo near build non-reproducible-wasm popd ``` -------------------------------- ### Deploy Approval Receiver Contract (Dev Account) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Deploys the built test-approval-receiver contract to the current dev account. Ensure you are in the 'test-approval-receiver' directory. ```bash pushd test-approval-receiver cargo near deploy build-non-reproducible-wasm popd ``` -------------------------------- ### Build Approval Receiver Contract (Reproducible) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Builds the test-approval-receiver contract using cargo-near for a reproducible WASM output within a Docker container. Use '--no-locked' for demo purposes. ```bash pushd test-approval-receiver cargo near build reproducible-wasm --no-locked popd ``` -------------------------------- ### Rust Private Method Equivalent Implementation Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Shows the equivalent implementation of a private method without the `#[private]` decorator, including a check to ensure only the contract itself can call the method. ```rust pub fn my_method(&mut self ) { if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { near_sdk::env::panic_str("Method my_method is private"); } ... ``` -------------------------------- ### Build NEAR Rust contract without ABI Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Builds a NEAR smart contract, skipping ABI generation. Useful for circumventing schema/ABI errors during build. ```sh cargo near build non-reproducible-wasm --no-abi ``` -------------------------------- ### Build Reproducible WASM Contract (Docker) Source: https://github.com/near/near-sdk-rs/blob/master/examples/callback-results/README.md This command builds a WASM contract with reproducibility enabled, typically within a Docker container for consistent build environments. The `--no-locked` flag is used for demo purposes to avoid committing Cargo.lock. ```bash cargo near build reproducible-wasm --no-locked ``` -------------------------------- ### Declare Payable Method Source: https://github.com/near/near-sdk-rs/blob/master/README.md This snippet shows how to declare a payable method in a NEAR smart contract using the `#[payable]` decorator. This allows the method to accept token transfers during its invocation. ```rust #[payable] pub fn my_method(&mut self) { ... } ``` -------------------------------- ### Derive Account ID Off-Chain Source: https://github.com/near/near-sdk-rs/blob/master/near-global-contracts/README.md Use StateInit and GlobalContractId to derive an account ID off-chain. This requires the 'borsh' feature to be enabled. ```rust use near_global_contracts::{StateInit, StateInitV1, GlobalContractId}; let state_init = StateInit::from(StateInitV1::code( GlobalContractId::AccountId("example.near".parse().unwrap()), )); let account_id = state_init.derive_account_id(); println!("{account_id}"); // 0s<40 hex chars> ``` -------------------------------- ### Build Approval Receiver Contract (Non-Reproducible) Source: https://github.com/near/near-sdk-rs/blob/master/examples/non-fungible-token/README.md Builds the test-approval-receiver contract using cargo-near for a non-reproducible WASM output. Ensure you are in the 'test-approval-receiver' directory. ```bash pushd test-approval-receiver cargo near build non-reproducible-wasm popd ``` -------------------------------- ### Equivalent of Private Method Source: https://github.com/near/near-sdk-rs/blob/master/README.md This snippet shows the equivalent implementation of a private method without using the `#[private]` decorator. It includes an explicit check to ensure the caller is the contract itself. ```rust pub fn my_method(&mut self ) { if near_sdk::env::current_account_id() != near_sdk::env::predecessor_account_id() { near_sdk::env::panic_str("Method my_method is private"); } ... } ``` -------------------------------- ### Send Signing Request Transaction Source: https://github.com/near/near-sdk-rs/blob/master/examples/mpc-contract/README.md Initiate a transaction to send a signing request. This transaction will wait for another account to provide data on-chain to resume. ```bash near contract call-function as-transaction test.near 'sign' json-args '{ "message": "message-to-sign" }' prepaid-gas '200.0 Tgas' attached-deposit '0 NEAR' sign-as test.near network-config sandbox sign-with-keychain send ``` -------------------------------- ### Add near-global-contracts dependency Source: https://github.com/near/near-sdk-rs/blob/master/near-global-contracts/README.md Add the near-global-contracts crate to your Cargo.toml file with the 'serde' and 'borsh' features enabled for off-chain usage. ```toml [dependencies] near-global-contracts = { version = "0.1", features = ["serde", "borsh"] } ``` -------------------------------- ### Build Non-Reproducible WASM Contract Source: https://github.com/near/near-sdk-rs/blob/master/examples/callback-results/README.md Use this command to build a WASM contract without ensuring reproducibility. This is suitable for general development and testing. ```bash cargo near build non-reproducible-wasm ``` -------------------------------- ### Declare Private Method Source: https://github.com/near/near-sdk-rs/blob/master/README.md This snippet demonstrates how to declare a private method in a NEAR smart contract using the `#[private]` decorator. This ensures that the method can only be called by the contract itself, enhancing security. ```rust #[private] pub fn my_method(&mut self) { ... } ``` -------------------------------- ### Rust Private Method Declaration Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Demonstrates the use of the `#[private]` decorator to declare a private method in a NEAR smart contract, restricting its callability to the contract itself. ```rust #[private] pub fn my_method(&mut self) { ... ``` -------------------------------- ### Define Smart Contract State Struct Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Defines the structure for a NEAR smart contract, including its state. The struct must implement the `Default` trait and be decorated with `#[near(contract_state)]`. ```rust use near_sdk::{near, env}; #[near(contract_state)] #[derive(Default)] pub struct MyContract { data: HashMap } ``` -------------------------------- ### Add Wasm Target Source: https://github.com/near/near-sdk-rs/blob/master/near-sdk/README.md Adds the wasm32-unknown-unknown target to your Rust toolchain, necessary for compiling Rust code to WebAssembly for NEAR contracts. ```sh rustup target add wasm32-unknown-unknown ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.