-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Plutus V3 cost model
Refactor UTXO check logic by moving it to BaseTest for reuse. Introduce support for detecting and evaluating Plutus V3 scripts in transactions, ensuring all three versions of Plutus scripts (V1, V2, V3) can be handled.
- Loading branch information
Showing
4 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/integrationTest/java/com/bloxbean/cardano/aiken/tx/evaluator/AlwaysTrueScriptV3Test.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.bloxbean.cardano.aiken.tx.evaluator; | ||
|
||
import com.bloxbean.cardano.aiken.AikenTransactionEvaluator; | ||
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.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.PlutusV3Script; | ||
import com.bloxbean.cardano.client.quicktx.QuickTxBuilder; | ||
import com.bloxbean.cardano.client.quicktx.ScriptTx; | ||
import com.bloxbean.cardano.client.quicktx.Tx; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Optional; | ||
import java.util.Random; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* Simple script tx example, which | ||
* 1. locks funds to a script address | ||
* 2. unlocks funds from the script address | ||
*/ | ||
public class AlwaysTrueScriptV3Test extends BaseTest { | ||
|
||
@Test | ||
void alwaysTrueScript() throws ApiException, InterruptedException { | ||
PlutusV3Script plutusScript = PlutusV3Script.builder() | ||
.type("PlutusScriptV3") | ||
.cborHex("46450101002499") | ||
.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 | ||
|
||
System.out.println("Trying to lock fund --------"); | ||
lockFund(scriptAddress, scriptAmt, plutusData); | ||
|
||
System.out.println("Trying to unlock fund --------"); | ||
//Script tx | ||
Optional<Utxo> optionalUtxo = ScriptUtxoFinders.findFirstByInlineDatum(utxoSupplier, scriptAddress, plutusData); | ||
ScriptTx scriptTx = new ScriptTx() | ||
.collectFrom(optionalUtxo.get(), plutusData) | ||
.payToAddress(senderAddress2, Amount.lovelace(scriptAmt)) | ||
.attachSpendingValidator(plutusScript); | ||
|
||
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService); | ||
Result<String> result1 = quickTxBuilder.compose(scriptTx) | ||
.feePayer(senderAddress) | ||
.withSigner(SignerProviders.signerFrom(sender)) | ||
.withTxEvaluator(new AikenTransactionEvaluator(backendService)) | ||
.completeAndWait(System.out::println); | ||
|
||
System.out.println(result1.getResponse()); | ||
assertTrue(result1.isSuccessful()); | ||
} | ||
|
||
private void lockFund(String scriptAddress, BigInteger scriptAmt, BigIntPlutusData plutusData) { | ||
Tx tx = new Tx(); | ||
tx.payToContract(scriptAddress, Amount.lovelace(scriptAmt), plutusData) | ||
.from(senderAddress2); | ||
|
||
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService); | ||
Result<String> result = quickTxBuilder.compose(tx) | ||
.withSigner(SignerProviders.signerFrom(sender2)) | ||
.completeAndWait(System.out::println); | ||
|
||
System.out.println(result.getResponse()); | ||
checkIfUtxoAvailable(result.getValue(), scriptAddress); | ||
} | ||
} |
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
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
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