Skip to content

Commit d4e0ab5

Browse files
committed
Change sendMany call to receive generic JSONObject instead of multiple parameters
1 parent 047fce9 commit d4e0ab5

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

bitgo-java-api/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515
annotationProcessor 'org.projectlombok:lombok:1.18.2'
1616
testAnnotationProcessor 'org.projectlombok:lombok:1.18.2'
1717
compile 'org.slf4j:slf4j-api:1.7.25',
18-
'com.fasterxml.jackson.core:jackson-databind:2.9.7'
18+
'com.fasterxml.jackson.core:jackson-databind:2.9.7',
19+
'org.json:json:20190722'
1920
testCompile 'junit:junit:4.12'
2021
}

bitgo-java-api/src/main/java/com/bitso/bitgo/v2/BitGoClient.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bitso.bitgo.v2.entity.SendCoinsResponse;
44
import com.bitso.bitgo.v2.entity.Wallet;
55
import com.bitso.bitgo.v2.entity.WalletTransferResponse;
6+
import org.json.JSONObject;
67

78
import java.io.IOException;
89
import java.math.BigDecimal;
@@ -39,26 +40,22 @@ Optional<String> login(String email, String password, String otp, boolean extens
3940

4041
/**
4142
* Invokes the sendmany method see https://www.bitgo.com/api/v2/?shell#send-transaction-to-many
42-
*
43-
* @param coin tbtc for test bitcoin, see full list at https://www.bitgo.com/api/v2/#coin-digital-currency-support
44-
* @param walletId The ID of the source wallet.
45-
* @param walletPass The wallet passphrase.
46-
* @param recipients A map with the recipients' addresses as keys and the corresponding
43+
* Required parameters are:
44+
* coin tbtc for test bitcoin, see full list at https://www.bitgo.com/api/v2/#coin-digital-currency-support
45+
* walletId The ID of the source wallet.
46+
* walletPass The wallet passphrase.
47+
* recipients A map with the recipients' addresses as keys and the corresponding
4748
* amounts as values. Amounts are in satoshis
48-
* @param sequenceId A unique identifier for this transaction (optional).
49-
* @param message Notes about the transaction (optional).
50-
* @param fee Fee (in satoshis), leave null for autodetect. Do not specify unless you are sure it is sufficient.
51-
* @param feeTxConfirmTarget Calculate fees per kilobyte, targeting transaction confirmation in this number of blocks. Default: 2, Minimum: 2, Maximum: 20
52-
* @param minConfirms only choose unspent inputs with a certain number of confirmations. We recommend setting this to 1 and using enforceMinConfirmsForChange
53-
* @param enforceMinConfirmsForChange Defaults to false. When constructing a transaction, minConfirms will only be enforced for unspents not originating from the wallet
49+
* sequenceId A unique identifier for this transaction (optional).
50+
* Optional parameters are:
51+
* message Notes about the transaction (optional).
52+
* fee Fee (in satoshis), leave null for autodetect. Do not specify unless you are sure it is sufficient.
53+
* feeTxConfirmTarget Calculate fees per kilobyte, targeting transaction confirmation in this number of blocks. Default: 2, Minimum: 2, Maximum: 20
54+
* minConfirms only choose unspent inputs with a certain number of confirmations. We recommend setting this to 1 and using enforceMinConfirmsForChange
55+
* enforceMinConfirmsForChange Defaults to false. When constructing a transaction, minConfirms will only be enforced for unspents not originating from the wallet
5456
* @return A SendCoinsResponse, or empty if there was a problem (although more likely in case of a problem it will throw).
5557
*/
56-
Optional<SendCoinsResponse> sendMany(String coin, String walletId, String walletPass,
57-
Map<String, BigDecimal> recipients,
58-
String sequenceId, String message,
59-
BigDecimal fee, BigDecimal feeTxConfirmTarget,
60-
int minConfirms, boolean enforceMinConfirmsForChange)
61-
throws IOException;
58+
Optional<SendCoinsResponse> sendMany(JSONObject parameters) throws IOException;
6259

6360
Optional<Map<String, Object>> getCurrentUserProfile() throws IOException;
6461

bitgo-java-api/src/main/java/com/bitso/bitgo/v2/BitGoClientImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Scanner;
2323
import java.util.concurrent.TimeUnit;
2424

25+
import org.json.JSONObject;
26+
2527
/**
2628
* Implementation of the BitGo client.
2729
*
@@ -104,12 +106,25 @@ public Optional<Wallet> getWalletByAddress(String coin, String waddress) throws
104106
}
105107

106108
@Override
107-
public Optional<SendCoinsResponse> sendMany(String coin, String walletId, String walletPass,
108-
Map<String, BigDecimal> recipients,
109-
String sequenceId, String message,
110-
BigDecimal fee, BigDecimal feeTxConfirmTarget,
111-
int minConfirms, boolean enforceMinConfirmsForChange)
112-
throws IOException {
109+
public Optional<SendCoinsResponse> sendMany(JSONObject parameters) throws IOException {
110+
// Get all needed parameters
111+
String coin = parameters.getString("coin");
112+
String walletId = parameters.getString("walletId");
113+
String walletPass = parameters.getString("walletPass");
114+
JSONObject targets = parameters.getJSONObject("recipients");
115+
HashMap<String, BigDecimal> recipients = new HashMap<String, BigDecimal>(targets.length());
116+
for (String address : targets.keySet()) {
117+
recipients.put(address, targets.getBigDecimal(address));
118+
}
119+
String sequenceId = parameters.getString("sequenceId");
120+
// Check for optional parameters or default them
121+
String message = parameters.has("message") ? parameters.getString("message") : "";
122+
BigDecimal fee = parameters.has("fee") ? parameters.getBigDecimal("fee") : BigDecimal.ZERO;
123+
BigDecimal feeTxConfirmTarget = parameters.has("feeTxConfirmTarget") ?
124+
parameters.getBigDecimal("feeTxConfirmTarget") : BigDecimal.ZERO;
125+
int minConfirms = parameters.has("minConfirms") ? parameters.getInt("minConfirms") : 0;
126+
boolean enforceMinConfirmsForChange = parameters.has("enforceMinConfirmsForChange") ?
127+
parameters.getBoolean("enforceMinConfirmsForChange") : false;
113128
String url = baseUrl + SEND_MANY_URL.replace("$COIN", coin).replace("$WALLET", walletId);
114129

115130
final List<Map<String, Object>> addr = new ArrayList<>(recipients.size());

bitgo-java-api/src/test/java/com/bitso/bitgo/util/TestClientV2.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.bitso.bitgo.v2.entity.Wallet;
77
import com.bitso.bitgo.v2.entity.WalletTransferResponse;
88
import lombok.extern.slf4j.Slf4j;
9+
import org.json.JSONObject;
910
import org.junit.Assert;
1011
import org.junit.Ignore;
1112
import org.junit.Test;
@@ -41,7 +42,7 @@ public class TestClientV2 {
4142
@Test
4243
@Ignore
4344
public void testSendMany() throws IOException {
44-
45+
JSONObject parameters = new JSONObject();
4546
int unlockResult = client.unlock("0000000", TimeUnit.HOURS.toSeconds(1));
4647
System.out.println(unlockResult);
4748
assertTrue(unlockResult != 400 && unlockResult != 401);
@@ -52,9 +53,15 @@ public void testSendMany() throws IOException {
5253
targets.put(TLTC_TEST_FAUCET_ADDRESS, amount);
5354
amount = amount.add(initAmount);
5455
}
55-
// targets.put("[ADDRESS2]", new BigDecimal("0.001"));
56-
Optional<SendCoinsResponse> resp = client.sendMany(COIN, WALLET_ID, WALLET_PASSPHRASE, targets, null,
57-
"test", null, null, 1, true);
56+
parameters.put("coin", COIN);
57+
parameters.put("walletId", WALLET_ID);
58+
parameters.put("walletPass", WALLET_PASSPHRASE);
59+
parameters.put("recipients", targets);
60+
parameters.put("message", "test");
61+
parameters.put("minConfirms", 1);
62+
parameters.put("enforceMinConfirmsForChange", true);
63+
parameters.put("sequenceId", "btc33");
64+
Optional<SendCoinsResponse> resp = client.sendMany(parameters);
5865
Assert.assertTrue(resp.isPresent());
5966
Assert.assertNotNull(resp.get().getTx());
6067

@@ -90,7 +97,7 @@ public void listWalletTransfers() throws IOException {
9097
// for (int i = 0; i < 30; i++) {
9198
// resp.getTransfers().remove(0);
9299
// }
93-
System.out.println("resp json = "+ SerializationUtil.mapper.writeValueAsString(resp));
100+
System.out.println("resp json = " + SerializationUtil.mapper.writeValueAsString(resp));
94101

95102

96103
}

0 commit comments

Comments
 (0)