Skip to content

Commit

Permalink
modify evm listener server
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcc committed Dec 15, 2023
1 parent 7dfbb5c commit 71122e3
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions bitcoin/evm_listener_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (eis *EVMListenerService) OnStart() error {
eis.Logger.Error("EVMListenerService listener deposit Marshal failed: ", "err", err)
return err
}
eis.Logger.Info("EVMListenerService listener deposit event: ", "deposit", string(value))
eis.Logger.Info("EVMListenerService listener deposit event: ", "num", i, "deposit", string(value))
} else if eventHash == common.HexToHash(eis.config.Evm.Withdraw) {
data := WithdrawEvent{
FromAddress: TopicToAddress(vlog, 1),
Expand All @@ -131,13 +131,18 @@ func (eis *EVMListenerService) OnStart() error {
eis.Logger.Error("EVMListenerService listener withdraw Marshal failed: ", "err", err)
return err
}
eis.Logger.Info("EVMListenerService listener withdraw event: ", "withdraw", string(value))
eis.Logger.Info("EVMListenerService listener withdraw event: ", "num", i, "withdraw", string(value))

amount := DataToBigInt(vlog, 1)
err = eis.transferToBtc(DataToString(vlog, 0), amount.Int64())
if err != nil {
eis.Logger.Error("EVMListenerService transferToBtc failed: ", "err", err)
// return err
// deal with Insufficient balance
if err.Error() == "unable to calculate change amount" {
time.Sleep(5 * time.Minute)
continue
}
return err
}
// eis.Logger.Info("EVMListenerService listener withdraw event: ", "withdraw", string(value))
}
Expand Down Expand Up @@ -169,22 +174,19 @@ func (eis *EVMListenerService) transferToBtc(destAddrStr string, amount int64) e
}

inputs := make([]btcjson.TransactionInput, 0, 10)
totalInputAmount := int64(0)
totalInputAmount := btcutil.Amount(0)
for _, unspentTx := range unspentTxs {
amountStr := strconv.FormatFloat(unspentTx.Amount*1e8, 'f', -1, 64)
unspentAmount, err := strconv.ParseInt(amountStr, 10, 64)
if err != nil {
eis.Logger.Error("EVMListenerService format unspentTx.Amount failed: ", "err", err)
return err
}
totalInputAmount += unspentAmount
inputs = append(inputs, btcjson.TransactionInput{
Txid: unspentTx.TxID,
Vout: unspentTx.Vout,
})
totalInputAmount += btcutil.Amount(unspentTx.Amount * 1e8)
if (int64(totalInputAmount) + eis.config.Fee) > amount {
break
}
}
// eis.Logger.Info("ListUnspentMinMaxAddresses", "totalInputAmount", totalInputAmount)
changeAmount := totalInputAmount - eis.config.Fee - amount // fee
changeAmount := int64(totalInputAmount) - eis.config.Fee - amount // fee
if changeAmount > 0 {
changeAddr, err := btcutil.DecodeAddress(sourceAddrStr, defaultNet)
if err != nil {
Expand Down Expand Up @@ -222,7 +224,7 @@ func (eis *EVMListenerService) transferToBtc(destAddrStr string, amount int64) e
eis.Logger.Error("EVMListenerService transferToBtc SendRawTransaction failed: ", "err", err)
return err
}
eis.Logger.Info("EVMListenerService tx success: ", "fromAddress", sourceAddrStr, "toAddress", destAddrStr, "hash", txHash.String())
eis.Logger.Info("EVMListenerService tx success: ", "fromAddress", sourceAddrStr, "toAddress", destAddrStr, "amount", amount, "hash", txHash.String())
return nil
}

Expand Down

0 comments on commit 71122e3

Please sign in to comment.