UNPKG

@ledgerhq/coin-hedera

Version:
43 lines 1.61 kB
import { Client, TransferTransaction, Hbar, AccountId, TransactionId, AccountBalanceQuery, HbarUnit, } from "@hashgraph/sdk"; import { HederaAddAccountError } from "../errors"; export function broadcastTransaction(transaction) { return transaction.execute(getClient()); } export async function buildUnsignedTransaction({ account, transaction, }) { const hbarAmount = Hbar.fromTinybars(transaction.amount); const accountId = account.freshAddress; return new TransferTransaction() .setNodeAccountIds([new AccountId(3)]) .setTransactionId(TransactionId.generate(accountId)) .setTransactionMemo(transaction.memo ?? "") .addHbarTransfer(accountId, hbarAmount.negated()) .addHbarTransfer(transaction.recipient, hbarAmount) .freeze(); } export async function getAccountBalance(address) { const accountId = AccountId.fromString(address); let accountBalance; try { accountBalance = await new AccountBalanceQuery({ accountId, }).execute(getBalanceClient()); } catch { throw new HederaAddAccountError(); } return { balance: accountBalance.hbars.to(HbarUnit.Tinybar), }; } let _hederaClient = null; let _hederaBalanceClient = null; function getClient() { _hederaClient ??= Client.forMainnet().setMaxNodesPerTransaction(1); //_hederaClient.setNetwork({ mainnet: "https://hedera.coin.ledger.com" }); return _hederaClient; } function getBalanceClient() { _hederaBalanceClient ??= Client.forMainnet(); return _hederaBalanceClient; } //# sourceMappingURL=network.js.map