solution: refer to current height when requesting balance

fix: #31
This commit is contained in:
Igor Artamonov
2020-11-30 19:58:31 -05:00
parent cfd73119f6
commit f89c4cc7f2
6 changed files with 52 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import io.emeraldpay.api.proto.Common
import io.emeraldpay.dshackle.BlockchainType
import io.emeraldpay.dshackle.SilentException
import io.emeraldpay.dshackle.config.TokensConfig
import io.emeraldpay.dshackle.upstream.Head
import io.emeraldpay.dshackle.upstream.MultistreamHolder
import io.emeraldpay.dshackle.upstream.Selector
import io.emeraldpay.dshackle.upstream.ethereum.EthereumMultistream
@@ -14,6 +15,7 @@ import io.emeraldpay.grpc.Chain
import io.infinitape.etherjar.domain.Address
import io.infinitape.etherjar.erc20.ERC20Token
import io.infinitape.etherjar.hex.Hex32
import io.infinitape.etherjar.hex.HexQuantity
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@@ -84,10 +86,11 @@ class TrackERC20Address(
}
fun getBalance(addr: TrackedAddress): Mono<BigInteger> {
return getUpstream(addr.chain)
val upstream = getUpstream(addr.chain)
return upstream
.getDirectApi(Selector.empty)
.flatMap { api ->
api.read(prepareEthCall(addr.token, addr.address))
api.read(prepareEthCall(addr.token, addr.address, upstream.getHead()))
.flatMap(JsonRpcResponse::requireStringResult)
.map {
Hex32.from(it).asQuantity().value
@@ -95,11 +98,12 @@ class TrackERC20Address(
}
}
fun prepareEthCall(token: ERC20Token, target: Address): JsonRpcRequest {
fun prepareEthCall(token: ERC20Token, target: Address, head: Head): JsonRpcRequest {
val call = token
.readBalanceOf(target)
.toJson()
return JsonRpcRequest("eth_call", listOf(call, "latest"))
val height = head.getCurrentHeight()?.let { HexQuantity.from(it).toHex() } ?: "latest"
return JsonRpcRequest("eth_call", listOf(call, height))
}
fun getUpstream(chain: Chain): EthereumMultistream {

View File

@@ -89,7 +89,8 @@ class EthereumDirectReader(
}
balanceReader = object : Reader<Address, Wei> {
override fun read(key: Address): Mono<Wei> {
val request = JsonRpcRequest("eth_getBalance", listOf(key.toHex(), "latest"))
val height = up.getHead().getCurrentHeight()?.let { HexQuantity.from(it).toHex() } ?: "latest"
val request = JsonRpcRequest("eth_getBalance", listOf(key.toHex(), height))
return readWithQuorum(request)
.timeout(Defaults.timeoutInternal, Mono.error(TimeoutException("Balance not read $key")))
.map {

View File

@@ -130,6 +130,7 @@ open class EthereumReader(
}
fun balance(): Reader<Address, Wei> {
//TODO include height as part of cache?
return CompoundReader(
balanceCache, directReader.balanceReader
)