Skip to content

Commit

Permalink
Wallet processor check with lowercase address
Browse files Browse the repository at this point in the history
  • Loading branch information
physcom committed Sep 9, 2024
1 parent ca37666 commit 1c88250
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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 {
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,16 +23,18 @@ class WalletController(
private val walletTransactionFacade: WalletTransactionFacade,
private val blockchains: List<Blockchain>
) {

@PostMapping
suspend fun saveMultiple(@Valid @RequestBody request: SaveOrderWalletRequest): PlaceOrderResponse {
return walletService.saveOrder(request)
}

@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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()

Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
6 changes: 3 additions & 3 deletions src/main/kotlin/io/openfuture/state/service/WebhookInvoker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down

0 comments on commit 1c88250

Please sign in to comment.