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

Unable to build a "Bid transaction" of an english auction with lucid API #215

Closed alan-suchanek closed 10 months ago

alan-suchanek commented 10 months ago

I'm trying to implement an english auction in lucid, following the Plutus Pioneer Program example, as documented here: https://plutus-pioneer-program.readthedocs.io/en/latest/pioneer/week1.html or here: https://travishorn.github.io/ppp-notes/01-eutxo-english-auction/04-auction-contract-eutxo-model/

In accordance with the example a bid transaction should have 2 inputs and 2 outputs: Input 1: The auction (smart contract) EUTxO (For example the value is 100 ADA - the previous highest bid) Input 2: A EUTxO representing my bid. (For example the value is 200 ADA) Output 1: The auction EUTxO again, but with the value changed. (The new value is 200 ADA) Output 2: 100 ADA to previous highest bidder. He is getting his bid back.

I'm not able to model this with the lucid.newTx() and the three methods available: payToContract, payToAddress and collectFrom

Am I missing anything. If so please provide an example how to model english auction bid transaction (could be added to the basic examples). If I'm right and this is not possible, could the collectFrom method be extended with third optional parameter - address of the wallet which should redeem smart contracts unspent transaction output.

Any feedback would be appreciated. Thank you!

zing-rsa commented 10 months ago

.payToAddress() is exactly what you need. The bidders address will be stored inside the datum at the script address, so you will:

  1. read that datum
  2. .collectFrom() your utxo and the contract utxo
  3. .payToContract() the new contract utxo
  4. .payToAddress() using the address and amount of lovelace from the old datum

payToContract configures the Tx to create a utxo with some assets at the given address(same as payToContract).

alan-suchanek commented 10 months ago

Thank you for the prompt reply, it worked as you suggested.