# How to deal with Coin Fragmentation from inside contract

**URL:** <https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555>\
**Category:** Move\
**Created:** [March 27, 2023, 6:07am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555 "2023-03-27T06:07:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodion\_at\_Chirp](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/rodion_at_chirp/32/17333_2.png) [@Rodion\_at\_Chirp](https://forums.sui.io/u/Rodion_at_Chirp)\
**Post date:** [March 27, 2023, 6:07am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/1 "2023-03-27T06:07:39Z")

</div>

Hi Friends!

Suppose I have a smart-contract which at some call needs to transfer certain amount of money from my wallet.

- do I need to pass specific coin object to such entry function, not only amount (especially if it is custom coin, not “SUI”)
- or in other words, can smart-contract just browse my wallet and pick suitable number of coins to make the required amount (probably splitting the last of them)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![josemvcerqueira](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/josemvcerqueira/32/2773_2.png) [@josemvcerqueira](https://forums.sui.io/u/josemvcerqueira)\
**Post date:** [March 27, 2023, 9:33am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/2 "2023-03-27T09:33:59Z")

</div>

I usually pass a vector of coins, then combine into a single coin and send back any extra value to the caller:

```auto
  public fun handle_coin_vector<X>(
      vector_x: vector<Coin<X>>,
      coin_in_value: u64,
      ctx: &mut TxContext
    ): Coin<X> {
      let coin_x = coin::zero<X>(ctx);

      if (vector::is_empty(&vector_x)){
        vector::destroy_empty(vector_x);
        return coin_x
      };

      pay::join_vec(&mut coin_x, vector_x);

      let coin_x_value = coin::value(&coin_x);
      if (coin_x_value > coin_in_value) pay::split_and_transfer(&mut coin_x, coin_x_value - coin_in_value, tx_context::sender(ctx), ctx);

      coin_x
    }

```

> <https://github.com/interest-protocol/sui-defi/blob/main/sources/library/utils.move>

---

<div class="post-metadata">

**Author:** ![Rodion\_at\_Chirp](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/rodion_at_chirp/32/17333_2.png) [@Rodion\_at\_Chirp](https://forums.sui.io/u/Rodion_at_Chirp)\
**Post date:** [March 27, 2023, 11:54am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/3 "2023-03-27T11:54:19Z")

</div>

Jose, Hi and thanks for suggestion!

So you mean I should anyway merge coins manually - either in the smart-contract itself or before it, by some different service or contract?

My initial idea was that there may be some magic method which can browse coins in owner’s address, but seemingly no such magic exists at this point (perhaps even not going to in future), right?

---

<div class="post-metadata">

**Author:** ![josemvcerqueira](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/josemvcerqueira/32/2773_2.png) [@josemvcerqueira](https://forums.sui.io/u/josemvcerqueira)\
**Post date:** [March 27, 2023, 1:56pm UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/4 "2023-03-27T13:56:49Z")

</div>

For now yes, but I think programmable transactions will enable coins to be merged or split at the TX lvl.

---

<div class="post-metadata">

**Author:** ![Rodion\_at\_Chirp](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/rodion_at_chirp/32/17333_2.png) [@Rodion\_at\_Chirp](https://forums.sui.io/u/Rodion_at_Chirp)\
**Post date:** [March 27, 2023, 2:19pm UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/5 "2023-03-27T14:19:15Z")

</div>

Oh, wow! thanks a lot, I never know yet of this upcoming feature, sounds very curious!

---

<div class="post-metadata">

**Author:** ![Nate.dev](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/nate.dev/32/238_2.png) [@Nate.dev](https://forums.sui.io/u/Nate.dev)\
**Post date:** [April 3, 2023, 3:55am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/7 "2023-04-03T03:55:39Z")

</div>

This makes sense if you already know all the respective coin object IDs, but if for example i want to check the coin balance of a user’s address and that address happens to have 50 coin objects it becomes very tiring to do this on the contract level

---

<div class="post-metadata">

**Author:** ![Rodion\_at\_Chirp](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/rodion_at_chirp/32/17333_2.png) [@Rodion\_at\_Chirp](https://forums.sui.io/u/Rodion_at_Chirp)\
**Post date:** [April 3, 2023, 7:42am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/8 "2023-04-03T07:42:58Z")

</div>

Yep, that’s true. Even if it is a single coin, not 50, to deal with one’s money one needs not only wallet id (and coin name), but specific id of the coin. Not very convenient in some situations.

---

<div class="post-metadata">

**Author:** ![honey](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@honey](https://forums.sui.io/u/honey)\
**Post date:** [April 7, 2023, 6:57am UTC](https://forums.sui.io/t/how-to-deal-with-coin-fragmentation-from-inside-contract/28555/9 "2023-04-07T06:57:46Z")

</div>

for the sui nft market any coin birdge is good
