|
8 | 8 | ec "github.com/bitcoin-sv/go-sdk/primitives/ec" |
9 | 9 | "github.com/bitcoin-sv/go-sdk/script" |
10 | 10 | "github.com/bitcoin-sv/go-sdk/transaction" |
| 11 | + feemodel "github.com/bitcoin-sv/go-sdk/transaction/fee_model" |
11 | 12 | "github.com/bitcoin-sv/go-sdk/transaction/template/p2pkh" |
12 | 13 | "github.com/stretchr/testify/require" |
13 | 14 | ) |
@@ -197,3 +198,97 @@ func TestSignUnsignedNew(t *testing.T) { |
197 | 198 | require.NotEmpty(t, input.UnlockingScript.Bytes()) |
198 | 199 | } |
199 | 200 | } |
| 201 | + |
| 202 | +func TestTransactionGetFee(t *testing.T) { |
| 203 | + // Use the BRC62Hex transaction |
| 204 | + tx, err := transaction.NewTransactionFromBEEFHex(BRC62Hex) |
| 205 | + require.NoError(t, err) |
| 206 | + |
| 207 | + // Get the fee |
| 208 | + fee, err := tx.GetFee() |
| 209 | + require.NoError(t, err) |
| 210 | + |
| 211 | + // Calculate expected fee |
| 212 | + totalInputSatoshis, err := tx.TotalInputSatoshis() |
| 213 | + require.NoError(t, err) |
| 214 | + totalOutputSatoshis := tx.TotalOutputSatoshis() |
| 215 | + expectedFee := totalInputSatoshis - totalOutputSatoshis |
| 216 | + |
| 217 | + // Verify the fee matches the expected fee |
| 218 | + require.Equal(t, expectedFee, fee) |
| 219 | +} |
| 220 | + |
| 221 | +func TestTransactionFee(t *testing.T) { |
| 222 | + // Example WIF and associated address |
| 223 | + privKeyWIF := "KznvCNc6Yf4iztSThoMH6oHWzH9EgjfodKxmeuUGPq5DEX5maspS" |
| 224 | + privKey, err := ec.PrivateKeyFromWif(privKeyWIF) |
| 225 | + require.NoError(t, err) |
| 226 | + |
| 227 | + address, err := script.NewAddressFromPublicKey(privKey.PubKey(), true) |
| 228 | + require.NoError(t, err) |
| 229 | + |
| 230 | + // Source transaction data (a real transaction) |
| 231 | + sourceRawTx := "0100000001b1e5bf6e0649f299bb2b20964090b5b0a02e96db182eecedb0a9e4e7af03e06e000000006b483045022100ca75f7f664fa3086a3430b0f5d4a531d26e8d2ef3a72f086e890c5618d858fed022006e9a3c9f08e1743b033a55c27fb9d6c6cf1a1f6e0c40090e229d4ff8e5ecb31412102798913bc057b344de675dac34faafe3dc2f312c758cd9068209f810877306d66ffffffff01b0f9d804000000001976a9144bd8c375bdac70fb6eb7261d6e6c70450787e6af88ac00000000" |
| 232 | + |
| 233 | + sourceTx, err := transaction.NewTransactionFromHex(sourceRawTx) |
| 234 | + require.NoError(t, err) |
| 235 | + |
| 236 | + // Create a new transaction |
| 237 | + tx := transaction.NewTransaction() |
| 238 | + |
| 239 | + // Create a P2PKH unlocker |
| 240 | + unlocker, err := p2pkh.Unlock(privKey, nil) |
| 241 | + require.NoError(t, err) |
| 242 | + |
| 243 | + // Add an input from the source transaction |
| 244 | + tx.AddInputFromTx(sourceTx, 0, unlocker) |
| 245 | + |
| 246 | + // Create a P2PKH locking script |
| 247 | + lockScript, err := p2pkh.Lock(address) |
| 248 | + require.NoError(t, err) |
| 249 | + |
| 250 | + // Add an output (sending 1,000,000 satoshis) |
| 251 | + tx.AddOutput(&transaction.TransactionOutput{ |
| 252 | + LockingScript: lockScript, |
| 253 | + Satoshis: 1000000, // 0.01 BSV |
| 254 | + }) |
| 255 | + |
| 256 | + // Add a change output |
| 257 | + tx.AddOutput(&transaction.TransactionOutput{ |
| 258 | + LockingScript: lockScript, |
| 259 | + Change: true, |
| 260 | + }) |
| 261 | + |
| 262 | + // Create a fee model with 500 satoshis per kilobyte |
| 263 | + feeModel := &feemodel.SatoshisPerKilobyte{ |
| 264 | + Satoshis: 500, // Fee rate |
| 265 | + } |
| 266 | + |
| 267 | + // Compute the fee |
| 268 | + err = tx.Fee(feeModel, transaction.ChangeDistributionEqual) |
| 269 | + require.NoError(t, err) |
| 270 | + |
| 271 | + // Sign the transaction |
| 272 | + err = tx.Sign() |
| 273 | + require.NoError(t, err) |
| 274 | + |
| 275 | + // Get the actual fee from the transaction |
| 276 | + fee, err := tx.GetFee() |
| 277 | + require.NoError(t, err) |
| 278 | + |
| 279 | + // Compute the expected fee using the fee model |
| 280 | + expectedFee, err := feeModel.ComputeFee(tx) |
| 281 | + require.NoError(t, err) |
| 282 | + |
| 283 | + // Verify that the actual fee matches the expected fee |
| 284 | + require.Equal(t, expectedFee, fee) |
| 285 | + |
| 286 | + // Verify that total inputs >= total outputs + fee |
| 287 | + totalInputs, err := tx.TotalInputSatoshis() |
| 288 | + require.NoError(t, err) |
| 289 | + totalOutputs := tx.TotalOutputSatoshis() |
| 290 | + require.True(t, totalInputs >= totalOutputs+fee) |
| 291 | + |
| 292 | + // Print the fee for informational purposes |
| 293 | + t.Logf("Computed fee: %d satoshis", fee) |
| 294 | +} |
0 commit comments