Skip to content

Commit 70c9f87

Browse files
committed
Fixed [FEEDBACK] issue #2734
1 parent 7b007b4 commit 70c9f87

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

docs/integrations/create-transactions.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ In [`send-tokens-easy.js`](https://github.com/near-examples/transaction-examples
4343
2. [`dotenv`](https://www.npmjs.com/package/dotenv) (used to load environment variables for private key)
4444

4545
```js
46-
const nearAPI = require("near-api-js");
47-
const { connect, KeyPair, keyStores, utils } = nearAPI;
46+
const { Near, Account, KeyPair, keyStores, utils } = require("near-api-js");
4847
require("dotenv").config();
4948
```
5049

51-
The second line above deconstructs several utilities from nearAPI that you will use to interact with the blockchain.
50+
The destructured utilities from near-api-js that you will use to interact with the blockchain:
5251

53-
- `connect` - create a connection to NEAR passing configuration variables
52+
- `Near` - create a connection to NEAR passing configuration variables
53+
- `Account` - creates an account object for interacting with the blockchain
5454
- `KeyPair` - creates a keyPair from the private key you'll provide in an `.env` file
5555
- `keyStores` - stores the keyPair that you will create from the private key and used to sign Transactions
5656
- `utils` - used to format NEAR amounts
@@ -72,7 +72,7 @@ When sending NEAR tokens (Ⓝ) during a transaction, the amount needs to be conv
7272
- To perform this you will use the [`near-api-js`](https://github.com/near/near-api-js) method [`parseNearAmount()`](https://github.com/near/near-api-js/blob/d4d4cf1ac3182fa998b1e004e6782219325a641b/src/utils/format.ts#L53-L63) (located in `utils/format`)
7373

7474
```js
75-
const amount = nearAPI.utils.format.parseNearAmount("1.5");
75+
const amount = utils.format.parseNearAmount("1.5");
7676
```
7777

7878
### Create a Key Store
@@ -110,9 +110,9 @@ const config = {
110110
};
111111

112112
// connect to NEAR! :)
113-
const near = await connect(config);
113+
const near = new Near(config);
114114
// create a NEAR account object
115-
const senderAccount = await near.account(sender);
115+
const senderAccount = new Account(near.connection, sender);
116116
```
117117

118118
You'll notice the last line uses your NEAR connection to create a `senderAccount` object that you'll use to perform the transaction.
@@ -152,7 +152,7 @@ In [`send-tokens-deconstructed.js`](https://github.com/near-examples/transaction
152152
3. [`dotenv`](https://www.npmjs.com/package/dotenv) (used to load environment variables)
153153

154154
```js
155-
const nearAPI = require("near-api-js");
155+
const { providers, KeyPair, utils, transactions } = require("near-api-js");
156156
const sha256 = require("js-sha256");
157157
require("dotenv").config();
158158
```
@@ -178,7 +178,7 @@ When sending NEAR tokens (Ⓝ) during a transaction, the amount needs to be conv
178178
- To perform this you will use the [`near-api-js`](https://github.com/near/near-api-js) method [`parseNearAmount()`](https://github.com/near/near-api-js/blob/d4d4cf1ac3182fa998b1e004e6782219325a641b/src/utils/format.ts#L53-L63) (located in `utils/format`)
179179

180180
```js
181-
const amount = nearAPI.utils.format.parseNearAmount("1.5");
181+
const amount = utils.format.parseNearAmount("1.5");
182182
```
183183

184184
---
@@ -188,9 +188,9 @@ const amount = nearAPI.utils.format.parseNearAmount("1.5");
188188
In this example, we will create a NEAR RPC `provider` that allows us to interact with the chain via [RPC endpoints](/api/rpc/introduction).
189189

190190
```js
191-
const provider = new nearAPI.providers.JsonRpcProvider(
192-
`https://rpc.${networkId}.near.org`
193-
);
191+
const provider = new providers.JsonRpcProvider({
192+
url: `https://rpc.${networkId}.near.org`
193+
});
194194
```
195195

196196
---
@@ -209,7 +209,7 @@ Once you have access to the private key of the sender's account, create an envir
209209

210210
```js
211211
const privateKey = process.env.SENDER_PRIVATE_KEY;
212-
const keyPair = nearAPI.KeyPair.fromString(privateKey);
212+
const keyPair = KeyPair.fromString(privateKey);
213213
```
214214

215215
---
@@ -293,10 +293,12 @@ const publicKey = keyPair.getPublicKey();
293293
- Current nonce can be retrieved using the `provider` we [created earlier](#setting-up-a-connection-to-near).
294294

295295
```js
296-
const accessKey = await provider.query(
297-
`access_key/${sender}/${publicKey.toString()}`,
298-
""
299-
);
296+
const accessKey = await provider.query({
297+
request_type: "view_access_key",
298+
finality: "final",
299+
account_id: sender,
300+
public_key: publicKey.toString(),
301+
});
300302
```
301303

302304
- now we can create a unique number for our transaction by incrementing the current `nonce`.
@@ -309,21 +311,21 @@ const nonce = ++accessKey.nonce;
309311

310312
- There are currently eight supported `Action` types. [[see here]](/protocol/transaction-anatomy#actions)
311313
- For this example, we are using `Transfer`
312-
- This transfer action can be created using the [imported `nearAPI` object](#imports) and the [formatted Ⓝ amount](#formatting-token-amounts) created earlier.
314+
- This transfer action can be created using the [imported `transactions` object](#imports) and the [formatted Ⓝ amount](#formatting-token-amounts) created earlier.
313315

314316
```js
315-
const actions = [nearAPI.transactions.transfer(amount)];
317+
const actions = [transactions.transfer(amount)];
316318
```
317319

318320
[[click here]](https://github.com/near/near-api-js/blob/d4d4cf1ac3182fa998b1e004e6782219325a641b/src/transaction.ts#L70-L72) to view source for `transfer()`.
319321

320322
### 6 `blockHash`
321323

322324
- Each transaction requires a current block hash (within 24hrs) to prove that the transaction was created recently.
323-
- Hash must be converted to an array of bytes using the `base_decode` method found in [`nearAPI`](#imports).
325+
- Hash must be converted to an array of bytes using the `base_decode` method found in [`utils`](#imports).
324326

325327
```js
326-
const recentBlockHash = nearAPI.utils.serialize.base_decode(
328+
const recentBlockHash = utils.serialize.base_decode(
327329
accessKey.block_hash
328330
);
329331
```
@@ -336,10 +338,10 @@ const recentBlockHash = nearAPI.utils.serialize.base_decode(
336338

337339
With all of our [required arguments](#transaction-requirements), we can construct the transaction.
338340

339-
- Using [`nearAPI`](#imports), we call on `createTransaction()` to perform this task.
341+
- Using [`transactions`](#imports), we call on `createTransaction()` to perform this task.
340342

341343
```js
342-
const transaction = nearAPI.transactions.createTransaction(
344+
const transaction = transactions.createTransaction(
343345
sender,
344346
publicKey,
345347
receiver,
@@ -357,11 +359,11 @@ const transaction = nearAPI.transactions.createTransaction(
357359

358360
Now that the transaction is created, we sign it before sending it to the NEAR blockchain. At the lowest level, there are four steps to this process.
359361

360-
1. Using [`nearAPI`](#imports), we call on `serialize()` to serialize the transaction in [Borsh](https://borsh.io/).
362+
1. Using [`utils`](#imports), we call on `serialize()` to serialize the transaction in [Borsh](https://borsh.io/).
361363

362364
```js
363-
const serializedTx = nearAPI.utils.serialize.serialize(
364-
nearAPI.transactions.SCHEMA.Transaction,
365+
const serializedTx = utils.serialize.serialize(
366+
transactions.SCHEMA.Transaction,
365367
transaction
366368
);
367369
```
@@ -378,12 +380,12 @@ const serializedTxHash = new Uint8Array(sha256.sha256.array(serializedTx));
378380
const signature = keyPair.sign(serializedTxHash);
379381
```
380382

381-
4. Construct the signed transaction using `near-api-js` [SignedTransaction class](https://github.com/near/near-api-js/blob/d4d4cf1ac3182fa998b1e004e6782219325a641b/src/transaction.ts#L112-L123).
383+
4. Construct the signed transaction using near-api-js [SignedTransaction class](https://github.com/near/near-api-js/blob/d4d4cf1ac3182fa998b1e004e6782219325a641b/src/transaction.ts#L112-L123).
382384

383385
```js
384-
const signedTransaction = new nearAPI.transactions.SignedTransaction({
386+
const signedTransaction = new transactions.SignedTransaction({
385387
transaction,
386-
signature: new nearAPI.transactions.Signature({
388+
signature: new transactions.Signature({
387389
keyType: transaction.publicKey.keyType,
388390
data: signature.signature,
389391
}),
@@ -395,7 +397,7 @@ const signedTransaction = new nearAPI.transactions.SignedTransaction({
395397
Final step is to encode and send the transaction.
396398

397399
- First we serialize transaction into [Borsh](https://borsh.io/), and store the result as `signedSerializedTx`. _(required for all transactions)_
398-
- Then we send the transaction via [RPC call](/api/rpc/introduction) using the `sendJsonRpc()` method nested inside [`near`](#setting-up-a-connection-to-near).
400+
- Then we send the transaction via [RPC call](/api/rpc/introduction) using the `sendJsonRpc()` method nested inside [`provider`](#setting-up-a-connection-to-near).
399401

400402
```js
401403
// encodes transaction to serialized Borsh (required for all transactions)
@@ -475,4 +477,4 @@ const transactionLink = `https://${prefix}nearblocks.io/txns/${result.transactio
475477
<a href="https://stackoverflow.com/questions/tagged/nearprotocol" target="_blank" rel="noopener noreferrer"><h8>Ask it on StackOverflow!</h8></a>
476478
:::
477479
478-
Happy Coding! 🚀
480+
Happy Coding! 🚀

0 commit comments

Comments
 (0)