Hedera
Search…
Local Provider
This feature is available in the Hedera JavaScript SDK only. (version >=2.14.0).
LocalProvider is a quality of life implementation that creates a provider using the HEDERA_NETWORK environment variable.
The LocalProvider() requires the following variable to be defined in the .env file. The .env file is located in the root directory of the project.
  • HEDERA_NETWORK
    • The network the wallet submits transactions to
.env
1
//Example .env file
2
HEDERA_NETWORK= previewnet/testnet/mainnet (select one network)
Copied!

class LocalProvider implements Wallet

Constructor

new LocalProvider()

Instantiates the LocalProvider object. The local provider is built using HEDERA_NETWORK network specified in the .env file.

Methods

<LocalProvider>.getAccountBalance() -> Promise <AccountBalance>

Returns the account balance of the account in the local wallet.

<LocalProvider>.getAccountInfo() -> Promise <AccountInfo>

Returns the account information of the account in the local wallet.

<LocalProvider>.getAccountRecords() -> Promise <AccountInfo>

Returns the last transaction records for this account using TransactionRecordQuery.

<LocalProvider>.getLedgerId()-> LedgerId

Returns the ledger ID (previewnet, testnet, or mainnet).

<LocalProvider>.getMirrorNetwork()-> string

The mirror network the wallet is connected to.

<LocalProvider>.getNetwork()-> [key: string]: string | AccountId

Returns the network map information.

<LocalProvider>.getTransactionReceipt()-> Promise<TransactionReceipt>

Returns the transaction receipt.

<LocalProvider>.waitForReceipt()-> Promise<TransactionReceipt>

Wait for the receipt for a transaction response.
<LocalProvider>.call(<RequestT, ResponseT, OutputT>(request: Executable<RequestT, ResponseT, OutputT>)-> Promise <Output>
Sign and send a request using the wallet.

Local Wallet Example:

1
require("dotenv").config();
2
import { Wallet, LocalProvider } from "@hashgraph/sdk";
3
​
4
async function main() {
5
​
6
const wallet = new Wallet(
7
process.env.OPERATOR_ID,
8
process.env.OPERATOR_KEY,
9
new LocalProvider()
10
);
11
​
12
const info = await wallet.getAccountInfo();
13
​
14
console.log(`info.key = ${info.key.toString()}`);
15
}
16
​
17
void main();
Copied!

Sign with Local Wallet Example:

1
require("dotenv").config();
2
const {Wallet, LocalProvider, TransferTransaction} = require("@hashgraph/sdk");
3
​
4
async function main() {
5
​
6
const wallet = new Wallet(
7
process.env.OPERATOR_ID,
8
process.env.OPERATOR_KEY,
9
new LocalProvider()
10
);
11
​
12
//Transfer 1 hbar from my wallet account to account 0.0.3
13
const transaction = await new TransferTransaction()
14
.addHbarTransfer(wallet.getAccountId(), -1)
15
.addHbarTransfer("0.0.3", 1)
16
.freezeWithSigner(wallet);
17
18
//Sign the transaction
19
const signTransaction = await transaction.signWithSigner(wallet);
20
​
21
//Submit the transaction
22
const response = await signTransaction.executeWithSigner(wallet);
23
​
24
//Get the receipt
25
const receipt = await wallet.getProvider().waitForReceipt(response);
26
27
console.log(`status: ${receipt.status.toString()}`);
28
}
29
​
30
main();
Copied!