Janji Berbasis Kontrak Dekat, Dekat Dompet, dan Dekat Klien RPC untuk Browser. Ini dirancang untuk memfasilitasi integrasi bereaksi dengan blockchain dekat dan menghindari boilerplate besar menyiapkan dompet dan kontrak.
Dokumentasi
view kontrak Menggunakan NPM:
# You can use any wallet selector version you want
npm install @wpdas/naxios @near-wallet-selector/[email protected]Menggunakan benang:
# You can use any wallet selector version you want
yarn add @wpdas/naxios @near-wallet-selector/[email protected]Impor gaya pemilih dompet dekat. Aplikasi ini membutuhkannya untuk membuat pemilih dompet dengan benar.
import '@near-wallet-selector/modal-ui/styles.css'Sangat mudah untuk mendapatkan dompet dan/atau kontrak API sekaligus. Lihatlah:
// web3Api.ts
import naxios from '@wpdas/naxios'
const naxiosInstance = new naxios ( {
rpcNodeUrl : 'https://free.rpc.fastnear.com' , // optional
contractId : CONTRACT_ID ,
network : 'testnet' ,
} )
/**
* NEAR Wallet API (Must be a single instance)
*/
export const walletApi = naxiosInstance . walletApi ( )
// Examples of contract API instance usage
/**
* Contract API
* This is going to use default contractId (CONTRACT_ID)
*/
export const contractApi = naxiosInstance . contractApi ( )
/**
* Another Contract API
*/
export const socialDBcontractApi = naxiosInstance . contractApi ( { contractId : 'v1.social08.testnet' } )
/**
* Greeting Contract API
*/
export const greetingContractApi = naxiosInstance . contractApi ( {
contractId : 'dev-1692221685438-15421910364142' ,
} )
/**
* NEAR RPC API
*/
export const rpcApi = naxiosInstance . rpcApi ( ) Anda dapat membuka modal pemilih dompet dekat dengan memanggil signInModal() :
import { walletApi } from './web3Api'
walletApi . signInModal ( ) Secara default, NaxiOS hanya menggunakan @dekat-Wallet-Selector/my-near-wallet sebagai cara menghubungkan dompet. Namun, Anda dapat menambahkan pemilih dompet lain sebagai berikut:
npm install @near-wallet-selector/ledger @near-wallet-selector/my-near-walletKemudian, Anda dapat memulai Naxius sebagai berikut:
import naxios from '@wpdas/naxios'
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet'
import { setupLedger } from '@near-wallet-selector/ledger'
const naxiosInstance = new naxios ( {
contractId : CONTRACT_ID ,
network : 'testnet' , // or mainnet, localnet
walletSelectorModules : [ setupMyNearWallet ( ) , setupLedger ( ) ] ,
} )
/**
* NEAR Wallet API (Must be a single instance)
*/
export const walletApi = naxiosInstance . walletApi ( )Cari tahu semua pemilih dompet dekat di sini: Pemilih Dompet Dekat
view : Lakukan panggilan baca saja untuk mengambil informasi dari jaringan. Ini memiliki parameter berikut:method : Nama metode kontrak.props? : Parameter opsional dengan args untuk metode kontrak.config? : Saat ini, ini hanya memiliki prop useCache . Ketika Usecache benar, ini akan menggunakan data cache yang tidak terasa alih-alih memanggil metode kontrak.call : Hubungi metode yang mengubah keadaan kontrak. Ini dibayarkan. Ini memiliki parameter berikut:method : Nama Metode Kontrakprops? : Parameter opsional dengan args untuk metode kontrak, gas , deposit untuk dilampirkan dan callbackUrl jika Anda ingin membawa pengguna ke halaman tertentu setelah transaksi berhasil.callMultiple : Hubungi beberapa metode yang mengubah keadaan kontrak. Ini dibayarkan dan memiliki parameter berikut:transactionsList : Daftar alat peraga transaksi. Anda dapat menggunakan buildTransaction(...) untuk membantu AndacallbackUrl? : Halaman untuk membawa pengguna setelah semua transaksi berhasil. accounts : Akun masuk.accountId : ID Akun Main/First Signed-In dalam daftar akun.contractId : ID kontrak.initNear : Menginisialisasi koneksi ke blockchain dekat. Ini disebut secara otomatis ketika ada interaksi kontrak.network : Jaringan Saat Ini ( testnet , mainnet atau localnet ).recentlySignedInWallets : mengembalikan ID-S dari 5 yang baru-baru ini ditandatangani di dompet.selectedWalletId : ID Dompet yang DipilihsignInModal : Buka Modal Dompet Signin.wallet : Contoh Dompet.walletSelector : Contoh Walletselector. Menggunakan metode view gratis.
import { greetingContractApi } from './web3Api'
// [free]
greetingContractApi . view < string > ( 'get_greeting' ) . then ( ( response ) => console . log ( response ) ) Anda perlu membayar untuk setiap permintaan yang Anda buat untuk metode call . Ini akan mengubah data dan menyimpannya di dalam blockchain.
import { greetingContractApi } from './web3Api'
// Set greeting [payable]
const args : { message : 'Hi there!!!' }
greetingContractApi . call < string | undefined > ( 'set_greeting' , args ) . then ( ( response ) => console . log ( response || 'done!' ) ) Serta call , Anda harus membayar untuk setiap permintaan yang Anda buat. Ini akan mengubah data dan menyimpannya di dalam blockchain.
import { buildTransaction } from '@wpdas/naxios'
import { contractApi } from './web3Api'
// Using the default instance's contract
const transactionA = buildTransaction ( 'set_greeting' , { args : { greeting : 'Hello my dear!' } } )
const transactionB = buildTransaction ( 'set_age' , { args : { age : 22 } } )
// Using diff contract
const transactionC = buildTransaction ( 'update_state' , {
receiverId : 'my-state-contract.testnet' ,
args : { allowed : true } ,
} )
// Optional
const callbackUrl = 'https://my-page.com/callback-success'
// [payable]
contractApi . callMultiple ( [ transactionA , transactionB , transactionC ] , callbackUrl ) . then ( ( ) => console . log ( 'Done!' ) ) Ada dua jenis sistem cache yang akan digunakan. Mereka adalah Memory Cache dan Storage Cache .
Memory Cache : Akan dihapus ketika aplikasi menyegarkan, karena datanya hanya hidup dalam memori.
Storage Cache : Data akan tetap ada bahkan ketika tab browser disegarkan. Data bertahan menggunakan penyimpanan lokal.
Saat instantiasi cache, Anda perlu memberikan expirationTime (dalam detik). Ini digunakan untuk mengetahui kapan cache harus dikembalikan alih -alih melakukan panggilan kontrak nyata. Ketika cache berakhir, panggilan nyata untuk kontrak dibuat. Metode masing -masing kontrak memiliki waktu kedaluwarsa sendiri.
// web3Api.ts
import naxios , { StorageCache } from '@wpdas/naxios'
// ...
/**
* Cached - Greeting Contract API
*/
export const cachedGreetingContractApi = naxiosInstance . contractApi ( {
contractId : 'dev-1692221685438-15421910364142' ,
cache : new StorageCache ( { expirationTime : 5 * 60 } ) , // 5 minutes
} ) Kemudian, untuk menggunakan view yang di -cache, Anda bisa melewati objek konfigurasi yang mengatakan Anda ingin menggunakan data yang di -cache.
import { cachedGreetingContractApi } from './web3Api'
// Fetch Greetings [free]
const args : { }
const config : { useCache : true }
cachedGreetingContractApi . view < string > ( 'get_greeting' , args , config ) . then ( ( response ) => console . log ( response ) )Naxios juga menyediakan akses ke RPC API dekat, sehingga Anda dapat menanyakan data apa pun yang Anda inginkan. Kunjungi Dokumen RPC API untuk mempelajari cara menggunakannya.
import { rpcApi } from './web3Api'
// Viewing account using Near RPC API
rpcApi
. query ( {
request_type : 'view_account' ,
finality : 'final' ,
account_id : 'wendersonpires.near' ,
} )
. then ( ( data ) => console . log ( 'Account Data:' , data ) ) buildTransaction Metode buildTransaction berguna ketika Anda perlu membangun badan transaksi kontrak, terutama ketika Anda ingin melakukan beberapa panggilan kontrak .
Lihat referensi di sini.
validateNearAddressIni digunakan untuk memeriksa apakah alamat adalah alamat dekat yang valid.
import { validateNearAddress } from '@wpdas/naxios'
console . log ( validateNearAddress ( 'fake.near' ) ) // true
console . log ( validateNearAddress ( 'fake.nears' ) ) // false
console . log ( validateNearAddress ( 'fake.testnet' ) ) // true
console . log ( validateNearAddress ( 'fake' ) ) // false calculateDepositByDataSizeHitung setoran yang diperlukan untuk data yang disimpan. (~ 0,00001n per byte) dengan sedikit tambahan untuk buffer
import { calculateDepositByDataSize } from '@wpdas/naxios'
const myData = { age : 22 , name : 'user name' }
console . log ( calculateDepositByDataSize ( myData ) ) // 0.00087 Near (not yocto NEAR) isClientPemeriksa sederhana untuk mengatakan apakah ini berjalan di server atau klien.
import { isClient } from '@wpdas/naxios'
if ( isClient ( ) ) {
console . log ( 'Hi from client' )
} else {
console . log ( 'Hi from server' )
} Jangan ragu untuk membuka masalah atau menarik permintaan. Untuk perubahan besar, buka masalah terlebih dahulu untuk membahas apa yang ingin Anda ubah.