Skip to content

Commit

Permalink
Enabled moved funds sweep task
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Feb 21, 2024
1 parent d1f8cbe commit 6f4f0c1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
110 changes: 110 additions & 0 deletions pkg/tbtcpg/moved_funds_sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package tbtcpg

import (
"fmt"

"github.com/ipfs/go-log/v2"
"go.uber.org/zap"

"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/tbtc"
)

type MovedFundsSweepTask struct {
chain Chain
btcChain bitcoin.Chain
}

func NewMovedFundsSweepTask(
chain Chain,
btcChain bitcoin.Chain,
) *MovedFundsSweepTask {
return &MovedFundsSweepTask{
chain: chain,
btcChain: btcChain,
}
}

func (mfst *MovedFundsSweepTask) Run(request *tbtc.CoordinationProposalRequest) (
tbtc.CoordinationProposal,
bool,
error,
) {
walletPublicKeyHash := request.WalletPublicKeyHash

taskLogger := logger.With(
zap.String("task", mfst.ActionType().String()),
zap.String("walletPKH", fmt.Sprintf("0x%x", walletPublicKeyHash)),
)

walletChainData, err := mfst.chain.GetWallet(walletPublicKeyHash)
if err != nil {
return nil, false, fmt.Errorf(
"cannot get wallet's chain data: [%w]",
err,
)
}

if walletChainData.State != tbtc.StateLive &&
walletChainData.State != tbtc.StateMovingFunds {
taskLogger.Infof("wallet not in Live or MoveFunds state")
return nil, false, nil
}

if walletChainData.PendingMovedFundsSweepRequestsCount == 0 {
taskLogger.Infof("wallet has no pending moved funds sweep requests")
return nil, false, nil
}

movingFundsTxHash, movingFundsOutputIdx, err := mfst.FindMovingFundsTxData(
walletPublicKeyHash,
)
if err != nil {
return nil, false, fmt.Errorf(
"cannot find moving funds transaction data: [%w]",
err,
)
}

proposal, err := mfst.ProposeMovedFundsSweep(
taskLogger,
walletPublicKeyHash,
movingFundsTxHash,
movingFundsOutputIdx,
0,
)
if err != nil {
return nil, false, fmt.Errorf(
"cannot prepare moved funds sweep proposal: [%w]",
err,
)
}

return proposal, true, nil
}

func (mfst *MovedFundsSweepTask) FindMovingFundsTxData(
walletPublicKeyHash [20]byte,
) (
txHash bitcoin.Hash,
txOutputIdx uint32,
err error,
) {
// TODO: Implement
return bitcoin.Hash{}, 0, nil
}

func (mfst *MovedFundsSweepTask) ActionType() tbtc.WalletActionType {
return tbtc.ActionMovedFundsSweep
}

func (mfst *MovedFundsSweepTask) ProposeMovedFundsSweep(
taskLogger log.StandardLogger,
walletPublicKeyHash [20]byte,
movingFundsTxHash bitcoin.Hash,
movingFundsTxIndex uint32,
fee int64,
) (*tbtc.MovedFundsSweepProposal, error) {
// TODO: Implement
return nil, nil
}
3 changes: 1 addition & 2 deletions pkg/tbtcpg/tbtcpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func NewProposalGenerator(
NewRedemptionTask(chain, btcChain),
NewHeartbeatTask(chain),
NewMovingFundsTask(chain, btcChain),
// TODO: Uncomment when moving funds support is implemented.
// newMovedFundsSweepTask(),
NewMovedFundsSweepTask(chain, btcChain),
}

return &ProposalGenerator{
Expand Down

0 comments on commit 6f4f0c1

Please sign in to comment.