Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit333cfe4

Browse files
committed
Contract creation API
1 parent0e1dccc commit333cfe4

File tree

8 files changed

+196
-5
lines changed

8 files changed

+196
-5
lines changed

‎src/main/java/io/goodforgod/api/etherscan/AccountAPIProvider.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public List<Balance> balances(@NotNull List<String> addresses) throws EtherScanE
9595
finalList<List<String>>addressesAsBatches =BasicUtils.partition(addresses,20);
9696

9797
for (finalList<String>batch :addressesAsBatches) {
98-
finalStringurlParams =ACT_BALANCE_MULTI_ACTION +TAG_LATEST_PARAM +ADDRESS_PARAM +toAddressParam(batch);
98+
finalStringurlParams =ACT_BALANCE_MULTI_ACTION +TAG_LATEST_PARAM +ADDRESS_PARAM +BasicUtils.toAddressParam(batch);
9999
finalBalanceResponseTOresponse =getRequest(urlParams,BalanceResponseTO.class);
100100
if (response.getStatus() !=1) {
101101
thrownewEtherScanResponseException(response);
@@ -111,10 +111,6 @@ public List<Balance> balances(@NotNull List<String> addresses) throws EtherScanE
111111
returnbalances;
112112
}
113113

114-
privateStringtoAddressParam(List<String>addresses) {
115-
returnString.join(",",addresses);
116-
}
117-
118114
@NotNull
119115
@Override
120116
publicList<Tx>txs(@NotNullStringaddress)throwsEtherScanException {

‎src/main/java/io/goodforgod/api/etherscan/ContractAPI.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
importio.goodforgod.api.etherscan.error.EtherScanException;
44
importio.goodforgod.api.etherscan.model.Abi;
5+
importio.goodforgod.api.etherscan.model.ContractCreation;
56
importorg.jetbrains.annotations.NotNull;
67

8+
importjava.util.List;
9+
710
/**
811
* EtherScan - API Descriptions <a href="https://docs.etherscan.io/api-endpoints/contracts">...</a>
912
*
@@ -21,4 +24,12 @@ public interface ContractAPI {
2124
*/
2225
@NotNull
2326
AbicontractAbi(@NotNullStringaddress)throwsEtherScanException;
27+
28+
/**
29+
* Returns a contract's deployer address and transaction hash it was created, up to 5 at a time.
30+
* @param contractAddresses - list of addresses to fetch
31+
* @throws EtherScanException parent exception class
32+
*/
33+
@NotNull
34+
List<ContractCreation>contractCreation(@NotNullList<String>contractAddresses)throwsEtherScanException;
2435
}

‎src/main/java/io/goodforgod/api/etherscan/ContractAPIProvider.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
importio.goodforgod.api.etherscan.http.EthHttpClient;
66
importio.goodforgod.api.etherscan.manager.RequestQueueManager;
77
importio.goodforgod.api.etherscan.model.Abi;
8+
importio.goodforgod.api.etherscan.model.ContractCreation;
9+
importio.goodforgod.api.etherscan.model.response.ContractCreationResponseTO;
810
importio.goodforgod.api.etherscan.model.response.StringResponseTO;
911
importio.goodforgod.api.etherscan.util.BasicUtils;
1012
importorg.jetbrains.annotations.NotNull;
1113

14+
importjava.util.List;
15+
importjava.util.stream.Collectors;
16+
1217
/**
1318
* Contract API Implementation
1419
*
@@ -22,6 +27,12 @@ final class ContractAPIProvider extends BasicProvider implements ContractAPI {
2227

2328
privatestaticfinalStringADDRESS_PARAM ="&address=";
2429

30+
privatestaticfinalStringACT_CONTRACT_CREATION_PARAM ="getcontractcreation";
31+
32+
privatestaticfinalStringACT_CONTRACT_CREATION =ACT_PREFIX +ACT_CONTRACT_CREATION_PARAM;
33+
34+
privatestaticfinalStringACT_CONTRACT_ADDRESSES_PARAM ="&contractaddresses=";
35+
2536
ContractAPIProvider(RequestQueueManagerrequestQueueManager,
2637
StringbaseUrl,
2738
EthHttpClientexecutor,
@@ -44,4 +55,23 @@ public Abi contractAbi(@NotNull String address) throws EtherScanException {
4455
?Abi.nonVerified()
4556
:Abi.verified(response.getResult());
4657
}
58+
59+
@NotNull
60+
@Override
61+
publicList<ContractCreation>contractCreation(@NotNullList<String>contractAddresses)throwsEtherScanException {
62+
BasicUtils.validateAddresses(contractAddresses);
63+
finalStringurlParam =ACT_CONTRACT_CREATION +ACT_CONTRACT_ADDRESSES_PARAM +BasicUtils.toAddressParam(contractAddresses);
64+
finalContractCreationResponseTOresponse =getRequest(urlParam,ContractCreationResponseTO.class);
65+
if (response.getStatus() !=1 &&response.getMessage().startsWith("NOTOK")) {
66+
thrownewEtherScanResponseException(response);
67+
}
68+
69+
returnresponse.getResult().stream()
70+
.map(to ->ContractCreation.builder()
71+
.withContractCreator(to.getContractCreator())
72+
.withContractAddress(to.getContractAddress())
73+
.withTxHash(to.getTxHash())
74+
.build()
75+
).collect(Collectors.toList());
76+
}
4777
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
packageio.goodforgod.api.etherscan.model;
2+
3+
importjava.util.Objects;
4+
5+
publicclassContractCreation {
6+
privatefinalStringcontractAddress;
7+
privatefinalStringcontractCreator;
8+
privatefinalStringtxHash;
9+
10+
privateContractCreation(StringcontractAddress,StringcontractCreator,StringtxHash) {
11+
this.contractAddress =contractAddress;
12+
this.contractCreator =contractCreator;
13+
this.txHash =txHash;
14+
}
15+
16+
publicStringgetContractAddress() {
17+
returncontractAddress;
18+
}
19+
20+
publicStringgetContractCreator() {
21+
returncontractCreator;
22+
}
23+
24+
publicStringgetTxHash() {
25+
returntxHash;
26+
}
27+
28+
@Override
29+
publicbooleanequals(Objecto) {
30+
if (this ==o)returntrue;
31+
if (o ==null ||getClass() !=o.getClass())returnfalse;
32+
ContractCreationthat = (ContractCreation)o;
33+
returnObjects.equals(contractAddress,that.contractAddress) &&Objects.equals(contractCreator,that.contractCreator) &&Objects.equals(txHash,that.txHash);
34+
}
35+
36+
@Override
37+
publicinthashCode() {
38+
returnObjects.hash(contractAddress,contractCreator,txHash);
39+
}
40+
41+
@Override
42+
publicStringtoString() {
43+
return"ContractCreation{" +
44+
"contractAddress='" +contractAddress +'\'' +
45+
", contractCreator='" +contractCreator +'\'' +
46+
", txHash='" +txHash +'\'' +
47+
'}';
48+
}
49+
50+
publicstaticContractCreationBuilderbuilder() {
51+
returnnewContractCreationBuilder();
52+
}
53+
54+
publicstaticfinalclassContractCreationBuilder {
55+
privateStringcontractAddress;
56+
privateStringcontractCreator;
57+
privateStringtxHash;
58+
59+
privateContractCreationBuilder() {}
60+
61+
publicContractCreationBuilderwithContractAddress(StringcontractAddress) {
62+
this.contractAddress =contractAddress;
63+
returnthis;
64+
}
65+
66+
publicContractCreationBuilderwithContractCreator(StringcontractCreator) {
67+
this.contractCreator =contractCreator;
68+
returnthis;
69+
}
70+
71+
publicContractCreationBuilderwithTxHash(StringtxHash) {
72+
this.txHash =txHash;
73+
returnthis;
74+
}
75+
76+
publicContractCreationbuild() {
77+
returnnewContractCreation(contractAddress,contractCreator,txHash);
78+
}
79+
}
80+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
packageio.goodforgod.api.etherscan.model.response;
2+
3+
publicclassContractCreationResponseTOextendsBaseListResponseTO<ContractCreationTO> {
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packageio.goodforgod.api.etherscan.model.response;
2+
3+
publicclassContractCreationTO {
4+
5+
privateStringcontractAddress;
6+
privateStringcontractCreator;
7+
privateStringtxHash;
8+
9+
publicStringgetContractAddress() {
10+
returncontractAddress;
11+
}
12+
13+
publicStringgetContractCreator() {
14+
returncontractCreator;
15+
}
16+
17+
publicStringgetTxHash() {
18+
returntxHash;
19+
}
20+
}

‎src/main/java/io/goodforgod/api/etherscan/util/BasicUtils.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,8 @@ public static List<List<String>> partition(List<String> list, int pairSize) {
149149

150150
returnpartitioned;
151151
}
152+
153+
publicstaticStringtoAddressParam(List<String>addresses) {
154+
returnString.join(",",addresses);
155+
}
152156
}

‎src/test/java/io/goodforgod/api/etherscan/contract/ContractApiTests.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
importio.goodforgod.api.etherscan.ApiRunner;
44
importio.goodforgod.api.etherscan.error.EtherScanInvalidAddressException;
55
importio.goodforgod.api.etherscan.model.Abi;
6+
importio.goodforgod.api.etherscan.model.ContractCreation;
67
importorg.junit.jupiter.api.Test;
78

9+
importjava.util.Arrays;
10+
importjava.util.Collections;
11+
importjava.util.List;
12+
813
/**
914
* @author GoodforGod
1015
* @since 03.11.2018
@@ -37,4 +42,45 @@ void correctParamWithEmptyExpectedResult() {
3742
assertNotNull(abi);
3843
assertTrue(abi.isVerified());
3944
}
45+
46+
@Test
47+
voidcorrectContractCreation() {
48+
List<ContractCreation>contractCreations =
49+
getApi().contract().contractCreation(Collections.singletonList("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413"));
50+
51+
assertEquals(1,contractCreations.size());
52+
ContractCreationcontractCreation =contractCreations.get(0);
53+
54+
assertEquals("0xbb9bc244d798123fde783fcc1c72d3bb8c189413",contractCreation.getContractAddress());
55+
assertEquals("0x793ea9692ada1900fbd0b80fffec6e431fe8b391",contractCreation.getContractCreator());
56+
assertEquals("0xe9ebfecc2fa10100db51a4408d18193b3ac504584b51a4e55bdef1318f0a30f9",contractCreation.getTxHash());
57+
}
58+
59+
@Test
60+
voidcorrectMultipleContractCreation() {
61+
List<ContractCreation>contractCreations =
62+
getApi().contract().contractCreation(Arrays.asList("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413","0x5EaC95ad5b287cF44E058dCf694419333b796123"));
63+
assertEquals(2,contractCreations.size());
64+
65+
ContractCreationcontractCreation1 =ContractCreation.builder()
66+
.withContractAddress("0xbb9bc244d798123fde783fcc1c72d3bb8c189413")
67+
.withContractCreator("0x793ea9692ada1900fbd0b80fffec6e431fe8b391")
68+
.withTxHash("0xe9ebfecc2fa10100db51a4408d18193b3ac504584b51a4e55bdef1318f0a30f9")
69+
.build();
70+
71+
ContractCreationcontractCreation2 =ContractCreation.builder()
72+
.withContractAddress("0x5eac95ad5b287cf44e058dcf694419333b796123")
73+
.withContractCreator("0x7c675b7450e878e5af8550b41df42d134674e61f")
74+
.withTxHash("0x79cdfec19e5a86d9022680a4d1c86d3d8cd76c21c01903a2f02c127a0a7dbfb3")
75+
.build();
76+
77+
assertTrue(contractCreations.contains(contractCreation1));
78+
assertTrue(contractCreations.contains(contractCreation2));
79+
}
80+
81+
@Test
82+
voidcontractCreationInvalidParamWithError() {
83+
assertThrows(EtherScanInvalidAddressException.class,
84+
() ->getApi().contract().contractCreation(Collections.singletonList("0xBBbc244D798123fDe783fCc1C72d3Bb8C189414")));
85+
}
4086
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp