트랜잭션 생성
트랜잭션을 구성하고 처리하려면 API JavaScript 라이브러리 near-api-js
가 필요합니다. 트랜잭션을 생성하는 방법에는 여러 가지가 있지만, 이 예제에서는 간단한 토큰 전송 트랜잭션을 생성하는 두 가지 방법을 소개합니다.
기본적으로 모든 트랜잭션에는 다음이 필요합니다.
signerId
(트랜잭션 주체의 계정 ID)signerPublicKey
receiverId
(트랜잭션 수신자의 계정 ID)nonceForPublicKey
(키가 사용될 때마다 nonce 값이 1씩 증가해야 함)actions
( [click here] for supported arguments)blockHash
(트랜잭션이 최근에 생성되었음을 증명하기 위한 현재 블록 해시(24시간 이내))
See Transaction Class for a more in depth outline.
HIGH LEVEL -- Create a transaction
Setup
- 다음 명령을 실행하여 transaction-examples 레퍼지토리를 복제합니다.
git clone https://github.com/near-examples/transaction-examples.git
- 설정 지침을 따르세요.
Imports
send-tokens-easy.js
에서 우리는 두 가지 의존성(dependency)을 사용합니다.
- NEAR API JavaScript 라이브러리
dotenv
(개인 키에 대한 환경 변수를 로드하는 데 사용)
const nearAPI = require("near-api-js");
const { connect, KeyPair, keyStores, utils } = nearAPI;
require("dotenv").config();
위의 두 번째 줄은 블록체인과 상호 작용하는 데 사용할 nearAPI의 여러 유틸리티를 다음과 같이 분해합니다.
connect
- NEAR에 대한 연결을 생성해 구성 변수 전달KeyPair
-.env
파일에 제공할 개인 키에서 keyPair 생성keyStores
- 개인 키에서 생성하고 트랜잭션 서명에 사용할 키 쌍 저장utils
- NEAR 토큰 단위을 지정하는 데 사용
Accounts & Network
다음으로, networkId
(betanet
, testnet
, 또는 mainnet
) 뿐만 아니라 sender
와 receiver
의 accountId
를 입력해야 합니다.
const sender = "sender.testnet";
const receiver = "receiver.testnet";
const networkId = "testnet";
Formatting Token Amounts
트랜잭션 중 NEAR 토큰(Ⓝ)을 보낼 때, 금액을 Yocto Ⓝ 또는 (10^-24)로 변환해야 합니다.
- 이를 수행하려면,
near-api-js
메서드parseNearAmount()
(utils/format
에 있음)를 사용합니다.
const amount = nearAPI.utils.format.parseNearAmount("1.5");
Create a Key Store
In order to sign transactions you will need to create a "Key Store" that will hold a full access key to sign your transactions. 이를 수행하는 방법에는 여러 가지가 있지만, 이 예제에서는 프로젝트의 .env
파일 또는 전역으로 내보낸 환경 변수에 저장된 개인 키를 사용합니다.
near-cli
를 사용하여 계정을 생성했거나 터미널에서near login
을 실행한 경우, 개인 키는/HOME/.near-credentials
에 있는.json
파일에서 찾을 수 있습니다.- If you created an account using NEAR Wallet, your key will be found in your browser's
Local Storage
.- 브라우저 개발자 도구에서...
Application
>>Storage
>>Local Storage
- 브라우저 개발자 도구에서...
// sets up an empty keyStore object in memory using near-api-js
const keyStore = new keyStores.InMemoryKeyStore();
// creates a keyPair from the private key provided in your .env file
const keyPair = KeyPair.fromString(process.env.SENDER_PRIVATE_KEY);
// adds the key you just created to your keyStore which can hold multiple keys (must be inside an async function)
await keyStore.setKey(networkId, sender, keyPair);
Setting up a connection to NEAR
이제 keyStore
뿐만 아니라 networkId
설정을 더 일찍 포함할 구성 객체를 사용하여 NEAR에 대한 연결을 생성합니다.
// configuration used to connect to NEAR
const prefix = (networkId === "testnet") ? "testnet" : "www";
const config = {
networkId,
keyStore,
nodeUrl: `https://rpc.${networkId}.near.org`,
walletUrl: `https://wallet.${networkId}.near.org`,
helperUrl: `https://helper.${networkId}.near.org`,
explorerUrl: `https://${prefix}.nearblocks.io`,
};
// connect to NEAR! :)
const near = await connect(config);
// create a NEAR account object
const senderAccount = await near.account(sender);
마지막 줄은 NEAR 연결을 사용하여 트랜잭션을 수행하는 데 사용할 senderAccount
객체를 생성하고 있습니다.
Create, Sign, & Send Transaction
이제 모든 것이 설정되었으므로 한 줄의 코드로 트랜잭션을 생성할 수 있습니다.
const result = await senderAccount.sendMoney(receiver, amount);
이 간단한 명령은 NEAR 블록체인에서 토큰 전송 트랜잭션을 구성, 서명 및 전송합니다. There is no need to create a result
variable aside from inspecting the response details from your transaction and even create a link to NearBlocks Explorer to view a GUI version of the transaction details.