|
| 1 | +package io.openfuture.state.blockchain.tron |
| 2 | + |
| 3 | +import io.openfuture.state.blockchain.Blockchain |
| 4 | +import io.openfuture.state.blockchain.dto.UnifiedBlock |
| 5 | +import io.openfuture.state.blockchain.dto.UnifiedTransaction |
| 6 | +import io.openfuture.state.domain.CurrencyCode |
| 7 | +import io.openfuture.state.util.HashUtils.decodeBase58 |
| 8 | +import io.openfuture.state.util.HashUtils.toHexString |
| 9 | +import io.openfuture.state.util.toLocalDateTime |
| 10 | +import kotlinx.coroutines.future.await |
| 11 | +import org.springframework.beans.factory.annotation.Qualifier |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty |
| 13 | +import org.springframework.stereotype.Component |
| 14 | +import org.web3j.abi.FunctionEncoder |
| 15 | +import org.web3j.abi.FunctionReturnDecoder |
| 16 | +import org.web3j.abi.TypeReference |
| 17 | +import org.web3j.abi.Utils |
| 18 | +import org.web3j.abi.datatypes.Address |
| 19 | +import org.web3j.abi.datatypes.generated.Uint256 |
| 20 | +import org.web3j.protocol.Web3j |
| 21 | +import org.web3j.protocol.core.DefaultBlockParameterName |
| 22 | +import org.web3j.protocol.core.DefaultBlockParameterNumber |
| 23 | +import org.web3j.protocol.core.methods.request.Transaction |
| 24 | +import org.web3j.protocol.core.methods.response.EthBlock |
| 25 | +import org.web3j.protocol.core.methods.response.EthCall |
| 26 | +import org.web3j.utils.Convert |
| 27 | +import java.math.BigDecimal |
| 28 | +import java.math.BigInteger |
| 29 | + |
| 30 | +@Component |
| 31 | +class TronShastaBlockchain(@Qualifier("web3jTronTestnet") private val web3jTronTestnet: Web3j) : Blockchain() { |
| 32 | + |
| 33 | + override suspend fun getLastBlockNumber(): Int = web3jTronTestnet.ethBlockNumber() |
| 34 | + .sendAsync().await() |
| 35 | + .blockNumber.toInt() |
| 36 | + |
| 37 | + override suspend fun getBlock(blockNumber: Int): UnifiedBlock { |
| 38 | + val parameter = DefaultBlockParameterNumber(blockNumber.toLong()) |
| 39 | + val block = web3jTronTestnet.ethGetBlockByNumber(parameter, true) |
| 40 | + .sendAsync().await() |
| 41 | + .block |
| 42 | + val transactions = obtainTransactions(block) |
| 43 | + val date = block.timestamp.toLong().toLocalDateTime() |
| 44 | + return UnifiedBlock(transactions, date, block.number.toLong(), block.hash) |
| 45 | + } |
| 46 | + |
| 47 | + override suspend fun getBalance(address: String): BigDecimal { |
| 48 | + val parameter = DefaultBlockParameterName.LATEST |
| 49 | + val ethAddress = base58ToEthAddress(address) |
| 50 | + val balanceWei = web3jTronTestnet.ethGetBalance(ethAddress, parameter) |
| 51 | + .sendAsync().await() |
| 52 | + .balance |
| 53 | + return Convert.fromWei(balanceWei.toString(), Convert.Unit.MWEI) |
| 54 | + } |
| 55 | + |
| 56 | + private fun base58ToEthAddress(address: String): String { |
| 57 | + val addressDecode58 = address.decodeBase58().toHexString() |
| 58 | + //eth address is 42 length |
| 59 | + return "0x" + addressDecode58.substring(2, 42) |
| 60 | + } |
| 61 | + |
| 62 | + override suspend fun getContractBalance(address: String, contractAddress: String): BigDecimal { |
| 63 | + val ethAddress = base58ToEthAddress(address) |
| 64 | + val ethContractAddress = base58ToEthAddress(contractAddress) |
| 65 | + println("ethAddress: $ethAddress") |
| 66 | + println("ethCContractAddress: $ethContractAddress") |
| 67 | + val functionBalance = org.web3j.abi.datatypes.Function( |
| 68 | + "balanceOf", |
| 69 | + listOf(Address(ethAddress)), |
| 70 | + listOf(object : TypeReference<Uint256>() {}) |
| 71 | + ) |
| 72 | + val encodedFunction = FunctionEncoder.encode(functionBalance) |
| 73 | + val ethCall: EthCall = web3jTronTestnet.ethCall( |
| 74 | + Transaction.createEthCallTransaction(ethAddress, ethContractAddress, encodedFunction), |
| 75 | + DefaultBlockParameterName.LATEST |
| 76 | + ).sendAsync().await() |
| 77 | + |
| 78 | + val value = ethCall.value |
| 79 | + val decode = FunctionReturnDecoder.decode(value, functionBalance.outputParameters) |
| 80 | + val contractBalance = BigInteger(value.substring(2, value.length), 16) |
| 81 | + decode.iterator().forEach { a -> println("a ${a.value} with ${a.typeAsString}") } |
| 82 | + |
| 83 | + println("Value $value") |
| 84 | + |
| 85 | + return Convert.fromWei(contractBalance.toString(), Convert.Unit.MWEI) |
| 86 | + } |
| 87 | + |
| 88 | + override suspend fun getCurrencyCode(): CurrencyCode { |
| 89 | + return CurrencyCode.TRON |
| 90 | + } |
| 91 | + |
| 92 | + private suspend fun obtainTransactions(ethBlock: EthBlock.Block): List<UnifiedTransaction> = ethBlock.transactions |
| 93 | + .map { it.get() as EthBlock.TransactionObject } |
| 94 | + .map { tx -> |
| 95 | + val to = tx.to ?: findContractAddress(tx.hash) |
| 96 | + val amount = Convert.fromWei(tx.value.toBigDecimal(), Convert.Unit.MWEI) |
| 97 | + UnifiedTransaction(tx.hash, tx.from, to, amount, true, to) |
| 98 | + } |
| 99 | + |
| 100 | + private suspend fun findContractAddress(transactionHash: String) = |
| 101 | + web3jTronTestnet.ethGetTransactionReceipt(transactionHash) |
| 102 | + .sendAsync().await() |
| 103 | + .transactionReceipt.get() |
| 104 | + .contractAddress |
| 105 | + |
| 106 | + companion object { |
| 107 | + private val DECODE_TYPES = Utils.convert( |
| 108 | + listOf( |
| 109 | + object : TypeReference<Address>(true) {}, |
| 110 | + object : TypeReference<Uint256>() {} |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + private const val TRANSFER_METHOD_SIGNATURE = "0xa9059cbb" |
| 115 | + private const val TRANSFER_INPUT_LENGTH = 138 |
| 116 | + } |
| 117 | +} |
0 commit comments