40: Add SPL token balance lookup to SolanaWallet

This commit is contained in:
2026-08-05 10:12:39 +02:00
parent d4e6d3438b
commit 5d940d37cc
2 changed files with 40 additions and 2 deletions
@@ -2,10 +2,13 @@ package com.r35157.cryptowallet.solana;
import com.r35157.libs.solana.SolanaSignedTransaction; import com.r35157.libs.solana.SolanaSignedTransaction;
import com.r35157.libs.solana.SolanaUnsignedTransaction; import com.r35157.libs.solana.SolanaUnsignedTransaction;
import com.r35157.libs.solana.valuetypes.economic.SolanaSPLTokenProgram;
import com.r35157.libs.valuetypes.basic.MoneyAmount; import com.r35157.libs.valuetypes.basic.MoneyAmount;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
/** /**
* A Solana wallet capable of identifying itself and signing transactions. * A Solana wallet capable of identifying itself and signing transactions.
@@ -33,6 +36,22 @@ public interface SolanaWallet {
ΩSolanaAmountΩ getSolanaBalance() ΩSolanaAmountΩ getSolanaBalance()
throws IOException, InterruptedException; throws IOException, InterruptedException;
/**
* Returns the balance of a specific SPL token owned by this wallet.
*
* @param mintAddress the SPL token mint address
* @param splProgram the SPL token program containing the token
* @return the token balance, or {@code null} if no token account exists
* @throws IOException if the balance cannot be fetched or decoded
* @throws InterruptedException if the calling thread is interrupted
* @throws IllegalStateException if multiple accounts exist for the mint
*/
@Nullable
ΩAmountΩ getSPLTokenBalance(
@NotNull ΩSPLMintAddressΩ mintAddress,
@NotNull SolanaSPLTokenProgram splProgram
) throws IOException, InterruptedException;
/** /**
* Signs an unsigned Solana transaction. * Signs an unsigned Solana transaction.
* *
@@ -3,13 +3,18 @@ package com.r35157.cryptowallet.solana.impl.ref;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.r35157.cryptowallet.solana.SolanaWallet; import com.r35157.cryptowallet.solana.SolanaWallet;
import com.r35157.libs.solana.SPLTokenHolding;
import com.r35157.libs.solana.SolanaBlockChain; import com.r35157.libs.solana.SolanaBlockChain;
import com.r35157.libs.solana.SolanaSignedTransaction; import com.r35157.libs.solana.SolanaSignedTransaction;
import com.r35157.libs.solana.SolanaUnsignedTransaction; import com.r35157.libs.solana.SolanaUnsignedTransaction;
import com.r35157.libs.solana.valuetypes.economic.SolanaSPLTokenProgram;
import com.r35157.libs.valuetypes.basic.MoneyAmount; import com.r35157.libs.valuetypes.basic.MoneyAmount;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
public class SolanaWalletImpl implements SolanaWallet { public class SolanaWalletImpl implements SolanaWallet {
public SolanaWalletImpl( public SolanaWalletImpl(
@@ -33,6 +38,20 @@ public class SolanaWalletImpl implements SolanaWallet {
return solanaBlockChain.getBalanceInSolana(address); return solanaBlockChain.getBalanceInSolana(address);
} }
@Override
public @Nullable ΩAmountΩ getSPLTokenBalance(
@NotNull ΩSPLMintAddressΩ mintAddress,
@NotNull SolanaSPLTokenProgram splProgram
) throws IOException, InterruptedException {
SPLTokenHolding tokenHolding = solanaBlockChain
.getSPLTokenHoldings(address, splProgram)
.get(mintAddress);
return tokenHolding == null
? null
: tokenHolding.uiAmount();
}
@Override @Override
public @NotNull SolanaSignedTransaction signTransaction( public @NotNull SolanaSignedTransaction signTransaction(
@NotNull SolanaUnsignedTransaction transaction @NotNull SolanaUnsignedTransaction transaction