@@ -4,6 +4,7 @@ import cc.etherspace.calladapter.CallAdapter
44import cc.etherspace.calladapter.PassThroughCallAdaptor
55import cc.etherspace.web3j.Web3jAdapter
66import com.google.common.reflect.TypeToken
7+ import kotlinx.coroutines.experimental.runBlocking
78import okhttp3.OkHttpClient
89import java.io.IOException
910import 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}
0 commit comments