diff --git a/backend-modules/koios/build.gradle b/backend-modules/koios/build.gradle index ed2549c9..52d2e97f 100644 --- a/backend-modules/koios/build.gradle +++ b/backend-modules/koios/build.gradle @@ -2,7 +2,13 @@ dependencies { api project(':core') api project(':backend') - api (libs.koios.java) + api (libs.koios.java) { + exclude group: 'org.slf4j', module: 'slf4j-api' + exclude group: 'org.bouncycastle', module: 'bcprov-jdk15to18' + exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind' + exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core' + exclude group: 'com.fasterxml.jackson.core', module: 'jackson-annotations ' + } } publishing { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bf2a7500..ab099bfe 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,24 +2,23 @@ koios-java = "io.github.cardano-community:koios-java-client:1.18.1" ogmios-java = "io.adabox:ogmios-java-client:1.0.0" -slf4j-api = "org.slf4j:slf4j-api:1.7.36" -slf4j-log4j = "org.slf4j:slf4j-log4j12:1.7.36" +slf4j-api = "org.slf4j:slf4j-api:2.0.11" +slf4j-reload4j = "org.slf4j:slf4j-reload4j:2.0.11" -bouncycastle-bcprov = "org.bouncycastle:bcprov-jdk15to18:1.71" -guava = "com.google.guava:guava:32.1.2-jre" +bouncycastle-bcprov = "org.bouncycastle:bcprov-jdk18on:1.77" +guava = "com.google.guava:guava:33.0.0-jre" co-nstant-in-cbor = "co.nstant.in:cbor:0.9" i2p-crypto-eddsa = "net.i2p.crypto:eddsa:0.3.0" -jackson-databind = "com.fasterxml.jackson.core:jackson-databind:2.15.0" +jackson-databind = "com.fasterxml.jackson.core:jackson-databind:2.15.3" retrofit2 = "com.squareup.retrofit2:retrofit:2.9.0" retrofit2-converter-jackson = "com.squareup.retrofit2:converter-jackson:2.9.0" -okhttp = "com.squareup.okhttp3:okhttp:4.10.0" +okhttp = "com.squareup.okhttp3:okhttp:4.12.0" -yaci = "com.bloxbean.cardano:yaci:0.2.4" aiken-java-binding = "com.bloxbean.cardano:aiken-java-binding:0.0.8" google-auto-service="com.google.auto.service:auto-service:1.1.1" javapoet="com.squareup:javapoet:1.13.0" google-testing-compile="com.google.testing.compile:compile-testing:0.21.0" -lombok = "org.projectlombok:lombok:1.18.28" +lombok = "org.projectlombok:lombok:1.18.30" diff --git a/quicktx/build.gradle b/quicktx/build.gradle index 797a6e69..a9cca7b1 100644 --- a/quicktx/build.gradle +++ b/quicktx/build.gradle @@ -3,7 +3,7 @@ dependencies { api project(':function') api project(':backend') - integrationTestImplementation(libs.slf4j.log4j) + integrationTestImplementation(libs.slf4j.reload4j) integrationTestImplementation(libs.aiken.java.binding) integrationTestImplementation project(':') diff --git a/settings.gradle b/settings.gradle index 05a75fd4..acab1192 100644 --- a/settings.gradle +++ b/settings.gradle @@ -34,5 +34,4 @@ include 'backend-modules:ogmios' include 'integration-test' //supplier modules -include 'supplier' -include 'supplier:local' +//include 'supplier' diff --git a/supplier/build.gradle b/supplier/build.gradle index e3e31e5c..e315bd41 100644 --- a/supplier/build.gradle +++ b/supplier/build.gradle @@ -1,5 +1,5 @@ dependencies { - api project(':supplier:local') + } publishing { diff --git a/supplier/local/README.md b/supplier/local/README.md deleted file mode 100644 index 7c2ae886..00000000 --- a/supplier/local/README.md +++ /dev/null @@ -1,3 +0,0 @@ -## Local Supplier Module (Experimental) (cardano-client-supplier-local) - -Local Supplier module to submit transaction to a local Cardano node through Yaci. diff --git a/supplier/local/build.gradle b/supplier/local/build.gradle deleted file mode 100644 index bea7a4f0..00000000 --- a/supplier/local/build.gradle +++ /dev/null @@ -1,18 +0,0 @@ -dependencies { - api project(':core-api') - - api libs.yaci - testImplementation project(':quicktx') - testImplementation libs.aiken.java.binding -} - -publishing { - publications { - mavenJava(MavenPublication) { - pom { - name = 'Cardano Client Supplier - Local' - description = 'Cardano Client Lib - Local Supplier Module' - } - } - } -} diff --git a/supplier/local/src/it/java/com/bloxbean/cardano/client/supplier/local/LocalTxTest.java b/supplier/local/src/it/java/com/bloxbean/cardano/client/supplier/local/LocalTxTest.java deleted file mode 100644 index 0e2cc4e1..00000000 --- a/supplier/local/src/it/java/com/bloxbean/cardano/client/supplier/local/LocalTxTest.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.bloxbean.cardano.client.supplier.local; - -import com.bloxbean.cardano.aiken.AikenTransactionEvaluator; -import com.bloxbean.cardano.client.account.Account; -import com.bloxbean.cardano.client.address.AddressProvider; -import com.bloxbean.cardano.client.api.TransactionProcessor; -import com.bloxbean.cardano.client.api.UtxoSupplier; -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.client.common.model.Networks; -import com.bloxbean.cardano.client.function.helper.ScriptUtxoFinders; -import com.bloxbean.cardano.client.function.helper.SignerProviders; -import com.bloxbean.cardano.client.metadata.Metadata; -import com.bloxbean.cardano.client.metadata.MetadataBuilder; -import com.bloxbean.cardano.client.plutus.spec.BigIntPlutusData; -import com.bloxbean.cardano.client.plutus.spec.PlutusV2Script; -import com.bloxbean.cardano.client.quicktx.QuickTxBuilder; -import com.bloxbean.cardano.client.quicktx.ScriptTx; -import com.bloxbean.cardano.client.quicktx.Tx; -import com.bloxbean.cardano.yaci.helper.LocalStateQueryClient; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.math.BigInteger; -import java.util.List; -import java.util.Optional; -import java.util.Random; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class LocalTxTest { - Account sender1; - Account sender2; - - String sender1Addr; - String sender2Addr; - - String receiver1 = "addr_test1qz3s0c370u8zzqn302nppuxl840gm6qdmjwqnxmqxme657ze964mar2m3r5jjv4qrsf62yduqns0tsw0hvzwar07qasqeamp0c"; - String receiver2 = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82"; - String receiver3 = "addr_test1qqqvjp4ffcdqg3fmx0k8rwamnn06wp8e575zcv8d0m3tjn2mmexsnkxp7az774522ce4h3qs4tjp9rxjjm46qf339d9sk33rqn"; - - UtxoSupplier utxoSupplier; - LocalProtocolSupplier protocolSupplier; - TransactionProcessor transactionProcessor; - - @BeforeEach - void setup() { - LocalClientProviderFactory factory = new LocalClientProviderFactory("/Users/satya/work/cardano-node/preprod-8/db/node.socket", 1); - LocalStateQueryClient localStateQueryClient = factory.getLocalStateQueryClient(); - factory.start(); - - utxoSupplier = new LocalUtxoSupplier(localStateQueryClient); - protocolSupplier = new LocalProtocolSupplier(localStateQueryClient); - transactionProcessor = new LocalTransactionProcessor(factory.getTxSubmissionClient()); - - //addr_test1qp73ljurtknpm5fgey5r2y9aympd33ksgw0f8rc5khheg83y35rncur9mjvs665cg4052985ry9rzzmqend9sqw0cdksxvefah - String senderMnemonic = "drive useless envelope shine range ability time copper alarm museum near flee wrist live type device meadow allow churn purity wisdom praise drop code"; - sender1 = new Account(Networks.testnet(), senderMnemonic); - sender1Addr = sender1.baseAddress(); - - //addr_test1qz5fcpvkg7pekqvv9ld03t5sx2w2c2fac67fzlaxw5844s83l4p6tr389lhgcpe4797kt7xkcxqvcc4a6qjshzsmta8sh3ncs4 - String sender2Mnemonic = "access else envelope between rubber celery forum brief bubble notice stomach add initial avocado current net film aunt quick text joke chase robust artefact"; - sender2 = new Account(Networks.testnet(), sender2Mnemonic); - sender2Addr = sender2.baseAddress(); - } - - @Test - @Disabled - void simplePayment() throws InterruptedException, ApiException { - Metadata metadata = MetadataBuilder.createMetadata(); - metadata.put(BigInteger.valueOf(100), "This is tx through local supplier"); - - QuickTxBuilder quickTxBuilder = new QuickTxBuilder(utxoSupplier, protocolSupplier, transactionProcessor); - - Tx tx = new Tx() - .payToAddress(receiver1, Amount.ada(1.5)) - .payToAddress(receiver2, Amount.ada(2.5)) - .attachMetadata(metadata) - .from(sender1Addr); - - Result result = quickTxBuilder.compose(tx) - .withSigner(SignerProviders.signerFrom(sender1)) - .complete(); - - System.out.println(result); - assertTrue(result.isSuccessful()); - checkIfUtxoAvailable(result.getValue(), sender1Addr); - } - - @Test - @Disabled - void alwaysTrueScript() throws ApiException, InterruptedException { - PlutusV2Script plutusScript = PlutusV2Script.builder() - .type("PlutusScriptV2") - .cborHex("49480100002221200101") - .build(); - - String scriptAddress = AddressProvider.getEntAddress(plutusScript, Networks.testnet()).toBech32(); - BigInteger scriptAmt = new BigInteger("2479280"); - - Random rand = new Random(); - int randInt = rand.nextInt(); - BigIntPlutusData plutusData = new BigIntPlutusData(BigInteger.valueOf(randInt)); //any random number - - Tx tx = new Tx(); - tx.payToContract(scriptAddress, Amount.lovelace(scriptAmt), plutusData) - .from(sender2Addr); - - QuickTxBuilder quickTxBuilder = new QuickTxBuilder(utxoSupplier, protocolSupplier, transactionProcessor); - Result result = quickTxBuilder.compose(tx) - .withSigner(SignerProviders.signerFrom(sender2)) - .complete(); - - System.out.println(result); - checkIfUtxoAvailable(result.getValue(), scriptAddress); - - Optional optionalUtxo = ScriptUtxoFinders.findFirstByInlineDatum(utxoSupplier, scriptAddress, plutusData); - ScriptTx scriptTx = new ScriptTx() - .collectFrom(optionalUtxo.get(), plutusData) - .payToAddress(receiver1, Amount.lovelace(scriptAmt)) - .attachSpendingValidator(plutusScript) - .withChangeAddress(scriptAddress, plutusData); - - Result result1 = quickTxBuilder.compose(scriptTx) - .feePayer(sender1Addr) - .withSigner(SignerProviders.signerFrom(sender1)) - .withTxEvaluator(new AikenTransactionEvaluator(utxoSupplier, protocolSupplier)) - .complete(); - - System.out.println(result); - assertTrue(result1.isSuccessful()); - - checkIfUtxoAvailable(result1.getValue(), sender1Addr); - } - - private void checkIfUtxoAvailable(String txHash, String address) throws ApiException, InterruptedException { - int counter = 0; - while(counter < 60) { - counter++; - Thread.sleep(3000); - System.out.println("Checking if txHash is available : " + txHash); - List utxos = utxoSupplier.getAll(address); - if (utxos == null || utxos.size() == 0) continue; - - Optional utxo = utxos.stream().filter(u -> u.getTxHash().equals(txHash)).findFirst(); - if (utxo.isPresent()) { - System.out.println("Utxo found >>>"); - return; - } - } - } -} diff --git a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalClientProviderFactory.java b/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalClientProviderFactory.java deleted file mode 100644 index 538afd2b..00000000 --- a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalClientProviderFactory.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.bloxbean.cardano.client.supplier.local; - -import com.bloxbean.cardano.yaci.helper.LocalClientProvider; -import com.bloxbean.cardano.yaci.helper.LocalStateQueryClient; -import com.bloxbean.cardano.yaci.helper.LocalTxSubmissionClient; - -/** - * Factory class to create LocalClientProvider. - */ -public class LocalClientProviderFactory { - private LocalClientProvider localClientProvider; - private LocalStateQueryClient localStateQueryClient; - private LocalTxSubmissionClient txSubmissionClient; - - /** - * Constructor - * @param nodeSocketFile Node socket file - * @param protocolMagicId Protocol magic id - */ - public LocalClientProviderFactory(String nodeSocketFile, long protocolMagicId) { - localClientProvider = new LocalClientProvider(nodeSocketFile, protocolMagicId); - - localStateQueryClient = localClientProvider.getLocalStateQueryClient(); - txSubmissionClient = localClientProvider.getTxSubmissionClient(); - } - - /** - * Start the LocalClientProvider instance - */ - public void start() { - localClientProvider.start(); - } - - /** - * Get LocalStateQueryClient instance - * @return LocalStateQueryClient - */ - public LocalStateQueryClient getLocalStateQueryClient() { - return localStateQueryClient; - } - - /** - * Get LocalTxSubmissionClient instance - * @return LocalTxSubmissionClient - */ - public LocalTxSubmissionClient getTxSubmissionClient() { - return txSubmissionClient; - } - - /** - * Shutdown the LocalClientProvider instance - */ - public void shutdown() { - if (localClientProvider != null) - localClientProvider.shutdown(); - } -} diff --git a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalProtocolSupplier.java b/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalProtocolSupplier.java deleted file mode 100644 index c4e74153..00000000 --- a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalProtocolSupplier.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.bloxbean.cardano.client.supplier.local; - -import co.nstant.in.cbor.model.Array; -import co.nstant.in.cbor.model.DataItem; -import co.nstant.in.cbor.model.Special; -import co.nstant.in.cbor.model.UnsignedInteger; -import com.bloxbean.cardano.client.api.ProtocolParamsSupplier; -import com.bloxbean.cardano.client.api.model.ProtocolParams; -import com.bloxbean.cardano.client.plutus.util.PlutusOps; -import com.bloxbean.cardano.yaci.core.model.ProtocolParamUpdate; -import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.CurrentProtocolParamQueryResult; -import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.CurrentProtocolParamsQuery; -import com.bloxbean.cardano.yaci.core.util.CborSerializationUtil; -import com.bloxbean.cardano.yaci.core.util.HexUtil; -import com.bloxbean.cardano.yaci.helper.LocalStateQueryClient; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.time.Duration; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -/** - * Protocol parameters supplier for local Cardano node. This is not thread safe. - */ -public class LocalProtocolSupplier implements ProtocolParamsSupplier { - public static final String PLUTUS_V_1 = "PlutusV1"; - public static final String PLUTUS_V_2 = "PlutusV2"; - private LocalStateQueryClient localStateQueryClient; - - /** - * Constructor - * @param localStateQueryClient LocalStateQueryClient - */ - public LocalProtocolSupplier(LocalStateQueryClient localStateQueryClient) { - this.localStateQueryClient = localStateQueryClient; - } - - @Override - public ProtocolParams getProtocolParams() { - //Try to release first before a new query to avoid stale data - try { - localStateQueryClient.release().block(Duration.ofSeconds(5)); - } catch (Exception e) { - //Ignore the error - } - - CurrentProtocolParamQueryResult currentProtocolParameters = - (CurrentProtocolParamQueryResult) localStateQueryClient.executeQuery(new CurrentProtocolParamsQuery()).block(); - - ProtocolParamUpdate protocolParamUpdate = currentProtocolParameters.getProtocolParams(); - - ProtocolParams protocolParams = new ProtocolParams(); - protocolParams.setMinFeeA(protocolParamUpdate.getMinFeeA()); - protocolParams.setMinFeeB(protocolParamUpdate.getMinFeeB()); - protocolParams.setMaxBlockSize(protocolParamUpdate.getMaxBlockSize()); - protocolParams.setMaxTxSize(protocolParamUpdate.getMaxTxSize()); - protocolParams.setMaxBlockHeaderSize(protocolParamUpdate.getMaxBlockHeaderSize()); - protocolParams.setKeyDeposit(String.valueOf(protocolParamUpdate.getKeyDeposit())); - protocolParams.setPoolDeposit(String.valueOf(protocolParamUpdate.getPoolDeposit())); - protocolParams.setEMax(protocolParamUpdate.getMaxEpoch()); - protocolParams.setNOpt(protocolParamUpdate.getNOpt()); - protocolParams.setA0(protocolParamUpdate.getPoolPledgeInfluence()); - protocolParams.setRho(protocolParamUpdate.getExpansionRate()); - protocolParams.setTau(protocolParamUpdate.getTreasuryGrowthRate()); - protocolParams.setDecentralisationParam(protocolParamUpdate.getDecentralisationParam()); //Deprecated. Not there - //protocolParams.setExtraEntropy(protocolParamUpdate.getExtraEntropy()); //TODO - protocolParams.setProtocolMajorVer(protocolParamUpdate.getProtocolMajorVer()); - protocolParams.setProtocolMinorVer(protocolParamUpdate.getProtocolMinorVer()); - protocolParams.setMinUtxo(String.valueOf(protocolParamUpdate.getMinUtxo())); - protocolParams.setMinPoolCost(String.valueOf(protocolParamUpdate.getMinPoolCost())); -// protocolParams.setNonce(currentProtocolParameters.getProtocolParameters().getNonce()); //TODO - - Map plutusV1CostModel - = cborToCostModel(protocolParamUpdate.getCostModels().get(0), PlutusOps.getOperations(1)); - Map plutusV2CostModel - = cborToCostModel(protocolParamUpdate.getCostModels().get(1), PlutusOps.getOperations(2)); - - LinkedHashMap> costModels = new LinkedHashMap<>(); - costModels.put("PlutusV1", plutusV1CostModel); - costModels.put("PlutusV2", plutusV2CostModel); - protocolParams.setCostModels(costModels); - - protocolParams.setPriceMem(protocolParamUpdate.getPriceMem()); - protocolParams.setPriceStep(protocolParamUpdate.getPriceStep()); - protocolParams.setMaxTxExMem(String.valueOf(protocolParamUpdate.getMaxTxExMem())); - protocolParams.setMaxTxExSteps(String.valueOf(protocolParamUpdate.getMaxTxExSteps())); - protocolParams.setMaxBlockExMem(String.valueOf(protocolParamUpdate.getMaxBlockExMem())); - protocolParams.setMaxBlockExSteps(String.valueOf(protocolParamUpdate.getMaxBlockExSteps())); - protocolParams.setMaxValSize(String.valueOf(protocolParamUpdate.getMaxValSize())); - protocolParams.setCollateralPercent(BigDecimal.valueOf(protocolParamUpdate.getCollateralPercent())); - protocolParams.setMaxCollateralInputs(protocolParamUpdate.getMaxCollateralInputs()); - protocolParams.setCoinsPerUtxoSize(String.valueOf(protocolParamUpdate.getAdaPerUtxoByte())); - return protocolParams; - } - - private Map cborToCostModel(String costModelCbor, List ops) { - Array array = (Array) CborSerializationUtil.deserializeOne(HexUtil.decodeHexString(costModelCbor)); - Map costModel = new LinkedHashMap<>(); - - if (ops.size() == array.getDataItems().size()) { - int index = 0; - for (DataItem di : array.getDataItems()) { - if (di == Special.BREAK) - continue; - BigInteger val = ((UnsignedInteger) di).getValue(); - costModel.put(ops.get(index++), val.longValue()); - } - } else { - int index = 0; - for (DataItem di : array.getDataItems()) { - if (di == Special.BREAK) - continue; - BigInteger val = ((UnsignedInteger) di).getValue(); - costModel.put(String.format("%03d", index++), val.longValue()); - } - } - - return costModel; - } -} diff --git a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalTransactionProcessor.java b/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalTransactionProcessor.java deleted file mode 100644 index 5c8a63a3..00000000 --- a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalTransactionProcessor.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.bloxbean.cardano.client.supplier.local; - -import com.bloxbean.cardano.client.api.TransactionProcessor; -import com.bloxbean.cardano.client.api.model.EvaluationResult; -import com.bloxbean.cardano.client.api.model.Result; -import com.bloxbean.cardano.client.api.model.Utxo; -import com.bloxbean.cardano.yaci.core.protocol.localtx.model.TxSubmissionRequest; -import com.bloxbean.cardano.yaci.helper.LocalTxSubmissionClient; -import com.bloxbean.cardano.yaci.helper.model.TxResult; -import reactor.core.publisher.Mono; - -import java.time.Duration; -import java.util.List; -import java.util.Set; - -/** - * Transaction processor implementation for local Cardano node. This class uses LocalTxSubmissionClient - * to submit transactions to local Cardano node. This class is not thread safe. - */ -public class LocalTransactionProcessor implements TransactionProcessor { - private LocalTxSubmissionClient txSubmissionClient; - - public LocalTransactionProcessor(LocalTxSubmissionClient txSubmissionClient) { - this.txSubmissionClient = txSubmissionClient; - } - - @Override - public Result submitTransaction(byte[] cborData) { - TxSubmissionRequest txSubmissionRequest = new TxSubmissionRequest(cborData); - Mono mono = txSubmissionClient.submitTx(txSubmissionRequest); - TxResult txResult = mono.block(Duration.ofSeconds(20)); - - if(txResult.isAccepted()) { - return Result.success(txResult.getTxHash()).withValue(txResult.getTxHash()).code(200); - } else { - return Result.success(txResult.getErrorCbor()).withValue(txResult.getErrorCbor()).code(500); - } - } - - @Override - public Result> evaluateTx(byte[] cbor, Set inputUtxos) { - throw new UnsupportedOperationException("Not supported"); - } -} diff --git a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalUtxoSupplier.java b/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalUtxoSupplier.java deleted file mode 100644 index 272a62fc..00000000 --- a/supplier/local/src/main/java/com/bloxbean/cardano/client/supplier/local/LocalUtxoSupplier.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.bloxbean.cardano.client.supplier.local; - -import com.bloxbean.cardano.client.address.Address; -import com.bloxbean.cardano.client.api.UtxoSupplier; -import com.bloxbean.cardano.client.api.common.OrderEnum; -import com.bloxbean.cardano.client.api.model.Utxo; -import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.UtxoByAddressQuery; -import com.bloxbean.cardano.yaci.core.protocol.localstate.queries.UtxoByAddressQueryResult; -import com.bloxbean.cardano.yaci.helper.LocalStateQueryClient; - -import java.time.Duration; -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -/** - * Utxo supplier implementation for local Cardano node. This is not thread safe. - */ -public class LocalUtxoSupplier implements UtxoSupplier { - private LocalStateQueryClient localStateQueryClient; - - public LocalUtxoSupplier(LocalStateQueryClient localStateQueryClient) { - this.localStateQueryClient = localStateQueryClient; - } - - @Override - public List getPage(String address, Integer nrOfItems, Integer page, OrderEnum order) { - if (page != null) - page = page + 1; - else - page = 1; - - if (page != 1) - return Collections.EMPTY_LIST; - - //Try to release first before a new query to avoid stale data - try { - localStateQueryClient.release().block(Duration.ofSeconds(5)); - } catch (Exception e) { - //Ignore the error - } - - UtxoByAddressQueryResult queryResult = (UtxoByAddressQueryResult) localStateQueryClient - .executeQuery(new UtxoByAddressQuery(new Address(address))).block(Duration.ofSeconds(10)); - - List utxos = queryResult.getUtxoList(); - //Replace . with empty string in unit as CCL doens't expect . in unit - utxos.forEach(utxo -> { - utxo.getAmount().forEach(amount -> { - String unit = amount.getUnit(); - unit = unit.replace(".", ""); - amount.setUnit(unit); - }); - }); - - return utxos; - } - - @Override - public Optional getTxOutput(String txHash, int outputIndex) { - throw new UnsupportedOperationException("Not supported"); - } -}