Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I chain transaction? #145

Open
jeffhappily opened this issue Jan 22, 2023 · 10 comments
Open

How can I chain transaction? #145

jeffhappily opened this issue Jan 22, 2023 · 10 comments

Comments

@jeffhappily
Copy link

I'm trying to use lucid to chain transaction, meaning to create a transaction that consumes output from another transaction in mempool, which is yet to be confirmed on-chain, it is possible to do it on Cardano, but it's not obvious how can I do it using lucid.

I tried to do it but I got this error

{
  "CannotCreateEvaluationContext": {
    "reason": "Unknown transaction input (missing from UTxO set): b7e37f8ebb8a113517055d0c637bfe7d4e266e816c83c034a25af70b31b1826a#0"
  }
}

The error seems to be coming from the call to construct function

lucid/src/lucid/tx.ts

Lines 584 to 588 in 4c92f56

await this.txBuilder.construct(
utxos,
changeAddress,
options?.nativeUplc === undefined ? true : options?.nativeUplc,
),
and the error message seems to be coming from Ogmios, but since the tx builder is from wasm, I'm not entirely sure.

But if this error truly comes from Ogmios, I do know that they support transaction chaining, in their EvaluateTx API, they take an argument called additionalUtxoSet, not sure if Lucid supports it or not.

Appreciate if I could get help on this issue.

@alessandrokonrad
Copy link
Contributor

alessandrokonrad commented Jan 22, 2023

Hey, what Lucid version are you on? I'm not sure if you are on an older version or have nativeUplc set to false. Lucid can run plutus core itself now, then you should not get the error message from ogmios. Chaining should work fine then.

@jeffhappily
Copy link
Author

@alessandrokonrad Removing the nativeUplc does get rid of the error, I got into another error which says something like TranslationLogicMissingInput, I'll look more into it later, but does this mean I have to somehow specify the additionalUtxoSet? Or do you have a simple example where how can transaction chaining be done in lucid?

Here's what I'm currently doing:

  const tx = await lucid
    .newTx()
    ...
    .complete();

  const signedTx = await tx.sign().complete();

  const h = signedTx.toHash();
  const i = signedTx.txSigned
    .to_js_value()
    .body.outputs.findIndex((out: any) => out.address === xxx);

  const outputJs = signedTx.txSigned.to_js_value().body.outputs[i];
  const output = signedTx.txSigned.body().outputs().get(i);

  const tx2 = lucid
    .newTx()
    .collectFrom(
      [
        {
          txHash: h,
          outputIndex: i,
          assets: valueToAssets(output.amount()),
          address: outputJs.address,
          datum: Buffer.from(
            signedTx.txSigned
              .body()
              .outputs()
              .get(i)
              .datum()
              .as_data()
              .get()
              .to_bytes()
          ).toString("hex"),
        },
      ],
      unitData
    )
    ...

@alessandrokonrad
Copy link
Contributor

TranslationLogicMissingInput means you miss the input. Are you submitting through blockfrost?
I was planning to add a more high level API for tx chaining, I thought some basic tx chaining is already possible, but there are some other complications I just realized. It doesn't work in all scenarios yet.

@jeffhappily
Copy link
Author

Yup, the provider is blockfrost, is it because blockfrost doesn't support tx chaining yet?

@jeffhappily
Copy link
Author

@alessandrokonrad In that case, would switching to another provider like Kupmios resolve this issue? Or it has nothing to do with the provider itself?

@alessandrokonrad
Copy link
Contributor

alessandrokonrad commented Jan 26, 2023

I think blockfrost actually supports chaining, but you can check out the the Kupmios provider too. I think you just have to try out what works and what not. The difficulty is just coin selection. Lucid may select UTxOs that you already spent. You can disable coin selection, but that makes it harder of course. As I said I may gonna write something more high level for this. If you have ideas for that too, feel free to share and contribute to Lucid.

@lemmon-714
Copy link

@alessandrokonrad a high-level API for tx-chaining would be quite awesome! :O

@lemmon-714
Copy link

I looked into it, and thought it might have to rest on keeping track of the spent/created outputs somewhere, and replace one or some of the utxo-getters. Rn I'm trying to wrap my head around how to best achieve this, as it appears this varies based on wallet/wallet-selection-method.

Will create a pull request once I got something.

@SynthLuvr
Copy link

Lucid may select UTxOs that you already spent

We get around this by tracking which UTxOs are spent and then removing them from coin selection. We're currently doing this tracking outside of the Lucid instance, but I think Lucid could be updated to track this internally within Lucid. The main issue would be that Lucid typically queries for new UTxOs during building of transactions, so any spent UTxOs would be re-added.

I think blockfrost actually supports chaining

Can confirm that Blockfrost supports chaining, up to a limit. We got to about 3 transactions chained before Blockfrost throws an error.

@will-break-it
Copy link

will-break-it commented Oct 30, 2023

Added an API that simplifies chaining txs here.
#231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants