Skip to content

Commit

Permalink
Add unit test we can handle spending funds and receiving funds in sam…
Browse files Browse the repository at this point in the history
…e tx (#3185)

* Add unit test we can handle spending funds and receiving funds in the same transaction inside the wallet

* Use fundRawTranaction() to make sure the utxo is reserved
  • Loading branch information
Christewart committed Jun 14, 2021
1 parent 4f936b2 commit aaa7b42
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ trait WalletApi extends StartStopAsync[WalletApi] {

def findTransaction(txId: DoubleSha256DigestBE): Future[Option[TransactionDb]]

/** Funds a transaction from the wallet.
* @return funded transaction send funds to desinations with the given fee rate
*/
def fundRawTransaction(
destinations: Vector[TransactionOutput],
feeRate: FeeUnit,
fromTagOpt: Option[AddressTag],
markAsReserved: Boolean): Future[Transaction]

def listTransactions(): Future[Vector[TransactionDb]]

/** Takes in a block header and updates our TxoStates to the new chain tip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ trait BitcoinSWalletTest
withNewConfiguredWallet(segwitWalletConf)(test)
}

/** Fixture for a wallet with default configuration with no funds in it */
def withNewWallet(test: OneArgAsyncTest, bip39PasswordOpt: Option[String])(
implicit walletAppConfig: WalletAppConfig): FutureOutcome =
makeDependentFixture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.bitcoins.wallet

import org.bitcoins.core.api.wallet.WalletApi
import org.bitcoins.core.currency._
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.transaction.{Transaction, TransactionOutput}
import org.bitcoins.core.wallet.fee.SatoshisPerByte
import org.bitcoins.testkit.wallet.BitcoinSWalletTest
import org.bitcoins.testkitcore.Implicits._
import org.bitcoins.testkitcore.gen.TransactionGenerators
Expand Down Expand Up @@ -134,6 +137,52 @@ class ProcessTransactionTest extends BitcoinSWalletTest {
assert(balance == 0.sats)
assert(unconfirmed == 0.sats)
}
}

it must "spend and receive funds in the same transaction where the funding utxo is reserved" in {
wallet =>
val fundingAddressF = wallet.getNewAddress()
val receivingAddressF = wallet.getNewAddress()
val amount = Bitcoins.one

val amtWithFee = amount + Satoshis(175) //for fee

//build funding tx
val fundingTxF: Future[(Transaction, UInt32)] = for {
fundingAddr <- fundingAddressF
fundingTx = TransactionGenerators.buildCreditingTransaction(
fundingAddr.scriptPubKey,
amtWithFee)
} yield fundingTx

val processedFundingTxF: Future[WalletApi] = for {
(fundingTx, _) <- fundingTxF
//make sure wallet is empty
balance <- wallet.getBalance()
_ = assert(balance == Bitcoins.zero)
processed <- wallet.processTransaction(fundingTx, None)
balance <- wallet.getBalance()
_ = assert(balance == amtWithFee)
} yield processed

//build spending tx
val spendingTxF = for {
receivingAddress <- receivingAddressF
wallet <- processedFundingTxF
destinations = Vector(
TransactionOutput(amount, receivingAddress.scriptPubKey))
spendingTx <- wallet.fundRawTransaction(
destinations = destinations,
feeRate = SatoshisPerByte.one,
fromTagOpt = None,
markAsReserved = true
)
processedSpendingTx <- wallet.processTransaction(transaction =
spendingTx,
blockHash = None)
balance <- processedSpendingTx.getBalance()
} yield assert(balance == amount)

spendingTxF
}
}

0 comments on commit aaa7b42

Please sign in to comment.