spacebudz / lucid

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.
https://lucid.spacebudz.io
MIT License
336 stars 133 forks source link

feat: chaining of txs #231

Open will-break-it opened 8 months ago

will-break-it commented 8 months ago

Example: One can chain any complete transaction by using the newly available chain API, which takes a function to select one or more transaction output(s) of tx1 that shall be used an inputs for tx2. These outputs can be script or wallet outputs.

const tx1 = await lucid.newTx()
   .payToAddress('addr_test...', { lovelace: 2_000_000n })
   .complete();

const tx2 = await tx1
    .chain(utxos => utxos.find(({ address }) => address === 'addr_test...')!) // filter tx1 outputs to be chained by some predicate of your choice
    .payToAddress('addr_test...', { lovelace: 2_000_000n })
    .payToAddress('addr_test...', { lovelace: 2_000_000n })
    .complete();
gavinharris-dev commented 4 months ago

Hi @will991 - When chaining do you filter out any 'spent' UTxOs from the UTxO set? I appreciate that this is not an RFC, however I was thinking that the complete method should (with an option):

  1. Remove any UTxO(s) that have been spent within the Completed Transaction;
  2. Add into the UTxO set any new UTxO(s) that would be created at the Wallet Address

Node: This is all predicated on the idea that with Lucid selectWallet* methods configure Lucid with a single 'address'.

will-break-it commented 4 months ago

@gavinharris-dev The current state of available UTxOs is stored here. And it is updated based on the Tx outputs matched by payment credentials here.

Hope that helps!