Refactor Bity (#166)

* Refactor bity config

* fix bug in refactor that would cause destinationKind amount to not load

* use function instead of string replace for building blockexplorers per PR comment
This commit is contained in:
Daniel Ternyak 2017-09-05 15:07:28 -05:00 committed by GitHub
parent de7d4fbaa2
commit 0989424d73
7 changed files with 79 additions and 61 deletions

View File

@ -57,7 +57,7 @@ export function getOrderStatus(orderid: string) {
}
function _getAllRates() {
return fetch(`${bityConfig.bityAPI}/v1/rate2/`)
return fetch(`${bityConfig.bityURL}/v1/rate2/`)
.then(checkHttpStatus)
.then(parseJSON);
}

View File

@ -96,19 +96,13 @@ export class BalanceSidebar extends React.Component {
<ul className="account-info">
{!!blockExplorer &&
<li>
<a
href={blockExplorer.address.replace('[[address]]', address)}
target="_blank"
>
<a href={blockExplorer.address(address)} target="_blank">
{`${network.name} (${blockExplorer.name})`}
</a>
</li>}
{!!tokenExplorer &&
<li>
<a
href={tokenExplorer.address.replace('[[address]]', address)}
target="_blank"
>
<a href={tokenExplorer.address(address)} target="_blank">
{`Tokens (${tokenExplorer.name})`}
</a>
</li>}

View File

@ -1,5 +1,5 @@
import React from 'react';
import bityConfig from 'config/bity';
import { ETHTxExplorer } from 'config/data';
import translate from 'translations';
export type TransactionSucceededProps = {
txHash: string
@ -7,7 +7,7 @@ export type TransactionSucceededProps = {
const TransactionSucceeded = ({ txHash }: TransactionSucceededProps) => {
// const checkTxLink = `https://www.myetherwallet.com?txHash=${txHash}/#check-tx-status`;
const txHashLink = bityConfig.ethExplorer.replace('[[txHash]]', txHash);
const txHashLink = ETHTxExplorer(txHash);
return (
<div>

View File

@ -1,39 +1,52 @@
export default {
serverURL: 'https://bity.myetherapi.com',
bityAPI: 'https://bity.com/api',
ethExplorer: 'https://etherscan.io/tx/[[txHash]]',
btcExplorer: 'https://blockchain.info/tx/[[txHash]]',
import { ETHTxExplorer, BTCTxExplorer } from './data';
type SupportedDestinationKind = 'ETH' | 'BTC' | 'REP';
const serverURL = 'https://bity.myetherapi.com';
const bityURL = 'https://bity.com/api';
const BTCMin = 0.01;
const BTCMax = 3;
// while Bity is supposedly OK with any order that is at least 0.01 BTC Worth, the order will fail if you send 0.01 BTC worth of ETH.
// This is a bad magic number, but will suffice for now
// value = percent higher/lower than 0.01 BTC worth
const buffers = {
ETH: 0.1,
REP: 0.2
};
// rate must be BTC[KIND]
export function kindMin(
BTCKINDRate: number,
kind: SupportedDestinationKind
): number {
const kindMin = BTCKINDRate * BTCMin;
return kindMin + kindMin * buffers[kind];
}
// rate must be BTC[KIND]
export function kindMax(
BTCKINDRate: number,
kind: SupportedDestinationKind
): number {
const kindMax = BTCKINDRate * BTCMax;
return kindMax - kindMax * buffers[kind];
}
const info = {
serverURL,
bityURL,
ETHTxExplorer,
BTCTxExplorer,
BTCMin,
BTCMax,
validStatus: ['RCVE', 'FILL', 'CONF', 'EXEC'],
invalidStatus: ['CANC'],
// while Bity is supposedly OK with any order that is at least 0.01 BTC Worth, the order will fail if you send 0.01 BTC worth of ETH.
// This is a bad magic number, but will suffice for now
ETHBuffer: 0.1, // percent higher/lower than 0.01 BTC worth
REPBuffer: 0.2, // percent higher/lower than 0.01 BTC worth
BTCMin: 0.01,
BTCMax: 3,
ETHMin: function(BTCETHRate: number) {
const ETHMin = BTCETHRate * this.BTCMin;
const ETHMinWithPadding = ETHMin + ETHMin * this.ETHBuffer;
return ETHMinWithPadding;
},
ETHMax: function(BTCETHRate: number) {
const ETHMax = BTCETHRate * this.BTCMax;
const ETHMaxWithPadding = ETHMax - ETHMax * this.ETHBuffer;
return ETHMaxWithPadding;
},
REPMin: function(BTCREPRate: number) {
const REPMin = BTCREPRate * this.BTCMin;
const REPMinWithPadding = REPMin + REPMin * this.REPBuffer;
return REPMinWithPadding;
},
REPMax: function(BTCREPRate: number) {
const REPMax = BTCREPRate * this.BTCMax;
const REPMaxWithPadding = REPMax - REPMax * this.ETHBuffer;
return REPMaxWithPadding;
},
postConfig: {
headers: {
'Content-Type': 'application/json; charset:UTF-8'
}
}
};
export default info;

View File

@ -15,14 +15,25 @@ export const ANNOUNCEMENT_MESSAGE = `
If you're interested in recieving updates about the MyEtherWallet V4 Alpha, you can subscribe via <a href="http://myetherwallet.us16.list-manage.com/subscribe?u=afced8afb6eb2968ba407a144&id=15a7c74eab">mailchimp</a> :)
`;
export const DONATION_ADDRESSES_MAP = {
const etherScan = 'https://etherscan.io';
const blockChainInfo = 'https://blockchain.info';
const ethPlorer = 'https://ethplorer.io';
export const ETHTxExplorer = (txHash: string): string =>
`${etherScan}/tx/${txHash}`;
export const BTCTxExplorer = (txHash: string): string =>
`${blockChainInfo}/tx/${txHash}`;
export const ETHAddressExplorer = (address: string): string =>
`${etherScan}/address/${address}`;
export const ETHTokenExplorer = (address: string): string =>
`${ethPlorer}/address/${address}`;
export const donationAddressMap = {
BTC: '1MEWT2SGbqtz6mPCgFcnea8XmWV5Z4Wc6',
ETH: '0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8',
REP: '0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8'
};
export const donationAddressMap = DONATION_ADDRESSES_MAP;
export const gasPriceDefaults = {
gasPriceMinGwei: 1,
gasPriceMaxGwei: 60
@ -142,12 +153,12 @@ export type NetworkConfig = {
unit: string,
blockExplorer?: {
name: string,
tx: string,
address: string
tx: Function,
address: Function
},
tokenExplorer?: {
name: string,
address: string
address: Function
},
chainId: number,
tokens: Token[],
@ -167,13 +178,13 @@ export const NETWORKS: { [key: string]: NetworkConfig } = {
unit: 'ETH',
chainId: 1,
blockExplorer: {
name: 'https://etherscan.io',
tx: 'https://etherscan.io/tx/[[txHash]]',
address: 'https://etherscan.io/address/[[address]]'
name: etherScan,
tx: ETHTxExplorer,
address: ETHAddressExplorer
},
tokenExplorer: {
name: 'Ethplorer.io',
address: 'https://ethplorer.io/address/[[address]]'
name: ethPlorer,
address: ETHTokenExplorer
},
tokens: require('./tokens/eth').default,
contracts: require('./contracts/eth.json')

View File

@ -11,7 +11,7 @@ import type {
DestinationAmountSwapAction,
ChangeStepSwapAction
} from 'actions/swapTypes';
import bityConfig from 'config/bity';
import bityConfig, { kindMin, kindMax } from 'config/bity';
import { toFixedIfLarger } from 'utils/formatters';
export type StateProps = {
@ -46,8 +46,8 @@ export default class CurrencySwap extends Component {
let bityMax;
if (kind !== 'BTC') {
const bityPairRate = this.props.bityRates['BTC' + kind];
bityMin = bityConfig[kind + 'Min'](bityPairRate);
bityMax = bityConfig[kind + 'Max'](bityPairRate);
bityMin = kindMin(bityPairRate, kind);
bityMax = kindMax(bityPairRate, kind);
} else {
bityMin = bityConfig.BTCMin;
bityMax = bityConfig.BTCMax;
@ -73,9 +73,9 @@ export default class CurrencySwap extends Component {
if (disabled && originAmount && !this.state.showedMinMaxError) {
const { bityRates } = this.props;
const ETHMin = bityConfig.ETHMin(bityRates.BTCETH);
const ETHMax = bityConfig.ETHMax(bityRates.BTCETH);
const REPMin = bityConfig.REPMax(bityRates.BTCREP);
const ETHMin = kindMin(bityRates.BTCETH, 'ETH');
const ETHMax = kindMax(bityRates.BTCETH, 'ETH');
const REPMin = kindMin(bityRates.BTCREP, 'REP');
const notificationMessage = `
Minimum amount ${bityConfig.BTCMin} BTC,

View File

@ -44,7 +44,7 @@ export default class SwapProgress extends Component {
const notificationMessage = translate('SUCCESS_3', true) + outputTx;
// everything but BTC is a token
if (destinationKind !== 'BTC') {
link = bityConfig.ethExplorer.replace('[[txHash]]', outputTx);
link = bityConfig.ETHTxExplorer(outputTx);
linkElement = (
<a href={link} target="_blank" rel="noopener">
${notificationMessage}
@ -52,7 +52,7 @@ export default class SwapProgress extends Component {
);
// BTC uses a different explorer
} else {
link = bityConfig.btcExplorer.replace('[[txHash]]', outputTx);
link = bityConfig.BTCTxExplorer(outputTx);
linkElement = (
<a href={link} target="_blank" rel="noopener">
${notificationMessage}