crosslightning-intermediary
Advanced tools
Comparing version 3.0.10 to 3.0.11
@@ -248,8 +248,5 @@ "use strict"; | ||
const blockDelta = new BN(timeout - current_block_height); | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] block delta: ", blockDelta.toString(10)); | ||
const expiryTimeout = blockDelta.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod); | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] expiry timeout: ", expiryTimeout.toString(10)); | ||
if (expiryTimeout.isNeg()) { | ||
if (blockDelta.lt(this.config.minCltv)) { | ||
yield cancelAndRemove(); | ||
console.error("[From BTC-LN: REST.GetInvoicePaymentAuth] Expire time is lower than 0"); | ||
console.error("[From BTC-LN: REST.GetInvoicePaymentAuth] Receive HTLC expires too soon (required: " + this.config.minCltv.toString(10) + ", got: " + blockDelta.toString(10) + ")"); | ||
throw { | ||
@@ -260,13 +257,6 @@ code: 20002, | ||
} | ||
let baseSD; | ||
//Solana workaround | ||
if (this.swapContract.getRawRefundFee != null) { | ||
baseSD = yield this.swapContract.getRawRefundFee(); | ||
} | ||
else { | ||
baseSD = (yield this.swapContract.getRefundFee()).mul(new BN(2)); | ||
} | ||
const swapValueInNativeCurrency = yield this.swapPricing.getFromBtcSwapAmount(invoiceAmount.sub(fee), this.swapContract.getNativeCurrencyAddress()); | ||
const apyPPM = new BN(Math.floor(this.config.securityDepositAPY * 1000000)); | ||
const variableSD = swapValueInNativeCurrency.mul(apyPPM).mul(expiryTimeout).div(new BN(1000000)).div(secondsInYear); | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] using cltv delta: ", this.config.minCltv.toString(10)); | ||
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod); | ||
//Cap expiryTimeout to minCltv, for predictable security deposit | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] expiry timeout: ", expiryTimeout.toString(10)); | ||
/* | ||
@@ -281,3 +271,3 @@ { | ||
*/ | ||
const payInvoiceObject = yield this.swapContract.createSwapData(crosslightning_base_1.ChainSwapType.HTLC, this.swapContract.getAddress(), invoice.description, useToken, sendAmount, invoice.id, new BN(Math.floor(Date.now() / 1000)).add(expiryTimeout), new BN(0), 0, false, true, baseSD.add(variableSD), new BN(0)); | ||
const payInvoiceObject = yield this.swapContract.createSwapData(crosslightning_base_1.ChainSwapType.HTLC, this.swapContract.getAddress(), invoice.description, useToken, sendAmount, invoice.id, new BN(Math.floor(Date.now() / 1000)).add(expiryTimeout), new BN(0), 0, false, true, invoiceData.data.getSecurityDeposit(), new BN(0)); | ||
if (invoiceData.state === FromBtcLnSwapAbs_1.FromBtcLnSwapState.CREATED) { | ||
@@ -389,3 +379,3 @@ invoiceData.data = payInvoiceObject; | ||
description: req.body.address, | ||
cltv_delta: this.config.minCltv.toString(10), | ||
cltv_delta: this.config.minCltv.add(new BN(5)).toString(10), | ||
expires_at: new Date(Date.now() + (req.body.expiry * 1000)).toISOString(), | ||
@@ -401,3 +391,16 @@ id: req.body.paymentHash, | ||
const createdSwap = new FromBtcLnSwapAbs_1.FromBtcLnSwapAbs(hodlInvoice.request, swapFee); | ||
createdSwap.data = yield this.swapContract.createSwapData(crosslightning_base_1.ChainSwapType.HTLC, this.swapContract.getAddress(), req.body.address, useToken, null, req.body.paymentHash, null, null, 0, false, true, new BN(0), new BN(0)); | ||
//Pre-compute the security deposit | ||
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod); | ||
let baseSD; | ||
//Solana workaround | ||
if (this.swapContract.getRawRefundFee != null) { | ||
baseSD = yield this.swapContract.getRawRefundFee(); | ||
} | ||
else { | ||
baseSD = (yield this.swapContract.getRefundFee()).mul(new BN(2)); | ||
} | ||
const swapValueInNativeCurrency = yield this.swapPricing.getFromBtcSwapAmount(amountBD.sub(swapFee), this.swapContract.getNativeCurrencyAddress()); | ||
const apyPPM = new BN(Math.floor(this.config.securityDepositAPY * 1000000)); | ||
const variableSD = swapValueInNativeCurrency.mul(apyPPM).mul(expiryTimeout).div(new BN(1000000)).div(secondsInYear); | ||
createdSwap.data = yield this.swapContract.createSwapData(crosslightning_base_1.ChainSwapType.HTLC, this.swapContract.getAddress(), req.body.address, useToken, null, req.body.paymentHash, null, null, 0, false, true, baseSD.add(variableSD), new BN(0)); | ||
yield this.storageManager.saveData(req.body.paymentHash, createdSwap); | ||
@@ -410,3 +413,4 @@ res.status(200).json({ | ||
total: amountInToken.sub(swapFeeInToken).toString(10), | ||
intermediaryKey: this.swapContract.getAddress() | ||
intermediaryKey: this.swapContract.getAddress(), | ||
securityDeposit: baseSD.add(variableSD).toString(10) | ||
} | ||
@@ -413,0 +417,0 @@ }); |
{ | ||
"name": "crosslightning-intermediary", | ||
"version": "3.0.10", | ||
"version": "3.0.11", | ||
"description": "Main functionality implementation for intermediary node, easily extensible to any chain", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -324,11 +324,5 @@ import * as BN from "bn.js"; | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] block delta: ", blockDelta.toString(10)); | ||
const expiryTimeout = blockDelta.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod); | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] expiry timeout: ", expiryTimeout.toString(10)); | ||
if (expiryTimeout.isNeg()) { | ||
if(blockDelta.lt(this.config.minCltv)) { | ||
await cancelAndRemove(); | ||
console.error("[From BTC-LN: REST.GetInvoicePaymentAuth] Expire time is lower than 0"); | ||
console.error("[From BTC-LN: REST.GetInvoicePaymentAuth] Receive HTLC expires too soon (required: "+this.config.minCltv.toString(10)+", got: "+blockDelta.toString(10)+")"); | ||
throw { | ||
@@ -340,15 +334,9 @@ code: 20002, | ||
let baseSD: BN; | ||
//Solana workaround | ||
if((this.swapContract as any).getRawRefundFee!=null) { | ||
baseSD = await (this.swapContract as any).getRawRefundFee(); | ||
} else { | ||
baseSD = (await this.swapContract.getRefundFee()).mul(new BN(2)); | ||
} | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] using cltv delta: ", this.config.minCltv.toString(10)); | ||
const swapValueInNativeCurrency = await this.swapPricing.getFromBtcSwapAmount(invoiceAmount.sub(fee), this.swapContract.getNativeCurrencyAddress()); | ||
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod); | ||
const apyPPM = new BN(Math.floor(this.config.securityDepositAPY*1000000)); | ||
//Cap expiryTimeout to minCltv, for predictable security deposit | ||
const variableSD = swapValueInNativeCurrency.mul(apyPPM).mul(expiryTimeout).div(new BN(1000000)).div(secondsInYear); | ||
console.log("[From BTC-LN: REST.GetInvoicePaymentAuth] expiry timeout: ", expiryTimeout.toString(10)); | ||
@@ -376,3 +364,3 @@ /* | ||
true, | ||
baseSD.add(variableSD), | ||
invoiceData.data.getSecurityDeposit(), | ||
new BN(0) | ||
@@ -509,3 +497,3 @@ ); | ||
description: req.body.address, | ||
cltv_delta: this.config.minCltv.toString(10), | ||
cltv_delta: this.config.minCltv.add(new BN(5)).toString(10), | ||
expires_at: new Date(Date.now()+(req.body.expiry*1000)).toISOString(), | ||
@@ -527,2 +515,17 @@ id: req.body.paymentHash, | ||
//Pre-compute the security deposit | ||
const expiryTimeout = this.config.minCltv.mul(this.config.bitcoinBlocktime.div(this.config.safetyFactor)).sub(this.config.gracePeriod); | ||
let baseSD: BN; | ||
//Solana workaround | ||
if((this.swapContract as any).getRawRefundFee!=null) { | ||
baseSD = await (this.swapContract as any).getRawRefundFee(); | ||
} else { | ||
baseSD = (await this.swapContract.getRefundFee()).mul(new BN(2)); | ||
} | ||
const swapValueInNativeCurrency = await this.swapPricing.getFromBtcSwapAmount(amountBD.sub(swapFee), this.swapContract.getNativeCurrencyAddress()); | ||
const apyPPM = new BN(Math.floor(this.config.securityDepositAPY*1000000)); | ||
const variableSD = swapValueInNativeCurrency.mul(apyPPM).mul(expiryTimeout).div(new BN(1000000)).div(secondsInYear); | ||
createdSwap.data = await this.swapContract.createSwapData( | ||
@@ -539,3 +542,3 @@ ChainSwapType.HTLC, | ||
true, | ||
new BN(0), | ||
baseSD.add(variableSD), | ||
new BN(0) | ||
@@ -552,3 +555,4 @@ ); | ||
total: amountInToken.sub(swapFeeInToken).toString(10), | ||
intermediaryKey: this.swapContract.getAddress() | ||
intermediaryKey: this.swapContract.getAddress(), | ||
securityDeposit: baseSD.add(variableSD).toString(10) | ||
} | ||
@@ -555,0 +559,0 @@ }); |
315013
6579