Skip to content

Commit

Permalink
skip deposit calculation if transaction doesn't involve TSS address
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Nov 14, 2024
1 parent f92effb commit f9a9f55
Show file tree
Hide file tree
Showing 3 changed files with 487 additions and 490 deletions.
37 changes: 16 additions & 21 deletions zetaclient/chains/bitcoin/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,6 @@ func (ob *Observer) CheckReceiptForBtcTxHash(ctx context.Context, txHash string,
return "", fmt.Errorf("block %d is not confirmed yet", blockVb.Height)
}

// calculate depositor fee
depositorFee, err := bitcoin.CalcDepositorFee(ob.btcClient, tx, ob.netParams)
if err != nil {
return "", errors.Wrapf(err, "error calculating depositor fee for inbound %s", tx.Txid)
}

// #nosec G115 always positive
event, err := GetBtcEvent(
ob.btcClient,
Expand All @@ -268,7 +262,6 @@ func (ob *Observer) CheckReceiptForBtcTxHash(ctx context.Context, txHash string,
uint64(blockVb.Height),
ob.logger.Inbound,
ob.netParams,
depositorFee,
)
if err != nil {
return "", err
Expand Down Expand Up @@ -322,13 +315,7 @@ func FilterAndParseIncomingTx(
continue // the first tx is coinbase; we do not process coinbase tx
}

// calculate depositor fee
depositorFee, err := bitcoin.CalcDepositorFee(rpcClient, &txs[idx], netParams)
if err != nil {
return nil, errors.Wrapf(err, "error calculating depositor fee for inbound %s", tx.Txid)
}

event, err := GetBtcEvent(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
event, err := GetBtcEvent(rpcClient, tx, tssAddress, blockNumber, logger, netParams)
if err != nil {
// unable to parse the tx, the caller should retry
return nil, errors.Wrapf(err, "error getting btc event for tx %s in block %d", tx.Txid, blockNumber)
Expand Down Expand Up @@ -390,12 +377,11 @@ func GetBtcEvent(
blockNumber uint64,
logger zerolog.Logger,
netParams *chaincfg.Params,
depositorFee float64,
) (*BTCInboundEvent, error) {
if netParams.Name == chaincfg.MainNetParams.Name {
return GetBtcEventWithoutWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
return GetBtcEventWithoutWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams)
}
return GetBtcEventWithWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
return GetBtcEventWithWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams)
}

// GetBtcEventWithoutWitness either returns a valid BTCInboundEvent or nil
Expand All @@ -408,11 +394,14 @@ func GetBtcEventWithoutWitness(
blockNumber uint64,
logger zerolog.Logger,
netParams *chaincfg.Params,
depositorFee float64,
) (*BTCInboundEvent, error) {
found := false
var value float64
var memo []byte
var (
found = false
value float64
depositorFee float64
memo []byte
)

if len(tx.Vout) >= 2 {
// 1st vout must have tss address as receiver with p2wpkh scriptPubKey
vout0 := tx.Vout[0]
Expand All @@ -429,6 +418,12 @@ func GetBtcEventWithoutWitness(
return nil, nil
}

// calculate depositor fee
depositorFee, err := bitcoin.CalcDepositorFee(rpcClient, &tx, netParams)
if err != nil {
return nil, errors.Wrapf(err, "error calculating depositor fee for inbound %s", tx.Txid)
}

// deposit amount has to be no less than the minimum depositor fee
if vout0.Value < depositorFee {
logger.Info().
Expand Down
Loading

0 comments on commit f9a9f55

Please sign in to comment.