Skip to content

Commit 55f640a

Browse files
committed
fix npe
1 parent c942e18 commit 55f640a

6 files changed

Lines changed: 47 additions & 64 deletions

File tree

src/main/java/com/binance/dex/api/client/BinanceDexApiClientFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public BinanceDexApiRestClient newRestClient(String baseUrl) {
2121
}
2222

2323
public BinanceDexApiNodeClient newNodeRpcClient() {
24-
return newNodeRpcClient(BinanceDexEnvironment.TEST_NET_NODE.getBaseUrl(), BinanceDexEnvironment.TEST_NET_NODE.getHrp());
24+
return newNodeRpcClient(BinanceDexEnvironment.PROD.getNodeUrl(), BinanceDexEnvironment.PROD.getHrp());
2525
}
2626

2727
public BinanceDexApiNodeClient newNodeRpcClient(String baseUrl, String hrp) {

src/main/java/com/binance/dex/api/client/BinanceDexEnvironment.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@
66
public class BinanceDexEnvironment {
77
public static final BinanceDexEnvironment PROD = new BinanceDexEnvironment(
88
"https://dex.binance.org",
9-
"wss://dex.binance.org/api/",
9+
"https://dataseed1.ninicoin.io",
10+
"wss://dataseed1.ninicoin.io/websocket",
1011
"bnb"
1112
);
1213
public static final BinanceDexEnvironment TEST_NET = new BinanceDexEnvironment(
1314
"https://testnet-dex.binance.org",
14-
"wss://testnet-dex.binance.org/api/",
15-
"tbnb"
16-
);
17-
public static final BinanceDexEnvironment TEST_NET_NODE = new BinanceDexEnvironment(
1815
"http://data-seed-pre-0-s3.binance.org",
1916
"wss://data-seed-pre-0-s3.binance.org/websocket",
2017
"tbnb"
2118
);
2219

2320
// Rest API base URL
2421
private String baseUrl;
22+
// RPC API base URL
23+
private String nodeUrl;
2524
// Websocket API base URL
2625
private String wsBaseUrl;
2726
// Address human readable part prefix
2827
private String hrp;
2928

30-
public BinanceDexEnvironment(String baseUrl, String wsBaseUrl, String hrp) {
29+
public BinanceDexEnvironment(String baseUrl,String nodeUrl ,String wsBaseUrl, String hrp) {
3130
this.baseUrl = baseUrl;
31+
this.nodeUrl = nodeUrl;
3232
this.wsBaseUrl = wsBaseUrl;
3333
this.hrp = hrp;
3434
}
@@ -68,4 +68,8 @@ public int hashCode() {
6868
.append(hrp)
6969
.toHashCode();
7070
}
71+
72+
public String getNodeUrl() {
73+
return nodeUrl;
74+
}
7175
}

src/main/java/com/binance/dex/api/client/impl/BinanceDexApiNodeClientImpl.java

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,16 @@ public AccountSequence getAccountSequence(String address) {
5555

5656
@Override
5757
public Infos getNodeInfo() {
58-
try {
59-
JsonRpcResponse<NodeInfos> rpcResponse = binanceDexNodeApi.getNodeStatus().execute().body();
60-
checkRpcResult(rpcResponse);
61-
NodeInfos nodeInfos = rpcResponse.getResult();
62-
return convert(nodeInfos);
63-
} catch (IOException e) {
64-
throw new RuntimeException(e);
65-
}
58+
JsonRpcResponse<NodeInfos> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getNodeStatus());
59+
checkRpcResult(rpcResponse);
60+
NodeInfos nodeInfos = rpcResponse.getResult();
61+
return convert(nodeInfos);
6662
}
6763

6864
@Override
6965
public List<Fees> getFees() {
7066
try {
71-
JsonRpcResponse<ABCIQueryResult> rpcResponse = binanceDexNodeApi.getFees().execute().body();
67+
JsonRpcResponse<ABCIQueryResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getFees());
7268
checkRpcResult(rpcResponse);
7369
byte[] value = rpcResponse.getResult().getResponse().getValue();
7470
int startIndex = 2;
@@ -87,7 +83,7 @@ public List<Fees> getFees() {
8783
public Account getAccount(String address) {
8884
String encodedAddress = "0x" + ARG_ACCOUNT_PREFIX + Hex.toHexString(Crypto.decodeAddress(address));
8985
try {
90-
JsonRpcResponse<AccountResult> response = binanceDexNodeApi.getAccount(encodedAddress).execute().body();
86+
JsonRpcResponse<AccountResult> response = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getAccount(encodedAddress));
9187
checkRpcResult(response);
9288
if(response.getResult().getResponse().getValue() != null){
9389
byte[] value = response.getResult().getResponse().getValue();
@@ -104,23 +100,19 @@ public Account getAccount(String address) {
104100

105101
@Override
106102
public List<Transaction> getBlockTransactions(Long height) {
107-
try {
108-
JsonRpcResponse<BlockInfoResult> response = binanceDexNodeApi
109-
.getBlockTransactions("\"tx.height=" + height.toString() + "\"").execute().body();
110-
checkRpcResult(response);
111-
return response.getResult().getTxs().stream()
112-
.map(transactionConverter::convert)
113-
.flatMap(List::stream)
114-
.collect(Collectors.toList());
115-
} catch (IOException e) {
116-
throw new RuntimeException(e);
117-
}
103+
JsonRpcResponse<BlockInfoResult> response = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi
104+
.getBlockTransactions("\"tx.height=" + height.toString() + "\""));
105+
checkRpcResult(response);
106+
return response.getResult().getTxs().stream()
107+
.map(transactionConverter::convert)
108+
.flatMap(List::stream)
109+
.collect(Collectors.toList());
118110
}
119111

120112
@Override
121113
public Transaction getTransaction(String hash) {
122114
try {
123-
JsonRpcResponse<TransactionResult> rpcResponse = binanceDexNodeApi.getTransaction("0x" + hash).execute().body();
115+
JsonRpcResponse<TransactionResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getTransaction("0x" + hash));
124116
checkRpcResult(rpcResponse);
125117
TransactionResult transactionResult = rpcResponse.getResult();
126118
byte[] txBytes = transactionResult.getTx();
@@ -152,25 +144,17 @@ public Transaction getTransaction(String hash) {
152144

153145
@Override
154146
public BlockMeta getBlockMetaByHeight(Long height) {
155-
try {
156-
JsonRpcResponse<BlockMeta.BlockMetaResult> rpcResponse = binanceDexNodeApi.getBlock(height).execute().body();
157-
checkRpcResult(rpcResponse);
158-
BlockMeta.BlockMetaResult result = rpcResponse.getResult();
159-
return result.getBlockMeta();
160-
} catch (Exception e) {
161-
throw new RuntimeException(e);
162-
}
147+
JsonRpcResponse<BlockMeta.BlockMetaResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getBlock(height));
148+
checkRpcResult(rpcResponse);
149+
BlockMeta.BlockMetaResult result = rpcResponse.getResult();
150+
return result.getBlockMeta();
163151
}
164152

165153
@Override
166154
public BlockMeta getBlockMetaByHash(String hash) {
167155
try {
168-
JsonRpcResponse<BlockMeta.BlockMetaResult> rpcResponse = binanceDexNodeApi.getBlock("0x" + hash).execute().body();
156+
JsonRpcResponse<BlockMeta.BlockMetaResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getBlock("0x" + hash));
169157
checkRpcResult(rpcResponse);
170-
if (null != rpcResponse.getError() && null != rpcResponse.getError().getCode() && rpcResponse.getError().getCode().intValue() != 0) {
171-
throw new RuntimeException(rpcResponse.getError().toString());
172-
}
173-
174158
BlockMeta.BlockMetaResult result = rpcResponse.getResult();
175159
return result.getBlockMeta();
176160
} catch (Exception e) {
@@ -182,7 +166,7 @@ public BlockMeta getBlockMetaByHash(String hash) {
182166
public com.binance.dex.api.client.domain.Token getTokenInfoBySymbol(String symbol) {
183167
try {
184168
String pathWithSymbol = "\"tokens/info/" + symbol + "\"";
185-
JsonRpcResponse<ABCIQueryResult> rpcResponse = binanceDexNodeApi.getTokenInfo(pathWithSymbol).execute().body();
169+
JsonRpcResponse<ABCIQueryResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getTokenInfo(pathWithSymbol));
186170
checkRpcResult(rpcResponse);
187171
byte[] value = rpcResponse.getResult().getResponse().getValue();
188172
int startIndex = getStartIndex(value);
@@ -197,22 +181,18 @@ public com.binance.dex.api.client.domain.Token getTokenInfoBySymbol(String symbo
197181

198182
@Override
199183
public List<StakeValidator> getStakeValidator() {
200-
try {
201-
JsonRpcResponse<ABCIQueryResult> rpcResponse = binanceDexNodeApi.getStakeValidators().execute().body();
202-
checkRpcResult(rpcResponse);
203-
byte[] value = rpcResponse.getResult().getResponse().getValue();
204-
return StakeValidator.fromJsonToArray(new String(value), hrp);
205-
} catch (IOException e) {
206-
throw new RuntimeException(e);
207-
}
184+
JsonRpcResponse<ABCIQueryResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getStakeValidators());
185+
checkRpcResult(rpcResponse);
186+
byte[] value = rpcResponse.getResult().getResponse().getValue();
187+
return StakeValidator.fromJsonToArray(new String(value), hrp);
208188
}
209189

210190
@Override
211191
public Proposal getProposalById(String proposalId) {
212192
try {
213193
Map.Entry proposalIdEntry = Maps.immutableEntry("ProposalID", proposalId);
214194
String requestData = "0x" + Hex.toHexString(EncodeUtils.toJsonStringSortKeys(proposalIdEntry).getBytes());
215-
JsonRpcResponse<ABCIQueryResult> rpcResponse = binanceDexNodeApi.getProposalById(requestData).execute().body();
195+
JsonRpcResponse<ABCIQueryResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.getProposalById(requestData));
216196
checkRpcResult(rpcResponse);
217197
ABCIQueryResult.Response response = rpcResponse.getResult().getResponse();
218198
if (response.getCode() != null) {
@@ -313,13 +293,11 @@ protected com.binance.dex.api.client.domain.Token convert(TokenInfo tokenInfo) {
313293

314294
protected List<TransactionMetadata> syncBroadcast(String requestBody, Wallet wallet) {
315295
try {
316-
CommitBroadcastResult commitBroadcastResult = binanceDexNodeApi.commitBroadcast(requestBody).execute().body().getResult();
296+
JsonRpcResponse<CommitBroadcastResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.commitBroadcast(requestBody));
297+
CommitBroadcastResult commitBroadcastResult = rpcResponse.getResult();
317298
TransactionMetadata transactionMetadata = new TransactionMetadata();
318299
transactionMetadata.setCode(commitBroadcastResult.getCheckTx().getCode());
319-
if (commitBroadcastResult != null
320-
&& commitBroadcastResult.getHeight() != null
321-
&& StringUtils.isNoneBlank(commitBroadcastResult.getHash())
322-
&& transactionMetadata.getCode() == 0) {
300+
if (commitBroadcastResult.getHeight() != null && StringUtils.isNoneBlank(commitBroadcastResult.getHash()) && transactionMetadata.getCode() == 0) {
323301
wallet.increaseAccountSequence();
324302
transactionMetadata.setHash(commitBroadcastResult.getHash());
325303
transactionMetadata.setHeight(commitBroadcastResult.getHeight());
@@ -331,21 +309,22 @@ protected List<TransactionMetadata> syncBroadcast(String requestBody, Wallet wal
331309
}
332310

333311
return Lists.newArrayList(transactionMetadata);
334-
} catch (BinanceDexApiException | IOException e) {
312+
} catch (BinanceDexApiException e) {
335313
wallet.invalidAccountSequence();
336314
throw new RuntimeException(e);
337315
}
338316
}
339317

340318
protected List<TransactionMetadata> asyncBroadcast(String requestBody, Wallet wallet) {
341319
try {
342-
AsyncBroadcastResult asyncBroadcastResult = binanceDexNodeApi.asyncBroadcast(requestBody).execute().body().getResult();
320+
JsonRpcResponse<AsyncBroadcastResult> rpcResponse = BinanceDexApiClientGenerator.executeSync(binanceDexNodeApi.asyncBroadcast(requestBody));
321+
AsyncBroadcastResult asyncBroadcastResult = rpcResponse.getResult();
343322
TransactionMetadata transactionMetadata = new TransactionMetadata();
344323

345324
transactionMetadata.setCode(asyncBroadcastResult.getCode());
346325
transactionMetadata.setLog(asyncBroadcastResult.getLog());
347326

348-
if (asyncBroadcastResult != null && asyncBroadcastResult.getCode().intValue() == 0) {
327+
if (asyncBroadcastResult.getCode() == 0) {
349328
wallet.increaseAccountSequence();
350329
transactionMetadata.setHash(asyncBroadcastResult.getHash());
351330
transactionMetadata.setData(asyncBroadcastResult.getData());
@@ -356,7 +335,7 @@ protected List<TransactionMetadata> asyncBroadcast(String requestBody, Wallet wa
356335
}
357336

358337
return Lists.newArrayList(transactionMetadata);
359-
} catch (BinanceDexApiException | IOException e) {
338+
} catch (BinanceDexApiException e) {
360339
wallet.invalidAccountSequence();
361340
throw new RuntimeException(e);
362341
}

src/test/java/com/binance/dex/api/client/examples/BinanceDexWSApiExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class BinanceDexWSApiExample {
2323

2424
@Before
2525
public void before(){
26-
binanceDexWSApi = BinanceDexClientWSFactory.getWSApiImpl(BinanceDexEnvironment.TEST_NET_NODE);
26+
binanceDexWSApi = BinanceDexClientWSFactory.getWSApiImpl(BinanceDexEnvironment.TEST_NET);
2727
}
2828

2929
@Test

src/test/java/com/binance/dex/api/client/examples/NodeClientExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class NodeClientExample {
2828

2929
@Before
3030
public void setup() {
31-
binanceDexNodeApi = BinanceDexApiClientFactory.newInstance().newNodeRpcClient();
31+
binanceDexNodeApi = BinanceDexApiClientFactory.newInstance().newNodeRpcClient(BinanceDexEnvironment.TEST_NET.getNodeUrl(),BinanceDexEnvironment.TEST_NET.getHrp());
3232
}
3333

3434
@Test

src/test/java/com/binance/dex/api/client/examples/WebsocketExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void onMessage(JsonRpcResponse response) {
3737
throw new RuntimeException(e);
3838
}
3939
}
40-
},BinanceDexEnvironment.TEST_NET_NODE.getWsBaseUrl());
40+
},BinanceDexEnvironment.TEST_NET.getWsBaseUrl());
4141
WebsocketLauncher.startUp(endpoint);
4242
}
4343

0 commit comments

Comments
 (0)