Reputation: 345
I've been trying to send funds to addresses i generated using the hedera-js-sdk, the transaction is sent successfully, but always fails with an INSUFFICIENT ACCOUNT BALANCE
error in the explorer.
Alternatively, when i copy a public key from portal.hedera.com
and try sending HBARs to it, the transaction is processed successfully.
I cant seem to figure out what im doing wrong.
Can anyone offer any help pls?
Thanks.
Below is the code and transaction hash:
const { PublicKey, Hbar, TransferTransaction, } = require("@hashgraph/sdk");
export const generateHederaAccountId = (publicKey: string) => {
let _publicKey = new PublicKey(publicKey)
return _publicKey.toAccountId(0, 0).toString();
}
export const sendHederaTransaction = async (testnet: boolean, body: TransferHederaParams) => {
const { signerPrivateKey, receiverId, amount } = body;
let { signerId } = body;
/*
can derive public key from privatekey, then generate account id from it.
*/
let client = await createHederaConnection(testnet)
const signedTransaction = await new TransferTransaction()
.addHbarTransfer(signerId, Hbar.fromTinybars(-amount))
.addHbarTransfer(receiverId, Hbar.fromTinybars(amount))
.execute(client);
return signedTransaction;
}
// SEND SIGNED TRANSACTION
let recieverAccountId = generateHederaAccountId('302a300506032b6570032100ebcb5483f9f43f99c60d34549b1c374a74f1601b9d729c67703dc21cada5a585')
let TransferHederaParams = {
signerId: '0.0.48605126',
signerPrivateKey: '43045e3124fe3358923defa78241fc30c286e1e30223ef7f3754ebdbccxxxxxxx',
receiverId: recieverAccountId,
amount: 4, // 50 tH
}
let send = await sendHederaTransaction(true, TransferHederaParams)
console.log({ send })
https://testnet.hederaexplorer.io/search-details/transaction/0.0.48605126-1666132879-360481214
Upvotes: 1
Views: 104
Reputation: 518
Looks like you're trying to create a Hedera account using auto-account creation, meaning that the account is created once HBAR is transferred to an alias.
Here's a working code snippet to do just that:
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
//Create the account alias
// const newAccountKey = PrivateKey.generateED25519();
const newPrivateKey = PrivateKey.generateECDSA();
const newPublicKey = newPrivateKey.publicKey;
const newAliasAccountId = newPublicKey.toAccountId(0, 0);
//Transfer hbar to the account alias
const transferToAliasTx = new TransferTransaction()
.addHbarTransfer(operatorId, new Hbar(-100))
.addHbarTransfer(newAliasAccountId, new Hbar(100))
.freezeWith(client);
const transferToAliasSign = await transferToAliasTx.sign(operatorKey);
const transferToAliasSubmit = await transferToAliasSign.execute(client);
const transferToAliasRx = await transferToAliasSubmit.getReceipt(client);
console.log(`- Transfer status: ${transferToAliasRx.status} \n`);
// Get a transaction record and query the record
const transferToAliasRec = await transferToAliasSubmit.getRecord(client);
const txRec = await new TransactionRecordQuery()
.setTransactionId(transferToAliasRec.transactionId)
.setIncludeChildren(true)
.execute(client);
console.log(`- New account ID: ${txRec.children[0].receipt.accountId.toString()} \n`);
console.log(`- Parent transaction ID: ${txRec.transactionId} \n`);
console.log(`- Child transaction ID: ${txRec.children[0].transactionId.toString()} \n`);
}
Upvotes: 0