# Get Ledger Balance for Principal ID

**URL:** https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863
**Category:** Developers
**Tags:** sns
**Created:** [August 4, 2024, 7:01pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863 "2024-08-04T19:01:19Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)
#### Post date: [August 4, 2024, 7:01pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/1 "2024-08-04T19:01:19Z")

</div>

So I usually get the ledger balance for a user using the backend but I want to use the frontend libraries available for me since they have a lot more features.

When getting a user’s account balance in motoko, I’d do this:

```auto

    public func getAccountBalance(principalId : Principal) : async Nat {
      let balance = await ledger.icrc1_balance_of({owner = principalId; subaccount = null});
      return balance;
    };

```

Giving me their account balance. I get an error when I try to replicate this on the frontend using the IcrcLedgerCanister type from the @dfinity/ledger-icrc library:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/2/b/2b37f8ff82a221fcfadc257c2f3f22136a1c2c76.png)

Specifically:

Canister called `ic0.trap` with message: failed to decode call arguments: Custom(Fail to decode argument 0 from table2 to record { owner : principal; subaccount : opt blob }\n\nCaused by:\n Deserialize error: invalid length 0, expected an array of length 32)

Now again on the backend I have a function that pads the 29 byte array to make it 32 bytes, just wondering if there is an example anywhere of how to do this on the frontend?

```auto

```

---

<div class="post-metadata">

### Author: ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)
#### Post date: [August 5, 2024, 4:33am UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/2 "2024-08-05T04:33:45Z")

</div>

> [@jamesbeadle](#):
>
> if there is an example anywhere of how to do this on the frontend?

Not exactly sure what you are looking for nor if its relates but, you can maybe have a look to the [neuronSubaccount](https://github.com/dfinity/ic-js/blob/2cb316fbcd90e8c588528ebdce41f75bae6cb5d5/packages/sns/src/utils/governance.utils.ts#L18) or [AccountIdentifier](https://github.com/dfinity/ic-js/blob/2cb316fbcd90e8c588528ebdce41f75bae6cb5d5/packages/ledger-icp/src/account_identifier.ts#L28) implementation.

---

<div class="post-metadata">

### Author: ![baolongt](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@baolongt](https://forum.dfinity.org/u/baolongt)
#### Post date: [August 5, 2024, 6:30am UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/3 "2024-08-05T06:30:33Z")

</div>

You can read my implement [here](https://github.com/Alpaca-Labs-ICP/token-adapter/blob/master/src/icrc1.ts#L85)  
or use this [lib](https://www.npmjs.com/package/@alpaca-icp/token-adapter) the same source above but as a package 😃

---

<div class="post-metadata">

### Author: ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)
#### Post date: [August 5, 2024, 5:09pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/4 "2024-08-05T17:09:04Z")

</div>

Someone deposited FPL to their OpenFPL principal ID, which is fine, it’s where the winnings will go too.

I want to show the balance, usually I do this by getting from the backend and sending a DTO to the frontend. But I’d like to try just getting the balance directly in the frontend to save the round trip.

---

<div class="post-metadata">

### Author: ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)
#### Post date: [August 5, 2024, 5:27pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/5 "2024-08-05T17:27:28Z")

</div>

Your original question is about left padding, so I answered your original question. 🤷‍♂️

If it’s another question, please share a code snippet and some details about the values, not sure what’s your issue.

---

<div class="post-metadata">

### Author: ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)
#### Post date: [August 5, 2024, 6:34pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/6 "2024-08-05T18:34:58Z")

</div>

My original question and the second comment are both about getting the FPL balance using some kind of frontend library. It’s fine I’ll just use the backend.

---

<div class="post-metadata">

### Author: ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)
#### Post date: [August 5, 2024, 6:50pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/7 "2024-08-05T18:50:50Z")

</div>

Ok, as you wish. Feel free to provide a snippet in which you get the issue and some data. Happy to have a look.

---

<div class="post-metadata">

### Author: ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)
#### Post date: [August 6, 2024, 2:05am UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/8 "2024-08-06T02:05:33Z")

</div>

````auto

async function getFPLBalance(): Promise<bigint> {
let identity: OptionIdentity;

authStore.subscribe(async (auth) => {
identity = auth.identity;
});

if (!identity) {
return 0n;
}

let principalId = identity.getPrincipal();

console.log(principalId);

const agent = await createAgent({
identity: identity,
host: import.meta.env.VITE_AUTH_PROVIDER_URL,
fetchRootKey: process.env.DFX_NETWORK === "local",
});

console.log(principalId);
const { balance } = IcrcLedgerCanister.create({
agent,
canisterId:
process.env.DFX_NETWORK === "ic"
? Principal.fromText("ddsp7-7iaaa-aaaaq-aacqq-cai")
: Principal.fromText("avqkn-guaaa-aaaaa-qaaea-cai"),
});

if (principalId) {
try {
let result = await balance({
owner: principalId,
subaccount: [],
certified: false,
});
console.log(result)
return result;
} catch (err: any) {
console.error(err);
}
}

return 0n;
} ``` this was what was giving me the error
````

---

<div class="post-metadata">

### Author: ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)
#### Post date: [August 6, 2024, 4:21am UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/9 "2024-08-06T04:21:43Z")

</div>

> [@jamesbeadle](#):
>
> ```auto
> let result = await balance({
> owner: principalId,
> subaccount: [],
> certified: false,
> });
> 
> ```

Thanks for the snippet.

Not sure exactly if your extracted few parts or if it’s one to one your implementation. If the latest, their might be a race condition regarding the `identity`, therefore, I would add a `console.log(principalId.toText()` just before the call to `balance` and do a manual test to observe if indeed the `owner` is correctly set.

As a second try, I would try to not provide the `subaccount` if it is not used.

```auto
console.log('Is there a race condition?', principalId.toText());

let result = await balance({
   owner: principalId,
   certified: false,
});

```

---

<div class="post-metadata">

### Author: ![jamesbeadle](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jamesbeadle/32/10110_2.png) [@jamesbeadle](https://forum.dfinity.org/u/jamesbeadle)
#### Post date: [August 6, 2024, 6:52pm UTC](https://forum.dfinity.org/t/get-ledger-balance-for-principal-id/33863/10 "2024-08-06T18:52:49Z")

</div>

Hi David,

You were spot on, the way to get it working was to remove the subaccount: and just have the owner propery in the params.

Thanks for the help.  
James
