Skip to content

Commit ccbc71f

Browse files
committed
transactionHash.requestTransactionReceipt() returns type depends on callAdapter
1 parent 3d877f5 commit ccbc71f

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

etherspace-java/src/main/java/cc/etherspace/EtherSpace.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import cc.etherspace.calladapter.CallAdapter
44
import cc.etherspace.calladapter.PassThroughCallAdaptor
55
import cc.etherspace.web3j.Web3jAdapter
66
import com.google.common.reflect.TypeToken
7-
import kotlinx.coroutines.experimental.runBlocking
87
import okhttp3.OkHttpClient
98
import java.io.IOException
10-
import java.lang.Thread.sleep
119
import java.lang.reflect.AnnotatedElement
1210
import java.lang.reflect.Method
1311
import java.lang.reflect.Proxy
@@ -59,7 +57,8 @@ class EtherSpace(val web3: Web3,
5957
if (functionName.isNotBlank()) functionName else method.name,
6058
params,
6159
actualReturnType,
62-
options)
60+
options,
61+
callAdapter)
6362
}
6463

6564
throw IllegalArgumentException("There is no Send/Call annotation on this method")
@@ -78,7 +77,8 @@ class EtherSpace(val web3: Web3,
7877
functionName: String,
7978
args: List<Any>,
8079
returnType: Type,
81-
options: Options): Any {
80+
options: Options,
81+
callAdapter: CallAdapter<Any, Any>): Any {
8282
val cd = options.credentials ?: credentials
8383
?: throw IllegalArgumentException("Credentials not set")
8484
val np = options.nonceProvider ?: nonceProvider
@@ -95,12 +95,10 @@ class EtherSpace(val web3: Web3,
9595
return when {
9696
returnTypeToken.isSubtypeOf(String::class.java) -> transactionHash
9797
returnTypeToken.isSubtypeOf(TransactionReceipt::class.java) -> {
98-
runBlocking {
99-
TransactionHash(web3, transactionHash).requestTransactionReceipt()
100-
}
98+
TransactionHash(web3, transactionHash, callAdapter).requestTransactionReceiptBlocking()
10199
}
102100
returnTypeToken.isSubtypeOf(TransactionHash::class.java) -> {
103-
TransactionHash(web3, transactionHash)
101+
TransactionHash(web3, transactionHash, callAdapter)
104102
}
105103
else -> throw IllegalArgumentException("Unknown return type:${returnType.typeName}")
106104
}

etherspace-java/src/main/java/cc/etherspace/TransactionHash.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package cc.etherspace
22

3-
import kotlinx.coroutines.experimental.delay
3+
import cc.etherspace.calladapter.CallAdapter
44
import java.io.IOException
55

66
data class TransactionHash(private val web3: Web3,
7-
val hash: String) {
8-
suspend fun requestTransactionReceipt(): TransactionReceipt {
7+
val hash: String,
8+
private val callAdapter: CallAdapter<Any, Any>) {
9+
@Suppress("UNCHECKED_CAST")
10+
fun <T> requestTransactionReceipt(): T {
11+
return callAdapter.adapt {
12+
return@adapt requestTransactionReceiptBlocking()
13+
} as T
14+
}
15+
16+
internal fun requestTransactionReceiptBlocking(): TransactionReceipt {
917
for (i in 1..EtherSpace.GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS) {
1018
val transactionReceipt = web3.eth.getTransactionReceipt(hash)
1119
if (transactionReceipt != null) {
1220
if (!transactionReceipt.success) throw TransactionFailedException(transactionReceipt)
1321
return transactionReceipt
1422
}
15-
delay(EtherSpace.GET_TRANSACTION_RECEIPT_POLLING_INTERVAL_IN_MS)
23+
Thread.sleep(EtherSpace.GET_TRANSACTION_RECEIPT_POLLING_INTERVAL_IN_MS)
1624
}
1725
throw IOException("transactionTimeout:transactionHash=$this")
1826
}

etherspace-java/src/test/java/cc/etherspace/calladapter/CoroutineGreeter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ interface CoroutineGreeter {
1313
@Send
1414
fun newGreeting(greeting: String, options: Options): Deferred<TransactionReceipt>
1515

16+
@Throws(IOException::class)
17+
@Send(functionName = "newGreeting")
18+
fun newGreeting_transactionHash(greeting: String): Deferred<TransactionHash>
19+
1620
@Throws(IOException::class)
1721
@Call
1822
fun greet(): Deferred<String>

etherspace-java/src/test/java/cc/etherspace/calladapter/CoroutineGreeterTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.etherspace.calladapter
22

33
import cc.etherspace.*
4+
import kotlinx.coroutines.experimental.Deferred
45
import kotlinx.coroutines.experimental.runBlocking
56
import org.amshove.kluent.`should be equal to`
67
import org.amshove.kluent.`should be greater than`
@@ -44,6 +45,20 @@ class CoroutineGreeterTest {
4445
}
4546
}
4647

48+
@Test
49+
fun newGreeting_transactionHash() {
50+
runBlocking {
51+
val hash = greeter.newGreeting_transactionHash("Hello World").await()
52+
hash.hash.length.`should be equal to`(66)
53+
val receipt = hash.requestTransactionReceipt<Deferred<TransactionReceipt>>().await()
54+
receipt.blockHash.length.`should be equal to`(66)
55+
receipt.transactionHash.length.`should be equal to`(66)
56+
receipt.from!!.`should be equal to`(Tests.TEST_WALLET_ADDRESS)
57+
receipt.to!!.`should be equal to`(Tests.TEST_CONTRACT_ADDRESS)
58+
receipt.logs.size.`should be greater than`(0)
59+
}
60+
}
61+
4762
@Test(expected = IOException::class)
4863
fun newGreeting_options() {
4964
runBlocking {

0 commit comments

Comments
 (0)