Skip to content

Commit 2a45a73

Browse files
committed
Update NonceProvider to be more extensible
1 parent e5ad986 commit 2a45a73

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ class EtherSpace(val web3: Web3,
8484
val np = options.nonceProvider ?: nonceProvider
8585

8686
val encodedFunction = web3.abi.encodeFunctionCall(args, functionName)
87-
val nonce = np.getNonce(web3, cd.address)
88-
val transactionObject = Web3.TransactionObject(cd.address,
89-
toAddress,
90-
encodedFunction,
91-
options,
92-
nonce)
93-
val transactionHash = web3.eth.sendTransaction(transactionObject, cd)
87+
val transactionHash = np.provideNonce(web3, cd.address) { nonce ->
88+
val transactionObject = Web3.TransactionObject(cd.address,
89+
toAddress,
90+
encodedFunction,
91+
options,
92+
nonce)
93+
web3.eth.sendTransaction(transactionObject, cd)
94+
}
95+
9496
val returnTypeToken = TypeToken.of(returnType)
9597
return when {
9698
returnTypeToken.isSubtypeOf(String::class.java) -> transactionHash
@@ -150,7 +152,11 @@ class EtherSpace(val web3: Web3,
150152
}
151153

152154
object TransactionCountNonceProvider : NonceProvider {
153-
override fun getNonce(web3: Web3, address: String): BigInteger {
155+
override fun provideNonce(web3: Web3, address: String, sendTransaction: (BigInteger) -> String): String {
156+
return sendTransaction(getNonce(web3, address))
157+
}
158+
159+
private fun getNonce(web3: Web3, address: String): BigInteger {
154160
return web3.eth.getTransactionCount(address, Web3.DefaultBlock.PENDING)
155161
}
156162
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package cc.etherspace
33
import java.math.BigInteger
44

55
interface NonceProvider {
6-
fun getNonce(web3: Web3, address: String): BigInteger
6+
fun provideNonce(web3: Web3, address: String, sendTransaction: (BigInteger) -> String): String
77
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class LocalGreeterTest {
4242
.toTypedArray()
4343
val arrays = listOf(SolArray5(array), SolArray5(array), SolArray5(array), SolArray5(array), SolArray5(array))
4444
val ret = greeter.twoDimensionArray(arrays, SolUint256(1), SolUint256(2))
45-
ret.`should equal`(SolUint256(7))
45+
ret.`should equal`(SolUint256(3))
4646
}
4747

4848
@Test

0 commit comments

Comments
 (0)