# Programmatically staking neurons

**URL:** <https://forum.dfinity.org/t/programmatically-staking-neurons/13019>\
**Category:** Developers\
**Created:** [May 16, 2022, 7:42pm UTC](https://forum.dfinity.org/t/programmatically-staking-neurons/13019 "2022-05-16T19:42:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rckprtr](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rckprtr/32/809_2.png) [@rckprtr](https://forum.dfinity.org/u/rckprtr)\
**Post date:** [May 16, 2022, 7:42pm UTC](https://forum.dfinity.org/t/programmatically-staking-neurons/13019/1 "2022-05-16T19:42:05Z")

</div>

Im trying to programmatically create neurons as Im trying to better understand how the NNS works.

My Current Issue is below:

`Account does not have enough funds to stake a neuron. Please make sure that account has at least 100000000 e8s (was 0 e8s)`

Ive been transferring small amounts to the `Minting Account` and Im using this code here: [nns-dapp/createNeuron.ts at 55a89a9b5d5d4241277822197efc62f3d3e8d6ce · dfinity/nns-dapp · GitHub](https://github.com/dfinity/nns-dapp/blob/55a89a9b5d5d4241277822197efc62f3d3e8d6ce/frontend/ts/src/canisters/createNeuron.ts#L37)

I can see my transactions on chain:

> **[Internet Computer Network Status](https://dashboard.internetcomputer.org/account/f3f46d03f1ad8a90dc052a906be6fd5250944c3c976c971731630c6424f6e36f)**
>
> Internet Computer Association - Internet Computer Network Status

With ICP going to the Minting Account

Snippet of my code:

```auto
  var principal = ledger.identity.getPrincipal();
  var aid = principalToAccountIdentifier(principal.toText(), 0);
  console.log("My AID:", aid)
  console.log("My current tokens:", await ledger.actor.account_balance({ account: toAccountIdVec(aid) }))

  var nonceBytes = crypto.randomBytes(8);
  var nonce = uint8ArrayToBigInt(nonceBytes);
  const toSubAccount = buildSubAccount(nonceBytes, principal);

  const governance_aid = principalToAccountIdentifier(
    "rrkah-fqaaa-aaaaa-aaaaq-cai",
    toSubAccount
  );

  console.log("governance_aid aid", governance_aid)
  console.log("To balance:", await ledger.actor.account_balance({ account: toAccountIdVec(governance_aid) }))

  console.log(await ledger.actor.transfer({
    'to': toAccountIdVec(governance_aid),
    'fee': { e8s: 0 },
    'memo': nonce,
    'from_subaccount': [],
    'created_at_time': [],
    'amount': { 'e8s': 100000 },
  }))

  let createResult = await nns.actor.claim_or_refresh_neuron_from_account({
    controller: [principal],
    memo: nonce
  })
  console.log(JSON.stringify(createResult))

```

SubAccount creation

```auto
function buildSubAccount(nonce, principal) {
  const padding = asciiStringToByteArray("neuron-stake");
  const shaObj = sha256.create();
  shaObj.update([0x0c, ...padding, ...principal.toUint8Array(), ...nonce]);
  return new Uint8Array(shaObj.array());
}

```

Should the balance of the Governance account I transfer to always be 0? Im open to share whatever else would help. Ive been using this code to transfer ICP between my Stoic wallet and the App as a test and everything worked fine.

I believe its my SubAccount or Memo field is incorrect, but I checked the repo

> <https://github.com/dfinity/ic/blob/a4815899ffa164acaccd0dc42b99b00ab9a14354/rs/nervous_system/common/src/ledger.rs#L127>

Thank you,  
Rick

---

<div class="post-metadata">

**Author:** ![rckprtr](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rckprtr/32/809_2.png) [@rckprtr](https://forum.dfinity.org/u/rckprtr)\
**Post date:** [May 16, 2022, 10:08pm UTC](https://forum.dfinity.org/t/programmatically-staking-neurons/13019/2 "2022-05-16T22:08:31Z")

</div>

> [@rckprtr](#):
>
> `https://github.com/dfinity/dfinity_wallet/blob/351e07d3e6d007b090117161a94ce8ec9d5a6b49/js-agent/src/canisters/createNeuron.ts#L63`

Resovled: `principalToAccountIdentifier` was not doing sub accounts correctly.

> <https://github.com/dfinity/nns-dapp/blob/351e07d3e6d007b090117161a94ce8ec9d5a6b49/js-agent/src/canisters/converter.ts#L90>

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [May 25, 2022, 12:07pm UTC](https://forum.dfinity.org/t/programmatically-staking-neurons/13019/3 "2022-05-25T12:07:44Z")

</div>

as an alternative this is also discussed here

> **[The definite guide for interacting with the IC from the command line](https://ic.associates/nns-command-line-guide/#create-neuron)**
>
> Seed rounder or presale investor looking for a way to manage your neurons? This guide will help!
