Skip to content

Commit b84bf1e

Browse files
committed
add TransactionHash
1 parent 278c4ad commit b84bf1e

5 files changed

Lines changed: 47 additions & 13 deletions

File tree

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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
78
import okhttp3.OkHttpClient
89
import java.io.IOException
910
import java.lang.Thread.sleep
@@ -72,7 +73,7 @@ class EtherSpace(val web3: Web3,
7273
gasPrice = it.gasPrice.toBigInteger())
7374
} ?: defaultOptions
7475

75-
@Throws(IOException::class)
76+
@Throws(IOException::class, TransactionFailedException::class)
7677
private fun invokeTransactionFunction(toAddress: String,
7778
functionName: String,
7879
args: List<Any>,
@@ -91,17 +92,15 @@ class EtherSpace(val web3: Web3,
9192
nonce)
9293
val transactionHash = web3.eth.sendTransaction(transactionObject, cd)
9394
val returnTypeToken = TypeToken.of(returnType)
94-
when {
95-
returnTypeToken.isSubtypeOf(String::class.java) -> return transactionHash
95+
return when {
96+
returnTypeToken.isSubtypeOf(String::class.java) -> transactionHash
9697
returnTypeToken.isSubtypeOf(TransactionReceipt::class.java) -> {
97-
for (i in 1..GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS) {
98-
val transactionReceipt = web3.eth.getTransactionReceipt(transactionHash)
99-
if (transactionReceipt != null) {
100-
return transactionReceipt
101-
}
102-
sleep(GET_TRANSACTION_RECEIPT_POLLING_INTERVAL_IN_MS)
98+
runBlocking {
99+
TransactionHash(web3, transactionHash).requestTransactionReceipt()
103100
}
104-
throw IOException("Unable to get transaction receipt because of timeout.")
101+
}
102+
returnTypeToken.isSubtypeOf(TransactionHash::class.java) -> {
103+
TransactionHash(web3, transactionHash)
105104
}
106105
else -> throw IllegalArgumentException("Unknown return type:${returnType.typeName}")
107106
}
@@ -160,7 +159,7 @@ class EtherSpace(val web3: Web3,
160159

161160
companion object {
162161
inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build()
163-
private const val GET_TRANSACTION_RECEIPT_POLLING_INTERVAL_IN_MS = 5_000L
164-
private const val GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS = 60
162+
internal const val GET_TRANSACTION_RECEIPT_POLLING_INTERVAL_IN_MS = 5_000L
163+
internal const val GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS = 60
165164
}
166165
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cc.etherspace
2+
3+
import java.io.IOException
4+
5+
class TransactionFailedException(val transactionReceipt: TransactionReceipt) : IOException()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cc.etherspace
2+
3+
import cc.etherspace.EtherSpace.Companion.GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS
4+
import kotlinx.coroutines.experimental.delay
5+
import java.io.IOException
6+
7+
data class TransactionHash(private val web3: Web3,
8+
val hash: String) {
9+
suspend fun requestTransactionReceipt(): TransactionReceipt {
10+
for (i in 1..EtherSpace.GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS) {
11+
val transactionReceipt = web3.eth.getTransactionReceipt(hash)
12+
if (transactionReceipt != null) {
13+
if (!transactionReceipt.success) throw TransactionFailedException(transactionReceipt)
14+
return transactionReceipt
15+
}
16+
delay(GET_TRANSACTION_RECEIPT_POLLING_ATTEMPTS)
17+
}
18+
throw IOException("transactionTimeout:transactionHash=$this")
19+
}
20+
}

etherspace-java/src/test/java/cc/etherspace/Greeter.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ interface Greeter {
1212
@Send(functionName = "newGreeting")
1313
fun newGreeting_functionName(greeting: String): TransactionReceipt
1414

15+
@Throws(IOException::class)
16+
@Send
17+
fun newGreeting_transactionHash(greeting: String): TransactionHash
18+
1519
@Throws(IOException::class)
1620
@Send
1721
fun newGreeting(greeting: String, options: Options): String

etherspace-java/src/test/java/cc/etherspace/GreeterTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package cc.etherspace
22

33
import okhttp3.OkHttpClient
44
import okhttp3.logging.HttpLoggingInterceptor
5-
import org.amshove.kluent.`should be greater than`
65
import org.amshove.kluent.`should be equal to`
6+
import org.amshove.kluent.`should be greater than`
77
import org.amshove.kluent.`should equal`
88
import org.junit.Before
99
import org.junit.Test
@@ -73,6 +73,12 @@ class GreeterTest {
7373
transactionHash.length.`should be equal to`(66)
7474
}
7575

76+
@Test
77+
fun newGreeting_transactionHash() {
78+
val transactionHash = greeter.newGreeting_transactionHash("Hello World")
79+
transactionHash.hash.length.`should be equal to`(66)
80+
}
81+
7682
@Test
7783
fun greet() {
7884
val greet = greeter.greet()

0 commit comments

Comments
 (0)