mirror of
https://github.com/aljazceru/ark.git
synced 2026-02-21 02:44:19 +01:00
* api-spec: move the api-spec to root and init go.mod * go mod tidy * move buf files in the root as well * gh action for api-spec changes only * gh action for api-spec on push and pr * introduce go.work and remove all replaces * solve dependencies and force btcd/btcec@v2.3.3 * go work sync * force btcd/btcec@v2.3.3 * go mod tidy
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package txbuilder
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/ark-network/ark/server/internal/core/domain"
|
|
"github.com/ark-network/ark/server/internal/core/ports"
|
|
)
|
|
|
|
func (b *txBuilder) selectUtxos(ctx context.Context, sweptRounds []domain.Round, amount uint64) ([]ports.TxInput, uint64, error) {
|
|
selectedConnectorsUtxos := make([]ports.TxInput, 0)
|
|
selectedConnectorsAmount := uint64(0)
|
|
|
|
for _, round := range sweptRounds {
|
|
if selectedConnectorsAmount >= amount {
|
|
break
|
|
}
|
|
connectors, err := b.wallet.ListConnectorUtxos(ctx, round.ConnectorAddress)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
for _, connector := range connectors {
|
|
if selectedConnectorsAmount >= amount {
|
|
break
|
|
}
|
|
|
|
selectedConnectorsUtxos = append(selectedConnectorsUtxos, connector)
|
|
selectedConnectorsAmount += connector.GetValue()
|
|
}
|
|
}
|
|
|
|
if len(selectedConnectorsUtxos) > 0 {
|
|
if err := b.wallet.LockConnectorUtxos(ctx, castToOutpoints(selectedConnectorsUtxos)); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
}
|
|
|
|
if selectedConnectorsAmount >= amount {
|
|
return selectedConnectorsUtxos, selectedConnectorsAmount - amount, nil
|
|
}
|
|
|
|
utxos, change, err := b.wallet.SelectUtxos(ctx, b.onchainNetwork().AssetID, amount-selectedConnectorsAmount)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return append(selectedConnectorsUtxos, utxos...), change, nil
|
|
}
|
|
|
|
func castToOutpoints(inputs []ports.TxInput) []ports.TxOutpoint {
|
|
outpoints := make([]ports.TxOutpoint, 0, len(inputs))
|
|
for _, input := range inputs {
|
|
outpoints = append(outpoints, input)
|
|
}
|
|
return outpoints
|
|
}
|