Skip to content

Improve Withdraw System #403

Description

@ClemensLey

Withdraw the Entire Amount

1. Update all Payments in on Tx

class Withdraw extends Contract {
  static exec(payments: Payment[]) {
    payments.forEach((payment) => payment.setAmount(0)
  }
}

To create a tx that spends all payments in one tx we have to call

const payments = ...
const { tx } = await computer.encode({
  exp: `Withdraw.exec([p0, ..., pn])`,
  env: {
    p0: payments[0]._rev,
    ...,
    pn: payments[n]._rev
  }
  fund: false
})

2 Add other UTXOs

The user want's to spend their entire amount to . We need to determine all "normal" utxos of the users, using

const utxos = await computer.db.wallet.getFormattedUtxos(computer.getAddress())
let amount = 0
const tx = new Transaction()
utxos.forEach((utxo) => {
  tx.addInput(utxo)
  amount += utxo.satoshis
})

We then have to update the amount in the change output

await computer.fund(tx) // adds change output

// update change output to go to <new-address>
const changeOutputIndex = tx.outs.length - 1
tx.updateOutput(changeOutputIndex, { scriptPubKey: <new-address> })

await computer.sign(tx)
await computer.broadcast(tx)

3. Move to the Wallet Drawer

The default wallet drawer in Components already deals with Payment objects. We should make sure that the wallet component works well both with and without a Payment modSpec. If the modSpec is passed in we have to deal with Payment objects, otherwise not.

In both cases the wallet drawer should have a form titled "withdraw". The form has a text filed "address" where the user can input and a button "withdraw". When the button is pressed a transaction is built that sends all coins to that address. If the modSpec was passed in, we use the system described above. If modSpec is not passed we might be able to use an empty transaction instead of the one created with computer.encode above.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions