Skip to content

Commit

Permalink
CLI command to list tBTC deposits (#3545)
Browse files Browse the repository at this point in the history
We added a CLI command to list the tBTC deposits. The command queries
the chain for revealed deposits and outputs their details to stdout.

To list all deposits run:
```
keep-client coordinator deposits
```

Following flags can be used with the command:
- `--wallet <address>` - filter deposits only for a given wallet
- `--hide-swept` - doesn't show deposits that were already swept
- `--sort-amount` - sort output by the deposit amount
- `--head <number>` - show `<number>` first deposits
- `--tail <number>` - show `<number>` last deposits

The command requires `ethereum` and `bitcoin` node details to be
provided in the config.

### Funding Transaction

The command outputs the Bitcoin funding transaction details in a
specific format:
`<transaction hash>:<output index>` (e.g.
`a3d1781b59d5e8680772a8bb7f897c4ff0459d3465d7fa678f80a4f0ec900574:1`).
The same format will be required by another CLI command to propose
deposits sweep.

## Example

Sample execution of the command for Goerli network:
```
✗ keep-client --config ./configs/forked/config.toml --goerli \
  coordinator \
  deposits \
  --wallet 0x03b74d6893ad46dfdd01b9e0e3b3385f4fce2d1e \
  --sort-amount \
  --head 5
```
**Output:**
```
 index                                     wallet value (BTC)                                                        deposit key                                                funding transaction confirmations swept
     0 0x03b74d6893ad46dfdd01b9e0e3b3385f4fce2d1e     0.01000 0x06cdc15cbf3f024d05d164b9fcabfe3a6af7180b20ed2b685cf3a2241426667f c0a91a81b401aae3bc639ef24bc686e1b59b83472a9b571510c34c03cc60414a:0             0 false
     1 0x03b74d6893ad46dfdd01b9e0e3b3385f4fce2d1e     0.01000 0xc0612aed21365628908474f634a60124eb4ab3440f133602aaedb06310a6275d 63cb18fdbe4ceb8b0747dbfcb11989605f78cffbe4379b9047124118cfc20aef:0         14212 false
     2 0x03b74d6893ad46dfdd01b9e0e3b3385f4fce2d1e     0.01000 0xc77584f754d98abef75223e7dadfc02484c87052670de6038514a9b951e94156 46a9b50dd44f11364d9924bd7995830cd7cd1a772910f22ed7ab507137653c6d:0         14199 false
     3 0x03b74d6893ad46dfdd01b9e0e3b3385f4fce2d1e     0.01000 0x89835e76759b3e0f2ca5057c0738d6f792083be4aaf8e064d9733720f9973e0c d07564117859bf7b3ee02bd6659d846d1ea5c4b7c204295ab1270948be615047:0         14052 false
     4 0x03b74d6893ad46dfdd01b9e0e3b3385f4fce2d1e     0.01000 0x78cd69a78750f31d49a4693281a97448581361c7e93a9a70ea3a4931199b6c87 8356227b87fb191d61cff2455cd3f8c33ae208c69a7698b5baf1a331db6d4d8f:1         13981 false
```
  • Loading branch information
lukasz-zimnoch authored May 8, 2023
2 parents 9a28977 + a52261a commit 5566ba0
Show file tree
Hide file tree
Showing 9 changed files with 503 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
PingCommand,
EthereumCommand,
MaintainerCommand,
CoordinatorCommand,
)
}

Expand Down
138 changes: 138 additions & 0 deletions cmd/coordinator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/keep-network/keep-core/config"
"github.com/keep-network/keep-core/pkg/bitcoin/electrum"
"github.com/keep-network/keep-core/pkg/chain/ethereum"
"github.com/keep-network/keep-core/pkg/coordinator"
)

var (
walletFlagName = "wallet"
hideSweptFlagName = "hide-swept"
sortByAmountFlagName = "sort-amount"
headFlagName = "head"
tailFlagName = "tail"
)

// CoordinatorCommand contains the definition of tBTC Wallet Coordinator tools.
var CoordinatorCommand = &cobra.Command{
Use: "coordinator",
Short: "tBTC Wallet Coordinator Tools",
Long: "The tool exposes commands for interactions with tBTC wallets.",
TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if err := clientConfig.ReadConfig(
configFilePath,
cmd.Flags(),
config.General, config.Ethereum, config.BitcoinElectrum,
); err != nil {
logger.Fatalf("error reading config: %v", err)
}
},
}

var depositsCommand = cobra.Command{
Use: "deposits",
Short: "get list of deposits",
Long: "Gets tBTC deposits details from the chain and prints them.",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

wallet, err := cmd.Flags().GetString(walletFlagName)
if err != nil {
return fmt.Errorf("failed to find wallet flag: %v", err)
}

hideSwept, err := cmd.Flags().GetBool(hideSweptFlagName)
if err != nil {
return fmt.Errorf("failed to find show swept flag: %v", err)
}

sortByAmount, err := cmd.Flags().GetBool(sortByAmountFlagName)
if err != nil {
return fmt.Errorf("failed to find show swept flag: %v", err)
}

head, err := cmd.Flags().GetInt(headFlagName)
if err != nil {
return fmt.Errorf("failed to find head flag: %v", err)
}

tail, err := cmd.Flags().GetInt(tailFlagName)
if err != nil {
return fmt.Errorf("failed to find tail flag: %v", err)
}

_, tbtcChain, _, _, _, err := ethereum.Connect(ctx, clientConfig.Ethereum)
if err != nil {
return fmt.Errorf(
"could not connect to Ethereum chain: [%v]",
err,
)
}

btcChain, err := electrum.Connect(ctx, clientConfig.Bitcoin.Electrum)
if err != nil {
return fmt.Errorf("could not connect to Electrum chain: [%v]", err)
}

return coordinator.ListDeposits(
tbtcChain,
btcChain,
wallet,
hideSwept,
sortByAmount,
head,
tail,
)
},
}

func init() {
initFlags(
CoordinatorCommand,
&configFilePath,
clientConfig,
config.General, config.Ethereum, config.BitcoinElectrum,
)

// Coordinator Command
CoordinatorCommand.PersistentFlags().String(
walletFlagName,
"",
"wallet public key hash",
)

// Deposits Subcommand
depositsCommand.Flags().Bool(
hideSweptFlagName,
false,
"hide swept deposits",
)

depositsCommand.Flags().Bool(
sortByAmountFlagName,
false,
"sort by deposit amount",
)

depositsCommand.Flags().Int(
headFlagName,
0,
"get head of deposits",
)

depositsCommand.Flags().Int(
tailFlagName,
0,
"get tail of deposits",
)

CoordinatorCommand.AddCommand(&depositsCommand)
}
11 changes: 9 additions & 2 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@ func (tc *TbtcChain) IsDKGResultValid(
// a boolean indicating whether the result is valid or not. The outcome parameter
// must be a pointer to a struct containing a boolean flag as the first field.
//
// TODO: Find a better way to get the validity flag. This would require
// changes in the contracts binding generator.
// TODO: Find a better way to get the validity flag. This would require changes
// in the contracts binding generator.
func parseDkgResultValidationOutcome(
outcome interface{},
) (bool, error) {
Expand Down Expand Up @@ -1091,6 +1091,13 @@ func computeMainUtxoHash(mainUtxo *bitcoin.UnspentTransactionOutput) [32]byte {
return mainUtxoHash
}

func (tc *TbtcChain) BuildDepositKey(
fundingTxHash bitcoin.Hash,
fundingOutputIndex uint32,
) *big.Int {
return buildDepositKey(fundingTxHash, fundingOutputIndex)
}

func buildDepositKey(
fundingTxHash bitcoin.Hash,
fundingOutputIndex uint32,
Expand Down
7 changes: 7 additions & 0 deletions pkg/coordinator/coordinator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package coordinator

import (
"github.com/ipfs/go-log"
)

var logger = log.Logger("keep-coordinator")
Loading

0 comments on commit 5566ba0

Please sign in to comment.