Skip to content

Instantly share code, notes, and snippets.

@satran004
satran004 / MultisigAddressExample.java
Created October 29, 2023 13:18
Spend from a multisig address
import com.bloxbean.cardano.client.account.Account;
import com.bloxbean.cardano.client.address.AddressProvider;
import com.bloxbean.cardano.client.api.model.Amount;
import com.bloxbean.cardano.client.backend.api.BackendService;
import com.bloxbean.cardano.client.backend.blockfrost.common.Constants;
import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService;
import com.bloxbean.cardano.client.cip.cip20.MessageMetadata;
import com.bloxbean.cardano.client.common.model.Networks;
import com.bloxbean.cardano.client.crypto.VerificationKey;
import com.bloxbean.cardano.client.example.Constant;
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;
@satran004
satran004 / contracttest.java
Created August 10, 2023 13:18
QuickTx - Contract Test
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;
Output output = Output.builder()
.address(senderAddress)
.assetName(LOVELACE)
.qty(adaToLovelace(1))
.build();
MessageMetadata metadata = MessageMetadata.create()
.add("Sender == Receiver");
TxBuilder txBuilder = output.outputBuilder()
@Test
public void sendToSelf() {
Tx tx1 = new Tx()
.payToAddress(sender1Addr, Amount.ada(1))
.attachMetadata(MessageMetadata.create().add("Send to self"))
.from(sender1Addr);
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService);
Result<String> result = quickTxBuilder
.compose(tx1)
String auxDataCbor = "d90103a100a119074eaa6840636f6e74657874781e68747470733a2f2f773369642e6f72672f6f70656e6261646765732f76326b6465736372697074696f6e75555a4820426c6f636b636861696e2043656e74657265656d61696c76636c617564696f2e746573736f6e6540757a682e6368626964783c68747470733a2f2f66696c65732e6966692e757a682e63682f62646c742f636572742f6f70656e6261646765732f6973737565725f69642e6a736f6e66697373756572783a50726f662e2044722e20436c617564696f204a2e20546573736f6e652c2043686169726d616e20262041636164656d6963204469726563746f72646e616d657819556e6976657273697479206f66205a75726963682c20555a48697075626c69634b6579606474797065664973737565726375726c781e68747470733a2f2f7777772e626c6f636b636861696e2e757a682e63682f6c766572696669636174696f6e6b5369676e65644261646765";
byte[] auxHashBytes = Blake2bUtil.blake2bHash256(HexUtil.decodeHexString(auxDataCbor));
System.out.println(HexUtil.encodeHexString(auxHashBytes));
@satran004
satran004 / LocalTxMonitorSample.java
Last active December 13, 2022 03:24
Local Tx Monitor Example
import com.bloxbean.cardano.client.exception.CborDeserializationException;
import com.bloxbean.cardano.client.transaction.spec.Transaction;
import com.bloxbean.cardano.client.transaction.util.TransactionUtil;
import com.bloxbean.cardano.yaci.core.common.Constants;
import com.bloxbean.cardano.yaci.helper.LocalClientProvider;
import com.bloxbean.cardano.yaci.helper.LocalTxMonitorClient;
import com.bloxbean.cardano.yaci.helper.model.MempoolStatus;
import java.util.List;
@satran004
satran004 / ReactiveWindow.java
Last active November 3, 2022 12:47
Connect to Cardano node through Yaci reactive api and aggregations for 5 min windows
import com.bloxbean.cardano.yaci.core.common.NetworkType;
import com.bloxbean.cardano.yaci.core.reactive.BlockStreamer;
import java.text.DecimalFormat;
import java.time.Duration;
import java.util.Date;
import java.util.TimeZone;
/**
* Get incoming block data through reactive BlockStreamer api
TxBuilder contractTxBuilder = output.outputBuilder()
.buildInputs(InputBuilders.createFromUtxos(List.of(scriptUtxo)))
.andThen(InputBuilders.referenceInputsFromUtxos(List.of(refScriptUtxo))) //CIP-31
.andThen(CollateralBuilders.collateralOutputs(senderAddress, Lists.newArrayList(collateralUtxos))) //CIP-40
.andThen(ScriptCallContextProviders.createFromScriptCallContext(scriptCallContext))
.andThen((context, txn) -> {
//Calculate ExUnit. It should be done before balanceTx for accurate fee calculation
//update estimate ExUnits
ExUnits estimatedExUnits;
try {
Output output = Output.builder()
.address(senderAddress)
.assetName(LOVELACE)
.qty(claimAmount)
.build();
ScriptCallContext scriptCallContext = ScriptCallContext
.builder()
.script(sumScript)
.exUnits(ExUnits.builder() //Exact exUnits will be calculated later