本文整理汇总了Java中org.bitcoinj.wallet.SendRequest.to方法的典型用法代码示例。如果您正苦于以下问题:Java SendRequest.to方法的具体用法?Java SendRequest.to怎么用?Java SendRequest.to使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bitcoinj.wallet.SendRequest
的用法示例。
在下文中一共展示了SendRequest.to方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import org.bitcoinj.wallet.SendRequest; //导入方法依赖的package包/类
/**
*
* @param callbacks
*/
@Override
public void execute(final CoinActionCallback<CurrencyCoin>... callbacks) {
this._callbacks = callbacks;
this._bitcoin.getWalletManager().wallet().addCoinsSentEventListener(this);
Coin balance = _bitcoin.getWalletManager().wallet().getBalance();
Coin amountCoin = Coin.parseCoin(_amount);
Address addr = Address.fromBase58(_bitcoin.getWalletManager().wallet().getParams(), _address);
SendRequest sendRequest = SendRequest.to(addr, amountCoin);
Coin feeCoin = BitcoinManager.CalculateFeeTxSizeBytes(sendRequest.tx, sendRequest.feePerKb.getValue());
long balValue = balance.getValue();
long amountValue = amountCoin.getValue();
long txFeeValue = feeCoin.getValue();
if (amountValue + txFeeValue > balValue) {
amountCoin = Coin.valueOf(balValue - txFeeValue);
sendRequest = SendRequest.to(addr, amountCoin);
}
try {
_bitcoin.getWalletManager().wallet().sendCoins(sendRequest);
} catch (InsufficientMoneyException e) {
for (CoinActionCallback<CurrencyCoin> callback : _callbacks) {
callback.onError(_bitcoin);
}
e.printStackTrace();
}
}
示例2: sendBitcoin
import org.bitcoinj.wallet.SendRequest; //导入方法依赖的package包/类
/** Sends Bitcoin to the specified bitcoin address.
* @author Francis Fasola
* @param address String representation of the public address.
* @param amount The amount of Bitcoin to send.
* @throws InsufficientMoneyException Not enough money.
* @throws ExecutionException Error during execution.
* @throws InterruptedException Error during execution. */
@SuppressWarnings("deprecation")
public void sendBitcoin(String address, String amount)
throws InsufficientMoneyException, ExecutionException, InterruptedException {
Address destinationAddress = Address.fromBase58(params, address);
SendRequest request = SendRequest.to(destinationAddress, Coin.parseCoin(amount));
SendResult result = wallet.sendCoins(request);
result.broadcastComplete.addListener(() -> {
System.out.println("Coins were sent. Transaction hash: " + result.tx.getHashAsString());
}, MoreExecutors.sameThreadExecutor());
}
示例3: applyTxFee
import org.bitcoinj.wallet.SendRequest; //导入方法依赖的package包/类
/**
*
* @param valueMessage
* @return
*/
@Override
public SpentValueMessage applyTxFee(SpentValueMessage valueMessage) {
Coin amountCoin = Coin.parseCoin(valueMessage.getAmount());
Address addr = Address.fromBase58(_coin.getWalletManager().wallet().getParams(), valueMessage.getAddress());
SendRequest sendRequest = SendRequest.to(addr, amountCoin);
Coin txValue = CalculateFeeTxSizeBytes(sendRequest.tx, sendRequest.feePerKb.getValue());
valueMessage.setTxFee(txValue.toPlainString());
String amountStr = amountCoin.toPlainString();
valueMessage.setAmount(amountStr);
return valueMessage;
}
示例4: payOutVirtualBalance
import org.bitcoinj.wallet.SendRequest; //导入方法依赖的package包/类
@Transactional(isolation = Isolation.SERIALIZABLE)
public PayoutResponse payOutVirtualBalance(ECKey accountOwner, String addressAsString) throws UserNotFoundException,
InsufficientMoneyException, IOException, BusinessException, InsufficientFunds {
final Address toAddress = Address.fromBase58(appConfig.getNetworkParameters(), addressAsString);
final Account account = accountService.getByClientPublicKey(accountOwner.getPubKey());
if (account == null)
throw new UserNotFoundException(accountOwner.getPublicKeyAsHex());
final Coin virtualBalance = Coin.valueOf(account.virtualBalance());
if (!virtualBalance.isPositive())
throw new InsufficientFunds();
final Coin potValue = getMicroPaymentPotValue();
LOG.info("Micropot value is {}", potValue);
if (potValue.isLessThan(virtualBalance)) {
eventService.warn(MICRO_PAYMENT_POT_EXHAUSTED, "Not enough coin in pot. " + virtualBalance + " needed " +
"but only " + potValue + " available.");
return new PayoutResponse();
}
Address changeAddress = appConfig.getMicroPaymentPotPrivKey().toAddress(appConfig.getNetworkParameters());
// Estimate fee by creating a send request
final Coin feePer1000Bytes = Coin.valueOf(feeService.fee() * 1000);
SendRequest estimateRequest = SendRequest.to(toAddress, virtualBalance);
estimateRequest.feePerKb = feePer1000Bytes;
estimateRequest.changeAddress = changeAddress;
walletService.getWallet().completeTx(estimateRequest);
final Coin requiredFee = estimateRequest.tx.getFee();
// Make actual request which subtracts fee from virtual balance
final Coin coinSendToUser = virtualBalance.minus(requiredFee);
SendRequest actualRequest = SendRequest.to(toAddress, coinSendToUser);
actualRequest.feePerKb = feePer1000Bytes;
actualRequest.changeAddress = changeAddress;
Wallet.SendResult sendResult = walletService.getWallet().sendCoins(actualRequest);
// At this point we must consider the coins to be gone, even in case of failure as it has been transmitted
// to the broadcaster.
account.virtualBalance(0L);
accountRepository.save(account);
// Wait for actual broadcast to succeed
Transaction broadcastedTx;
try {
broadcastedTx = sendResult.broadcastComplete.get();
} catch (InterruptedException | ExecutionException e) {
eventService.error(EventType.MICRO_PAYMENT_PAYOUT_ERROR, "Could not broadcast payout request "
+ "for account " + accountOwner.getPublicKeyAsHex() + ". Transaction: "
+ DTOUtils.toHex(sendResult.tx.bitcoinSerialize()) + " Reason:" + e.toString());
e.printStackTrace();
return new PayoutResponse();
}
PayoutResponse response = new PayoutResponse();
response.transaction = DTOUtils.toHex(broadcastedTx.bitcoinSerialize());
response.valuePaidOut = coinSendToUser.getValue();
return response;
}