coinbase / mesh-bitcoin

Bitcoin Mesh API Implementation
Apache License 2.0
110 stars 121 forks source link

Fee calculation is wrong #131

Closed dvc94ch closed 1 year ago

dvc94ch commented 1 year ago

So the preprocess request takes the utxo input operations, but not the outputs. Then it estimates the size and returns it as options to be passed to the metadata endpoint. The metadata endpoint then calculates a fee, which is too low, since the estimated size doesn't account for the outputs. Then the payloads takes the inputs and outputs where the first output is the amount to send and the second output is sum(inputs) - amount_to_send - suggested_fee, resulting in the transaction getting rejected.

dvc94ch commented 1 year ago

looks like I missread the code:

// ConstructionPreprocess implements the /construction/preprocess
// endpoint.
func (s *ConstructionAPIService) ConstructionPreprocess(
    ctx context.Context,
    request *types.ConstructionPreprocessRequest,
) (*types.ConstructionPreprocessResponse, *types.Error) {
    descriptions := &parser.Descriptions{
        OperationDescriptions: []*parser.OperationDescription{
            {
                Type: bitcoin.InputOpType,
                Account: &parser.AccountDescription{
                    Exists: true,
                },
                Amount: &parser.AmountDescription{
                    Exists:   true,
                    Sign:     parser.NegativeAmountSign,
                    Currency: s.config.Currency,
                },
                CoinAction:   types.CoinSpent,
                AllowRepeats: true,
            },
        },
    }

    matches, err := parser.MatchOperations(descriptions, request.Operations)
    if err != nil {
        return nil, wrapErr(ErrUnclearIntent, err)
    }

this actually ignores outputs, doesn't reject them