JavaScript API를 사용하여 NEAR와 상호 작용
빠른 참고
near-api-js
란 무엇인가요?
near-api-js
는 NEAR 블록체인과 상호 작용하는 완전한 라이브러리입니다. 이는 브라우저 또는 Node.js 런타임에서 사용될 수 있습니다.
일반적으로 KeyStore
를 사용한 connect
로 연결을 생성할 수 있습니다. 연결 객체를 통해, 다음과 같은 것들을 할 수 있습니다.
- 브라우저에서 지갑과 상호 작용.
- 다음과 같은 것들을 위해 계정 객체 인스턴스화:
- 토큰 전송
- 컨트랙트 배포
- 계정 검사, 생성 또는 삭제
- 계정 키 관리
- 컨트랙트 객체를 인스턴스화하여 스마트 컨트랙트 메서드 호출
라이브러리에는 일부 utils 함수도 포함되어 있습니다.
near-api-js
와 near-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
공급자가 존재합니다.
- Browser
- Node
import * as nearAPI from "near-api-js";
const nearAPI = require("near-api-js");
키 저장소(Key Store)
트랜잭션에 서명하는 경우 키 저장소 를 생성해야 합 니다. 브라우저에서 사용자에게 지갑으로 로그인하도록 요청하면, LocalStorage KeyStore가 사용됩니다.
- Using Browser
- Using Credentials Directory
- Using a File
- Using a private key string
// creates keyStore using private key in local storage
const { keyStores } = nearAPI;
const myKeyStore = new keyStores.BrowserLocalStorageKeyStore();
// creates a keyStore that searches for keys in .near-credentials
// requires credentials stored locally by using a NEAR-CLI command: `near login`
// https://docs.near.org/tools/cli#near-login
const { keyStores } = nearAPI;
const homedir = require("os").homedir();
const CREDENTIALS_DIR = ".near-credentials";
const credentialsPath = require("path").join(homedir, CREDENTIALS_DIR);
const myKeyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);
// creates keyStore from a provided file
// you will need to pass the location of the .json key pair
const { KeyPair, keyStores } = require("near-api-js");
const fs = require("fs");
const homedir = require("os").homedir();
const ACCOUNT_ID = "near-example.testnet"; // NEAR account tied to the keyPair
const NETWORK_ID = "testnet";
// path to your custom keyPair location (ex. function access key for example account)
const KEY_PATH = "/.near-credentials/near-example-testnet/get_token_price.json";
const credentials = JSON.parse(fs.readFileSync(homedir + KEY_PATH));
const myKeyStore = new keyStores.InMemoryKeyStore();
myKeyStore.setKey(
NETWORK_ID,
ACCOUNT_ID,
KeyPair.fromString(credentials.private_key)
);
// creates keyStore from a private key string
// you can define your key here or use an environment variable
const { keyStores, KeyPair } = nearAPI;
const myKeyStore = new keyStores.InMemoryKeyStore();
const PRIVATE_KEY =
"by8kdJoJHu7uUkKfoaLd2J2Dp1q1TigeWMG123pHdu9UREqPcshCM223kWadm";
// creates a public / private key pair using the provided private key
const keyPair = KeyPair.fromString(PRIVATE_KEY);
// adds the keyPair you created to keyStore
await myKeyStore.setKey("testnet", "example-account.testnet", keyPair);
NEAR에 연결
connect
에서 반환된 객체는 API의 모든 명령에 대한 진입점입니다. 트랜잭션에 서명하려면 연결을 만들기 위한 KeyStore
가 필요합니다.
- TestNet
- MainNet
- LocalNet
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);
const { connect } = nearAPI;
const connectionConfig = {
networkId: "mainnet",
keyStore: myKeyStore, // first create a key store
nodeUrl: "https://rpc.mainnet.near.org",
walletUrl: "https://wallet.mainnet.near.org",
helperUrl: "https://helper.mainnet.near.org",
explorerUrl: "https://nearblocks.io",
};
const nearConnection = await connect(connectionConfig);
const { connect } = nearAPI;
const connectionConfig = {
networkId: "local",
nodeUrl: "http://localhost:3030",
walletUrl: "http://localhost:4000/wallet",
};
const nearConnection = await connect(connectionConfig);