forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor desktop's BsqSendView, share with api
Moved just enough code out of BsqSendView to avoid desktop/api 'sendbsq' duplication, at the cost of adding 1 new method to BsqSendView. - Created new BsqTransferModel to hold tx details shared by desktop and api. - Created new BsqTransferService to send bsq using a BsqTransferModel shared by desktop and api. - Uncommented CoreWalletsService#sendBsq implementation. - Uncommented sendbsq tests.
- Loading branch information
Showing
5 changed files
with
201 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
core/src/main/java/bisq/core/btc/model/BsqTransferModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package bisq.core.btc.model; | ||
|
||
import bisq.core.dao.state.model.blockchain.TxType; | ||
import bisq.core.util.coin.CoinUtil; | ||
|
||
import org.bitcoinj.core.Coin; | ||
import org.bitcoinj.core.LegacyAddress; | ||
import org.bitcoinj.core.Transaction; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public final class BsqTransferModel { | ||
|
||
private final LegacyAddress receiverAddress; | ||
private final Coin receiverAmount; | ||
private final Transaction preparedSendTx; | ||
private final Transaction txWithBtcFee; | ||
private final Transaction signedTx; | ||
private final Coin miningFee; | ||
private final int txSize; | ||
private final TxType txType; | ||
|
||
public BsqTransferModel(LegacyAddress receiverAddress, | ||
Coin receiverAmount, | ||
Transaction preparedSendTx, | ||
Transaction txWithBtcFee, | ||
Transaction signedTx) { | ||
this.receiverAddress = receiverAddress; | ||
this.receiverAmount = receiverAmount; | ||
this.preparedSendTx = preparedSendTx; | ||
this.txWithBtcFee = txWithBtcFee; | ||
this.signedTx = signedTx; | ||
this.miningFee = signedTx.getFee(); | ||
this.txSize = signedTx.bitcoinSerialize().length; | ||
this.txType = TxType.TRANSFER_BSQ; | ||
} | ||
|
||
public String getReceiverAddressAsString() { | ||
return receiverAddress.toString(); | ||
} | ||
|
||
public double getMiningFeeInSatoshisPerByte() { | ||
return CoinUtil.getFeePerByte(miningFee, txSize); | ||
} | ||
|
||
public double getTxSizeInKb() { | ||
return txSize / 1000d; | ||
} | ||
|
||
public String toShortString() { | ||
return "{" + "\n" + | ||
" receiverAddress='" + getReceiverAddressAsString() + '\'' + "\n" + | ||
", receiverAmount=" + receiverAmount + "\n" + | ||
", txWithBtcFee.txId=" + txWithBtcFee.getTxId() + "\n" + | ||
", miningFee=" + miningFee + "\n" + | ||
", miningFeeInSatoshisPerByte=" + getMiningFeeInSatoshisPerByte() + "\n" + | ||
", txSizeInKb=" + getTxSizeInKb() + "\n" + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BsqTransferModel{" + "\n" + | ||
" receiverAddress='" + getReceiverAddressAsString() + '\'' + "\n" + | ||
", receiverAmount=" + receiverAmount + "\n" + | ||
", preparedSendTx=" + preparedSendTx + "\n" + | ||
", txWithBtcFee=" + txWithBtcFee + "\n" + | ||
", signedTx=" + signedTx + "\n" + | ||
", miningFee=" + miningFee + "\n" + | ||
", miningFeeInSatoshisPerByte=" + getMiningFeeInSatoshisPerByte() + "\n" + | ||
", txSize=" + txSize + "\n" + | ||
", txSizeInKb=" + getTxSizeInKb() + "\n" + | ||
", txType=" + txType + "\n" + | ||
'}'; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
core/src/main/java/bisq/core/btc/wallet/BsqTransferService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package bisq.core.btc.wallet; | ||
|
||
import bisq.core.btc.exceptions.BsqChangeBelowDustException; | ||
import bisq.core.btc.exceptions.TransactionVerificationException; | ||
import bisq.core.btc.exceptions.WalletException; | ||
import bisq.core.btc.model.BsqTransferModel; | ||
|
||
import org.bitcoinj.core.Coin; | ||
import org.bitcoinj.core.InsufficientMoneyException; | ||
import org.bitcoinj.core.LegacyAddress; | ||
import org.bitcoinj.core.Transaction; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Singleton | ||
public class BsqTransferService { | ||
|
||
private final WalletsManager walletsManager; | ||
private final BsqWalletService bsqWalletService; | ||
private final BtcWalletService btcWalletService; | ||
|
||
@Inject | ||
public BsqTransferService(WalletsManager walletsManager, | ||
BsqWalletService bsqWalletService, | ||
BtcWalletService btcWalletService) { | ||
this.walletsManager = walletsManager; | ||
this.bsqWalletService = bsqWalletService; | ||
this.btcWalletService = btcWalletService; | ||
} | ||
|
||
public BsqTransferModel getBsqTransferModel(LegacyAddress address, | ||
Coin receiverAmount) | ||
throws TransactionVerificationException, | ||
WalletException, | ||
BsqChangeBelowDustException, | ||
InsufficientMoneyException { | ||
|
||
Transaction preparedSendTx = bsqWalletService.getPreparedSendBsqTx(address.toString(), receiverAmount); | ||
Transaction txWithBtcFee = btcWalletService.completePreparedSendBsqTx(preparedSendTx, true); | ||
Transaction signedTx = bsqWalletService.signTx(txWithBtcFee); | ||
|
||
return new BsqTransferModel(address, | ||
receiverAmount, | ||
preparedSendTx, | ||
txWithBtcFee, | ||
signedTx); | ||
} | ||
|
||
public void sendFunds(BsqTransferModel bsqTransferModel, TxBroadcaster.Callback callback) { | ||
log.info("Publishing BSQ transfer {}", bsqTransferModel.toShortString()); | ||
walletsManager.publishAndCommitBsqTx(bsqTransferModel.getTxWithBtcFee(), | ||
bsqTransferModel.getTxType(), | ||
callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters