Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add external liquidity providers #198

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 67 additions & 29 deletions server/internal/core/application/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"context"
"encoding/hex"
"fmt"
txbuilder "github.com/ark-network/ark/internal/infrastructure/tx-builder/covenant"
"sync"
"time"

"github.com/ark-network/ark/common"
"github.com/ark-network/ark/common/tree"
"github.com/ark-network/ark/internal/core/domain"

Check failure on line 14 in server/internal/core/application/service.go

View workflow job for this annotation

GitHub Actions / unit tests

could not import github.com/ark-network/ark/internal/core/domain (-: import cycle not allowed in test) (typecheck)
"github.com/ark-network/ark/internal/core/ports"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
Expand Down Expand Up @@ -385,41 +386,78 @@
return
}

sweptRounds, err := s.repoManager.Rounds().GetSweptRounds(ctx)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to retrieve swept rounds: %s", err))
log.WithError(err).Warn("failed to retrieve swept rounds")
return
}
if round.RequiresLiquidityProvider {
fmt.Println("This round requires a liquidity provider.")

unsignedPoolTx, tree, connectorAddress, err := s.builder.BuildPoolTx(s.pubkey, payments, s.minRelayFee, sweptRounds)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to create pool tx: %s", err))
log.WithError(err).Warn("failed to create pool tx")
return
}
log.Debugf("pool tx created for round %s", round.Id)
liquidityTxBuilder := txbuilder.NewLiquidityTxBuilder(s.builder)

// TODO BTC make the senders sign the tree
unsignedPoolTx, tree, connectorAddress, err := liquidityTxBuilder.BuildPoolTxFromLiquidityProvider(payments, s.minRelayFee, round.LiquidityProvider, s.pubkey)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to create pool tx: %s", err))
log.WithError(err).Warn("failed to create pool tx")
return
}
log.Debugf("pool tx created for round %s", round.Id)

connectors, forfeitTxs, err := s.builder.BuildForfeitTxs(s.pubkey, unsignedPoolTx, payments, s.minRelayFee)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to create connectors and forfeit txs: %s", err))
log.WithError(err).Warn("failed to create connectors and forfeit txs")
return
}
// TODO BTC make the senders sign the tree

log.Debugf("forfeit transactions created for round %s", round.Id)
connectors, forfeitTxs, err := s.builder.BuildForfeitTxs(round.LiquidityProvider.PubKey, unsignedPoolTx, payments, s.minRelayFee)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to create connectors and forfeit txs: %s", err))
log.WithError(err).Warn("failed to create connectors and forfeit txs")
return
}

events, err := round.StartFinalization(connectorAddress, connectors, tree, unsignedPoolTx)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to start finalization: %s", err))
log.WithError(err).Warn("failed to start finalization")
return
}
changes = append(changes, events...)
log.Debugf("forfeit transactions created for round %s", round.Id)

events, err := round.StartFinalization(connectorAddress, connectors, tree, unsignedPoolTx)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to start finalization: %s", err))
log.WithError(err).Warn("failed to start finalization")
return
}
changes = append(changes, events...)

s.forfeitTxs.push(forfeitTxs)
} else {
fmt.Println("This round does not require a liquidity provider.")

sweptRounds, err := s.repoManager.Rounds().GetSweptRounds(ctx)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to retrieve swept rounds: %s", err))
log.WithError(err).Warn("failed to retrieve swept rounds")
return
}

unsignedPoolTx, tree, connectorAddress, err := s.builder.BuildPoolTx(s.pubkey, payments, s.minRelayFee, sweptRounds)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to create pool tx: %s", err))
log.WithError(err).Warn("failed to create pool tx")
return
}
log.Debugf("pool tx created for round %s", round.Id)

// TODO BTC make the senders sign the tree

s.forfeitTxs.push(forfeitTxs)
connectors, forfeitTxs, err := s.builder.BuildForfeitTxs(s.pubkey, unsignedPoolTx, payments, s.minRelayFee)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to create connectors and forfeit txs: %s", err))
log.WithError(err).Warn("failed to create connectors and forfeit txs")
return
}

log.Debugf("forfeit transactions created for round %s", round.Id)

events, err := round.StartFinalization(connectorAddress, connectors, tree, unsignedPoolTx)
if err != nil {
changes = round.Fail(fmt.Errorf("failed to start finalization: %s", err))
log.WithError(err).Warn("failed to start finalization")
return
}
changes = append(changes, events...)

s.forfeitTxs.push(forfeitTxs)
}

log.Debugf("started finalization stage for round: %s", round.Id)
}
Expand Down
16 changes: 16 additions & 0 deletions server/internal/core/domain/liquidity_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package domain

import (
"github.com/ark-network/ark/internal/core/ports"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)

type LiquidityProvider struct {
PubKey *secp256k1.PublicKey
UTXO []ports.TxInput
FeeRate uint64
}

func (lp *LiquidityProvider) CheckFeeRate(minRate, maxRate uint64) bool {
return lp.FeeRate >= minRate && lp.FeeRate <= maxRate
}
32 changes: 17 additions & 15 deletions server/internal/core/domain/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ type Stage struct {
}

type Round struct {
Id string
StartingTimestamp int64
EndingTimestamp int64
Stage Stage
Payments map[string]Payment
Txid string
UnsignedTx string
ForfeitTxs []string
CongestionTree tree.CongestionTree
Connectors []string
ConnectorAddress string
DustAmount uint64
Version uint
Swept bool // true if all the vtxos are vtxo.Swept or vtxo.Redeemed
changes []RoundEvent
Id string
StartingTimestamp int64
EndingTimestamp int64
Stage Stage
Payments map[string]Payment
Txid string
UnsignedTx string
ForfeitTxs []string
CongestionTree tree.CongestionTree
Connectors []string
ConnectorAddress string
DustAmount uint64
Version uint
Swept bool // true if all the vtxos are vtxo.Swept or vtxo.Redeemed
RequiresLiquidityProvider bool // true if this round requires a liquidity provider
LiquidityProvider *LiquidityProvider
changes []RoundEvent
}

func NewRound(dustAmount uint64) *Round {
Expand Down
Loading
Loading