Last active
August 26, 2025 14:34
-
-
Save nkrsic/2d593d998c6ee26da09bfb690083be4a to your computer and use it in GitHub Desktop.
Calling near contracts from Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Account } from "@near-js/accounts"; | |
| import { actionCreators } from "@near-js/transactions"; | |
| import { JsonRpcProvider } from "@near-js/providers"; | |
| import { KeyPairSigner } from "@near-js/signers"; | |
| import { NEAR } from "@near-js/tokens"; | |
| import { USDC } from "@near-js/tokens/testnet"; | |
| import * as dotenv from "dotenv" | |
| dotenv.config() | |
| const signer = KeyPairSigner.fromSecretKey(process.env.SENDER_PRIVATE_KEY) | |
| const provider = new JsonRpcProvider({ | |
| url: process.env.TESTNET_RPC_URL | |
| }) | |
| const accountId = process.env.SENDER | |
| const account = new Account(accountId, provider, signer) | |
| const nearAmount = await account.getBalance(NEAR) | |
| const nearAmountDecimal = NEAR.toDecimal(nearAmount) | |
| const usdcAmount = await account.getBalance(USDC) | |
| const usdcAmountDecimal = USDC.toDecimal(usdcAmount) | |
| console.log(`NEAR balance of ${accountId}: ${nearAmountDecimal}`) | |
| console.log(`USDC balance of ${accountId}: ${usdcAmountDecimal}`) | |
| // Calling contract from 'hello_near' project | |
| const CONTRACT_ID = "nk-contract-a.testnet" | |
| const METHOD_NAME = "get_balance" | |
| const METHOD_ARGS = { key: "nk-deployer.testnet" } | |
| const result = await provider.callFunction( | |
| CONTRACT_ID, | |
| METHOD_NAME, | |
| METHOD_ARGS | |
| ) | |
| console.log(JSON.stringify(result, null, 2)) | |
| // Call deposit() on hello_near contract | |
| // | |
| // This approach succeeds at the function call, but it doesn't | |
| // return the tx hash or receipt information. In order to get it, | |
| // see below with signAndSendTransaction | |
| const depositResult = await account.callFunction({ | |
| contractId: CONTRACT_ID, | |
| methodName: "deposit", | |
| args: { amt: "100000000" }, | |
| deposit: NEAR.toUnits(0.06), // 0.06 NEAR | |
| gas: "30000000000000" // 30 TGas | |
| }) | |
| console.log(`depositResult:`) | |
| console.log({ depositResult }) | |
| // provider.signAndSendTransaction approach | |
| // | |
| const outcome = await account.signAndSendTransaction({ | |
| receiverId: CONTRACT_ID, | |
| actions: [ | |
| actionCreators.functionCall( | |
| "deposit", | |
| { amt: "100000000" }, | |
| "30000000000000", // gas | |
| NEAR.toUnits(0.06) // deposit in yoctoNEAR | |
| ), | |
| actionCreators.functionCall( | |
| "deposit", | |
| { amt: "100000000" }, | |
| "30000000000000", // gas | |
| NEAR.toUnits(0.07) // deposit in yoctoNEAR | |
| ), | |
| ], | |
| }); | |
| console.log("Transaction outcome:") | |
| console.log(JSON.stringify(outcome, null, 2)) // dumps all action outcomes | |
| console.log("Tx Hash:") | |
| console.log(JSON.stringify(outcome.transaction_outcome.id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment