Skip to content

Commit 6acf5ab

Browse files
authored
Merge pull request #61 from EbenezerGH/add-smart-contract-abi
Add ABI Contracts
2 parents dc6dd5d + 16fc5a1 commit 6acf5ab

File tree

9 files changed

+242
-3
lines changed

9 files changed

+242
-3
lines changed

app/src/main/java/jfyg/etherscan/helloetherescan/MainActivity.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.util.Log
66
import io.reactivex.android.schedulers.AndroidSchedulers
77
import io.reactivex.rxkotlin.subscribeBy
88
import jfyg.account.Account
9+
import jfyg.contract.SmartContract
910
import jfyg.stat.Stat
1011
import kotlinx.android.synthetic.main.activity_main.*
1112

@@ -21,22 +22,29 @@ class MainActivity : AppCompatActivity() {
2122
//************************************************ Used To Test Singles returned from etherscanapi Module
2223
val stat = Stat()
2324
val account = Account()
25+
val contract = SmartContract()
2426

2527

2628
fab.setOnClickListener {
2729

2830
//stat test
29-
stat.getLastPriceInBtc()?.observeOn(AndroidSchedulers.mainThread())
31+
stat.getLastPriceInBtc().observeOn(AndroidSchedulers.mainThread())
3032
?.subscribeBy {
3133
Log.d(TAG, "The current price of Ether in Btc: $it")
3234
}
3335

3436
//account test
35-
account.getTransactions("0x2c1ba59d6f58433fb1eaee7d20b26ed83bda51a3")?.observeOn(AndroidSchedulers.mainThread())
37+
account.getTransactions("0x2c1ba59d6f58433fb1eaee7d20b26ed83bda51a3").observeOn(AndroidSchedulers.mainThread())
3638
?.subscribeBy {
3739
Log.d(TAG, "The Account Size of Transactions is: ${it.size}")
3840
}
3941

42+
//contracts test
43+
contract.getContractABI("0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413").observeOn(AndroidSchedulers.mainThread())
44+
?.subscribeBy {
45+
Log.d(TAG, "The EBI has returned: $it")
46+
}
47+
4048
//transaction test
4149
}
4250
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jfyg.contract
2+
3+
import io.reactivex.Single
4+
import jfyg.queries.QueryMediator
5+
6+
class SmartContract : SmartContractContract {
7+
private val query = QueryMediator()
8+
private val abiQuery = query.abiContract("contract",
9+
"getabi",
10+
"0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413")
11+
12+
override fun getContractABI(account: String?): Single<String> = query.abiContract("contract",
13+
"getabi",
14+
account).map { it.result }
15+
16+
override fun getNetworkStatus(): Single<String> = abiQuery.map { it.status }
17+
18+
override fun getNetworkMessage(): Single<String> = abiQuery.map { it.message }
19+
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package jfyg.contract
2+
3+
import io.reactivex.Single
4+
5+
6+
internal interface SmartContractContract {
7+
8+
/**
9+
* Return contract ABI for Verified Contract Source Code
10+
*/
11+
fun getContractABI(account: String?): Single<String>
12+
13+
/**
14+
* Return network status
15+
*/
16+
fun getNetworkStatus(): Single<String>
17+
18+
/**
19+
* Return network message
20+
*/
21+
fun getNetworkMessage(): Single<String>
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package jfyg.model
2+
3+
data class SmartContracts(var result: String? = null)

etherscanapi/src/main/java/jfyg/network/NetworkService.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import jfyg.response.account.AccountBlockResponse
66
import jfyg.response.account.AccountInternalTransactionResponse
77
import jfyg.response.account.AccountMultiBalanceResponse
88
import jfyg.response.account.AccountTransactionResponse
9+
import jfyg.response.contract.SmartContractResponse
910
import jfyg.response.stat.StatPriceResponse
1011
import jfyg.response.stat.StatSupplyResponse
1112
import retrofit2.http.GET
@@ -65,4 +66,10 @@ internal interface NetworkService {
6566
@Query("sort") sort: String?,
6667
@Query("apikey") apikey: String?): Single<AccountInternalTransactionResponse>
6768

69+
@GET("api")
70+
fun getSmartContract(@Query("module") module: String?,
71+
@Query("action") action: String?,
72+
@Query("address") address: String?,
73+
@Query("apikey") apikey: String?): Single<SmartContractResponse>
74+
6875
}

etherscanapi/src/main/java/jfyg/queries/QueryMediator.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import jfyg.response.account.AccountBlockResponse
88
import jfyg.response.account.AccountInternalTransactionResponse
99
import jfyg.response.account.AccountMultiBalanceResponse
1010
import jfyg.response.account.AccountTransactionResponse
11+
import jfyg.response.contract.SmartContractResponse
1112
import jfyg.response.stat.StatPriceResponse
1213
import jfyg.response.stat.StatSupplyResponse
1314

1415
/**
1516
* A mediator between the responses and errors that come from every query
1617
*/
17-
internal class QueryMediator : AccountQueries, StatQueries {
18+
internal class QueryMediator : AccountQueries, StatQueries, SmartContractQueries {
1819

1920
override fun accountBalance(module: String?,
2021
action: String?,
@@ -50,6 +51,9 @@ internal class QueryMediator : AccountQueries, StatQueries {
5051
sort: String?): Single<AccountInternalTransactionResponse> =
5152
RestClient().getQuery().getAccountInternalTransactions(module, action, address, startblock, endblock, sort, ApiKey.takeOff.callApiKey())
5253

54+
override fun abiContract(module: String?, action: String?, address: String?): Single<SmartContractResponse> =
55+
RestClient().getQuery().getSmartContract(module, action, address, ApiKey.takeOff.callApiKey())
56+
5357
override fun statPrice(module: String,
5458
action: String): Single<StatPriceResponse> =
5559
RestClient().getQuery().getStat(module, action, ApiKey.takeOff.callApiKey())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package jfyg.queries
2+
3+
import io.reactivex.Single
4+
import jfyg.response.account.AccountBalanceResponse
5+
import jfyg.response.contract.SmartContractResponse
6+
7+
internal interface SmartContractQueries {
8+
9+
/**
10+
* Get EBI for a contracts from a given address
11+
*/
12+
fun abiContract(module: String?,
13+
action: String?,
14+
address: String?): Single<SmartContractResponse>
15+
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package jfyg.response.contract
2+
3+
import jfyg.response.BaseResponse
4+
5+
/**
6+
* Encoded Solidity Contract call (ABI)
7+
*/
8+
internal data class SmartContractResponse(var result: String? = null) : BaseResponse()

0 commit comments

Comments
 (0)