NEAR
NEAR is a user-friendly, scalable layer-1 blockchain platform that uses a sharded proof-of-stake consensus mechanism. It features human-readable account names, supports smart contracts in Rust and AssemblyScript, and offers low transaction fees.
Core Concepts
Setting up your wallet
Use the Meteor Wallet to create a Near account. Then go to Settings > Security and Recovery > Export Private Key
to copy your private key.
Use the Near Testnet Faucet to airdrop fund your testnet wallet.
Using the SDK
Initialization
Import the SDK and create a new instance:
import { NEAR } from "@kynesyslabs/demosdk/xm-websdk"
const rpc_url = "https://rpc.testnet.near.org"
const networkId = "testnet"
const instance = await NEAR.create(rpc_url, networkId)
You can then use the instance to do a read operation on near.
const balance = await instance.getBalance("cwilvx.testnet")
console.log(`Balance: ${balance}Ⓝ`)
Connecting your wallet
To perform token transfers or other transactions, you need to connect your wallet to the SDK. You also need to provide the accountId to be associated with your private key.
await instance.connectWallet(
"ed25519:4QRNiJk7A584sU3aV1xhqdbdairYJybjHJq9z78s7ntw2JVeBYybZ4r4Ec",
{
accountId: "cwilvx.testnet",
},
)
You can view the address of your connected wallet using the getAddress
method.
const address = instance.getAddress()
console.log(`Address: ${address}`)
Token transfer
To create a transaction to transfer Ⓝ on near, you can use the preparePay
or prepareTranfer
methods:
const signedTx = await instance.preparePay("cwilvx.testnet", "1.5")
The signed tx is a Uint8Array
that can be used in a XM work step.
Signing Messages
const message = "Hello, world!"
// Signing
const signature = await instance.signMessage(message)
console.log(signature)
// Verifying signature
const verified = await instance.verifyMessage(
message,
signature,
instance.getAddress(),
)
expect(verified).toBe(true)
Creating accounts
You can call the createAccount
method to create an account, passing the accountId and the Ⓝ amount to deposit to the created account. You can optionally specify the curve to use when generating the key pair for the new account.
const { keyPair, signedTx } = await instance.createAccount(
"other.me.testnet",
"1",
)
console.log(keyPair.getPublicKey().toString())
The method returns the signed transaction for creating the new account on Near, and its key pair.
Deleting accounts
You can delete the connected account by calling the deleteAccount
method and passing the NEAR account that will receive the remaining Ⓝ balance from the account being deleted.
const signedTx = await instance.deleteAccount("cwilvx.testnet")
Hacking
To create custom transactions for the Near blockchain using the Demos Near SDK, you can access the underlying API to create transactions.
instance.actions
Transaction Actions
instance.provider
The near class instance provided by near-api-js
instance.wallet
Your connected KeyPair
instance.signTransaction(s)
Sign transactions
Resources
Here's the converted list with markdown links:
Last updated