bloxbean / cardano-client-lib

Cardano client library in Java
https://cardano-client.dev
MIT License
118 stars 47 forks source link

Plutus Blueprint annotation processor enhancements #410

Closed satran004 closed 1 month ago

satran004 commented 1 month ago
  1. In validator's getScriptAddress, use correct plutus version from preamble.
  2. Proper error message for compilation failure. For exp: compiler silently fails when blueprint json file is empty
  3. New 'getPlutusScript' method in Validator class
  4. Remove Lombok dependency. Generate getter/setter methods in Datum and Redeemer class
  5. Generate "DataImpl" class for both Datum and Redeemer. The new Impl class extends generated Datum/Redeemer and implements Data interface with toPlutusData and fromPlutusData method.
  6. A new annotation @ExtendWith(Clazz) to extend the validator class to additional tx building and submission capabilities like lock, unlock, deploy etc. Example: LockUnlockValidatorExtender

    Example: Generate Validator class and extend to LockUnlockValidatorExtender to provide additional functionalities

    
    @Blueprint(fileInResources = "blueprint/_helloworld.json", packageName = "org.test.demo")
    @ExtendWith(LockUnlockValidatorExtender.class)
    public interface HelloWorldBlueprint {

}


 Sample code for usage of `Validator` class
   String address = account.baseAddress();

    //validator
    HelloWorldValidator validator = new HelloWorldValidator(Networks.testnet())
            .withBackendService(backendService);

    //Deploy the contract to create reference input
    var txResult = validator.deploy(address, SignerProviders.signerFrom(account), (msg) -> System.out.println(msg));
    validator.withReferenceTxInput(txResult.getTxHash(), 0);
    System.out.println(txResult);

    //Datum to lock
    var helloWorldDatum = new HelloWorldDatumImpl();
    helloWorldDatum.setOwner(account.getBaseAddress().getPaymentCredentialHash().get());

    var amounts = List.of(Amount.ada(20));

    //Lock the contract
    var txResult1 = validator.lock(address, amounts, helloWorldDatum, SignerProviders.signerFrom(account), s -> System.out.println(s));
    System.out.println(txResult1);

    //Unlock the contract
    var receiver = new PubKeyReceiver(account.baseAddress(), Amount.ada(20));
    var redeemer = new HelloWorldRedeemerImpl();
    redeemer.setMsg("Hello, World!".getBytes(StandardCharsets.UTF_8));

    var txResult2 = validator.unlock(List.of(receiver), account.baseAddress(), helloWorldDatum,  redeemer,
            SignerProviders.signerFrom(account), null, s -> System.out.println(s), account.getBaseAddress());


 **TODO**

 - [x] Handle parameterized contract
 - [x] Generate one copy of a Datum/Redeemer class instead of multiple versions if same datum is used in multiple validators.
 - [x] Handle blueprint json without ref schema/definitions
 - [x] Handle Enum
 - [x] Verify new api classes in Validator impl. PubKeyReceiver, ChangeReceiver etc.