NEAR 102
An introductory workshop
for Ethereum developers
near.org
Available Recordings
- youtube.com/watch?v=K0Bkc0rsW8U�- crowdcast.io/e/hacktherainbow/13
- crowdcast.io/e/hacktherainbow/7�
Workshop Preparation
Please complete the following steps before the workshop starts:�
If you have time, feel free to complete the following steps as well:�
Why NEAR?
NEAR is cheaper, faster and easier to use
Development�
Production�
Several examples and starters
github.com / near / create-near-app
npx create-near-app banana
��Ethereum ⇔ NEAR Cookbook
Get started in minutes
account |
|
state |
|
transaction |
|
gas |
|
block |
|
Same-same … but different
primitives
| Ethereum | NEAR |
standards | ERC20 / ERC721 | |
transaction results | Etherscan | either view transaction in NEAR Explorer or use near tx-status <tx_hash> |
special data types | address | accounts are String type and validation is available in Rust with env::is_valid_account_id |
mapping | Collections ( Rust & AssemblyScript ) | |
payable methods | payable | #[payable] ( Rust decorator ) |
event model | Events | polling EXPERIMENTAL_changes RPC method |
running a node | ganache | nearup ( repo ) |
Same-same … but different
features
NEAR
Deployment typically follows these steps:�
step 2 can be decomposed into multiple Txs
Same-same … but different
Ethereum
Deployment typically follows these steps:�
deployment
Same-same … but different
Ethereum Transactions
NEAR Transactions
origin (signer, pk)
destination (receiver)
uniqueness (nonce)
handled elsewhere
recency (block_hash)
origin (v,r,s)
destination (recipient)
uniqueness (nonce)
cost control (gas price & limit)
transactions
attached NEAR tokens
Mapping concepts
NEAR Transactions
attached Ether
transactions
Ethereum Transactions
Four categories of actions
Mapping concepts
NEAR Transactions
selector (function signature)
arguments (encoded)
transactions
Ethereum Transactions
Mapping concepts
NEAR supports 8 composable primitive Actions
transactions
Mapping concepts
Ethereum
�Custom Language → Custom VM
Pros
�Cons
NEAR
�AssemblyScript / Rust → Wasm VM
Pros
Cons
runtime
Rust
Pros
Cons
AssemblyScript
Pros
Cons
NEAR contract languages
development
In some ways … different
asynchronous calls
B
A
C
D
In some ways … different
asynchronous calls
B
A
C
D
In some ways … same
no surprise here
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
In some ways … same
no surprise here
External “batch” calls are used to chain a series of Actions together to be executed on some target account�
B
In some ways … different
asynchronous 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
In some ways … different
asynchronous calls
Internal “promise” calls which are used to invoke methods on other contracts (or recursively on the current contract)
D
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
1
2
3
4
1
2
3
4
Rainbow Bridge: NEAR Ethereum
Rainbow Bridge: NEAR Ethereum
How do I
get started?
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 a frontend and a backend
Frontend (UX)
Backend (contracts)
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
What just
happened?
NEAR exposes two development interfaces
dApp UX development
dApp Contract Development
NEAR exposes two development interfaces
dApp UX development
dApp Contract Development
NEAR exposes two development interfaces
dApp UX development
dApp Contract Development
NEAR exposes two development interfaces
dApp UX development
dApp Contract Development
Structure of the Sample Application
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
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
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
Account ≈ Contract + State
Runtime Layer
state storage
virtual
machine
code
load
read
write
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
Writing Contracts
Rust (contract data)
AssemblyScript (contract data)
AssemblyScript & Rust contract alignment
Rust (contract behavior)
AssemblyScript (contract behavior)
AssemblyScript & Rust contract alignment
“To enable WebAssembly to be read and edited by humans, there is a textual representation of the wasm binary format. This is an intermediate form designed to be exposed in text editors, browser developer tools, etc.”
-- Understanding WebAssembly text format
Although we will not spend time on this topic, this format has proven consistently readable (especially with AssemblyScript) if only to sanity check that exported contract functions are visible and near-sdk-as runtime components are being called in the right places.
The output is always a .wasm file
Walking through �the Guest Book
Guest Book
User Interface
Guest Book
Filesystem
Guest Book
Contract Data Model : assembly/model.ts
note: @nearBindgen marks the class as serializable
Contract
Contract Behavior : assembly/main.ts
Guest Book
Contract
Guest Book
Network Connection : src/config.js
Frontend
Guest Book
Frontend
Configuration : src/index.js
Guest Book
Authentication : src/App.js
�
note: useCallback() is a React Hook
Frontend
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
Demo: Monitoring
Mapping
Concepts
Ethereum supports two types of accounts
From Ethereum to NEAR
accounts
From Ethereum to NEAR
NEAR supports one type of account
* NEAR accounts follow a human friendly DNS naming pattern with similar scoping rules for ownership and control. For example user.testnet controls app1.user.testnet and app2.user.testnet.
accounts
Ethereum
* codeHash may be either:
NEAR
** code_hash is similar to Ethereum
*** each access key tracks its own nonce��note that receipts are used to manage cross-contract calls
From Ethereum to NEAR
accounts
Exploring the
Platform
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
share bit.ly / near-102
Thank you!
visit near.help
Touch base on Telegram
Discover us on Discord
Suggest topics on StackOverflow