@@ -5,11 +5,14 @@ import io.openfuture.state.blockchain.dto.UnifiedBlock
5
5
import io.openfuture.state.blockchain.dto.UnifiedTransaction
6
6
import io.openfuture.state.domain.CurrencyCode
7
7
import io.openfuture.state.util.toLocalDateTime
8
+ import kotlinx.coroutines.Dispatchers
8
9
import kotlinx.coroutines.future.await
10
+ import kotlinx.coroutines.withContext
9
11
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
10
12
import org.springframework.stereotype.Component
11
13
import org.web3j.abi.FunctionEncoder
12
14
import org.web3j.abi.FunctionReturnDecoder
15
+ import org.web3j.protocol.core.DefaultBlockParameterName.LATEST
13
16
import org.web3j.abi.TypeReference
14
17
import org.web3j.abi.datatypes.Address
15
18
import org.web3j.abi.datatypes.generated.Uint256
@@ -20,6 +23,7 @@ import org.web3j.protocol.core.methods.request.Transaction
20
23
import org.web3j.protocol.core.methods.response.EthBlock
21
24
import org.web3j.protocol.core.methods.response.EthCall
22
25
import org.web3j.utils.Convert
26
+ import java.lang.Exception
23
27
import java.math.BigDecimal
24
28
import java.math.BigInteger
25
29
@@ -31,6 +35,27 @@ class EthereumBlockchain(private val web3j: Web3j) : Blockchain() {
31
35
.sendAsync().await()
32
36
.blockNumber.toInt()
33
37
38
+ override suspend fun getNonce (address : String ): BigInteger = web3j.ethGetTransactionCount(address, LATEST ).send().transactionCount
39
+ override suspend fun broadcastTransaction (signedTransaction : String ): String {
40
+ val result = web3j.ethSendRawTransaction(signedTransaction).send()
41
+
42
+ if (result.hasError()) {
43
+ throw Exception (result.error.message)
44
+ }
45
+
46
+ while (! web3j.ethGetTransactionReceipt(result.transactionHash).send().transactionReceipt.isPresent) {
47
+ withContext(Dispatchers .IO ) {
48
+ Thread .sleep(1000 )
49
+ }
50
+ }
51
+
52
+ return web3j.ethGetTransactionReceipt(result.transactionHash).send().transactionReceipt.get().transactionHash
53
+ }
54
+
55
+ override suspend fun getTransactionStatus (transactionHash : String ): Boolean {
56
+ TODO (" Not yet implemented" )
57
+ }
58
+
34
59
override suspend fun getBlock (blockNumber : Int ): UnifiedBlock {
35
60
val parameter = DefaultBlockParameterNumber (blockNumber.toLong())
36
61
val block = web3j.ethGetBlockByNumber(parameter, true )
@@ -42,8 +67,7 @@ class EthereumBlockchain(private val web3j: Web3j) : Blockchain() {
42
67
}
43
68
44
69
override suspend fun getBalance (address : String ): BigDecimal {
45
- val parameter = DefaultBlockParameterName .LATEST
46
- val balanceWei = web3j.ethGetBalance(address, parameter)
70
+ val balanceWei = web3j.ethGetBalance(address, LATEST )
47
71
.sendAsync().await()
48
72
.balance
49
73
return Convert .fromWei(balanceWei.toString(), Convert .Unit .ETHER )
@@ -59,7 +83,7 @@ class EthereumBlockchain(private val web3j: Web3j) : Blockchain() {
59
83
val encodedFunction = FunctionEncoder .encode(functionBalance)
60
84
val ethCall: EthCall = web3j.ethCall(
61
85
Transaction .createEthCallTransaction(address, contractAddress, encodedFunction),
62
- DefaultBlockParameterName . LATEST
86
+ LATEST
63
87
).sendAsync().await()
64
88
65
89
val value = ethCall.value
@@ -68,6 +92,17 @@ class EthereumBlockchain(private val web3j: Web3j) : Blockchain() {
68
92
return Convert .fromWei(contractBalance.toString(), Convert .Unit .ETHER )
69
93
}
70
94
95
+ override suspend fun getGasPrice (): BigInteger {
96
+ return web3j.ethGasPrice().sendAsync().await().gasPrice
97
+ }
98
+
99
+ override suspend fun getGasLimit (): BigInteger {
100
+ return web3j
101
+ .ethGetBlockByNumber(LATEST , false )
102
+ .sendAsync().await()
103
+ .block.gasLimit
104
+ }
105
+
71
106
override suspend fun getCurrencyCode (): CurrencyCode {
72
107
return CurrencyCode .ETHEREUM
73
108
}
0 commit comments