Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
231 stars 125 forks source link

Issues with moving a utxo locked in always_succeeds contract #678

Open devMoney71 opened 1 month ago

devMoney71 commented 1 month ago

I am a beginner who just started developing with Cardano. I am writing a javascript code to move a utxo locked in the trivial.always_succeeds contract (preprod: addr_test1wquu2gxsvfa2lfeg7ljd6yq59dmuy4up8sm02l3vhz8h9fg4q3ckq), using a reference UTXO. I managed to sent a utxo with datum "TEST" to the contract, but failed to withdraw it to my address.

Here is a part of my code.

Create a TransactionBuilder and a TxInputsBuilder:

let txBuilder = CardanoWasm.TransactionBuilder.new(txBuilderCfg);
let txInputsBuilder = CardanoWasm.TxInputsBuilder.new();

Create a TransactionInput for a transaction, and also a reference TransactionInput :

let txIn = CardanoWasm.TransactionInput.new(
    CardanoWasm.TransactionHash.from_bytes(Buffer.from(utxoHash, 'hex')),
    parseInt(utxoIndex, 10)
);
let txInRef = CardanoWasm.TransactionInput.new(
    CardanoWasm.TransactionHash.from_bytes(Buffer.from(referenceUtxoHash, 'hex')),
    parseInt(referenceUtxoIndex, 10)
);

And build the input of transction:

txInputsBuilder.add_plutus_script_input(
    plutusWitness,
    txIn,
    CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(utxoAmount))
);
txBuilder.set_inputs(txInputsBuilder);

Here plutusWitness is defined as follows:

let scriptHash = "39c520d0627aafa728f7e4dd10142b77c257813c36f57e2cb88f72a5";
let script = CardanoWasm.PlutusScriptSource.new_ref_input_with_lang_ver(
    CardanoWasm.ScriptHash.from_hex(scriptHash),
    txInRef,
    CardanoWasm.Language.new_plutus_v2()
);
let datum = CardanoWasm.DatumSource.new(CardanoWasm.PlutusData.new_bytes(Buffer.from("TEST", "utf-8")));
let redeemer = CardanoWasm.Redeemer.new(
    CardanoWasm.RedeemerTag.new_spend(),
    CardanoWasm.BigNum.from_str('0'),
    CardanoWasm.PlutusData.new_bytes(Buffer.from(datumStr, "utf-8")),
    CardanoWasm.ExUnits.new(
        CardanoWasm.BigNum.from_str("7000000"),
        CardanoWasm.BigNum.from_str("3000000000")
    )
);
let plutusWitness = CardanoWasm.PlutusWitness.new_with_ref(
        script,
    datum,
    redeemer
);

In fact, I am not sure those variables are set correctly. The hash of the script is "39c520d0627aafa728f7e4dd10142b77c257813c36f57e2cb88f72a5", I found this hash at "https://preprod.cardanoscan.io/transaction/be8217d6682be1d1888ca112896345612f0d6dec4552970188a9d1cbcf47e17b?tab=utxo".

And then, I add collateral utxo.

let collateralBuilder = CardanoWasm.TxInputsBuilder.new();
let collateralInput = CardanoWasm.TransactionInput.new(
  CardanoWasm.TransactionHash.from_bytes(Buffer.from(collateralUtxo.tx_hash, 'hex')),
  parseInt(collateralUtxo.tx_index, 10)
);
collateralBuilder.add_input(
  CardanoWasm.Address.from_bech32(public_key),
  collateralInput,
  CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(collateral_quantity))
);
txBuilder.set_collateral(collateralBuilder);

After that, I use calc_script_data_hash:

let costModel = CardanoWasm.TxBuilderConstants.plutus_vasil_cost_models().get(CardanoWasm.Language.new_plutus_v2());
let costModels = CardanoWasm.Costmdls.new();
costModels.insert(CardanoWasm.Language.new_plutus_v2(), costModel);
txBuilder.calc_script_data_hash(costModels);

Then, add change: txBuilder.add_change_if_needed(CardanoWasm.Address.from_bech32(to));

And then, build tx: let tx = txBuilder.build_tx();

And sign with payment_key (this is for collateral) and submit transaction using blockfrost api:

let txBytes = tx.to_bytes();
let signedTxBytes = signTransaction(txBytes, payment_key);
let result = await submitTransaction(signedTxBytes);

But got the following error.

{
  contents: {
    contents: {
      contents: {
        era: 'ShelleyBasedEraBabbage',
        error: [
          {
            contents: {
              contents: {
                contents: [
                  [
                    {
                      kind: 'AlonzoSpending',
                      value: {
                        item: '542077cd26ff83bc5de764be8aa3dfa985f8479c983153bbcc7ee690101cbc98#TxIx 0'
                      }
                    },
                    'dc90c02719b5b3b1d56b5f22a39225dd9da5cf4d435c048a0c8657b4'
                  ]
                ],
                tag: 'MissingRedeemers'
              },
              tag: 'AlonzoInBabbageUtxowPredFailure'
            },
            tag: 'UtxowFailure'
          },
          {
            contents: {
              contents: {
                contents: [
                  'd6483414bef2d593b18792aeb898cd6c2d88933d30e49ea234cdba87219ea719',
                  '75be5e37c2a7f07027713f0cf852aabedb84498d31182e96a4d2390dc2817d3f'
                ],
                tag: 'PPViewHashesDontMatch'
              },
              tag: 'AlonzoInBabbageUtxowPredFailure'
            },
            tag: 'UtxowFailure'
          },
          {
            contents: {
              contents: 'AlonzoInBabbageUtxoPredFailure (UtxosFailure (CollectErrors [NoRedeemer (AlonzoSpending (TxIn (TxId {unTxId = SafeHash "542077cd26ff83bc5de764be8aa3dfa985f8479c983153bbcc7ee690101cbc98"}) (TxIx 0)))]))',
              tag: 'UtxoFailure'
            },
            tag: 'UtxowFailure'
          }
        ],
        kind: 'ShelleyTxValidationError'
      },
      tag: 'TxValidationErrorInCardanoMode'
    },
    tag: 'TxCmdTxSubmitValidationError'
  },
  tag: 'TxSubmitFail'
}

I would appreciate any help you can provide on this issue. Thank you.

lisicky commented 1 month ago

Hey @devMoney71 ! You need to provide collateral inputs to not get the error "Plutus inputs are present, but no collateral inputs are added". Also builder.build() returns tx body without any witnesses that's why you got the error from cardano node, you need to use builder.build_tx()

devMoney71 commented 1 month ago

Hey @devMoney71 ! You need to provide collateral inputs to not get the error "Plutus inputs are present, but no collateral inputs are added". Also builder.build() returns tx body without any witnesses that's why you got the error from cardano node, you need to use builder.build_tx()

Thank you for your reply. I used build_tx and also add collateral as following:

let collateralBuilder = CardanoWasm.TxInputsBuilder.new();
let collateralInput = CardanoWasm.TransactionInput.new(
  CardanoWasm.TransactionHash.from_bytes(Buffer.from(collateralUtxo.tx_hash, 'hex')),
  parseInt(collateralUtxo.tx_index, 10)
);
collateralBuilder.add_input(
  CardanoWasm.Address.from_bech32(addr),
  collateralInput,
  CardanoWasm.Value.new(CardanoWasm.BigNum.from_str(quantity))
);
txBuilder.set_collateral(collateralBuilder);

But I am still getting errors.

{
  contents: {
    contents: {
      contents: {
        era: 'ShelleyBasedEraBabbage',
        error: [
          {
            contents: {
              contents: {
                contents: [
                  [
                    {
                      kind: 'AlonzoSpending',
                      value: {
                        item: '542077cd26ff83bc5de764be8aa3dfa985f8479c983153bbcc7ee690101cbc98#TxIx 0'
                      }
                    },
                    'dc90c02719b5b3b1d56b5f22a39225dd9da5cf4d435c048a0c8657b4'
                  ]
                ],
                tag: 'MissingRedeemers'
              },
              tag: 'AlonzoInBabbageUtxowPredFailure'
            },
            tag: 'UtxowFailure'
          },
          {
            contents: {
              contents: {
                contents: [
                  'd6483414bef2d593b18792aeb898cd6c2d88933d30e49ea234cdba87219ea719',
                  '75be5e37c2a7f07027713f0cf852aabedb84498d31182e96a4d2390dc2817d3f'
                ],
                tag: 'PPViewHashesDontMatch'
              },
              tag: 'AlonzoInBabbageUtxowPredFailure'
            },
            tag: 'UtxowFailure'
          },
          {
            contents: {
              contents: 'AlonzoInBabbageUtxoPredFailure (UtxosFailure (CollectErrors [NoRedeemer (AlonzoSpending (TxIn (TxId {unTxId = SafeHash "542077cd26ff83bc5de764be8aa3dfa985f8479c983153bbcc7ee690101cbc98"}) (TxIx 0)))]))',
              tag: 'UtxoFailure'
            },
            tag: 'UtxowFailure'
          }
        ],
        kind: 'ShelleyTxValidationError'
      },
      tag: 'TxValidationErrorInCardanoMode'
    },
    tag: 'TxCmdTxSubmitValidationError'
  },
  tag: 'TxSubmitFail'
}
lisicky commented 1 month ago

According to errors you don't have a redeemer in your witnesses set, do not modify witnesses set after you built a tx except vkey and bootstrap witnesses

lisicky commented 1 week ago

Hey @devMoney71 ! Did it helped ?