Skip to content

Commit 1b72804

Browse files
committed
chore(ts-sdk-sui): added new function
Signed-off-by: kaancaglan <[email protected]>
1 parent 52dcff8 commit 1b72804

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

ts-sdk-sui/src/Sui.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,48 @@ export const readCoinBalances = (contractAddress: string, address: string) =>
387387
})
388388
return coins
389389
})
390+
391+
export const getAllCoinsUnique = (address: string) =>
392+
Effect.gen(function*() {
393+
const client = (yield* PublicClient).client
394+
395+
const params = {
396+
owner: address,
397+
}
398+
399+
const coins = yield* Effect.tryPromise({
400+
try: async () => {
401+
const result = await client.getAllCoins(params)
402+
return result.data
403+
},
404+
catch: err =>
405+
new ReadCoinError({
406+
cause: extractErrorDetails(err as ReadCoinError),
407+
}),
408+
})
409+
410+
// Group by coinType and sum balances
411+
const coinMap: Record<string, bigint> = {}
412+
413+
for (const coin of coins) {
414+
const coinType = coin.coinType
415+
const balance = BigInt(coin.balance)
416+
417+
if (!coinMap[coinType]) {
418+
coinMap[coinType] = balance
419+
} else {
420+
coinMap[coinType] += balance
421+
}
422+
}
423+
424+
// Convert to array of objects
425+
const result = Object.entries(coinMap).map(([coinType, totalBalance]) => ({
426+
coinType,
427+
balance: totalBalance.toString(), // or keep as BigInt if preferred
428+
}))
429+
430+
return result
431+
})
390432

391433
// /**
392434
// * Read the balance of an ERC20 token for a specific address

0 commit comments

Comments
 (0)