Skip to content

Commit a3232b6

Browse files
committed
Add customized NonceProvider
1 parent 945c5ae commit a3232b6

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import java.lang.reflect.AnnotatedElement
1111
import java.lang.reflect.Method
1212
import java.lang.reflect.Proxy
1313
import java.lang.reflect.Type
14+
import java.math.BigInteger
1415

1516
class EtherSpace(val web3: Web3,
1617
val credentials: Credentials?,
17-
private val callAdapters: List<CallAdapter<Any, Any>>) {
18+
private val callAdapters: List<CallAdapter<Any, Any>>,
19+
private val nonceProvider: NonceProvider) {
1820

1921
fun <T> create(contract: SolAddress, service: Class<T>): T = create(contract.address, service)
2022

@@ -78,9 +80,10 @@ class EtherSpace(val web3: Web3,
7880
options: Options): Any {
7981
val cd = options.credentials ?: credentials
8082
?: throw IllegalArgumentException("Credentials not set")
83+
val np = options.nonceProvider ?: nonceProvider
84+
8185
val encodedFunction = web3.abi.encodeFunctionCall(args, functionName)
82-
// TODO Better way to get a nonce?
83-
val nonce = web3.eth.getTransactionCount(cd.address, Web3.DefaultBlock.PENDING)
86+
val nonce = np.getNonce(web3, cd.address)
8487
val transactionObject = Web3.TransactionObject(cd.address,
8588
toAddress,
8689
encodedFunction,
@@ -130,6 +133,8 @@ class EtherSpace(val web3: Web3,
130133

131134
var client: OkHttpClient? = null
132135

136+
var nonceProvider: NonceProvider = TransactionCountNonceProvider
137+
133138
fun provider(provider: String) = apply { this.provider = provider }
134139

135140
fun credentials(credentials: Credentials) = apply { this.credentials = credentials }
@@ -142,7 +147,14 @@ class EtherSpace(val web3: Web3,
142147
val web3 = Web3jAdapter(provider, client)
143148
return EtherSpace(web3,
144149
credentials,
145-
callAdapters + PassThroughCallAdaptor())
150+
callAdapters + PassThroughCallAdaptor(),
151+
nonceProvider)
152+
}
153+
}
154+
155+
object TransactionCountNonceProvider : NonceProvider {
156+
override fun getNonce(web3: Web3, address: String): BigInteger {
157+
return web3.eth.getTransactionCount(address, Web3.DefaultBlock.PENDING)
146158
}
147159
}
148160

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cc.etherspace
2+
3+
import java.math.BigInteger
4+
5+
interface NonceProvider {
6+
fun getNonce(web3: Web3, address: String): BigInteger
7+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import java.math.BigInteger
1010
data class Options @JvmOverloads constructor(val value: BigInteger = BigInteger.ZERO,
1111
val gas: BigInteger = Contract.GAS_LIMIT,
1212
val gasPrice: BigInteger = ManagedTransaction.GAS_PRICE,
13-
val credentials: Credentials? = null)
13+
val credentials: Credentials? = null,
14+
val nonceProvider: NonceProvider? = null)

0 commit comments

Comments
 (0)