Hello. I have problem with SUI objects.
Balance - 0.19 SUI 0x4805998aec408aaea27fda52be120d798b7ec79e0eb59fa2392cd1ba2f53dace (i can’t send links because forum doesnt allowing it, but i’ll leave address here)
My script getting objects of coin and merging or use single object before transaction
It got
Using single coin: 0xbf71417931c5f4cc98a97dbe98844f584d14bc8e5850971c56f519fbbde2b505
It got right object with 0.19 SUI - 0xbf71417931c5f4cc98a97dbe98844f584d14bc8e5850971c56f519fbbde2b505
But if i try to send 0.002 SUI, it returns
Send token error: Total gas available 2185532, transaction requires 3464000
Why? I not sending full balance, why i dont have enough gas
My code here:
`async def send_token(self, *, to_address: str | Wallet, coin_type: str, amount: float):
“”“Send token to a specific wallet”“”
try:
if isinstance(to_address, Wallet):
to_address = str(to_address.address)
metadata = await self.cs.get_coin_metadata(coin_type)
amount = convert_amount(amount, metadata.decimals)
log.info(f"Sending {amount} {metadata.symbol} to {to_address}")
coins = await self.cs.get_coin_objects(self.address, coin_type)
if not coin_type:
raise Exception(f"No tokens found for type {coin_type}")
selected_coins, is_enough = calculate_coins(amount, coins)
if not is_enough:
raise Exception("Not enough tokens")
txn: AsyncSuiTransaction = self.client.transaction(
client=self.client, initial_sender=self.address
)
primary_coin_id = selected_coins[0].coin_object_id
if len(selected_coins) == 1:
log.info(f"Using single coin: {primary_coin_id}")
scres = await txn.split_coin(coin=primary_coin_id, amounts=[amount])
else:
log.info(
f"Merging {len(selected_coins)} of {len(coins)}... in {primary_coin_id}"
)
coins_to_merge = [coin.coin_object_id for coin in selected_coins[1:]]
await txn.merge_coins(
merge_to=primary_coin_id, merge_from=coins_to_merge
)
scres = await txn.split_coin(coin=primary_coin_id, amounts=[amount])
await txn.transfer_objects(
transfers=scres if isinstance(scres, list) else [scres], # type: ignore
recipient=to_address,
)
txn_dict = await txn.build_and_sign()
result = await self.client.execute_query_node(
with_node=qn.ExecuteTransaction(**txn_dict)
)
if result.is_ok():
await self.client.wait_for_transaction(digest=result.result_data.digest)
log.success(f"Token transfer successful: {result.result_data.digest}")
return result.result_data.digest
else:
log.error(f"Token transfer failed: {result.result_string}")
raise Exception(result.result_string)
except Exception as e:
log.error(f"Send token error: {str(e)}")
raise e
async def send_sui(self, to_address: str | Wallet, amount: float) → str:
“”“Send SUI to a specific wallet”“”
if isinstance(to_address, Wallet):
to_address = str(to_address.address)
return await self.send_token(
to_address=to_address, coin_type=“0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI”, amount=amount
)`
Calculate coins func:
def calculate_coins(amount: int, coins: list[Any]) → Tuple[list[Any], bool]:
sorted_coins = sorted(coins, key=lambda x: int(x.balance), reverse=True)
total_balance = 0
selected_coins = []
for coin in sorted_coins:
selected_coins.append(coin)
total_balance += int(coin.balance)
if total_balance >= amount:
break
- Get all token objects
- calculate_coins func (for calculate how many objects we need to get needed amount)
- Merge and send
It’s simple. But it’s dont work (
Please help me.
Also i would like to know how to debug it, because i can’t get full error log in pysui ![]()