본문으로 건너뛰기

JavaScript API를 사용하여 NEAR와 상호 작용

빠른 참고

near-api-js란 무엇인가요?

near-api-js는 NEAR 블록체인과 상호 작용하는 완전한 라이브러리입니다. 이는 브라우저 또는 Node.js 런타임에서 사용될 수 있습니다.

일반적으로 KeyStore를 사용한 connect로 연결을 생성할 수 있습니다. 연결 객체를 통해, 다음과 같은 것들을 할 수 있습니다.

  • 브라우저에서 지갑과 상호 작용.
  • 다음과 같은 것들을 위해 계정 객체 인스턴스화:
    • 토큰 전송
    • 컨트랙트 배포
    • 계정 검사, 생성 또는 삭제
    • 계정 키 관리
  • 컨트랙트 객체를 인스턴스화하여 스마트 컨트랙트 메서드 호출

라이브러리에는 일부 utils 함수도 포함되어 있습니다.

To quickly get started with integrating NEAR in a web browser, read our Web Frontend integration article. :::
near-api-jsnear-sdk-js의 차이점에 유의하세요:

JavaScript SDK 는 스마트 컨트랙트 개발을 위한 라이브러리입니다. 여기에는 스마트 컨트랙트 코드를 작성하는 데 사용하는 클래스와 함수가 포함되어 있습니다.

JavaScript API 는 NEAR와 상호 작용할 수 있는 모든 가능한 명령을 위한 완전한 라이브러리입니다. 이는 RPC 엔드포인트 래퍼, 브라우저에서 NEAR 지갑과 상호 작용하는 라이브러리, 키 관리 도구입니다. :::


설치

패키지에 near-api-js를 의존성(dependency)으로 포함합니다.

npm i --save near-api-js

Import

브라우저 또는 Node.js 런타임에서 API 라이브러리를 사용할 수 있습니다. 일부 기능은 여러 환경 중 하나에서만 사용할 수 있습니다. 예를 들어 WalletConnection는 브라우저 전용이며, 각 환경마다 서로 다른 KeyStore 공급자가 존재합니다.

import * as nearAPI from "near-api-js";

키 저장소(Key Store)

트랜잭션에 서명하는 경우 키 저장소 를 생성해야 합니다. 브라우저에서 사용자에게 지갑으로 로그인하도록 요청하면, LocalStorage KeyStore가 사용됩니다.

// creates keyStore using private key in local storage

const { keyStores } = nearAPI;
const myKeyStore = new keyStores.BrowserLocalStorageKeyStore();

Class BrowserLocalStorageKeyStore

NEAR에 연결

connect에서 반환된 객체는 API의 모든 명령에 대한 진입점입니다. 트랜잭션에 서명하려면 연결을 만들기 위한 KeyStore가 필요합니다.

const { connect } = nearAPI;

const connectionConfig = {
networkId: "testnet",
keyStore: myKeyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
const nearConnection = await connect(connectionConfig);

connect 모듈

RPC Failover

RPC providers can experience intermittent downtime, connectivity issues, or rate limits that cause client transactions to fail. This can be prevented by using the FailoverRpcProvider that supports multiple RPC providers.

const jsonProviders = [
new JsonRpcProvider({
url: 'https://rpc.mainnet.near.org',
}),
new JsonRpcProvider(
{
url: 'https://another-rpc.cloud.com',
headers: { 'X-Api-Key': 'some string' },
},
{ retries: 3, backoff: 2, wait: 500 }
),
];
const provider = new FailoverRpcProvider(jsonProviders);

await connect({
networkId: 'mainnet',
provider: provider,
// this isn't used if `provider` is specified, but is still required for backward compativility
nodeUrl: 'https://rpc.mainnet.near.org',
});

Class FailoverRpcProvider

Was this page helpful?