Skip to content

Commit

Permalink
Implemented move funds task
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Dec 11, 2023
1 parent f1c8275 commit 80e34b6
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions pkg/tbtcpg/moving_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,53 @@ func (mft *MovingFundsTask) Run(request *tbtc.CoordinationProposalRequest) (
zap.String("walletPKH", fmt.Sprintf("0x%x", walletPublicKeyHash)),
)

walletMainUtxo, err := tbtc.DetermineWalletMainUtxo(
walletPublicKeyHash,
mft.chain,
mft.btcChain,
)
if err != nil {
return nil, false, fmt.Errorf(
"cannot get wallet's maun UTXO: [%w]",
err,
)
}

walletBalance := int64(0)
if walletMainUtxo != nil {
walletBalance = walletMainUtxo.Value
}

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

ok, err := mft.ValidateSourceWallet(taskLogger, walletChainData, walletBalance)
if err != nil {
return nil, false, fmt.Errorf("cannot validate wallet: [%w]", err)
}

if !ok {
taskLogger.Info("wallet not eligible for moving funds")
return nil, false, nil
}

targetWallets, commitmentSubmitted, err := mft.FindTargetWallets(
taskLogger,
walletChainData,
)
if err != nil {
return nil, false, fmt.Errorf("cannot find target wallets: [%w]", err)
}

proposal, err := mft.ProposeMovingFunds(
taskLogger,
walletPublicKeyHash,
targetWallets,
)
if err != nil {
return nil, false, fmt.Errorf(
Expand All @@ -49,17 +93,72 @@ func (mft *MovingFundsTask) Run(request *tbtc.CoordinationProposalRequest) (
)
}

if !commitmentSubmitted {
mft.SubmitMovingFundsCommitment()
}

return proposal, false, nil
}

func (mft *MovingFundsTask) FindTargetWallets(
taskLogger log.StandardLogger,
walletChainData *tbtc.WalletChainData,
) ([][20]byte, bool, error) {
if walletChainData.MovingFundsTargetWalletsCommitmentHash == [32]byte{} {
taskLogger.Infof("Move funds commitment has not been submitted yet")
// TODO: Find the target wallets among Live wallets.
} else {
taskLogger.Infof("Move funds commitment has already been submitted")
// TODO: Find the target wallets from the emitted event.
}
return nil, false, nil
}

func (mft *MovingFundsTask) ValidateSourceWallet(
taskLogger log.StandardLogger,
walletChainData *tbtc.WalletChainData,
walletBalance int64,
) (bool, error) {
if walletBalance <= 0 {
// The wallet's balance cannot be `0`. Since we are dealing with
// a signed integer we also check it's not negative just in case.
taskLogger.Infof("source wallet does not have a positive balance")
return false, nil
}

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

if walletChainData.PendingRedemptionsValue > 0 {
taskLogger.Infof("source wallet has pending redemptions")
return false, nil
}

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

return true, nil
}

func (mft *MovingFundsTask) SubmitMovingFundsCommitment() ([][20]byte, error) {
// TODO: Implement
return nil, nil
}

func (mft *MovingFundsTask) ProposeMovingFunds(
taskLogger log.StandardLogger,
walletPublicKeyHash [20]byte,
targetWallets [][20]byte,
) (*tbtc.MovingFundsProposal, error) {
taskLogger.Infof("preparing a moving funds proposal")

proposal := &tbtc.MovingFundsProposal{
WalletPublicKeyHash: walletPublicKeyHash,
TargetWallets: targetWallets,
}

taskLogger.Infof("validating the moving funds proposal")
Expand Down

0 comments on commit 80e34b6

Please sign in to comment.