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.

Home Page:https://lucid.spacebudz.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

alan-suchanek opened this issue · comments

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

  • payToContract (works OK to model my bid)
  • payToAddress (seems useless in this scenario)
  • collectFrom (redeems unspent transaction output of the smart contract, but to my wallet, not to the wallet of the previous bidder)

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!

commented

.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).

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