# Can't get all balance of contract

**URL:** <https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356>\
**Category:** Sway\
**Created:** [January 12, 2023, 3:27pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356 "2023-01-12T15:27:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sway](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sway/32/103_2.png) [@sway](https://forum.fuel.network/u/sway)\
**Post date:** [January 12, 2023, 3:27pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/1 "2023-01-12T15:27:13Z")

</div>

Here is the link to my Money Box smart contract which I am trying to get the total balance of the specified token

[https://github.com/sway-gang/money-box/blob/total\_balance\_of/src/main.sw](https://github.com/sway-gang/money-box/blob/total_balance_of/src/main.sw)

```auto
 fn total_balance_of(asset_id: ContractId) -> u64 {
        balance_of(contract_id(), asset_id)
    }

```

But when I run this contract with following test I keep getting 0 as token balance

[https://github.com/sway-gang/money-box/blob/total\_balance\_of/tests/local\_tests/main\_test.rs](https://github.com/sway-gang/money-box/blob/total_balance_of/tests/local_tests/main_test.rs)

```auto
let contracts = [Bech32ContractId::from(contract_id)];
    let total_balance = methods
        .total_balance_of(contract_id)
        .set_contracts(&contracts)
        .simulate()
        .await
        .unwrap();
    println!("total_balance: {}", total_balance.value);

```

Please help)

---

<div class="post-metadata">

**Author:** ![sarah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sarah/32/13_2.png) [@sarah](https://forum.fuel.network/u/sarah)\
**Post date:** [January 12, 2023, 4:06pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/2 "2023-01-12T16:06:29Z")

</div>

Your link doesn’t work for me, is that a public repo?

---

<div class="post-metadata">

**Author:** ![sway](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sway/32/103_2.png) [@sway](https://forum.fuel.network/u/sway)\
**Post date:** [January 12, 2023, 4:18pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/3 "2023-01-12T16:18:22Z")

</div>

fixed, sorry) now its public

---

<div class="post-metadata">

**Author:** ![sarah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sarah/32/13_2.png) [@sarah](https://forum.fuel.network/u/sarah)\
**Post date:** [January 12, 2023, 5:26pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/4 "2023-01-12T17:26:08Z")

</div>

~~Thanks! This looks like a type mismatch, as `contract_id()` returns a `ContractId`, so the key you provided is of type `(ContractId, ContractId)` instead of `(Address, ContractId)`. You might want to consider using `Identity` instead, which can be either an `Address` or `ContractId`:  
 [Blockchain Types - The Sway Programming Language](https://fuellabs.github.io/sway/v0.33.0/book/basics/blockchain_types.html#identity-type)~~

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 13, 2023, 3:24pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/5 "2023-01-13T15:24:08Z")

</div>

> [@sarah](#):
>
> Identity

Can you please send a sample code with the problem fixed?

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 13, 2023, 3:55pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/6 "2023-01-13T15:55:15Z")

</div>

Tried using identity, same result.

```auto
 let identity = Identity::ContractId(contract_id);
    let total_balance = methods
        .total_balance_of(identity)
        .set_contracts(&cotarcts)
        .simulate()
        .await
        .unwrap();
    println!("total_balance: {}", total_balance.value);

```

And the address cannot be used as an argument because of a compilation error

```auto
error
  --> /Users/alexey/projects/fuel/money-box/src/main.sw:93:35
   |
91 | 
92 | // };
93 | balance_of(contract_id(), asset_id)
   | ^^^^^^^^ Mismatched types.
expected: ContractId
found: Address.
help: The argument that has been provided to this function's type does not match the declared type of the parameter in the function declaration.
94 | }
95 | }
   |
____

```

---

<div class="post-metadata">

**Author:** ![sarah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/sarah/32/13_2.png) [@sarah](https://forum.fuel.network/u/sarah)\
**Post date:** [January 13, 2023, 5:25pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/7 "2023-01-13T17:25:14Z")

</div>

My apologies, the type is not the issue here. It is actually just the order of the arguments passed to the `balance_of` function. It should be `balance_of(asset_id, contract_id())` . You can see the full code for this function here: [sway/context.sw at 949dfb98bd8ad0d89c5a59a33b391892e665a760 · FuelLabs/sway · GitHub](https://github.com/FuelLabs/sway/blob/949dfb98bd8ad0d89c5a59a33b391892e665a760/sway-lib-std/src/context.sw#L14)

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 13, 2023, 5:37pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/8 "2023-01-13T17:37:46Z")

</div>

That’s works  
Thanks a lot!!! ❤

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 13, 2023, 5:40pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/9 "2023-01-13T17:40:34Z")

</div>

It would be better to use `this_balance(asset_id)` in that case

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [February 3, 2023, 1:40pm UTC](https://forum.fuel.network/t/cant-get-all-balance-of-contract/1356/10 "2023-02-03T13:40:42Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
