Skip to content

Commit

Permalink
Merge 4b3e0cd into be6f0a0
Browse files Browse the repository at this point in the history
  • Loading branch information
grandsmarquis committed Jun 5, 2019
2 parents be6f0a0 + 4b3e0cd commit eaaa4f9
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 25 deletions.
47 changes: 34 additions & 13 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default class BitcoinBitcoinJsLibSwapProvider extends Provider {

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

return {
Expand All @@ -187,6 +187,13 @@ export default class BitcoinBitcoinJsLibSwapProvider extends Provider {
}
}

async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
const refundSwapTransaction = await this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration,
tx => tx._raw.vout.find(vout => vout.scriptPubKey.addresses.includes(refundAddress))
)
return refundSwapTransaction
}

async getSwapSecret (claimTxHash) {
const claimTx = await this.getMethod('getTransactionByHash')(claimTxHash)
const script = Buffer.from(claimTx._raw.vin[0].scriptSig.hex, 'hex')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export default class BitcoinBitcoreRpcProvider extends BitcoinRpcProvider {
const address = await this.getNewAddress()
addresses.push(address)
}

return addresses
}

Expand Down
4 changes: 1 addition & 3 deletions packages/bitcoin-rpc-provider/lib/BitcoinRpcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ export default class BitcoinRpcProvider extends JsonRpcProvider {

if (!newAddress) return null

return new Address({
address: newAddress
})
return new Address(newAddress)
}

async getUnusedAddress () {
Expand Down
9 changes: 8 additions & 1 deletion packages/bitcoin-swap-provider/lib/BitcoinSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default class BitcoinSwapProvider extends Provider {
refundAddress,
secretHash,
expiration,
tx => tx._raw.vin.find(vin => vin.txid === initiationTxHash)
tx => tx._raw.vout.find(vout => vout.scriptPubKey.addresses.includes(recipientAddress))
)

return {
Expand All @@ -208,6 +208,13 @@ export default class BitcoinSwapProvider extends Provider {
}
}

async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration) {
const refundSwapTransaction = await this.findSwapTransaction(recipientAddress, refundAddress, secretHash, expiration,
tx => tx._raw.vout.find(vout => vout.scriptPubKey.addresses.includes(refundAddress))
)
return refundSwapTransaction
}

async getSwapSecret (claimTxHash) {
const claimTx = await this.getMethod('getTransactionByHash')(claimTxHash)
const script = Buffer.from(claimTx._raw.vin[0].scriptSig.hex, 'hex')
Expand Down
19 changes: 19 additions & 0 deletions packages/client/lib/Swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,23 @@ 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ export default class EthereumErc20SwapProvider extends Provider {
const claimTransaction = await this.getMethod('getTransactionByHash')(claimTxHash)
return claimTransaction.input.substring(8)
}

async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
const initiationTransaction = await this.getMethod('getTransactionReceipt')(initiationTxHash)
let refundSwapTransaction = false

while (!refundSwapTransaction) {
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)
if (block) {
refundSwapTransaction = block.transactions.find(transaction =>
transaction.to === initiationTransaction.contractAddress &&
transaction.input === ensureAddressStandardFormat(SOL_REFUND_FUNCTION) &&
block.timestamp >= expiration
)
blockNumber++
}
await sleep(5000)
}
return refundSwapTransaction
}
}

EthereumErc20SwapProvider.version = version
1 change: 1 addition & 0 deletions packages/ethereum-rpc-provider/lib/EthereumRpcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class EthereumRpcProvider extends JsonRpcProvider {

const tx = {
from: ensure0x(addressToString(from)),
to: to ? ensure0x(addressToString(from)) : null,
value: ensure0x(BigNumber(value).toString(16))
}

Expand Down
25 changes: 22 additions & 3 deletions packages/ethereum-swap-provider/lib/EthereumSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ export default class EthereumSwapProvider extends Provider {
async findClaimSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
let claimSwapTransaction = false

while (!claimSwapTransaction) {
const initiationTransaction = await this.getMethod('getTransactionReceipt')(initiationTxHash)
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)
Expand All @@ -158,15 +157,14 @@ export default class EthereumSwapProvider extends Provider {

if (transaction) {
const transactionReceipt = await this.getMethod('getTransactionReceipt')(transaction.hash)
if (transactionReceipt.status === '1') claimSwapTransaction = transaction
if (transactionReceipt.status === '1' && transaction.input !== '') claimSwapTransaction = transaction
}

blockNumber++
}

await sleep(5000)
}

claimSwapTransaction.secret = await this.getSwapSecret(claimSwapTransaction.hash)

return claimSwapTransaction
Expand All @@ -176,6 +174,27 @@ export default class EthereumSwapProvider extends Provider {
const claimTransaction = await this.getMethod('getTransactionByHash')(claimTxHash)
return claimTransaction.input
}

async findRefundSwapTransaction (initiationTxHash, recipientAddress, refundAddress, secretHash, expiration, startBlock) {
let blockNumber = startBlock || await this.getMethod('getBlockHeight')()
const initiationTransaction = await this.getMethod('getTransactionReceipt')(initiationTxHash)
let refundSwapTransaction = false

while (!refundSwapTransaction) {
const block = await this.getMethod('getBlockByNumber')(blockNumber, true)
if (block) {
refundSwapTransaction = block.transactions.find(transaction =>
transaction.to === initiationTransaction.contractAddress &&
transaction.input === '' &&
block.timestamp >= expiration
)

blockNumber++
}
await sleep(5000)
}
return refundSwapTransaction
}
}

EthereumSwapProvider.version = version
2 changes: 1 addition & 1 deletion packages/utils/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Address {

function addressToString (any) {
if (typeof any === 'string') return any

return String(new Address(any))
}

Expand Down
7 changes: 7 additions & 0 deletions test/integration/swap/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ async function refund (chain, initiationTxId, secretHash, swapParams) {
return refundTxId
}

async function waitRefund(chain, initiationTxId, secretHash, swapParams) {
const refundTxId = await chain.client.swap.findRefundSwapTransaction(initiationTxId, swapParams.recipientAddress, swapParams.refundAddress, secretHash, swapParams.expiration)
console.log(`${chain.id} Refunded ${refundTxId}`)
return refundTxId
}

async function expectBalance (chain, address, func, comparison) {
const balanceBefore = await chain.client.chain.getBalance([address])
await func()
Expand Down Expand Up @@ -183,6 +189,7 @@ export {
initiateAndVerify,
claimAndVerify,
refund,
waitRefund,
getSwapParams,
expectBalance,
sleep,
Expand Down
Loading

0 comments on commit eaaa4f9

Please sign in to comment.