@@ -11,10 +11,12 @@ import java.lang.reflect.AnnotatedElement
1111import java.lang.reflect.Method
1212import java.lang.reflect.Proxy
1313import java.lang.reflect.Type
14+ import java.math.BigInteger
1415
1516class 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
0 commit comments