Created
August 15, 2023 13:21
-
-
Save satran004/b9d0ecfb4fe78f3d4d680d4b508604b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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.backend.api.BackendService; | |
import com.bloxbean.cardano.client.backend.api.DefaultProtocolParamsSupplier; | |
import com.bloxbean.cardano.client.backend.api.DefaultUtxoSupplier; | |
import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService; | |
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.plutus.spec.BigIntPlutusData; | |
import com.bloxbean.cardano.client.plutus.spec.BytesPlutusData; | |
import com.bloxbean.cardano.client.plutus.spec.ConstrPlutusData; | |
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 java.util.List; | |
import java.util.Optional; | |
public class ContractTest { | |
BackendService backendService = new BFBackendService("http://localhost:8080/api/v1/", ""); | |
public void contractExec() throws ApiException { | |
//Network network = new Network(0b0000, 42); | |
DefaultUtxoSupplier utxoSupplier = new DefaultUtxoSupplier(backendService.getUtxoService()); | |
Account senderAccount = | |
new Account( | |
Networks.testnet(), | |
"damp wish scrub sentence vibrant gauge tumble raven game extend winner acid side amused vote edge affair buzz hospital slogan patient drum day vital"); | |
String senderAddress = senderAccount.baseAddress(); | |
System.out.println(senderAddress); | |
String cborHexScript = "590183590180010000323232323232323232322223232533300a3232533300c3370e900100089919299980719b87480000044ccdda24412102c66e7d8966b5c555af5805989da9fbf8db95e15631ce358c3a1710c96267906300488120aadf7de782034fbe3d3db2cb13c0cd91bf41cb08fac7bd61d54453cf6e82b45000488140dc4dc264a9fef17a3f253449cf8c397ab6f16fb3d63d86940b5586823dfd02ae3b461bb4336b5ecbaefd6627aa922efc048fec0c881c10c4c9428fca69c132a200132324a26eb4c04c004c030018c030014c0280085281805000998031804001a40042930b1900299299980519b87480000044c8c94ccc03cc04400852616375c601e002601000a2c60100086400664a66601266e1d20000011533300c300700314985854ccc024cdc3a400400226464a66601c60200042930b1bad300e001300700316300700233001001480008888cccc01ccdc38008018059199980280299b8000448008c0340040080088c014dd5000918019baa0015734aae7555cf2ab9f5742ae881"; | |
PlutusV2Script plutusScript = | |
PlutusV2Script.builder() | |
.cborHex(cborHexScript) | |
.build(); | |
String scriptAddress = AddressProvider.getEntAddress(plutusScript, Networks.testnet()).toBech32(); | |
System.out.println("Script Address >> " + scriptAddress); | |
byte[] datumBytes = new byte[28]; | |
ConstrPlutusData plutusData = ConstrPlutusData.of(0, BytesPlutusData.of(datumBytes)); | |
ConstrPlutusData redeemer = ConstrPlutusData.of(1, BigIntPlutusData.of(10)); | |
Tx tx = new Tx(); | |
tx.payToContract(scriptAddress, List.of(Amount.ada(1000.0)), plutusData) | |
.from(senderAddress); | |
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService); | |
Result<String> result = | |
quickTxBuilder | |
.compose(tx) | |
.feePayer(senderAddress) | |
.withSigner(SignerProviders.signerFrom(senderAccount)) | |
.completeAndWait(System.out::println); | |
System.out.println("Result " + result.isSuccessful()); | |
if (result.isSuccessful()) | |
checkIfUtxoAvailable(result.getValue(), scriptAddress); | |
Utxo sumUtxo = ScriptUtxoFinders.findFirstByInlineDatum(utxoSupplier, scriptAddress, plutusData).get(); | |
System.out.println(sumUtxo); | |
ScriptTx scriptTx = | |
new ScriptTx() | |
.collectFrom(sumUtxo, redeemer) | |
.attachSpendingValidator(plutusScript) | |
.payToAddress(senderAddress, Amount.ada(9.0)) | |
.withChangeAddress(scriptAddress, plutusData); | |
Result<String> result1 = | |
quickTxBuilder | |
.compose(scriptTx) | |
.feePayer(senderAddress) | |
.withSigner(SignerProviders.signerFrom(senderAccount)) | |
.withTxEvaluator( | |
new AikenTransactionEvaluator( | |
utxoSupplier, | |
new DefaultProtocolParamsSupplier(backendService.getEpochService()))) | |
.withTxInspector(transaction -> System.out.println(transaction)) | |
.completeAndWait(System.out::println); | |
System.out.println(result1.getResponse()); | |
System.out.println("Result2 " + result1.isSuccessful()); | |
} | |
protected void checkIfUtxoAvailable(String txHash, String address) { | |
Optional<Utxo> utxo = Optional.empty(); | |
int count = 0; | |
while (utxo.isEmpty()) { | |
if (count++ >= 20) | |
break; | |
List<Utxo> utxos = new DefaultUtxoSupplier(backendService.getUtxoService()).getAll(address); | |
utxo = utxos.stream().filter(u -> u.getTxHash().equals(txHash)) | |
.findFirst(); | |
System.out.println("Try to get new output... txhash: " + txHash); | |
try { | |
Thread.sleep(1000); | |
} catch (Exception e) {} | |
} | |
} | |
public static void main(String[] args) throws ApiException { | |
ContractTest test = new ContractTest(); | |
test.contractExec(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment