Skip to content

Commit

Permalink
Rebase and regenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Apr 19, 2024
1 parent 4e7ca84 commit 70da655
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,146 @@ import com.facebook.react.bridge.*
import ls_sdk.*
import java.util.*

fun asPreparePaymentResponse(preparePaymentResponse: ReadableMap): PreparePaymentResponse? {
fun asPrepareReceiveRequest(prepareReceiveRequest: ReadableMap): PrepareReceiveRequest? {
if (!validateMandatoryFields(
preparePaymentResponse,
prepareReceiveRequest,
arrayOf(),
)
) {
return null
}
val payerAmountSat =
if (hasNonNullKey(
prepareReceiveRequest,
"payerAmountSat",
)
) {
prepareReceiveRequest.getDouble("payerAmountSat").toULong()
} else {
null
}
val receiverAmountSat =
if (hasNonNullKey(
prepareReceiveRequest,
"receiverAmountSat",
)
) {
prepareReceiveRequest.getDouble("receiverAmountSat").toULong()
} else {
null
}
return PrepareReceiveRequest(
payerAmountSat,
receiverAmountSat,
)
}

fun readableMapOf(prepareReceiveRequest: PrepareReceiveRequest): ReadableMap {
return readableMapOf(
"payerAmountSat" to prepareReceiveRequest.payerAmountSat,
"receiverAmountSat" to prepareReceiveRequest.receiverAmountSat,
)
}

fun asPrepareReceiveRequestList(arr: ReadableArray): List<PrepareReceiveRequest> {
val list = ArrayList<PrepareReceiveRequest>()
for (value in arr.toArrayList()) {
when (value) {
is ReadableMap -> list.add(asPrepareReceiveRequest(value)!!)
else -> throw LsSdkException.Generic(errUnexpectedType("${value::class.java.name}"))
}
}
return list
}

fun asPrepareReceiveResponse(prepareReceiveResponse: ReadableMap): PrepareReceiveResponse? {
if (!validateMandatoryFields(
prepareReceiveResponse,
arrayOf(
"id",
"fundingAmount",
"fundingAddress",
"pairHash",
"payerAmountSat",
"feesSat",
),
)
) {
return null
}
val id = preparePaymentResponse.getString("id")!!
val fundingAmount = preparePaymentResponse.getDouble("fundingAmount").toULong()
val fundingAddress = preparePaymentResponse.getString("fundingAddress")!!
return PreparePaymentResponse(
id,
fundingAmount,
fundingAddress,
val pairHash = prepareReceiveResponse.getString("pairHash")!!
val payerAmountSat = prepareReceiveResponse.getDouble("payerAmountSat").toULong()
val feesSat = prepareReceiveResponse.getDouble("feesSat").toULong()
return PrepareReceiveResponse(
pairHash,
payerAmountSat,
feesSat,
)
}

fun readableMapOf(preparePaymentResponse: PreparePaymentResponse): ReadableMap {
fun readableMapOf(prepareReceiveResponse: PrepareReceiveResponse): ReadableMap {
return readableMapOf(
"id" to preparePaymentResponse.id,
"fundingAmount" to preparePaymentResponse.fundingAmount,
"fundingAddress" to preparePaymentResponse.fundingAddress,
"pairHash" to prepareReceiveResponse.pairHash,
"payerAmountSat" to prepareReceiveResponse.payerAmountSat,
"feesSat" to prepareReceiveResponse.feesSat,
)
}

fun asPreparePaymentResponseList(arr: ReadableArray): List<PreparePaymentResponse> {
val list = ArrayList<PreparePaymentResponse>()
fun asPrepareReceiveResponseList(arr: ReadableArray): List<PrepareReceiveResponse> {
val list = ArrayList<PrepareReceiveResponse>()
for (value in arr.toArrayList()) {
when (value) {
is ReadableMap -> list.add(asPreparePaymentResponse(value)!!)
is ReadableMap -> list.add(asPrepareReceiveResponse(value)!!)
else -> throw LsSdkException.Generic(errUnexpectedType("${value::class.java.name}"))
}
}
return list
}

fun asReceivePaymentRequest(receivePaymentRequest: ReadableMap): ReceivePaymentRequest? {
fun asPrepareSendResponse(prepareSendResponse: ReadableMap): PrepareSendResponse? {
if (!validateMandatoryFields(
receivePaymentRequest,
arrayOf(),
prepareSendResponse,
arrayOf(
"id",
"payerAmountSat",
"receiverAmountSat",
"totalFees",
"fundingAddress",
"invoice",
),
)
) {
return null
}
val invoiceAmountSat =
if (hasNonNullKey(
receivePaymentRequest,
"invoiceAmountSat",
)
) {
receivePaymentRequest.getDouble("invoiceAmountSat").toULong()
} else {
null
}
val onchainAmountSat =
if (hasNonNullKey(
receivePaymentRequest,
"onchainAmountSat",
)
) {
receivePaymentRequest.getDouble("onchainAmountSat").toULong()
} else {
null
}
return ReceivePaymentRequest(
invoiceAmountSat,
onchainAmountSat,
val id = prepareSendResponse.getString("id")!!
val payerAmountSat = prepareSendResponse.getDouble("payerAmountSat").toULong()
val receiverAmountSat = prepareSendResponse.getDouble("receiverAmountSat").toULong()
val totalFees = prepareSendResponse.getDouble("totalFees").toULong()
val fundingAddress = prepareSendResponse.getString("fundingAddress")!!
val invoice = prepareSendResponse.getString("invoice")!!
return PrepareSendResponse(
id,
payerAmountSat,
receiverAmountSat,
totalFees,
fundingAddress,
invoice,
)
}

fun readableMapOf(receivePaymentRequest: ReceivePaymentRequest): ReadableMap {
fun readableMapOf(prepareSendResponse: PrepareSendResponse): ReadableMap {
return readableMapOf(
"invoiceAmountSat" to receivePaymentRequest.invoiceAmountSat,
"onchainAmountSat" to receivePaymentRequest.onchainAmountSat,
"id" to prepareSendResponse.id,
"payerAmountSat" to prepareSendResponse.payerAmountSat,
"receiverAmountSat" to prepareSendResponse.receiverAmountSat,
"totalFees" to prepareSendResponse.totalFees,
"fundingAddress" to prepareSendResponse.fundingAddress,
"invoice" to prepareSendResponse.invoice,
)
}

fun asReceivePaymentRequestList(arr: ReadableArray): List<ReceivePaymentRequest> {
val list = ArrayList<ReceivePaymentRequest>()
fun asPrepareSendResponseList(arr: ReadableArray): List<PrepareSendResponse> {
val list = ArrayList<PrepareSendResponse>()
for (value in arr.toArrayList()) {
when (value) {
is ReadableMap -> list.add(asReceivePaymentRequest(value)!!)
is ReadableMap -> list.add(asPrepareSendResponse(value)!!)
else -> throw LsSdkException.Generic(errUnexpectedType("${value::class.java.name}"))
}
}
Expand Down Expand Up @@ -172,27 +225,23 @@ fun asWalletInfo(walletInfo: ReadableMap): WalletInfo? {
arrayOf(
"balanceSat",
"pubkey",
"activeAddress",
),
)
) {
return null
}
val balanceSat = walletInfo.getDouble("balanceSat").toULong()
val pubkey = walletInfo.getString("pubkey")!!
val activeAddress = walletInfo.getString("activeAddress")!!
return WalletInfo(
balanceSat,
pubkey,
activeAddress,
)
}

fun readableMapOf(walletInfo: WalletInfo): ReadableMap {
return readableMapOf(
"balanceSat" to walletInfo.balanceSat,
"pubkey" to walletInfo.pubkey,
"activeAddress" to walletInfo.activeAddress,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,52 @@ class LiquidSwapSDKModule(reactContext: ReactApplicationContext) : ReactContextB
}
}

@ReactMethod
fun prepareSendPayment(
invoice: String,
promise: Promise,
) {
executor.execute {
try {
val res = getBindingWallet().prepareSendPayment(invoice)
promise.resolve(readableMapOf(res))
} catch (e: Exception) {
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
}
}
}

@ReactMethod
fun sendPayment(
req: ReadableMap,
promise: Promise,
) {
executor.execute {
try {
val preparePaymentResponse =
asPreparePaymentResponse(req) ?: run {
throw LsSdkException.Generic(errMissingMandatoryField("req", "PreparePaymentResponse"))
val prepareSendResponse =
asPrepareSendResponse(req) ?: run {
throw LsSdkException.Generic(errMissingMandatoryField("req", "PrepareSendResponse"))
}
val res = getBindingWallet().sendPayment(prepareSendResponse)
promise.resolve(readableMapOf(res))
} catch (e: Exception) {
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
}
}
}

@ReactMethod
fun prepareReceivePayment(
req: ReadableMap,
promise: Promise,
) {
executor.execute {
try {
val prepareReceiveRequest =
asPrepareReceiveRequest(req) ?: run {
throw LsSdkException.Generic(errMissingMandatoryField("req", "PrepareReceiveRequest"))
}
val res = getBindingWallet().sendPayment(preparePaymentResponse)
val res = getBindingWallet().prepareReceivePayment(prepareReceiveRequest)
promise.resolve(readableMapOf(res))
} catch (e: Exception) {
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
Expand All @@ -104,11 +138,11 @@ class LiquidSwapSDKModule(reactContext: ReactApplicationContext) : ReactContextB
) {
executor.execute {
try {
val receivePaymentRequest =
asReceivePaymentRequest(req) ?: run {
throw LsSdkException.Generic(errMissingMandatoryField("req", "ReceivePaymentRequest"))
val prepareReceiveResponse =
asPrepareReceiveResponse(req) ?: run {
throw LsSdkException.Generic(errMissingMandatoryField("req", "PrepareReceiveResponse"))
}
val res = getBindingWallet().receivePayment(receivePaymentRequest)
val res = getBindingWallet().receivePayment(prepareReceiveResponse)
promise.resolve(readableMapOf(res))
} catch (e: Exception) {
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
Expand Down
Loading

0 comments on commit 70da655

Please sign in to comment.