Creating a neuron programmatically with Javascript

I am trying to create a neuron programmatically using the js library. However I am running into the following error.

Stake neuron error: Ce [Error]: Unrecognized ClaimOrRefresh error in {"command":[{"Error":{"error_message":"Account does not have enough funds to stake a neuron. Please make sure that account has at least 100000000 e8s (was 0 e8s)","error_type":14}}]}

Here is the code snippet that I am using.

 const nonce = BigInt(0); // You can use different nonces for multiple neurons
        const subAccount = SubAccount.fromPrincipal(principal);
        console.log("subAccount", subAccount);
        const governancePrincipal = Principal.fromText(GOVERNANCE_CANISTER_ID);
        
        await ledger.transfer({
          to: AccountIdentifier.fromPrincipal({
            principal: governancePrincipal,
            subAccount: subAccount,
          }),
          amount: BigInt(1000000),
          fee: DEFAULT_FEE,
          memo: nonce,  // Use same nonce as memo
          fromSubAccount: undefined,
          createdAt: undefined,
        });

I suspect the issue is with how i derive the subaccount. But I am not sure of another method to use since I am using the Subaccount methods from the dfinity packages

Have you tried calling the icrc1_balance_of method of the Ledger canister with that principal and subaccount? What is the result?

2 Likes

Assuming it’s frontend code, have you considered using the stakeNeuron function from @dfinity/governance? If I get it right, it handles both the transfer and the neuron claiming process.

If you already are, that should resolve the issue. If not, you might find your answer by looking into its implementation.

Hope that helps!

4 Likes

Let me try this out now and see. Thank you for the suggestion

1 Like

It actually worked well. :star_struck::star_struck::star_struck:

2 Likes