Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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) {
arrivedAtTip = true
}
if (block) {
swapTransaction = block.transactions.find(predicate)
blockNumber++
}
if (arrivedAtTip) {
await sleep(5000)
}
}
return swapTransaction
}
this.doesTransactionMatchSwapParams(
transaction,
value,
recipientAddress,
refundAddress,
secretHash,
expiration
)
)
blockNumber++
} else {
arrivedAtTip = true
}
if (arrivedAtTip) { await sleep(5000) }
}
return initiateSwapTransaction
}
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 findSwapTransaction (recipientAddress, refundAddress, secretHash, expiration, predicate) {
let blockNumber = await this.getMethod('getBlockHeight')()
let swapTransaction = null
while (!swapTransaction) {
let block
try {
block = await this.getMethod('getBlockByNumber')(blockNumber, true)
} catch (e) { }
if (block) {
swapTransaction = block.transactions.find(predicate)
blockNumber++
}
await sleep(5000)
}
return swapTransaction
}
if (block && initiationTransaction) {
const transaction = block.transactions.find(
transaction => transaction.to === initiationTransaction.contractAddress
)
if (transaction) {
const transactionReceipt = await this.getMethod('getTransactionReceipt')(transaction.hash)
if (transactionReceipt.status === '1' && transaction.input !== '') claimSwapTransaction = transaction
}
blockNumber++
} else {
arrivedAtTip = true
}
if (arrivedAtTip) { await sleep(5000) }
}
claimSwapTransaction.secret = await this.getSwapSecret(claimSwapTransaction.hash)
return claimSwapTransaction
}
async initiateSwap (value, recipientAddress, refundAddress, secretHash, expiration) {
const bytecode = this.createSwapScript(recipientAddress, refundAddress, secretHash, expiration)
const txHash = await this.getMethod('sendTransaction')(null, 0, bytecode)
let initiationTransactionReceipt = null
while (initiationTransactionReceipt === null) {
initiationTransactionReceipt = await this.getMethod('getTransactionReceipt')(txHash)
await sleep(5000)
}
await this.getMethod('sendTransaction')(initiationTransactionReceipt.contractAddress, value)
return txHash
}