Skip to content

Commit

Permalink
Merge pull request #72 from OpenFuturePlatform/interoperability
Browse files Browse the repository at this point in the history
Added new methods for noncustodial wallet import
  • Loading branch information
borbuevbeksultan committed May 1, 2023
2 parents 29afeb3 + af5f054 commit 112dae5
Show file tree
Hide file tree
Showing 38 changed files with 346 additions and 164 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/io/openfuture/state/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.runApplication

@ConfigurationPropertiesScan("io.openfuture.state.property")
@ConfigurationPropertiesScan(*arrayOf("io.openfuture.state.property", "io.openfuture.state.config.property"))
@SpringBootApplication
class Application

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.math.BigDecimal
import java.math.BigInteger

@Component
class RopstenBlockchain(private val web3jTest: Web3j): Blockchain() {
class GoerliBlockchain(private val web3jTest: Web3j): Blockchain() {

override suspend fun getLastBlockNumber(): Int = web3jTest.ethBlockNumber()
.sendAsync().await()
Expand Down Expand Up @@ -80,7 +80,7 @@ class RopstenBlockchain(private val web3jTest: Web3j): Blockchain() {
.sendAsync().await()
.transactionReceipt

var address = "";
var address = ""
transactionReceipt.get().logs.forEach{
address = it.address
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.web3j.protocol.core.DefaultBlockParameterNumber
import org.web3j.protocol.core.methods.response.EthBlock
import org.web3j.utils.Convert

//@Component
@Component
class BinanceTestnetBlockchain(@Qualifier("web3jBinanceTestnet") private val web3jBinanceTestnet: Web3j): Blockchain() {

override suspend fun getLastBlockNumber(): Int = web3jBinanceTestnet.ethBlockNumber()
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/io/openfuture/state/config/EthereumConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.openfuture.state.config

import io.openfuture.state.property.EthereumProperties
import io.openfuture.state.property.EthereumAlchemyMainnetProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.web3j.protocol.Web3j
Expand All @@ -10,8 +10,8 @@ import org.web3j.protocol.http.HttpService
class EthereumConfig {

@Bean
fun web3j(ethereumProperties: EthereumProperties): Web3j {
return Web3j.build(HttpService(ethereumProperties.nodeAddress))
fun web3j(ethereumProperties: EthereumAlchemyMainnetProperties): Web3j {
return Web3j.build(HttpService(ethereumProperties.address))
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.openfuture.state.config

import io.openfuture.state.property.EthereumTestProperties
import io.openfuture.state.property.EthereumAlchemyTestnetProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.web3j.protocol.Web3j
import org.web3j.protocol.http.HttpService

@Configuration
class EthereumTestConfig {

@Bean
fun web3jTest(ethereumTestProperties: EthereumTestProperties): Web3j {
return Web3j.build(HttpService(ethereumTestProperties.nodeAddress))
fun web3jTest(ethereumAlchemyTestnetProperties: EthereumAlchemyTestnetProperties): Web3j {
return Web3j.build(HttpService(ethereumAlchemyTestnetProperties.address))
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.openfuture.state.config

import io.openfuture.state.config.property.OpenApiProperties
import io.openfuture.state.property.OpenApiProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestTemplate
Expand Down
24 changes: 10 additions & 14 deletions src/main/kotlin/io/openfuture/state/controller/OrderController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.openfuture.state.controller

import io.openfuture.state.domain.Order
import io.openfuture.state.domain.Wallet
import io.openfuture.state.exception.NotFoundException
import io.openfuture.state.repository.OrderRepository
import io.openfuture.state.service.TransactionService
import io.openfuture.state.service.WalletService
Expand All @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono

@RestController
@RequestMapping("/api/orders")
Expand All @@ -22,20 +23,15 @@ class OrderController(

@GetMapping("/{orderKey}")
suspend fun getOrderState(@PathVariable orderKey: String): OrderStateResponse? {

val wallets = walletService.findAllByOrderKey(orderKey)
orderRepository.findByOrderKey(orderKey).awaitFirstOrNull()?.let {
return convertToOrderStateResponse(it, wallets)
}
return null
val walletsResponse: List<WalletResponse> = wallets.map { convertToWalletResponse(it) }

}
return orderRepository.findByOrderKey(orderKey)
.map { o -> OrderStateResponse(o.placedAt.plusHours(7), o.amount, o.paid, walletsResponse) }
.switchIfEmpty(Mono.error(NotFoundException("Order with $orderKey not found")))
.awaitSingle()

private suspend fun convertToOrderStateResponse(order: Order, wallets: List<Wallet>): OrderStateResponse {
val orderDate = order.placedAt.plusHours(7)
val amount = order.amount
val paid = order.paid
val walletsResponse: List<WalletResponse> = wallets.map { convertToWalletResponse(it) }
return OrderStateResponse(orderDate, amount, paid, walletsResponse)
}

private suspend fun convertToWalletResponse(wallet: Wallet): WalletResponse {
Expand All @@ -48,12 +44,12 @@ class OrderController(
it.date,
it.blockHeight,
it.blockHash,
wallet.rate,
wallet.userData.rate,
it.native,
it.token
)
}
return WalletResponse(wallet.identity.address, wallet.identity.blockchain, wallet.rate, transactionsResponse)
return WalletResponse(wallet.identity.address, wallet.identity.blockchain, wallet.userData.rate, transactionsResponse)
}

}
62 changes: 40 additions & 22 deletions src/main/kotlin/io/openfuture/state/controller/WalletController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ package io.openfuture.state.controller

import io.openfuture.state.blockchain.Blockchain
import io.openfuture.state.domain.Wallet
import io.openfuture.state.domain.WalletPaymentDetail
import io.openfuture.state.repository.OrderRepository
import io.openfuture.state.service.WalletService
import io.openfuture.state.service.WalletTransactionFacade
import io.openfuture.state.service.dto.PlaceOrderResponse
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import java.math.BigDecimal
import java.time.LocalDateTime
import java.util.*
import java.util.stream.Collector
import javax.validation.Valid
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotEmpty
import kotlin.streams.toList

@RestController
@RequestMapping("/api/wallets")
class WalletController(private val walletService: WalletService, private val blockchains: List<Blockchain>) {
class WalletController(
private val walletService: WalletService,
private val walletTransactionFacade: WalletTransactionFacade,
private val blockchains: List<Blockchain>
) {

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

Expand All @@ -38,6 +51,11 @@ class WalletController(private val walletService: WalletService, private val blo
return WalletDto(wallet)
}

@GetMapping("/application/{applicationId}")
suspend fun findByApplication(@PathVariable applicationId: String): List<WalletPaymentDetail> {
return walletTransactionFacade.getOrderByApplication(applicationId)
}

@DeleteMapping("/blockchain/{blockchain}/address/{address}")
suspend fun deleteStateBynAddress(@PathVariable blockchain: String, @PathVariable address: String): Boolean {
walletService.deleteByIdentity(blockchain, address)
Expand All @@ -56,36 +74,37 @@ class WalletController(private val walletService: WalletService, private val blo
data class WalletDto(
val id: String,
val address: String,
val webhook: String,
val blockchain: String,
val applicationId: String,
val blockchain: String,
val lastUpdateDate: LocalDateTime,
val nonce: Int,
val lastUpdateDate: LocalDateTime
val webhook: String,
) {
constructor(wallet: Wallet) : this(
wallet.id,
wallet.identity.address,
wallet.webhook,
wallet.identity.blockchain,
wallet.applicationId,
wallet.nonce,
wallet.identity.blockchain,
wallet.lastUpdate,
wallet.userData.nonce,
wallet.webhook
)
}

data class SaveOrderWalletRequest(
@field:NotBlank
val webhook: String,
val blockchains: ArrayList<BlockChainDataRequest>,
val applicationId: String,
var metadata: WalletMetaDataRequest = WalletMetaDataRequest()
@field:NotEmpty
val blockchains: ArrayList<BlockChainDataRequest>,
var metadata: WalletMetaDataRequest = WalletMetaDataRequest(),
@field:NotBlank
val webhook: String
)

data class UpdateOrderWalletRequest(
@field:NotBlank
val webhook: String,
val applicationId: String,
var metadata: WalletMetaDataRequest = WalletMetaDataRequest()
var metadata: WalletMetaDataRequest = WalletMetaDataRequest(),
@field:NotBlank
val webhook: String
)

data class BlockChainDataRequest(
Expand All @@ -96,15 +115,12 @@ class WalletController(private val walletService: WalletService, private val blo
data class SaveWalletRequest(
@field:NotBlank
val address: String,

@field:NotBlank
val webhook: String?,

@field:NotBlank
val applicationId: String,

@field:NotBlank
val blockchain: String
val blockchain: String,
@field:NotBlank
val webhook: String?
)

data class WalletMetaDataRequest(
Expand All @@ -124,7 +140,9 @@ class WalletController(private val walletService: WalletService, private val blo
var source: String = UUID.randomUUID().toString(),

@field:NotBlank
val test: Boolean = true
val test: Boolean = true,

var metadata: Any? = null
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.openfuture.state.controller

import io.openfuture.state.service.WalletService
import io.openfuture.state.service.dto.AddWatchResponse
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/wallets/v2/")
class WalletControllerV2(
private val walletService: WalletService
) {

@PostMapping("add")
suspend fun addWallet(@RequestBody request: AddWalletStateForUserRequest): AddWatchResponse {
return walletService.addWallet(request)
}

}

data class AddWalletStateForUserRequest(
val webhook: String,
val blockchains: ArrayList<BlockChain>,
val applicationId: String,
val userId: String,
val test: Boolean,
val metadata: Any?
)

data class BlockChain(
val address: String,
val blockchain: String
)
8 changes: 3 additions & 5 deletions src/main/kotlin/io/openfuture/state/domain/Order.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ data class Order(
val applicationId: String,
val amount: BigDecimal,//in USD
val productCurrency: String,//USD
var paid: BigDecimal = BigDecimal.ZERO,//USD
val source: String
var paid: BigDecimal = BigDecimal.ZERO//USD
) {
constructor(
orderKey: String,
applicationId: String,
amount: BigDecimal,
productCurrency: String,
source: String
productCurrency: String
) : this(
ObjectId().toHexString(), LocalDateTime.now(), LocalDateTime.now(), orderKey, applicationId, amount, productCurrency, BigDecimal.ZERO, source
ObjectId().toHexString(), LocalDateTime.now(), LocalDateTime.now(), orderKey, applicationId, amount, productCurrency, BigDecimal.ZERO
)
}
20 changes: 14 additions & 6 deletions src/main/kotlin/io/openfuture/state/domain/Wallet.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package io.openfuture.state.domain

import org.bson.types.ObjectId
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.core.mapping.Field
import org.springframework.data.mongodb.core.mapping.MongoId
import java.math.BigDecimal
import java.time.LocalDateTime
import javax.validation.constraints.NotBlank

@Document
data class Wallet(
Expand All @@ -18,10 +17,19 @@ data class Wallet(
val applicationId: String,
@LastModifiedDate
var lastUpdate: LocalDateTime = LocalDateTime.now(),
@CreatedDate
var createdDate: LocalDateTime = LocalDateTime.now(),
@MongoId
val id: String = ObjectId().toHexString(),
val rate: BigDecimal = BigDecimal.ZERO,
var nonce: Int = 0,
@Field("order")
var order: Order? = null
var userData: UserData,
var walletType: WalletType
)

data class UserData(
val id: String = ObjectId().toHexString(),
var nonce: Int = 0,
var order: Order? = null,
var userId: String? = null,
val rate: BigDecimal = BigDecimal.ZERO,
val metadata: Any? = null
)
17 changes: 12 additions & 5 deletions src/main/kotlin/io/openfuture/state/domain/WalletPaymentDetail.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package io.openfuture.state.domain

import java.math.BigDecimal

data class WalletPaymentDetail(
var orderId: String,
var orderKey: String,
var amount: String,
var productCurrency: String,
var source: String,
val paymentCurrency: String
var amount: BigDecimal,
var totalPaid: BigDecimal = BigDecimal.ZERO,
var currency: String,
val blockchains: List<BlockchainWallets>
)

data class BlockchainWallets(
val address: String,
val blockchain: String,
val rate: BigDecimal
)
Loading

0 comments on commit 112dae5

Please sign in to comment.