From 58aa36b7e706783c649b5c689e27b45219ee3a57 Mon Sep 17 00:00:00 2001 From: Pietralberto Mazza <18440657+altafan@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:08:53 +0100 Subject: [PATCH] Hotfix: restore vtxos watch & handle dust when crafting pool tx (#89) * Add check before printing log * Check for uncofirmed or confirmed utxos (vtxos) * Handle dust amount when crafting pool tx * Clean --- asp/internal/core/application/service.go | 4 +- .../infrastructure/ocean-wallet/service.go | 3 +- .../tx-builder/covenant/builder.go | 77 +++++++++++-------- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/asp/internal/core/application/service.go b/asp/internal/core/application/service.go index b246cfd..8d52c76 100644 --- a/asp/internal/core/application/service.go +++ b/asp/internal/core/application/service.go @@ -375,7 +375,9 @@ func (s *service) listenToRedemptions() { time.Sleep(100 * time.Millisecond) continue } - log.Debugf("redeemed %d vtxos", len(vtxos)) + if len(vtxos) > 0 { + log.Debugf("redeemed %d vtxos", len(vtxos)) + } break } } diff --git a/asp/internal/infrastructure/ocean-wallet/service.go b/asp/internal/infrastructure/ocean-wallet/service.go index 9b744f4..96557a8 100644 --- a/asp/internal/infrastructure/ocean-wallet/service.go +++ b/asp/internal/infrastructure/ocean-wallet/service.go @@ -107,7 +107,8 @@ func (s *service) listenToNotificaitons() { return } - if msg.GetEventType() != pb.UtxoEventType_UTXO_EVENT_TYPE_NEW { + if msg.GetEventType() != pb.UtxoEventType_UTXO_EVENT_TYPE_NEW && + msg.GetEventType() != pb.UtxoEventType_UTXO_EVENT_TYPE_CONFIRMED { continue } vtxos := toVtxos(msg.GetUtxos()) diff --git a/asp/internal/infrastructure/tx-builder/covenant/builder.go b/asp/internal/infrastructure/tx-builder/covenant/builder.go index 5f57559..b1a8dad 100644 --- a/asp/internal/infrastructure/tx-builder/covenant/builder.go +++ b/asp/internal/infrastructure/tx-builder/covenant/builder.go @@ -17,6 +17,7 @@ import ( const ( connectorAmount = uint64(450) + dustLimit = uint64(450) ) type txBuilder struct { @@ -184,12 +185,18 @@ func (b *txBuilder) createPoolTx( return nil, err } + var dust uint64 if change > 0 { - outputs = append(outputs, psetv2.OutputArgs{ - Asset: b.net.AssetID, - Amount: change, - Script: aspScript, - }) + if change < dustLimit { + dust = change + change = 0 + } else { + outputs = append(outputs, psetv2.OutputArgs{ + Asset: b.net.AssetID, + Amount: change, + Script: aspScript, + }) + } } ptx, err := psetv2.New(nil, outputs, nil) @@ -216,37 +223,45 @@ func (b *txBuilder) createPoolTx( return nil, err } - if feeAmount == change { - // fees = change, remove change output - ptx.Outputs = ptx.Outputs[:len(ptx.Outputs)-1] - } else if feeAmount < change { - // change covers the fees, reduce change amount - ptx.Outputs[len(ptx.Outputs)-1].Value = change - feeAmount + if dust > feeAmount { + feeAmount = dust } else { - // change is not enough to cover fees, re-select utxos - if change > 0 { - // remove change output if present - ptx.Outputs = ptx.Outputs[:len(ptx.Outputs)-1] - } - newUtxos, change, err := b.wallet.SelectUtxos(ctx, b.net.AssetID, feeAmount-change) - if err != nil { - return nil, err - } + feeAmount += dust + } - if change > 0 { - if err := updater.AddOutputs([]psetv2.OutputArgs{ - { - Asset: b.net.AssetID, - Amount: change, - Script: aspScript, - }, - }); err != nil { + if dust == 0 { + if feeAmount == change { + // fees = change, remove change output + ptx.Outputs = ptx.Outputs[:len(ptx.Outputs)-1] + } else if feeAmount < change { + // change covers the fees, reduce change amount + ptx.Outputs[len(ptx.Outputs)-1].Value = change - feeAmount + } else { + // change is not enough to cover fees, re-select utxos + if change > 0 { + // remove change output if present + ptx.Outputs = ptx.Outputs[:len(ptx.Outputs)-1] + } + newUtxos, change, err := b.wallet.SelectUtxos(ctx, b.net.AssetID, feeAmount-change) + if err != nil { return nil, err } - } - if err := addInputs(updater, newUtxos); err != nil { - return nil, err + if change > 0 { + if err := updater.AddOutputs([]psetv2.OutputArgs{ + { + Asset: b.net.AssetID, + Amount: change, + Script: aspScript, + }, + }); err != nil { + return nil, err + } + } + + if err := addInputs(updater, newUtxos); err != nil { + return nil, err + } } }