Created
March 4, 2025 08:49
-
-
Save satran004/f58ad7d68ccd03fb1da2c4c13029897f to your computer and use it in GitHub Desktop.
NativeScript-Lock-UnLock
This file contains hidden or 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.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.backend.api.BackendService; | |
| import com.bloxbean.cardano.client.backend.api.DefaultUtxoSupplier; | |
| import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService; | |
| import com.bloxbean.cardano.client.coinselection.impl.DefaultUtxoSelector; | |
| import com.bloxbean.cardano.client.common.model.Networks; | |
| import com.bloxbean.cardano.client.crypto.VerificationKey; | |
| import com.bloxbean.cardano.client.crypto.cip1852.DerivationPath; | |
| import com.bloxbean.cardano.client.exception.CborSerializationException; | |
| import com.bloxbean.cardano.client.function.helper.SignerProviders; | |
| import com.bloxbean.cardano.client.quicktx.QuickTxBuilder; | |
| import com.bloxbean.cardano.client.quicktx.Tx; | |
| import com.bloxbean.cardano.client.transaction.spec.Asset; | |
| import com.bloxbean.cardano.client.transaction.spec.script.ScriptAll; | |
| import com.bloxbean.cardano.client.transaction.spec.script.ScriptPubkey; | |
| import java.math.BigInteger; | |
| import java.util.List; | |
| import static com.bloxbean.cardano.yaci.core.util.Constants.LOVELACE; | |
| public class NativeScriptLockTest { | |
| private static String mnemonic = "test test test test test test test test test test test test test test test test test test test test test test test sauce"; | |
| private static Account account1 = new Account(Networks.testnet(), mnemonic, DerivationPath.createExternalAddressDerivationPathForAccount(0)); | |
| private static Account account2 = new Account(Networks.testnet(), mnemonic, DerivationPath.createExternalAddressDerivationPathForAccount(1)); | |
| private static String receiverAddress = "addr_test1qzuwfzavnlt6n9hzxnycxhkhl27ut2s53hfnl6qy6hkj0hsha58vshf9fv8mn5g8rsfyqrhefufq5uw6c9836ay448vsq8axcv"; | |
| private static BackendService backendService = new BFBackendService(" http://localhost:8080/api/v1/", "dummy key"); | |
| private ScriptAll nativeScript; | |
| private String mintScriptAddress; | |
| public NativeScriptLockTest() throws CborSerializationException { | |
| nativeScript = new ScriptAll(); | |
| ScriptPubkey scriptPubkeyA = ScriptPubkey.create(VerificationKey.create(account1.publicKeyBytes())); | |
| ScriptPubkey scriptPubkeyB = ScriptPubkey.create(VerificationKey.create(account2.publicKeyBytes())); | |
| nativeScript.addScript(scriptPubkeyA); | |
| nativeScript.addScript(scriptPubkeyB); | |
| mintScriptAddress = AddressProvider.getEntAddress(nativeScript, Networks.testnet()).toBech32(); | |
| } | |
| public void lockTx() { | |
| //Mint and lock fund | |
| Tx tx = new Tx() | |
| .payToAddress(mintScriptAddress, Amount.ada(20)) | |
| .mintAssets(nativeScript, new Asset("TestToken", BigInteger.ONE), mintScriptAddress) | |
| .from(account1.baseAddress()); | |
| var result = new QuickTxBuilder(backendService) | |
| .compose(tx) | |
| .withSigner(SignerProviders.signerFrom(account1)) | |
| .withSigner(SignerProviders.signerFrom(account2)) | |
| .completeAndWait(System.out::println); | |
| System.out.println(result); | |
| } | |
| public void unlockTx() throws ApiException { | |
| var utxoSelector = new DefaultUtxoSelector(new DefaultUtxoSupplier(backendService.getUtxoService())); | |
| var treasuryUtxo = utxoSelector.findFirst(mintScriptAddress, utxo -> true) //some dummy predicate | |
| .orElseThrow(); | |
| var lockedUtxoAmt = treasuryUtxo.getAmount() | |
| .stream() | |
| .filter(amount -> amount.getUnit().equals(LOVELACE)) | |
| .findFirst() | |
| .map(amount -> amount.getQuantity()).orElse(BigInteger.ZERO); | |
| //We alredy know there is also a TestToken with quantity 1, so use it directly in mintAssets | |
| Tx tx = new Tx() | |
| .collectFrom(List.of(treasuryUtxo)) | |
| .payToAddress(receiverAddress, Amount.lovelace(lockedUtxoAmt)) | |
| .mintAssets(nativeScript, new Asset("TestToken", BigInteger.ONE.negate())) | |
| .from(mintScriptAddress); | |
| var result = new QuickTxBuilder(backendService) | |
| .compose(tx) | |
| .feePayer(receiverAddress) | |
| .withSigner(SignerProviders.signerFrom(account1)) | |
| .withSigner(SignerProviders.signerFrom(account2)) | |
| .completeAndWait(System.out::println); | |
| System.out.println(result); | |
| } | |
| public static void main(String[] args) throws Exception { | |
| NativeScriptLockTest lockTest = new NativeScriptLockTest(); | |
| lockTest.lockTx(); | |
| // lockTest.unlockTx(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment