diff --git a/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java b/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java index aac1cdf4..b0f6f83f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java +++ b/src/main/java/com/sparrowwallet/sparrow/whirlpool/Whirlpool.java @@ -16,7 +16,7 @@ import com.samourai.whirlpool.client.wallet.WhirlpoolWalletService; import com.samourai.whirlpool.client.wallet.beans.*; import com.samourai.whirlpool.client.wallet.data.dataPersister.DataPersisterFactory; import com.samourai.whirlpool.client.wallet.data.dataSource.DataSourceFactory; -import com.samourai.whirlpool.client.wallet.data.pool.PoolData; +import com.samourai.whirlpool.client.wallet.data.pool.ExpirablePoolSupplier; import com.samourai.whirlpool.client.wallet.data.utxo.UtxoSupplier; import com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfig; import com.samourai.whirlpool.client.whirlpool.ServerApi; @@ -25,7 +25,6 @@ import com.sparrowwallet.drongo.ExtendedKey; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.Network; import com.sparrowwallet.drongo.Utils; -import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.Sha256Hash; import com.sparrowwallet.drongo.protocol.Transaction; @@ -69,6 +68,9 @@ public class Whirlpool { private final TorClientService torClientService; private final WhirlpoolWalletService whirlpoolWalletService; private final WhirlpoolWalletConfig config; + private Tx0ParamService tx0ParamService; + private ExpirablePoolSupplier poolSupplier; + private Tx0Service tx0Service; private HD_Wallet hdWallet; private String walletId; private String mixToWalletId; @@ -84,6 +86,9 @@ public class Whirlpool { this.whirlpoolWalletService = new WhirlpoolWalletService(); this.config = computeWhirlpoolWalletConfig(torProxy); + this.tx0ParamService = new Tx0ParamService(SparrowMinerFeeSupplier.getInstance(), config); + this.poolSupplier = new ExpirablePoolSupplier(config.getRefreshPoolsDelay(), config.getServerApi(), tx0ParamService); + this.tx0Service = new Tx0Service(config); WhirlpoolEventService.getInstance().register(this); } @@ -98,26 +103,23 @@ public class Whirlpool { WhirlpoolWalletConfig whirlpoolWalletConfig = new WhirlpoolWalletConfig(dataSourceFactory, httpClientService, stompClientService, torClientService, serverApi, whirlpoolServer.getParams(), false); whirlpoolWalletConfig.setDataPersisterFactory(dataPersisterFactory); - + whirlpoolWalletConfig.setPartner("SPARROW"); return whirlpoolWalletConfig; } public Collection getPools() throws Exception { - Tx0ParamService tx0ParamService = getTx0ParamService(); - PoolData poolData = new PoolData(config.getServerApi().fetchPools(), tx0ParamService); - return poolData.getPools(); + this.poolSupplier.load(); + return poolSupplier.getPools(); } public Tx0Preview getTx0Preview(Pool pool, Collection utxos) throws Exception { - Tx0Config tx0Config = new Tx0Config(); - tx0Config.setChangeWallet(WhirlpoolAccount.BADBANK); - Tx0FeeTarget tx0FeeTarget = Tx0FeeTarget.BLOCKS_4; - Tx0FeeTarget mixFeeTarget = Tx0FeeTarget.BLOCKS_4; + // preview all pools + Tx0Config tx0Config = computeTx0Config(); + Tx0Previews tx0Previews = tx0Service.tx0Previews(utxos, tx0Config); - Tx0ParamService tx0ParamService = getTx0ParamService(); - - Tx0Service tx0Service = new Tx0Service(config); - return tx0Service.tx0Preview(utxos, tx0Config, tx0ParamService.getTx0Param(pool, tx0FeeTarget, mixFeeTarget)); + // pool preview + String poolId = pool.getPoolId(); + return tx0Previews.getTx0Preview(poolId); } public Tx0 broadcastTx0(Pool pool, Collection utxos) throws Exception { @@ -130,17 +132,14 @@ public class Whirlpool { throw new IllegalStateException("Failed to find UTXOs in Whirlpool wallet"); } - Tx0Config tx0Config = new Tx0Config(); - tx0Config.setChangeWallet(WhirlpoolAccount.BADBANK); - Tx0FeeTarget tx0FeeTarget = Tx0FeeTarget.BLOCKS_4; - Tx0FeeTarget mixFeeTarget = Tx0FeeTarget.BLOCKS_4; - - return whirlpoolWallet.tx0(whirlpoolUtxos, pool, tx0Config, tx0FeeTarget, mixFeeTarget); + Tx0Config tx0Config = computeTx0Config(); + return whirlpoolWallet.tx0(whirlpoolUtxos, pool, tx0Config); } - private Tx0ParamService getTx0ParamService() { - SparrowMinerFeeSupplier minerFeeSupplier = SparrowMinerFeeSupplier.getInstance(); - return new Tx0ParamService(minerFeeSupplier, config); + private Tx0Config computeTx0Config() { + Tx0FeeTarget tx0FeeTarget = Tx0FeeTarget.BLOCKS_4; + Tx0FeeTarget mixFeeTarget = Tx0FeeTarget.BLOCKS_4; + return new Tx0Config(tx0ParamService, poolSupplier, tx0FeeTarget, mixFeeTarget, WhirlpoolAccount.BADBANK); } public void setHDWallet(String walletId, Wallet wallet) { diff --git a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowDataSource.java b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowDataSource.java index 2c3852d0..8aea2f66 100644 --- a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowDataSource.java +++ b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowDataSource.java @@ -158,9 +158,8 @@ public class SparrowDataSource extends WalletResponseDataSource { walletResponse.info.latest_block.time = AppServices.getLatestBlockHeader() == null ? 1 : AppServices.getLatestBlockHeader().getTime(); walletResponse.info.fees = new LinkedHashMap<>(); - MinerFee minerFee = getMinerFeeSupplier().getValue(); for(MinerFeeTarget target : MinerFeeTarget.values()) { - walletResponse.info.fees.put(target.getValue(), minerFee.get(target)); + walletResponse.info.fees.put(target.getValue(), getMinerFeeSupplier().getFee(target)); } return walletResponse; diff --git a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowMinerFeeSupplier.java b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowMinerFeeSupplier.java index 12a2a387..3eddd8a2 100644 --- a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowMinerFeeSupplier.java +++ b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowMinerFeeSupplier.java @@ -1,11 +1,13 @@ package com.sparrowwallet.sparrow.whirlpool.dataSource; -import com.samourai.wallet.api.backend.MinerFee; import com.samourai.wallet.api.backend.MinerFeeTarget; import com.samourai.whirlpool.client.wallet.data.minerFee.MinerFeeSupplier; import com.sparrowwallet.sparrow.AppServices; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; public class SparrowMinerFeeSupplier implements MinerFeeSupplier { private static final int FALLBACK_FEE_RATE = 75; @@ -30,15 +32,6 @@ public class SparrowMinerFeeSupplier implements MinerFeeSupplier { return getMinimumFeeForTarget(Integer.parseInt(feeTarget.getValue())); } - @Override - public MinerFee getValue() { - Map fees = new LinkedHashMap<>(); - for (MinerFeeTarget minerFeeTarget : MinerFeeTarget.values()) { - fees.put(minerFeeTarget.getValue(), getFee(minerFeeTarget)); - } - return new MinerFee(fees); - } - private Integer getMinimumFeeForTarget(int targetBlocks) { List> feeRates = new ArrayList<>(AppServices.getTargetBlockFeeRates().entrySet()); Collections.reverse(feeRates); diff --git a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowWalletStateSupplier.java b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowWalletStateSupplier.java index 6bde174c..8335cc78 100644 --- a/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowWalletStateSupplier.java +++ b/src/main/java/com/sparrowwallet/sparrow/whirlpool/dataSource/SparrowWalletStateSupplier.java @@ -23,7 +23,7 @@ public class SparrowWalletStateSupplier implements WalletStateSupplier { private final WhirlpoolClientConfig config; private IIndexHandler externalIndexHandler; - public SparrowWalletStateSupplier(String walletId, WhirlpoolClientConfig config) throws Exception { + public SparrowWalletStateSupplier(String walletId, WhirlpoolClientConfig config) { this.walletId = walletId; this.indexHandlerWallets = new LinkedHashMap<>(); this.config = config; @@ -89,11 +89,6 @@ public class SparrowWalletStateSupplier implements WalletStateSupplier { // nothing required } - @Override - public void setWalletIndex(WhirlpoolAccount whirlpoolAccount, AddressType addressType, Chain chain, int i) throws Exception { - // nothing required - } - @Override public void load() throws Exception { // nothing required