near-contract-tools

Helpful functions and macros for developing smart contracts on NEAR Protocol.

This package is a collection of common tools and patterns in NEAR smart contract development:

Not to be confused with near-contract-standards, which contains official implementations of standardized NEPs.

WARNING: This is still early software, and there may be breaking changes between versions. I'll try my best to keep the docs & changelogs up-to-date. Don't hesitate to create an issue if find anything wrong.

Example

Ownership

```rust use nearcontracttools::{Ownable, ownership::OwnershipController}; use nearsdk::{AccountId, nearbindgen};

[derive(Ownable)]

[near_bindgen]

struct Contract { // ... }

[near_bindgen]

impl Contract { #[init] pub fn new(owner_id: AccountId) -> Self { let contract = Self { // ... };

    // Initialize the owner of the contract
    contract.init_owner(owner_id);

    contract
}

pub fn owner_only_method(&self) {
    self.require_owner();

    // ...
}

} ```

This creates a smart contract which exposes the Ownable trait to the blockchain:

rust pub trait Ownable { fn own_get_owner(&self) -> Option<AccountId>; fn own_get_proposed_owner(&self) -> Option<AccountId>; fn own_renounce_owner(&mut self); fn own_propose_owner(&mut self, account_id: Option<AccountId>); fn own_accept_owner(&mut self); }

Events

```rust use nearcontracttools::event::*; use nearcontracttools::Event; use serde::Serialize;

[derive(Serialize)]

pub struct Nep171NftMintData { pub ownerid: String, pub tokenids: Vec, }

[derive(Event, Serialize)]

[event(standard = "nep171", version = "1.0.0")]

[serde(untagged)]

pub enum Nep171 { #[event(name = "nft_mint")] NftMint(Vec), }

let myevent = Nep171::NftMint(vec![Nep171NftMintData { ownerid: "owner".tostring(), tokenids: vec!["token1".tostring(), "token2".tostring()], }]);

my_event.emit(); // Emits event to the blockchain ```

Authors