Skip to content

Commit

Permalink
Round down fee to smooth out responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Jul 12, 2024
1 parent a919aa5 commit 02127db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
29 changes: 15 additions & 14 deletions chains/btc/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import (
var (
signingTimeout = 30 * time.Minute

INPUT_SIZE = 180
OUTPUT_SIZE = 34
INPUT_SIZE uint64 = 180
OUTPUT_SIZE uint64 = 34
FEE_ROUNDING_FACTOR uint64 = 5
)

type MempoolAPI interface {
Expand Down Expand Up @@ -238,7 +239,7 @@ func (e *Executor) rawTx(proposals []*BtcTransferProposal, resource config.Resou
if err != nil {
return nil, nil, err
}
feeEstimate, err := e.fee(int64(len(proposals)), int64(len(proposals)))
feeEstimate, err := e.fee(uint64(len(proposals)), uint64(len(proposals)))
if err != nil {
return nil, nil, err
}
Expand All @@ -249,26 +250,26 @@ func (e *Executor) rawTx(proposals []*BtcTransferProposal, resource config.Resou
if inputAmount < outputAmount {
return nil, nil, fmt.Errorf("utxo input amount %d less than output amount %d", inputAmount, outputAmount)
}
fee, err := e.fee(int64(len(utxos)), int64(len(proposals))+1)
fee, err := e.fee(uint64(len(utxos)), uint64(len(proposals))+1)
if err != nil {
return nil, nil, err
}

returnAmount := int64(inputAmount) - fee - outputAmount
returnAmount := inputAmount - fee - outputAmount
if returnAmount > 0 {
// return extra funds
returnScript, err := txscript.PayToAddrScript(resource.Address)
if err != nil {
return nil, nil, err
}
txOut := wire.NewTxOut(returnAmount, returnScript)
txOut := wire.NewTxOut(int64(returnAmount), returnScript)
tx.AddTxOut(txOut)
}
return tx, utxos, err
}

func (e *Executor) outputs(tx *wire.MsgTx, proposals []*BtcTransferProposal) (int64, error) {
outputAmount := int64(0)
func (e *Executor) outputs(tx *wire.MsgTx, proposals []*BtcTransferProposal) (uint64, error) {
outputAmount := uint64(0)
for _, prop := range proposals {
addr, err := btcutil.DecodeAddress(prop.Data.Recipient, &e.chainCfg)
if err != nil {
Expand All @@ -278,16 +279,16 @@ func (e *Executor) outputs(tx *wire.MsgTx, proposals []*BtcTransferProposal) (in
if err != nil {
return 0, err
}
txOut := wire.NewTxOut(prop.Data.Amount, destinationAddrByte)
txOut := wire.NewTxOut(int64(prop.Data.Amount), destinationAddrByte)
tx.AddTxOut(txOut)
outputAmount += prop.Data.Amount
}
return outputAmount, nil
}

func (e *Executor) inputs(tx *wire.MsgTx, address btcutil.Address, outputAmount int64) (int64, []mempool.Utxo, error) {
func (e *Executor) inputs(tx *wire.MsgTx, address btcutil.Address, outputAmount uint64) (uint64, []mempool.Utxo, error) {
usedUtxos := make([]mempool.Utxo, 0)
inputAmount := int64(0)
inputAmount := uint64(0)
utxos, err := e.mempool.Utxos(address.String())
if err != nil {
return 0, nil, err
Expand All @@ -302,23 +303,23 @@ func (e *Executor) inputs(tx *wire.MsgTx, address btcutil.Address, outputAmount
tx.AddTxIn(txIn)

usedUtxos = append(usedUtxos, utxo)
inputAmount += int64(utxo.Value)
inputAmount += uint64(utxo.Value)
if inputAmount > outputAmount {
break
}
}
return inputAmount, usedUtxos, nil
}

func (e *Executor) fee(numOfInputs, numOfOutputs int64) (int64, error) {
func (e *Executor) fee(numOfInputs, numOfOutputs uint64) (uint64, error) {
recommendedFee, err := e.mempool.RecommendedFee()
if err != nil {
return 0, err
}

log.Debug().Msgf("Fee: %+v", recommendedFee)
log.Debug().Msgf("Economy: %d", recommendedFee.EconomyFee)
return (numOfInputs*int64(INPUT_SIZE) + numOfOutputs*int64(OUTPUT_SIZE)) * recommendedFee.EconomyFee, nil
return (numOfInputs*INPUT_SIZE + numOfOutputs*OUTPUT_SIZE) * ((recommendedFee.EconomyFee / FEE_ROUNDING_FACTOR) * FEE_ROUNDING_FACTOR), nil
}

func (e *Executor) sendTx(tx *wire.MsgTx, signatures []taproot.Signature) (*chainhash.Hash, error) {
Expand Down
4 changes: 2 additions & 2 deletions chains/btc/executor/message-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

type BtcTransferProposalData struct {
Amount int64
Amount uint64
Recipient string
DepositNonce uint64
ResourceId [32]byte
Expand Down Expand Up @@ -64,7 +64,7 @@ func ERC20MessageHandler(msg *transfer.TransferMessage) (*proposal.Proposal, err
bigAmount.Div(bigAmount, divisor)

return proposal.NewProposal(msg.Source, msg.Destination, BtcTransferProposalData{
Amount: bigAmount.Int64(),
Amount: bigAmount.Uint64(),
Recipient: string(recipient),
DepositNonce: msg.Data.DepositNonce,
ResourceId: msg.Data.ResourceId,
Expand Down
10 changes: 5 additions & 5 deletions chains/btc/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type Utxo struct {
}

type Fee struct {
FastestFee int64
HalfHourFee int64
MinimumFee int64
EconomyFee int64
HourFee int64
FastestFee uint64
HalfHourFee uint64
MinimumFee uint64
EconomyFee uint64
HourFee uint64
}

type MempoolAPI struct {
Expand Down

0 comments on commit 02127db

Please sign in to comment.