Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
feat: get fee estimation from middleware (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Feb 21, 2019
1 parent 34776bf commit 63e7240
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/constants/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const COMPLETE_REFUND = 'COMPLETE_REFUND';
export const SET_REFUND_FILE = 'SET_REFUND_FILE';
export const SET_REFUND_TXHASH = 'SET_REFUND_TXHASH';
export const SET_REFUND_DESTINATION = 'SET_REFUND_DESTINATION';
export const SET_REFUND_TRANSACTION = 'SET_REFUND_TRANSACTION';
export const SET_REFUND_TRANSACTION_HASH = 'SET_REFUND_TRANSACTION_HASH';
export const REFUND_REQUEST = 'REFUND_REQUEST';
export const REFUND_RESPONSE = 'REFUND_RESPONSE';
16 changes: 16 additions & 0 deletions src/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios';
import {
boltzApi,
bitcoinNetwork,
litecoinNetwork,
bitcoinExplorer,
Expand Down Expand Up @@ -126,3 +128,17 @@ export const getSampleAddress = symbol => {
export const getSampleInvoice = symbol => {
return symbol === 'BTC' ? bitcoinInvoice : litecoinInvoice;
};

export const getFeeEstimation = callback => {
const url = `${boltzApi}/getfeeestimation`;
return () => {
axios
.get(url)
.then(response => callback(response.data))
.catch(error => {
window.alert(
`Failed to get fee estimations: ${error.response.data.error}`
);
});
};
};
84 changes: 48 additions & 36 deletions src/views/refund/refundActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { ECPair, address, Transaction } from 'bitcoinjs-lib';
import { constructRefundTransaction, detectSwap } from 'boltz-core';
import { boltzApi } from '../../constants';
import * as actionTypes from '../../constants/actions';
import { getHexBuffer, getNetwork } from '../../scripts/utils';
import {
getHexBuffer,
getNetwork,
getFeeEstimation,
} from '../../scripts/utils';

const verifyRefundFile = (fileJSON, keys) => {
const verify = keys.every(key => fileJSON.hasOwnProperty(key));
Expand Down Expand Up @@ -40,11 +44,6 @@ export const setDestinationAddress = address => ({
payload: address,
});

const setRefundTransaction = transaction => ({
type: actionTypes.SET_REFUND_TRANSACTION,
payload: transaction,
});

const setRefundTransactionHash = hash => ({
type: actionTypes.SET_REFUND_TRANSACTION_HASH,
payload: hash,
Expand All @@ -62,6 +61,32 @@ export const refundResponse = (success, response) => ({
},
});

const refundTransaction = (
refundFile,
response,
destinationAddress,
currency,
feeEstimation
) => {
const redeemScript = getHexBuffer(refundFile.redeemScript);
const lockupTransaction = Transaction.fromHex(response.data.transactionHex);

// TODO: make sure the provided lockup transaction hash was correct and show more specific error if not
return constructRefundTransaction(
[
{
redeemScript,
txHash: lockupTransaction.getHash(),
keys: ECPair.fromPrivateKey(getHexBuffer(refundFile.privateKey)),
...detectSwap(redeemScript, lockupTransaction),
},
],
address.toOutputScript(destinationAddress, getNetwork(currency)),
refundFile.timeoutBlockHeight,
feeEstimation[currency]
);
};

export const startRefund = (
refundFile,
transactionHash,
Expand All @@ -79,37 +104,24 @@ export const startRefund = (
transactionHash,
})
.then(response => {
const redeemScript = getHexBuffer(refundFile.redeemScript);
const lockupTransaction = Transaction.fromHex(
response.data.transactionHex
);

// TODO: make sure the provided lockup transaction hash was correct and show more specific error if not
const refundTransaction = constructRefundTransaction(
[
{
redeemScript,
txHash: lockupTransaction.getHash(),
keys: ECPair.fromPrivateKey(getHexBuffer(refundFile.privateKey)),
...detectSwap(redeemScript, lockupTransaction),
},
],
address.toOutputScript(destinationAddress, getNetwork(currency)),
refundFile.timeoutBlockHeight,
1
);

const refundTransactionHex = refundTransaction.toHex();
const refundTransactionHash = refundTransaction.getId();

dispatch(setRefundTransaction(refundTransactionHex));
dispatch(setRefundTransactionHash(refundTransactionHash));

dispatch(
broadcastRefund(currency, refundTransactionHex, () => {
dispatch(refundResponse(true, response.data));

cb();
getFeeEstimation(feeEstimation => {
const transaction = refundTransaction(
refundFile,
response,
destinationAddress,
currency,
feeEstimation
);

dispatch(setRefundTransactionHash(transaction.getId()));
dispatch(
broadcastRefund(currency, transaction.toHex(), () => {
dispatch(refundResponse(true, response.data));

cb();
})
);
})
);
})
Expand Down
6 changes: 0 additions & 6 deletions src/views/refund/refundReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ const reducer = (state = initalState, action) => {
destinationAddress: action.payload,
};

case actionTypes.SET_REFUND_TRANSACTION:
return {
...state,
refundTransaction: action.payload,
};

case actionTypes.SET_REFUND_TRANSACTION_HASH:
return {
...state,
Expand Down
4 changes: 2 additions & 2 deletions src/views/refund/steps/uploadRefundFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ const StyledUploadRefundFile = ({
<FaCheckCircle size={240} className={classes.icon} />
) : (
<DropZone className={classes.dropZone} onFileRead={setRefundFile}>
<p className={classes.info}>Drag the Refund JSON File Here</p>
<p className={classes.info}>Drag the refund JSON file here</p>
<span className={classes.info}>or</span>
<FileUpload text={'Select file'} onFileRead={setRefundFile} />
</DropZone>
)}
<p className={classes.info}>Paste the lockup transaction hash</p>
<p className={classes.info}>Paste the hash of the lockup transaction</p>
<InputArea
height={50}
width={500}
Expand Down
28 changes: 21 additions & 7 deletions src/views/reverse/reverseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { Transaction, ECPair, address } from 'bitcoinjs-lib';
import { detectSwap, constructClaimTransaction } from 'boltz-core';
import { boltzApi } from '../../constants';
import * as actionTypes from '../../constants/actions';
import { toSatoshi, getHexBuffer, getNetwork } from '../../scripts/utils';
import {
toSatoshi,
getHexBuffer,
getNetwork,
getFeeEstimation,
} from '../../scripts/utils';

export const initReverseSwap = state => ({
type: actionTypes.INIT_REVERSE_SWAP,
Expand Down Expand Up @@ -76,7 +81,7 @@ export const startReverseSwap = (swapInfo, nextStage) => {
};
};

const claimTransaction = (swapInfo, response, preimage) => {
const claimTransaction = (swapInfo, response, preimage, feeEstimation) => {
const redeemScript = getHexBuffer(response.redeemScript);
const lockupTransaction = Transaction.fromHex(response.lockupTransaction);

Expand All @@ -91,7 +96,7 @@ const claimTransaction = (swapInfo, response, preimage) => {
},
],
address.toOutputScript(swapInfo.address, getNetwork(swapInfo.quote)),
2,
feeEstimation[swapInfo.quote],
false
);
};
Expand All @@ -106,11 +111,20 @@ const startListening = (dispatch, swapInfo, response, nextStage) => {
dispatch(setReverseSwapStatus('Waiting for invoice to be paid...'));
nextStage();
} else {
const claimTx = claimTransaction(swapInfo, response, data.preimage);
dispatch(
broadcastClaim(swapInfo.quote, claimTx.toHex(), () => {
dispatch(reverseSwapResponse(true, response));
nextStage();
getFeeEstimation(feeEstimation => {
const claimTx = claimTransaction(
swapInfo,
response,
data.preimage,
feeEstimation
);
dispatch(
broadcastClaim(swapInfo.quote, claimTx.toHex(), () => {
dispatch(reverseSwapResponse(true, response));
nextStage();
})
);
})
);
}
Expand Down

0 comments on commit 63e7240

Please sign in to comment.