crosslightning-sdk-base
Advanced tools
Comparing version 9.2.0-beta24 to 9.2.0-beta25
@@ -85,3 +85,10 @@ import { IFromBTCSwap } from "../IFromBTCSwap"; | ||
txsClaim(): Promise<TXType[]>; | ||
/** | ||
* Claims and finishes the swap | ||
* | ||
* @param noWaitForConfirmation Do not wait for transaction confirmation | ||
* @param abortSignal Abort signal to stop waiting for transaction confirmation | ||
*/ | ||
claim(noWaitForConfirmation?: boolean, abortSignal?: AbortSignal): Promise<string>; | ||
serialize(): any; | ||
} |
@@ -19,2 +19,3 @@ "use strict"; | ||
const BN = require("bn.js"); | ||
const crosslightning_base_1 = require("crosslightning-base"); | ||
const ISwap_1 = require("../../ISwap"); | ||
@@ -192,2 +193,32 @@ const buffer_1 = require("buffer"); | ||
} | ||
/** | ||
* Claims and finishes the swap | ||
* | ||
* @param noWaitForConfirmation Do not wait for transaction confirmation | ||
* @param abortSignal Abort signal to stop waiting for transaction confirmation | ||
*/ | ||
claim(noWaitForConfirmation, abortSignal) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let txIds; | ||
try { | ||
txIds = yield this.wrapper.contract.sendAndConfirm(yield this.txsClaim(), !noWaitForConfirmation, abortSignal); | ||
} | ||
catch (e) { | ||
this.logger.info("claim(): Failed to claim ourselves, checking swap claim state..."); | ||
if (this.state === FromBTCSwapState.CLAIM_CLAIMED) { | ||
this.logger.info("claim(): Transaction state is CLAIM_CLAIMED, swap was successfully claimed by the watchtower"); | ||
return this.claimTxId; | ||
} | ||
if ((yield this.wrapper.contract.getCommitStatus(this.data)) === crosslightning_base_1.SwapCommitStatus.PAID) { | ||
this.logger.info("claim(): Transaction commit status is PAID, swap was successfully claimed by the watchtower"); | ||
yield this._saveAndEmit(FromBTCSwapState.CLAIM_CLAIMED); | ||
return null; | ||
} | ||
throw e; | ||
} | ||
this.claimTxId = txIds[0]; | ||
yield this._saveAndEmit(this.CLAIM_STATE); | ||
return txIds[0]; | ||
}); | ||
} | ||
////////////////////////////// | ||
@@ -194,0 +225,0 @@ //// Storage |
@@ -159,9 +159,25 @@ "use strict"; | ||
continue; | ||
const eventMeta = event.meta; | ||
let swapChanged = false; | ||
if (event instanceof crosslightning_base_1.InitializeEvent) | ||
if (event instanceof crosslightning_base_1.InitializeEvent) { | ||
swapChanged = yield this.processEventInitialize(swap, event); | ||
if (event instanceof crosslightning_base_1.ClaimEvent) | ||
if ((eventMeta === null || eventMeta === void 0 ? void 0 : eventMeta.txId) != null && swap.commitTxId !== eventMeta.txId) { | ||
swap.commitTxId = eventMeta.txId; | ||
swapChanged || (swapChanged = true); | ||
} | ||
} | ||
if (event instanceof crosslightning_base_1.ClaimEvent) { | ||
swapChanged = yield this.processEventClaim(swap, event); | ||
if (event instanceof crosslightning_base_1.RefundEvent) | ||
if ((eventMeta === null || eventMeta === void 0 ? void 0 : eventMeta.txId) != null && swap.claimTxId !== eventMeta.txId) { | ||
swap.claimTxId = eventMeta.txId; | ||
swapChanged || (swapChanged = true); | ||
} | ||
} | ||
if (event instanceof crosslightning_base_1.RefundEvent) { | ||
swapChanged = yield this.processEventRefund(swap, event); | ||
if ((eventMeta === null || eventMeta === void 0 ? void 0 : eventMeta.txId) != null && swap.refundTxId !== eventMeta.txId) { | ||
swap.refundTxId = eventMeta.txId; | ||
swapChanged || (swapChanged = true); | ||
} | ||
} | ||
this.logger.info("processEvents(): " + event.constructor.name + " processed for " + swap.getPaymentHashString() + " swap: ", swap); | ||
@@ -168,0 +184,0 @@ if (swapChanged) { |
@@ -24,4 +24,2 @@ "use strict"; | ||
const SwapType_1 = require("./SwapType"); | ||
const MempoolApi_1 = require("../btc/mempool/MempoolApi"); | ||
const MempoolBitcoinRpc_1 = require("../btc/mempool/MempoolBitcoinRpc"); | ||
const MempoolBtcRelaySynchronizer_1 = require("../btc/mempool/synchronizer/MempoolBtcRelaySynchronizer"); | ||
@@ -42,18 +40,7 @@ const LnForGasWrapper_1 = require("./swapforgas/ln/LnForGasWrapper"); | ||
options.bitcoinNetwork = options.bitcoinNetwork == null ? BitcoinNetwork_1.BitcoinNetwork.TESTNET : options.bitcoinNetwork; | ||
switch (options.bitcoinNetwork) { | ||
case BitcoinNetwork_1.BitcoinNetwork.MAINNET: | ||
this.bitcoinNetwork = bitcoinjs_lib_1.networks.bitcoin; | ||
this.mempoolApi = new MempoolApi_1.MempoolApi("https://mempool.space/api/", options.getRequestTimeout); | ||
break; | ||
case BitcoinNetwork_1.BitcoinNetwork.TESTNET: | ||
this.bitcoinNetwork = bitcoinjs_lib_1.networks.testnet; | ||
this.mempoolApi = new MempoolApi_1.MempoolApi("https://mempool.space/testnet/api/", options.getRequestTimeout); | ||
break; | ||
default: | ||
throw new Error("Unsupported bitcoin network"); | ||
} | ||
this.prices = options.pricing; | ||
this.swapContract = swapContract; | ||
this.chainEvents = chainEvents; | ||
this.bitcoinRpc = new MempoolBitcoinRpc_1.MempoolBitcoinRpc(this.mempoolApi); | ||
this.bitcoinRpc = bitcoinRpc; | ||
this.mempoolApi = bitcoinRpc.api; | ||
this.btcRelay = btcRelay; | ||
@@ -60,0 +47,0 @@ this.synchronizer = new MempoolBtcRelaySynchronizer_1.MempoolBtcRelaySynchronizer(btcRelay, bitcoinRpc); |
{ | ||
"name": "crosslightning-sdk-base", | ||
"version": "9.2.0-beta24", | ||
"version": "9.2.0-beta25", | ||
"description": "CrossLightning SDK chain-agnostic base", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -7,4 +7,4 @@ import {IFromBTCSwap} from "../IFromBTCSwap"; | ||
import * as BN from "bn.js"; | ||
import {SwapData} from "crosslightning-base"; | ||
import {isISwapInit, ISwapInit, Token} from "../../ISwap"; | ||
import {SwapCommitStatus, SwapData} from "crosslightning-base"; | ||
import {isISwapInit, ISwapInit} from "../../ISwap"; | ||
import {Buffer} from "buffer"; | ||
@@ -235,3 +235,34 @@ | ||
/** | ||
* Claims and finishes the swap | ||
* | ||
* @param noWaitForConfirmation Do not wait for transaction confirmation | ||
* @param abortSignal Abort signal to stop waiting for transaction confirmation | ||
*/ | ||
async claim(noWaitForConfirmation?: boolean, abortSignal?: AbortSignal): Promise<string> { | ||
let txIds: string[]; | ||
try { | ||
txIds = await this.wrapper.contract.sendAndConfirm( | ||
await this.txsClaim(), !noWaitForConfirmation, abortSignal | ||
); | ||
} catch (e) { | ||
this.logger.info("claim(): Failed to claim ourselves, checking swap claim state..."); | ||
if(this.state===FromBTCSwapState.CLAIM_CLAIMED) { | ||
this.logger.info("claim(): Transaction state is CLAIM_CLAIMED, swap was successfully claimed by the watchtower"); | ||
return this.claimTxId; | ||
} | ||
if((await this.wrapper.contract.getCommitStatus(this.data))===SwapCommitStatus.PAID) { | ||
this.logger.info("claim(): Transaction commit status is PAID, swap was successfully claimed by the watchtower"); | ||
await this._saveAndEmit(FromBTCSwapState.CLAIM_CLAIMED); | ||
return null; | ||
} | ||
throw e; | ||
} | ||
this.claimTxId = txIds[0]; | ||
await this._saveAndEmit(this.CLAIM_STATE); | ||
return txIds[0]; | ||
} | ||
////////////////////////////// | ||
@@ -238,0 +269,0 @@ //// Storage |
@@ -284,6 +284,25 @@ import { | ||
const eventMeta: {blockTime: number, txId: string} | null = (event as any).meta; | ||
let swapChanged: boolean = false; | ||
if(event instanceof InitializeEvent) swapChanged = await this.processEventInitialize(swap, event); | ||
if(event instanceof ClaimEvent) swapChanged = await this.processEventClaim(swap, event); | ||
if(event instanceof RefundEvent) swapChanged = await this.processEventRefund(swap, event); | ||
if(event instanceof InitializeEvent) { | ||
swapChanged = await this.processEventInitialize(swap, event); | ||
if(eventMeta?.txId!=null && swap.commitTxId!==eventMeta.txId) { | ||
swap.commitTxId = eventMeta.txId; | ||
swapChanged ||= true; | ||
} | ||
} | ||
if(event instanceof ClaimEvent) { | ||
swapChanged = await this.processEventClaim(swap, event); | ||
if(eventMeta?.txId!=null && swap.claimTxId!==eventMeta.txId) { | ||
swap.claimTxId = eventMeta.txId; | ||
swapChanged ||= true; | ||
} | ||
} | ||
if(event instanceof RefundEvent) { | ||
swapChanged = await this.processEventRefund(swap, event); | ||
if(eventMeta?.txId!=null && swap.refundTxId!==eventMeta.txId) { | ||
swap.refundTxId = eventMeta.txId; | ||
swapChanged ||= true; | ||
} | ||
} | ||
@@ -290,0 +309,0 @@ this.logger.info("processEvents(): "+event.constructor.name+" processed for "+swap.getPaymentHashString()+" swap: ", swap); |
@@ -9,3 +9,3 @@ import {BitcoinNetwork} from "../btc/BitcoinNetwork"; | ||
import {IntermediaryDiscovery, SwapBounds} from "../intermediaries/IntermediaryDiscovery"; | ||
import {networks, Network, address} from "bitcoinjs-lib"; | ||
import {Network, address} from "bitcoinjs-lib"; | ||
import {decode as bolt11Decode} from "bolt11"; | ||
@@ -108,19 +108,7 @@ import * as BN from "bn.js"; | ||
switch (options.bitcoinNetwork) { | ||
case BitcoinNetwork.MAINNET: | ||
this.bitcoinNetwork = networks.bitcoin; | ||
this.mempoolApi = new MempoolApi("https://mempool.space/api/", options.getRequestTimeout); | ||
break; | ||
case BitcoinNetwork.TESTNET: | ||
this.bitcoinNetwork = networks.testnet; | ||
this.mempoolApi = new MempoolApi("https://mempool.space/testnet/api/", options.getRequestTimeout); | ||
break; | ||
default: | ||
throw new Error("Unsupported bitcoin network"); | ||
} | ||
this.prices = options.pricing; | ||
this.swapContract = swapContract; | ||
this.chainEvents = chainEvents; | ||
this.bitcoinRpc = new MempoolBitcoinRpc(this.mempoolApi); | ||
this.bitcoinRpc = bitcoinRpc; | ||
this.mempoolApi = bitcoinRpc.api; | ||
this.btcRelay = btcRelay; | ||
@@ -127,0 +115,0 @@ this.synchronizer = new MempoolBtcRelaySynchronizer(btcRelay, bitcoinRpc); |
892948
20476