Skip to content

Commit

Permalink
Api swap (#35)
Browse files Browse the repository at this point in the history
* Fire success function when wallet other than Badger pays

* Updating price API to match Bitcoin.com products

* Remove legacy test code
  • Loading branch information
Joey authored Mar 10, 2020
1 parent b8c3653 commit a8ce8bd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
42 changes: 21 additions & 21 deletions src/hoc/BadgerBase/BadgerBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,16 @@ const BadgerBase = (Wrapped: React.AbstractComponent<any>) => {
this.setState({ step: 'pending' });
console.info('Badger payInvoice begin', paymentRequestUrl);
payInvoice({ url: paymentRequestUrl })
.then(({ memo }) => {
console.info('Badger send success:', memo);
successFn && successFn(memo);
this.paymentSendSuccess();
})
.catch((err) => {
console.info('Badger send cancel', err);
failFn && failFn(err);
this.setState({ step: 'fresh' });
});
.then(({ memo }) => {
console.info('Badger send success:', memo);
successFn && successFn(memo);
this.paymentSendSuccess();
})
.catch((err) => {
console.info('Badger send cancel', err);
failFn && failFn(err);
this.setState({ step: 'fresh' });
});
return;
}

Expand All @@ -238,20 +238,20 @@ const BadgerBase = (Wrapped: React.AbstractComponent<any>) => {
if (opReturn && opReturn.length) {
sendParams.opReturn = opReturn;
}

this.setState({ step: 'pending' });
console.info('Badger sendAssets begin', sendParams);
sendAssets(sendParams)
.then(({txid}) => {
console.info('Badger send success:', txid);
successFn && successFn(txid);
this.paymentSendSuccess();
})
.catch(err => {
console.info('Badger send cancel', err);
failFn && failFn(err);
this.setState({ step: 'fresh' });
});
.then(({ txid }) => {
console.info('Badger send success:', txid);
successFn && successFn(txid);
this.paymentSendSuccess();
})
.catch((err) => {
console.info('Badger send cancel', err);
failFn && failFn(err);
this.setState({ step: 'fresh' });
});
};

gotoLoginState = () => {
Expand Down
22 changes: 11 additions & 11 deletions src/utils/badger-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BigNumber from 'bignumber.js';
import { currencySymbolMap, type CurrencyCode } from './currency-helpers';

const buildPriceEndpoint = (currency: CurrencyCode) => {
return `https://index-api.bitcoin.com/api/v0/cash/price/${currency}`;
return `https://markets.api.bitcoin.com/rates/convertor?c=BCH&q=${currency}`;
};

const getAddressUnconfirmed = async (address: string): Promise<string[]> => {
Expand Down Expand Up @@ -57,15 +57,15 @@ const formatAmount = (amount: ?number, decimals: ?number): string => {
};

const priceToSatoshis = (BCHRate: number, price: number): number => {
const singleDollarValue = new BigNumber(BCHRate).div(100);
const singleDollarValue = new BigNumber(BCHRate);
const satoshisPerBCH = new BigNumber(100000000);
const singleDollarSatoshis = satoshisPerBCH.div(singleDollarValue);

return +singleDollarSatoshis.times(price).integerValue(BigNumber.ROUND_FLOOR);
};

const priceToFiat = (BCHRate: number, price: number): number => {
const singleDollarValue = new BigNumber(BCHRate).div(100);
const singleDollarValue = new BigNumber(BCHRate);
return +singleDollarValue.times(price);
};

Expand All @@ -75,7 +75,7 @@ const fiatToSatoshis = async (
): Promise<number> => {
const priceRequest = await fetch(buildPriceEndpoint(currency));
const result = await priceRequest.json();
const fiatPrice = result.price;
const fiatPrice = result[currency].rate;
const satoshis = priceToSatoshis(fiatPrice, price);
return satoshis;
};
Expand All @@ -86,20 +86,20 @@ const bchToFiat = async (
): Promise<number> => {
const priceRequest = await fetch(buildPriceEndpoint(currency));
const result = await priceRequest.json();
const fiatPrice = result.price;
const fiatPrice = result[currency].rate;

const fiatInvoiceTotal = priceToFiat(fiatPrice, price);
return fiatInvoiceTotal;
};

const adjustAmount = (amount: ?number, decimals: number, fromSatoshis: ?boolean): ?number => {
const adjustAmount = (
amount: ?number,
decimals: number,
fromSatoshis: ?boolean
): ?number => {
decimals = decimals || 0;
const shiftBy = !fromSatoshis ? decimals : decimals * -1;
return amount
? new BigNumber(amount)
.shiftedBy(shiftBy)
.toString()
: null;
return amount ? new BigNumber(amount).shiftedBy(shiftBy).toString() : null;
};

export {
Expand Down

0 comments on commit a8ce8bd

Please sign in to comment.