From 1c88250bd16f0fa91e93bfb43f3a51cca5dd5892 Mon Sep 17 00:00:00 2001 From: Elaman Nazarkulov Date: Tue, 10 Sep 2024 00:42:24 +0600 Subject: [PATCH] Wallet processor check with lowercase address --- .../blockchain/tron/TronShastaBlockchain.kt | 11 +++----- .../state/controller/WalletController.kt | 10 ++++--- .../state/controller/WalletControllerV2.kt | 27 ++++--------------- .../state/service/DefaultWalletService.kt | 26 +++++++++++++++++- .../openfuture/state/service/WalletService.kt | 1 + .../state/service/WebhookInvoker.kt | 6 ++--- .../state/webhook/DefaultWebhookExecutor.kt | 2 +- .../state/webhook/WebhookRestClient.kt | 3 ++- 8 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/main/kotlin/io/openfuture/state/blockchain/tron/TronShastaBlockchain.kt b/src/main/kotlin/io/openfuture/state/blockchain/tron/TronShastaBlockchain.kt index a6ba053..62e35a2 100644 --- a/src/main/kotlin/io/openfuture/state/blockchain/tron/TronShastaBlockchain.kt +++ b/src/main/kotlin/io/openfuture/state/blockchain/tron/TronShastaBlockchain.kt @@ -62,8 +62,7 @@ class TronShastaBlockchain(@Qualifier("web3jTronTestnet") private val web3jTronT override suspend fun getContractBalance(address: String, contractAddress: String): BigDecimal { val ethAddress = base58ToEthAddress(address) val ethContractAddress = base58ToEthAddress(contractAddress) - println("ethAddress: $ethAddress") - println("ethCContractAddress: $ethContractAddress") + val functionBalance = org.web3j.abi.datatypes.Function( "balanceOf", listOf(Address(ethAddress)), @@ -76,13 +75,9 @@ class TronShastaBlockchain(@Qualifier("web3jTronTestnet") private val web3jTronT ).sendAsync().await() val value = ethCall.value - val decode = FunctionReturnDecoder.decode(value, functionBalance.outputParameters) val contractBalance = BigInteger(value.substring(2, value.length), 16) - decode.iterator().forEach { a -> println("a ${a.value} with ${a.typeAsString}") } - - println("Value $value") - return Convert.fromWei(contractBalance.toString(), Convert.Unit.MWEI) + return Convert.fromWei(contractBalance.toBigDecimal(), Convert.Unit.MWEI) } override suspend fun getCurrencyCode(): CurrencyCode { @@ -93,7 +88,7 @@ class TronShastaBlockchain(@Qualifier("web3jTronTestnet") private val web3jTronT .map { it.get() as EthBlock.TransactionObject } .map { tx -> val to = tx.to ?: findContractAddress(tx.hash) - val amount = Convert.fromWei(tx.value.toBigDecimal(), Convert.Unit.MWEI) + val amount = Convert.fromWei(tx.value.toBigDecimal(), Convert.Unit.GWEI) UnifiedTransaction(tx.hash, tx.from, to, amount, true, to) } diff --git a/src/main/kotlin/io/openfuture/state/controller/WalletController.kt b/src/main/kotlin/io/openfuture/state/controller/WalletController.kt index fc985fe..44ba9ae 100644 --- a/src/main/kotlin/io/openfuture/state/controller/WalletController.kt +++ b/src/main/kotlin/io/openfuture/state/controller/WalletController.kt @@ -1,11 +1,13 @@ package io.openfuture.state.controller import io.openfuture.state.blockchain.Blockchain +import io.openfuture.state.config.AppProperties import io.openfuture.state.domain.wallet.Wallet import io.openfuture.state.domain.wallet.WalletPaymentDetail import io.openfuture.state.service.WalletService import io.openfuture.state.service.WalletTransactionFacade import io.openfuture.state.service.dto.PlaceOrderResponse +import org.springframework.beans.factory.annotation.Autowired import org.springframework.web.bind.annotation.* import java.math.BigDecimal import java.time.LocalDateTime @@ -21,7 +23,6 @@ class WalletController( private val walletTransactionFacade: WalletTransactionFacade, private val blockchains: List ) { - @PostMapping suspend fun saveMultiple(@Valid @RequestBody request: SaveOrderWalletRequest): PlaceOrderResponse { return walletService.saveOrder(request) @@ -29,8 +30,11 @@ class WalletController( @PostMapping("/single") suspend fun saveSingle(@Valid @RequestBody request: SaveWalletRequest): WalletDto { - val blockchain = findBlockchain(request.blockchain) - val wallet = walletService.save(blockchain, request.address, request.webhook!!, request.applicationId) + + val blockchainName = walletService.getBlockchainName(request.blockchain) + val blockchain = findBlockchain(blockchainName) + + val wallet = walletService.save(blockchain, request.address.lowercase(), request.webhook!!, request.applicationId) return WalletDto(wallet) } diff --git a/src/main/kotlin/io/openfuture/state/controller/WalletControllerV2.kt b/src/main/kotlin/io/openfuture/state/controller/WalletControllerV2.kt index 2410ec5..1bbef33 100644 --- a/src/main/kotlin/io/openfuture/state/controller/WalletControllerV2.kt +++ b/src/main/kotlin/io/openfuture/state/controller/WalletControllerV2.kt @@ -19,9 +19,6 @@ class WalletControllerV2( private val blockchainLookupService: BlockchainLookupService ) { - @Autowired - lateinit var appProperties: AppProperties - @PostMapping("add") suspend fun addWallet(@RequestBody request: AddWalletStateForUserRequest): AddWatchResponse { return walletService.addWallet(request) @@ -34,28 +31,14 @@ class WalletControllerV2( // val CONTRACT_ADDRESS = "0x337610d27c682E347C9cD60BD4b3b107C9d34dDd" // USDT - BNB // val CONTRACT_ADDRESS = "0xdAC17F958D2ee523a2206206994597C13D831ec7" // USDT - TRX - val blockchain = if (appProperties.isProdEnabled == "true") { - when (request.blockchainName) { - "ETH" -> "EthereumBlockchain" - "BNB" -> "BinanceBlockchain" - "TRX" -> "TronBlockchain" - "BTC" -> "BitcoinBlockchain" - else -> "EthereumBlockchain" - } - } else { - when (request.blockchainName) { - "ETH" -> "GoerliBlockchain" - "BNB" -> "BinanceTestnetBlockchain" - "TRX" -> "TronShastaBlockchain" - else -> "GoerliBlockchain" - } - - } + val blockchain = walletService.getBlockchainName(request.blockchainName) val chain = blockchainLookupService.findBlockchain(blockchain) val balance = - if (request.contractAddress == null) chain.getBalance(request.address) - else chain.getContractBalance(request.address, request.contractAddress) + if (request.contractAddress == null) + chain.getBalance(request.address) + else + chain.getContractBalance(request.address, request.contractAddress) println("Balance response for address: ${request.address} is $balance") return WalletBalanceResponse( diff --git a/src/main/kotlin/io/openfuture/state/service/DefaultWalletService.kt b/src/main/kotlin/io/openfuture/state/service/DefaultWalletService.kt index d098fcc..262a531 100644 --- a/src/main/kotlin/io/openfuture/state/service/DefaultWalletService.kt +++ b/src/main/kotlin/io/openfuture/state/service/DefaultWalletService.kt @@ -5,6 +5,7 @@ import io.openfuture.state.blockchain.dto.UnifiedBlock import io.openfuture.state.blockchain.dto.UnifiedTransaction import io.openfuture.state.client.CoinGateHttpClientApi import io.openfuture.state.component.open.DefaultOpenApi +import io.openfuture.state.config.AppProperties import io.openfuture.state.controller.AddWalletStateForUserRequest import io.openfuture.state.controller.WalletController import io.openfuture.state.domain.* @@ -26,6 +27,7 @@ import kotlinx.coroutines.reactive.awaitFirstOrNull import kotlinx.coroutines.reactive.awaitSingle import lombok.extern.slf4j.Slf4j import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service import java.math.BigDecimal import kotlin.math.pow @@ -42,6 +44,8 @@ class DefaultWalletService( private val openApi: DefaultOpenApi ) : WalletService { + @Autowired + lateinit var appProperties: AppProperties override suspend fun findByIdentity(blockchain: String, address: String): Wallet { val identity = WalletIdentity(blockchain, address) return walletRepository.findByIdentity(identity).awaitFirstOrNull() @@ -159,7 +163,7 @@ class DefaultWalletService( override suspend fun addTransactions(blockchain: Blockchain, block: UnifiedBlock) { for (transaction in block.transactions) { - val identity = WalletIdentity(blockchain.getName(), transaction.to) + val identity = WalletIdentity(blockchain.getName(), transaction.to.lowercase()) val wallet = walletRepository.findByIdentity(identity).awaitFirstOrNull()//walletRepository.findByIdentity(identity.blockchain, identity.address).awaitFirstOrNull() @@ -171,6 +175,26 @@ class DefaultWalletService( //do nothing } + override fun getBlockchainName(requestBlockchainName: String) : String { + return if (appProperties.isProdEnabled == "true") { + when (requestBlockchainName) { + "ETH" -> "EthereumBlockchain" + "BNB" -> "BinanceBlockchain" + "TRX" -> "TronBlockchain" + "BTC" -> "BitcoinBlockchain" + else -> "EthereumBlockchain" + } + } else { + when (requestBlockchainName) { + "ETH" -> "GoerliBlockchain" + "BNB" -> "BinanceTestnetBlockchain" + "TRX" -> "TronShastaBlockchain" + else -> "GoerliBlockchain" + } + + } + } + private suspend fun saveTransaction(wallet: Wallet, block: UnifiedBlock, unifiedTransaction: UnifiedTransaction) { log.info("Saving Transaction") if (!transactionRepository.existsTransactionByHash(unifiedTransaction.hash)) { diff --git a/src/main/kotlin/io/openfuture/state/service/WalletService.kt b/src/main/kotlin/io/openfuture/state/service/WalletService.kt index a356d61..bae4ef9 100644 --- a/src/main/kotlin/io/openfuture/state/service/WalletService.kt +++ b/src/main/kotlin/io/openfuture/state/service/WalletService.kt @@ -37,5 +37,6 @@ interface WalletService { suspend fun addTransactions(blockchain: Blockchain, block: UnifiedBlock) suspend fun updateWebhookStatus(wallet: Wallet, status: WebhookStatus) + fun getBlockchainName(requestBlockchainName: String): String } diff --git a/src/main/kotlin/io/openfuture/state/service/WebhookInvoker.kt b/src/main/kotlin/io/openfuture/state/service/WebhookInvoker.kt index d30dcfa..7cbc717 100644 --- a/src/main/kotlin/io/openfuture/state/service/WebhookInvoker.kt +++ b/src/main/kotlin/io/openfuture/state/service/WebhookInvoker.kt @@ -40,14 +40,14 @@ class WebhookInvoker( val signature = openApi.generateSignature(wallet.identity.address, woocommerceDto) log.info("Invoking webhook signature $signature") webhookRestClient.doPostWoocommerce(wallet.webhook, signature, woocommerceDto) - } else webhookRestClient.doPost(wallet.webhook, WebhookPayloadDto(transaction, userId = wallet.userData.userId, metadata = wallet.userData)) + } else webhookRestClient.doPost(wallet.webhook, wallet.identity.address, WebhookPayloadDto(transaction, userId = wallet.userData.userId, metadata = wallet.userData)) - webhookRestClient.doPost(wallet.webhook, webhookBody) + webhookRestClient.doPost(wallet.webhook, wallet.identity.address, webhookBody) } suspend fun invoke(webHook: String, transaction: Transaction, metadata: Any, userId: String?) = runBlocking { log.info("Invoking webhook $webHook $metadata $userId $transaction") - webhookRestClient.doPost(webHook, WebhookPayloadDto(transaction, userId, metadata)) + webhookRestClient.doPost(webHook, transaction.walletIdentity.address, WebhookPayloadDto(transaction, userId, metadata)) } companion object { diff --git a/src/main/kotlin/io/openfuture/state/webhook/DefaultWebhookExecutor.kt b/src/main/kotlin/io/openfuture/state/webhook/DefaultWebhookExecutor.kt index 5001500..c6c6ee6 100644 --- a/src/main/kotlin/io/openfuture/state/webhook/DefaultWebhookExecutor.kt +++ b/src/main/kotlin/io/openfuture/state/webhook/DefaultWebhookExecutor.kt @@ -33,7 +33,7 @@ class DefaultWebhookExecutor( val response = if (wallet.walletType == WalletType.FOR_ORDER) restClient.doPostWoocommerce(wallet.webhook, signature, woocommerceDto) - else restClient.doPost(wallet.webhook, WebhookPayloadDto(transaction, wallet.userData.userId, wallet.userData)) + else restClient.doPost(wallet.webhook, wallet.identity.address, WebhookPayloadDto(transaction, wallet.userData.userId, wallet.userData)) webhookInvocationService.registerInvocation(wallet, transactionTask, response) if (response.status.is2xxSuccessful) { diff --git a/src/main/kotlin/io/openfuture/state/webhook/WebhookRestClient.kt b/src/main/kotlin/io/openfuture/state/webhook/WebhookRestClient.kt index 8d1069d..7b84501 100644 --- a/src/main/kotlin/io/openfuture/state/webhook/WebhookRestClient.kt +++ b/src/main/kotlin/io/openfuture/state/webhook/WebhookRestClient.kt @@ -6,6 +6,7 @@ import org.springframework.http.MediaType import org.springframework.stereotype.Component import org.springframework.web.reactive.function.BodyInserters import org.springframework.web.reactive.function.client.WebClient +import java.math.BigDecimal import java.net.UnknownHostException import java.util.concurrent.TimeUnit @@ -15,7 +16,7 @@ class WebhookRestClient(builder: WebClient.Builder) { private val client: WebClient = builder.build() - suspend fun doPost(url: String, body: Any): WebhookResponse { + suspend fun doPost(url: String, address: String, body: Any): WebhookResponse { println("webhook body $body") return try { val response = client.post()