Skip to content

Commit

Permalink
Swap find functions - find on startBlock with optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
monokh committed Aug 20, 2019
1 parent 4d51970 commit 028e780
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 35 deletions.
28 changes: 15 additions & 13 deletions packages/bitcoin-swap-provider/lib/BitcoinSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import networks from '@liquality/bitcoin-networks'
import { version } from '../package.json'

export default class BitcoinSwapProvider extends Provider {
// TODO: have a generate InitSwap and generate RecipSwap
// InitSwap should use checkSequenceVerify instead of checkLockTimeVerify

constructor (chain = { network: networks.bitcoin }, mode = 'p2wsh') {
super()
this._network = chain.network
Expand Down Expand Up @@ -201,31 +198,36 @@ export default class BitcoinSwapProvider extends Provider {
return this.doesTransactionMatchSwapParams(initiationTransaction, value, recipientAddress, refundAddress, secretHash, expiration)
}

async findSwapTransaction (recipientAddress, refundAddress, secretHash, expiration, predicate) {
let blockNumber = await this.getMethod('getBlockHeight')() // TODO: Are mempool TXs possible?
async findSwapTransaction (recipientAddress, refundAddress, secretHash, expiration, startBlock, predicate) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')() // TODO: Are mempool TXs possible?
let swapTransaction = null
let arrivedAtTip = false
while (!swapTransaction) {
let block
try {
block = await this.getMethod('getBlockByNumber')(blockNumber, true)
} catch (e) { }
} catch (e) {
arrivedAtTip = true
}
if (block) {
swapTransaction = block.transactions.find(predicate)
blockNumber++
}
await sleep(5000)
if (arrivedAtTip) {
await sleep(5000)
}
}
return swapTransaction
}

async findInitiateSwapTransaction (value, recipientAddress, refundAddress, secretHash, expiration) {
return this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration,
async findInitiateSwapTransaction (value, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
return this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration, startBlock,
tx => this.doesTransactionMatchSwapParams(tx, value, recipientAddress, refundAddress, secretHash, expiration)
)
}

async findClaimSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
const claimSwapTransaction = await this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration,
async findClaimSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
const claimSwapTransaction = await this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration, startBlock,
tx => tx._raw.vout.find(vout => vout.scriptPubKey.addresses && vout.scriptPubKey.addresses.includes(recipientAddress))
)

Expand All @@ -235,8 +237,8 @@ export default class BitcoinSwapProvider extends Provider {
}
}

async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
const refundSwapTransaction = await this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration,
async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
const refundSwapTransaction = await this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration, startBlock,
tx => tx._raw.vout.find(vout => vout.scriptPubKey.addresses && vout.scriptPubKey.addresses.includes(refundAddress))
)
return refundSwapTransaction
Expand Down
120 changes: 120 additions & 0 deletions packages/bitcoin-swap-provider/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions packages/client/lib/Swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ export default class Swap {
return this.client.getMethod('findClaimSwapTransaction')(initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock)
}

/**
* Refund the swap
* @param {!string} initiationTxHash - The transaction hash of the swap initiation.
* @param {!string} recipientAddress - Recepient address for the swap in hex.
* @param {!string} refundAddress - Refund address for the swap in hex.
* @param {!string} secretHash - Secret hash for the swap in hex.
* @param {!number} expiration - Expiration time for the swap.
* @param {number} [startBlock] - The block number to start finding from (Optional)
* @return {Promise<string, TypeError>} Resolves with refund swap transaction hash.
* Rejects with InvalidProviderResponseError if provider's response is invalid.
*/
async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock = null) {
if (!(/^[A-Fa-f0-9]+$/.test(initiationTxHash))) {
throw new TypeError('Initiation transaction hash should be a valid hex string')
}

return this.client.getMethod('findRefundSwapTransaction')(initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock)
}

/**
* Generate a secret.
* @param {!string} message - Message to be used for generating secret.
Expand Down Expand Up @@ -164,23 +183,4 @@ export default class Swap {

return this.client.getMethod('refundSwap')(initiationTxHash, recipientAddress, refundAddress, secretHash, expiration)
}

/**
* Refund the swap
* @param {!string} initiationTxHash - The transaction hash of the swap initiation.
* @param {!string} recipientAddress - Recepient address for the swap in hex.
* @param {!string} refundAddress - Refund address for the swap in hex.
* @param {!string} secretHash - Secret hash for the swap in hex.
* @param {!number} expiration - Expiration time for the swap.
* @param {!number} startBlock - The minimum block to start the search from
* @return {Promise<string, TypeError>} Resolves with refund swap transaction hash.
* Rejects with InvalidProviderResponseError if provider's response is invalid.
*/
async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
if (!(/^[A-Fa-f0-9]+$/.test(initiationTxHash))) {
throw new TypeError('Initiation transaction hash should be a valid hex string')
}

return this.client.getMethod('findRefundSwapTransaction')(initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock)
}
}
34 changes: 31 additions & 3 deletions packages/ethereum-swap-provider/lib/EthereumSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,28 @@ export default class EthereumSwapProvider extends Provider {
return transactionMatchesSwapParams && initiationTransactionReceipt.status === '1'
}

async findSwapTransaction (value, recipientAddress, refundAddress, secretHash, expiration, startBlock, predicate) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
let swapTransaction = false

while (!swapTransaction) {
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)

if (block) {
swapTransaction = block.transactions.find(predicate)
blockNumber++
}

await sleep(5000)
}

return swapTransaction
}

async findInitiateSwapTransaction (value, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
let initiateSwapTransaction = false
let arrivedAtTip = false

while (!initiateSwapTransaction) {
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)
Expand All @@ -135,9 +154,11 @@ export default class EthereumSwapProvider extends Provider {
)

blockNumber++
} else {
arrivedAtTip = true
}

await sleep(5000)
if (arrivedAtTip) { await sleep(5000) }
}

return initiateSwapTransaction
Expand All @@ -146,6 +167,7 @@ export default class EthereumSwapProvider extends Provider {
async findClaimSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
let claimSwapTransaction = false
let arrivedAtTip = false
while (!claimSwapTransaction) {
const initiationTransaction = await this.getMethod('getTransactionReceipt')(initiationTxHash)
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)
Expand All @@ -161,9 +183,11 @@ export default class EthereumSwapProvider extends Provider {
}

blockNumber++
} else {
arrivedAtTip = true
}

await sleep(5000)
if (arrivedAtTip) { await sleep(5000) }
}
claimSwapTransaction.secret = await this.getSwapSecret(claimSwapTransaction.hash)

Expand All @@ -179,6 +203,7 @@ export default class EthereumSwapProvider extends Provider {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
const initiationTransaction = await this.getMethod('getTransactionReceipt')(initiationTxHash)
let refundSwapTransaction = false
let arrivedAtTip = false

while (!refundSwapTransaction) {
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)
Expand All @@ -190,8 +215,11 @@ export default class EthereumSwapProvider extends Provider {
)

blockNumber++
} else {
arrivedAtTip = true
}
await sleep(5000)

if (arrivedAtTip) { await sleep(5000) }
}
return refundSwapTransaction
}
Expand Down
Loading

0 comments on commit 028e780

Please sign in to comment.