Skip to content

Commit

Permalink
Implement ERC20 balance batching request
Browse files Browse the repository at this point in the history
Closed #146
  • Loading branch information
furenster committed Feb 12, 2025
1 parent 2866e15 commit 2a6c1d0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class TestEvmBalance {
return Result.success(JSONRpcResponse(null))
}

override suspend fun callBatch(request: List<JSONRpcRequest<List<Any>>>): Result<List<JSONRpcResponse<String>>> {
return Result.success(
listOf(
JSONRpcResponse(
result = "0x00000000000000000000000000000000000000000000000000000000001fdcf1",
)
)
)
}

}
class BalanceService : EvmBalancesService {
var nativeBalanceParam: String = ""
Expand Down Expand Up @@ -135,6 +145,19 @@ class TestEvmBalance {
override suspend fun callNumber(request: JSONRpcRequest<List<Any>>): Result<JSONRpcResponse<EvmRpcClient.EvmNumber?>> {
TODO("Not yet implemented")
}

override suspend fun callBatch(request: List<JSONRpcRequest<List<Any>>>): Result<List<JSONRpcResponse<String>>> {
return Result.success(
listOf(
JSONRpcResponse(
result = "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ee7e9ccfb529f2c1cc02c0aea8aced7ec7e98b5e0000000000000000000000009941bce2601fc93478df9f5f6cc83f4ffc1d71d80000000000000000000000000000000000000000000000000deacafbb23d1ee90000000000000000000000000000000000000000000000000dc3088951e56b15",
),
JSONRpcResponse(
result = "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
),
)
)
}
}
val balanceClient = EvmBalanceClient(
Chain.SmartChain,
Expand Down Expand Up @@ -173,6 +196,19 @@ class TestEvmBalance {
}
return Result.success(JSONRpcResponse(EvmRpcClient.EvmNumber(BigInteger(result.removePrefix("0x"), 16))))
}

override suspend fun callBatch(request: List<JSONRpcRequest<List<Any>>>): Result<List<JSONRpcResponse<String>>> {
return Result.success(
listOf(
JSONRpcResponse(
result = "0x00000000000000000000000000000000000000000000000000000002eedef652",
),
JSONRpcResponse(
result = "0x000000000000000000000000000000000000000000000000006a94d74f430000",
),
)
)
}
}
val balanceClient = EvmBalanceClient(Chain.SmartChain, callService, balanceService, SmartchainStakeClient(Chain.SmartChain, CallService()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class TestFeeCalculator {
override suspend fun callNumber(request: JSONRpcRequest<List<Any>>): Result<JSONRpcResponse<EvmRpcClient.EvmNumber?>> {
return Result.success(JSONRpcResponse(EvmRpcClient.EvmNumber(BigInteger.ONE)))
}

override suspend fun callBatch(request: List<JSONRpcRequest<List<Any>>>): Result<List<JSONRpcResponse<String>>> {
TODO("Not yet implemented")
}
}

@Test
Expand All @@ -89,6 +93,10 @@ class TestFeeCalculator {
override suspend fun callNumber(request: JSONRpcRequest<List<Any>>): Result<JSONRpcResponse<EvmRpcClient.EvmNumber?>> {
throw Exception("Call number fail")
}

override suspend fun callBatch(request: List<JSONRpcRequest<List<Any>>>): Result<List<JSONRpcResponse<String>>> {
throw Exception("Call number fail")
}
}
try {
runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.gemwallet.android.blockchain.clients.ethereum
import com.gemwallet.android.blockchain.clients.BalanceClient
import com.gemwallet.android.blockchain.clients.ethereum.services.EvmBalancesService
import com.gemwallet.android.blockchain.clients.ethereum.services.EvmCallService
import com.gemwallet.android.blockchain.clients.ethereum.services.batch
import com.gemwallet.android.blockchain.clients.ethereum.services.createCallRequest
import com.gemwallet.android.blockchain.clients.ethereum.services.getBalance
import com.gemwallet.android.blockchain.rpc.model.JSONRpcRequest
import com.gemwallet.android.ext.asset
import com.gemwallet.android.math.hexToBigInteger
import com.gemwallet.android.model.AssetBalance
import com.wallet.core.primitives.Asset
import com.wallet.core.primitives.Chain
Expand Down Expand Up @@ -35,15 +38,18 @@ class EvmBalanceClient(
return emptyList()
}
val result = mutableListOf<AssetBalance>()
for (token in tokens) {
val data = "0x70a08231000000000000000000000000${address.removePrefix("0x")}"
val contract = token.id.tokenId ?: continue
val params = mapOf(
"to" to contract,
"data" to data,
val tokens = tokens.filter { it.id.tokenId != null }
val requests = tokens.map { token ->
callService.createCallRequest(
to = token.id.tokenId!!,
data = "0x70a08231000000000000000000000000${address.removePrefix("0x")}",
tag = "latest",
)
val balance = callService.callNumber(JSONRpcRequest.create(EvmMethod.Call, listOf(params, "latest")))
.getOrNull()?.result?.value ?: continue
}
val response = callService.batch(requests)
response.mapIndexedNotNull { index, data ->
val balance = data.hexToBigInteger() ?: return@mapIndexedNotNull null
val token = tokens[index]
result.add(AssetBalance.create(token, available = balance.toString()))
}
return result
Expand Down

0 comments on commit 2a6c1d0

Please sign in to comment.