com.bloxbean.cardano.client.backend.gql.GqlUtxoService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cardano-client-backend-gql Show documentation
Show all versions of cardano-client-backend-gql Show documentation
Cardano Client Library - GraphQL Backend
package com.bloxbean.cardano.client.backend.gql;
import com.apollographql.apollo.api.Input;
import com.bloxbean.cardano.client.backend.api.UtxoService;
import com.bloxbean.cardano.client.api.common.OrderEnum;
import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Amount;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.api.model.Utxo;
import com.bloxbean.cardano.gql.UtxosQuery;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.bloxbean.cardano.client.common.CardanoConstants.LOVELACE;
public class GqlUtxoService extends BaseGqlService implements UtxoService {
private final Logger logger = LoggerFactory.getLogger(GqlUtxoService.class);
public GqlUtxoService(String gqlUrl) {
super(gqlUrl);
}
public GqlUtxoService(String gqlUrl, Map headers) {
super(gqlUrl, headers);
}
public GqlUtxoService(String gqlUrl, OkHttpClient okHttpClient) {
super(gqlUrl, okHttpClient);
}
@Override
public Result> getUtxos(String address, int count, int page) throws ApiException {
if(page == 0)
page = 1;
page = page - 1;
int offset = count * page;
UtxosQuery.Data data = null;
try {
data = execute(new UtxosQuery(Input.optional(address), Input.optional(count), Input.optional(offset)));
if (data == null || data.utxos() == null || data.utxos().size() == 0)
return processSuccessResult(Collections.EMPTY_LIST);
} catch (Exception e) {
if(logger.isDebugEnabled())
logger.warn("Utxos not found", e);
return Result.error("No utxos found").withValue(e);
}
List utxos = data.utxos().stream()
.map(ux -> {
List amounts = new ArrayList<>();
Amount lovelaceAmt = Amount.builder()
.unit(LOVELACE)
.quantity(new BigInteger(ux.value()))
.build();
amounts.add(lovelaceAmt);
for(UtxosQuery.Token token: ux.tokens()) {
amounts.add(Amount.builder()
.unit(String.valueOf(token.asset().assetId()))
.quantity(new BigInteger(token.quantity()))
.build()
);
}
Utxo utxo = Utxo.builder()
.txHash((String) ux.txHash())
.outputIndex(ux.index())
.amount(amounts).build();
return utxo;
}).collect(Collectors.toList());
return processSuccessResult(utxos);
}
@Override
public Result> getUtxos(String address, int count, int page, OrderEnum order) throws ApiException {
return getUtxos(address, count, page);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy