NEAR 101
An introductory workshop
for web developers
near.org
Workshop Preparation
Please complete the following steps before the workshop starts:�
If you have time, feel free to complete the following steps as well:�
web2 → web3
Coming from web2 to web3: a primer
| web2 | web3 |
topology | client-server | client-(server+blockchain) |
app dev technology |
|
|
security | accounts
| accounts�asymmetric key pairs (public and private keys) |
servers
| servers�← web2 + consensus (PoW, PoS, etc) | |
key performance | fast. cheap. data is “native” | unassailable. permanent. money is "native" |
Dissecting an
Application
Guest Book
Guest Book
Guest Book (backend)
Contract Data Model : assembly/model.ts
note: @nearBindgen marks the class as serializable
Contract Behavior : assembly/main.ts
Guest Book (backend)
Guest Book (frontend)
Network Connection : src/config.js
Configuration : src/index.js
Guest Book (frontend)
Guest Book (frontend)
Authentication : src/App.js
�
note: useCallback() is a React Hook
CRUD : src/App.js
Guest Book (frontend)
Monitoring the application ...
http post https://rpc.testnet.near.org \
jsonrpc=2.0 id=dontcare
method=query params:='{
"request_type": "view_state",
"finality": "final",
"account_id": "test",
"prefix_base64": "U1RBVEU="
}’
And, using the watch command and some JSON formatting with jq, automatically
refresh a convenient view while we use
the Guestbook example. Here’s how.
Also see here for our JSON RPC API docs
Guest Book
NEAR has two paths for extension
dApp UX development
dApp Contract Development
Runtime Layer
state storage
virtual
machine
code
load
apply
result
read
write
Your dApp
send
receive
Blockchain Layer
RPC Interface
near-api-js
contracts
near-sdk-as
near-sdk-rs
deploy
NEAR Protocol for Developers
Lifecycle of a transaction
Runtime Layer
state storage
virtual
machine
code
load
apply
result
read
write
Your dApp
send
receive
Blockchain Layer
RPC Interface
near-api-js
contracts
near-sdk-as
near-sdk-rs
1
2
3
4
5
6
7
deploy
write
read`
Runtime Layer
state storage
virtual
machine
code
load
apply
result
Your dApp
send
receive
Blockchain Layer
RPC Interface
near-api-js
contracts
near-sdk-as
near-sdk-rs
deploy
Structure of the Sample Application
Build with NEAR
create-near-app
To start building on NEAR run the following* in your terminal:
npx create-near-app --help
* you must have Node.js installed to run npx
create-near-app
Choose templates for a frontend and a contract:
Frontend
Contract
create-near-app
Choose templates for a frontend and a contract:
Frontend
Contract
create-near-app
step #1
npx create-near-app banana
create-near-app
step #2
yarn dev�
( or use npm run dev )
step #3
create-near-app
Sign in to NEAR Wallet
( 1 ) create new account
( 2 ) authorize your app
step #4
create-near-app
Change the message
( 1 ) edit the greeting
( 2 ) click Save
( 3 ) observe a → b
a
b
b
step #5 … n�
create-near-app
Rust
Pros
Cons
AssemblyScript
Pros
Cons
AssemblyScript has its place
Exploring the
Platform
NEAR Explorer
NEAR Wallet
NEAR Wallet
NEAR CLI
near-api-js
Library designed to connect to the NEAR platform from any JavaScript execution context (server-side or client-side JS)
Client-side
Server-side
near-api-js
Server-side
�UnencryptedFileSystemKeyStore
Client-side
�BrowserLocalStorageKeyStore
near-api-js
near-api-js
Your dApp
Examples at near.dev
NEAR Explorer
NEAR Wallet
NEAR CLI
send
receive
RPC Interface
Blockchain Layer
near-api-js uses JSON RPC API
near-api-js
Your dApp
Examples at near.dev
NEAR Explorer
NEAR Wallet
NEAR CLI
send
receive
RPC Interface
Blockchain Layer
near-api-js uses JSON RPC API
near-sdk-as
Library designed to support the development of valid NEAR contracts using AssemblyScript (AS)
near-sdk-rs
Library designed to support the development of valid NEAR contracts using Rust (RS)
Accounts
Transactions
& State
���
see https://docs.near.org/docs/concepts/account�see https://docs.near.org/docs/concepts/storage
NEAR Accounts
���
NEAR Transactions
���
NEAR State
Account ≈ Contract + State
Runtime Layer
state storage
virtual
machine
code
load
read
write
Your dApp
send
receive
Blockchain Layer
RPC Interface
near-api-js
contracts
near-sdk-as
near-sdk-rs
result
apply
Account ≈ Contract + State
Runtime Layer
state storage
virtual
machine
code
load
result
read
write
apply
Account ≈ Contract + State
Runtime Layer
virtual
machine
code
load
metadata
result
read
write
data
apply
state storage
Runtime Layer
result
apply
Account ≈ Contract + State
virtual
machine
code
load
read
write
key | value |
STATE | your contract code |
message | “hello world” |
counter | 3 |
prefix::identifier | value at collection key |
prefix::len | PersistentVector length |
prefix::last | PersistentDeque last item |
state storage
Scale with NEAR
Cross-contract calls enable scalability
shard.1.vm
shard.2.vm
…
send
receive
cross-contract call
cross-contract call
Your
dApp
Each NEAR account is assumed to live on its own shard, even if two accounts are physically located in the same shard. This makes any method invocation from one contract to another a “cross-contract call” (contract = account + state)
From the perspective of a contract developer, cross-contract calls look a lot like promises in popular languages. They may or may not return a value, support the orchestration of call chains and are capable of merging multiple calls into one.
cross-contract call
Cross-contract calls enable scalability
shard.1.vm
shard.2.vm
send
receive
internal
NEAR supports two contexts for cross-contract calls�
�
Both external and internal cross-contract calls support “batch” calls and “promise” calls
Your
dApp
external
Exploring cross-contract calls
B
A
C
D
B & A are NOT cross-contract calls
C & D are cross-contract calls
Exploring cross-contract calls
B
A
C
D
B & A are NOT cross-contract calls
C & D are cross-contract calls
Exploring cross-contract calls
External “promise” calls which are used to invoke methods on multiple contracts using the standard Promise interface
This example was taken from MDN (just assume the functions wrap near-api-js)
A
Exploring cross-contract calls
External “batch” calls are used to chain a series of Actions together to be executed on some target account�
B
Exploring cross-contract calls
Internal “batch” calls which are used to chain a series of Actions together to be executed on some target account (including being applied to the current contract)
C
Exploring cross-contract calls
Internal “promise” calls which are used to invoke methods on other contracts (or recursively on the current contract)
D
Testing
deploy
MainNet
simulation
integration
deploy
TestNet
unit
build
contract
Our APIs in a landscape of testing
2
AssemblyScript
contracts
near-sdk-as
Rust
contracts
near-sdk-rs
1
3
unit testing
( as-pect )
unit testing
( #[test] )
Runtime API
near-vm CLI
blockchain
near-api-js API
NEAR CLI
Contract testing is convenient and maturing
simulation tests
integration tests
AssemblyScript
contracts
near-sdk-as
unit testing
( as-pect )
internal
1
unit testing
( as-pect )
Contract testing is convenient and maturing
integration tests
AssemblyScript
contracts
near-sdk-as
unit testing
( as-pect )
simulation tests
simulation
near-vm CLI
2
Contract testing is convenient and maturing
integration tests
AssemblyScript
contracts
near-sdk-as
unit testing
( as-pect )
simulation tests
simulation
2
Runtime API
Contract testing is convenient and maturing
integration tests
AssemblyScript
contracts
near-sdk-as
unit testing
( as-pect )
simulation tests
simulation
3
NEAR CLI
Contract testing is convenient and maturing
integration tests
AssemblyScript
contracts
near-sdk-as
unit testing
( as-pect )
simulation tests
simulation
3
near-api-js API
share bit.ly / near-101
Thank you!
visit near.help
Touch base on Telegram
Discover us on Discord
Suggest topics on StackOverflow