什么是Near API?
Near API提供对Near区块链的编程访问,包括账户管理、智能合约部署和交易查询。Near是一个高性能、低费用的Layer1区块链。
公共RPC端点
| 网络 | RPC URL |
|---|---|
| Mainnet | https://rpc.mainnet.near.org |
| Testnet | https://rpc.testnet.near.org |
| Mainnet (第三方) | https://rpc.near.org |
JavaScript示例
const nearAPI = require('near-api-js');
const { connect, KeyPair, keyStores } = nearAPI;
const config = {
networkId: 'mainnet',
nodeUrl: 'https://rpc.mainnet.near.org',
walletUrl: 'https://wallet.mainnet.near.org',
helperUrl: 'https://helper.mainnet.near.org',
explorerUrl: 'https://explorer.mainnet.near.org',
};
async function connectToNear() {
const near = await connect({
...config,
keyStore: new keyStores.InMemoryKeyStore()
});
return near;
}
async function getAccountBalance(accountId) {
const near = await connectToNear();
const account = await near.account(accountId);
const balance = await account.getAccountBalance();
return balance;
}
// 使用示例
// getAccountBalance('example.near').then(console.log);
Python示例
import requests
NEAR_RPC = "https://rpc.mainnet.near.org"
def query_account(account_id):
"""查询账户信息"""
params = {"account_id": account_id}
response = requests.post(
NEAR_RPC,
json={
"jsonrpc": "2.0",
"id": "dontcare",
"method": "query",
"params": {
"request_type": "view_account",
"account_id": account_id,
"finality": "final"
}
}
)
return response.json()
def get_block():
"""获取最新区块"""
response = requests.post(
NEAR_RPC,
json={
"jsonrpc": "2.0",
"id": "dontcare",
"method": "block",
"params": {"finality": "final"}
}
)
return response.json()
# 使用示例
# account_info = query_account("example.near")
# print(account_info)
主要RPC方法
| 方法 | 说明 |
|---|---|
| query | 查询账户、合约状态 |
| block | 获取区块信息 |
| tx | 查询交易状态 |
| gas_price | 获取Gas价格 |
提示: 对于生产环境,建议使用付费的专用节点服务如Infura或Alchemy的Near服务,以获得更好的稳定性和速率限制。
开始交易NEAR
交易NEAR及其他加密货币:注册OKX