Hedera Hashgraph Local Node AccountCreateTransaction Error

123 views Asked by At

I am currently trying to set up my local network by following the steps at https://docs.hedera.com/hedera/sdks-and-apis/sdks/set-up-your-local-network.

I have the code:

const {
    Client,
    PrivateKey,
    Hbar,
    AccountId,
    AccountCreateTransaction,
} = require("@hashgraph/sdk");
require('dotenv').config();

const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = process.env.MY_PRIVATE_KEY;
const myPublicKey = process.env.MY_PUBLIC_KEY;


async function main() {


    console.log("account id is: " + process.env.MY_ACCOUNT_ID);

    const node = {"127.0.0.1:50211": new AccountId(3)};
    const client = Client.forNetwork(node).setMirrorNetwork("127.0.0.1:5600");

    //set the transsaction fee paying account
    client.setOperator(myAccountId, myPrivateKey);
    console.log('success');


    console.log(PrivateKey.fromString(myPublicKey));
    const newAccount = await new AccountCreateTransaction()
        
        .setKey(PrivateKey.fromString(myPublicKey))
        .setInitialBalance(new Hbar(1))
        .execute(client);

     //Get receipt
     const receipt = await newAccount.getReceipt(client);

     //Get the account ID
     const newAccountId = receipt.accountId;
     console.log(newAccountId);

}

void main();

The output that I consistently get represents failure at AccountCreateTransaction step:

/Users/{name}/Desktop/{org_name}/hello-hedera-js-sdk/node_modules/@hashgraph/sdk/lib/Executable.cjs:679
    throw new Error(`max attempts of ${maxAttempts.toString()} was reached for request with last error being: ${persistentError != null ? persistentError.toString() : ""}`);
          ^

Error: max attempts of 10 was reached for request with last error being: GrpcServiceError: gRPC service failed with status: TIMEOUT
    at AccountCreateTransaction.execute (/Users/{name}/Desktop/{org_name}/hello-hedera-js-sdk/node_modules/@hashgraph/sdk/lib/Executable.cjs:679:11)
    at async main (/Users/drew/Desktop/Greensmith/hello-hedera-js-sdk/localtx.js:31:24)

I have tried straight up copying and pasting the code found at the hedera site and even that returns the same error in my terminal. I have the hedera-local-node cluster running on my docker desktop and there were no issues starting it up. The console returns 'success' before the error indicating that client.setOperator function is working properly.

I have tried copying and pasting code directly from the site with given private_key and account_id. The same error returns. I have also tried rm -rf node_modules and then reinstalling with npm with no success. I was expecting nothing to be returned and the transaction to go through, so that I could confirm on my end with checking at http://localhost:5551/api/v1/transactions - the local mirror endpoint URL.

EDIT Eventually solved, turned out that I needed to scale up my RAM on Docker


1

There are 1 answers

0
min11benja On

I had the same issue, what fixed it for me was having my .env variables with no strings

So before I had my .env varaiables like so

.env file code

MY_ACCOUNT_ID="0.0.5939242"
MY_PRIVATE_KEY="465654654654300706052b.."

I was getting a type error so looking at other peoples code I saw they used the .fromString(..) method from the AccountId & PrivateKey Object, So I added that to my code like so:

index.js code

 const myAccountId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
 const myPrivateKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);

Then I was getting the throw new Error(max attempts of ${maxAttempts.toString()} was reached for request with last error being: ${persistentError != null ? persistentError.toString() : ""});

So then I removed the "..." from the .env file and the code worked!

Here is my code repo

also make sure your code is in the same line when using object.functions()

The Hedera developer portal has it formated like so but that is WRONG!

const newAccount = await new AccountCreateTransaction()
    .setKey(newAccountPublicKey)
    .setInitialBalance(Hbar.fromTinybars(1000))
    .execute(client);

Dont use line breaks

const newAccount = await new AccountCreateTransaction().setKey(newAccountPublicKey).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);