1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Typed contract interfaces.
//!
//! This module provides the [`Contract`] trait for creating type-safe contract
//! clients using the `#[near_kit::contract]` proc macro.
//!
//! # Why Typed Contracts?
//!
//! Without typed contracts, method names and arguments are stringly-typed:
//!
//! ```rust,no_run
//! # use near_kit::*;
//! # async fn example(near: &Near) -> Result<(), Error> {
//! // Typos compile fine but fail at runtime
//! let count: u64 = near.view("counter.near", "get_counnt").await?; // typo!
//! # Ok(())
//! # }
//! ```
//!
//! With typed contracts, the compiler catches errors:
//!
//! ```ignore
//! let counter = near.contract::<Counter>("counter.near");
//! let count = counter.get_count().await?; // Compile-time checked!
//! ```
//!
//! # Defining a Contract Interface
//!
//! Use the `#[near_kit::contract]` macro on a trait:
//!
//! ```ignore
//! use near_kit::*;
//! use serde::Serialize;
//!
//! #[near_kit::contract]
//! pub trait Counter {
//! // View method: &self, no #[call] attribute
//! fn get_count(&self) -> u64;
//!
//! // Change method: &mut self + #[call] attribute
//! #[call]
//! fn increment(&mut self);
//!
//! // Change method with arguments
//! #[call]
//! fn add(&mut self, args: AddArgs);
//!
//! // Payable method (can receive NEAR deposit)
//! #[call(payable)]
//! fn donate(&mut self);
//! }
//!
//! #[derive(Serialize)]
//! pub struct AddArgs {
//! pub value: u64,
//! }
//! ```
//!
//! # Using a Typed Contract
//!
//! ```ignore
//! async fn example(near: &Near) -> Result<(), Error> {
//! let counter = near.contract::<Counter>("counter.testnet");
//!
//! // View calls
//! let count = counter.get_count().await?;
//!
//! // Change calls
//! counter.increment().await?;
//! counter.add(AddArgs { value: 5 }).await?;
//!
//! // Payable calls with deposit
//! counter.donate().deposit(NearToken::from_near(1)).await?;
//!
//! // Override gas
//! counter.add(AddArgs { value: 10 }).gas(Gas::from_tgas(50)).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! # Composing Typed Calls in Transactions
//!
//! The macro also generates static `FunctionCall` constructors on the struct,
//! so typed calls can be mixed with other actions in a single transaction:
//!
//! ```ignore
//! // Compose state_init + typed call in one transaction
//! near.transaction(contract_id)
//! .state_init(state_init, NearToken::ZERO)
//! .add_action(Counter::increment())
//! .send().await?;
//!
//! // Compose calls from multiple contract standards
//! near.transaction("token.near")
//! .add_action(StorageManagement::storage_deposit(deposit_args))
//! .add_action(FungibleToken::ft_transfer_call(transfer_args))
//! .send().await?;
//! ```
//!
//! # Serialization Formats
//!
//! By default, arguments are serialized as JSON and view responses are
//! deserialized from JSON. Use `#[near_kit::contract(borsh)]` to switch
//! both directions to Borsh (call args are Borsh-encoded, view responses
//! are Borsh-decoded):
//!
//! ```ignore
//! use borsh::BorshSerialize;
//!
//! #[derive(BorshSerialize)]
//! pub struct UploadArgs {
//! pub data: Vec<u8>,
//! }
//!
//! #[near_kit::contract(borsh)]
//! pub trait DataStore {
//! fn get_size(&self) -> u64;
//!
//! #[call]
//! fn upload(&mut self, args: UploadArgs);
//! }
//! ```
//!
//! You can also override the format per-method with `#[borsh]` or `#[json]`:
//!
//! ```ignore
//! #[near_kit::contract] // default: JSON
//! pub trait MixedContract {
//! fn get_status(&self) -> String; // JSON (default)
//!
//! #[call]
//! fn set_config(&mut self, args: Config); // JSON (default)
//!
//! #[call]
//! #[borsh]
//! fn upload_data(&mut self, args: Data); // Borsh (override)
//! }
//! ```
use crateNear;
use crateAccountId;
/// Marker trait for typed contract interfaces.
///
/// This trait is automatically implemented by the `#[near_kit::contract]` macro
/// for each contract interface you define. It provides the associated `Client` type
/// that is used by [`Near::contract`](crate::Near::contract).
///
/// # Example
///
/// Given a contract definition:
///
/// ```ignore
/// #[near_kit::contract]
/// pub trait MyContract {
/// fn get_value(&self) -> u64;
/// #[call]
/// fn set_value(&mut self, args: SetArgs);
/// }
/// ```
///
/// The macro generates a unit struct `MyContract` with composable
/// `FunctionCall` constructors, a `MyContractClient` for the simple case,
/// and this implementation:
///
/// ```ignore
/// impl Contract for MyContract {
/// type Client = MyContractClient;
/// }
/// ```
/// Trait for contract client constructors.
///
/// This trait is implemented by the generated client structs to enable
/// construction via [`Near::contract`](crate::Near::contract).