Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/pick coins earlier #775

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 27 additions & 90 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import serialize from 'Utils/serialize';
import preparePairs from 'Utils/preparePairs';
import i18n from 'Src/i18n';
import generateDepth from '../utils/generateDepth';
import pickedPair from './pickPair';

export const errorAlert = payload => ({
type: types.ERROR_ALERT,
Expand Down Expand Up @@ -275,19 +276,37 @@ export const fetchPrice = payload => dispatch => {
});
};

export const fetchPairs = ({ base, quote } = {}) => dispatch => {
export const fetchPairs = ({ base, quote } = {}) => async dispatch => {
const url = `${config.API_BASE_URL}/pair/`;
const request = axios.get(url);

let params = urlParams();
const pathNameParams = window.location.pathname.split('/');
// Checks if pathname section of url has params.
if (pathNameParams[1] === 'pair') {
params = {};
params.pair = pathNameParams[2].toUpperCase();
}

let { base: deposit, quote: receive } = await pickedPair({ base, quote }, params);
dispatch(
selectCoin({
deposit,
receive,
prev: {
deposit,
receive,
},
lastSelected: 'deposit',
selectedByUser: {
deposit: false,
receive: false,
},
})
);

return request
.then(async response => {
let params = urlParams();
const pathNameParams = window.location.pathname.split('/');
// Checks if pathname section of url has params.
if (pathNameParams[1] === 'pair') {
params = {};
params.pair = pathNameParams[2].toUpperCase();
}
const pairs = response.data.filter(pair => {
if (params && params.hasOwnProperty('test')) {
return !pair.disabled;
Expand All @@ -301,88 +320,6 @@ export const fetchPairs = ({ base, quote } = {}) => dispatch => {
type: types.PAIRS_FETCHED,
payload: processedPairs,
});

let depositCoin = base;
let receiveCoin = quote;

const loadPair = pair => {
const url = `${config.API_BASE_URL}/pair/${pair.toUpperCase()}/`;
return new Promise((resolve, reject) => {
axios
.get(url)
.then(res => resolve(res.data))
.catch(err => {
resolve(pickRandomPair());
});
});
};

const pickMostTraded = () => {
return new Promise((resolve, reject) => {
axios
.get(`${config.API_BASE_URL}/pair/most_traded/`)
.then(res => resolve(res.data))
.catch(err => resolve(null));
});
};

const pickRandomPair = async () => {
const pair = pairs[Math.floor(Math.random() * pairs.length)];
depositCoin = pair.quote;
receiveCoin = pair.base;
};

// Picks random deposit and receive coins.
const pickCoins = async () => {
// Checks if url has params. If yes then update accordingly and if no then pick random coins.
if (base && quote) {
try {
const pair = await loadPair(`${base}${quote}`);
if (pair) {
depositCoin = pair.quote;
receiveCoin = pair.base;
}
} catch (err) {
/* istanbul ignore next */
console.log('Error:', err);
}
} else if (params && params.hasOwnProperty('pair')) {
try {
const pair = await loadPair(params.pair);
if (pair) {
depositCoin = pair.quote;
receiveCoin = pair.base;
}
} catch (err) {
/* istanbul ignore next */
console.log('Error:', err);
}
} else {
const pair = await pickMostTraded();
if (pair) {
depositCoin = pair.quote;
receiveCoin = pair.base;
} else {
pickRandomPair();
}
}
};
await pickCoins();
dispatch(
selectCoin({
deposit: depositCoin,
receive: receiveCoin,
prev: {
deposit: depositCoin,
receive: receiveCoin,
},
lastSelected: 'deposit',
selectedByUser: {
deposit: false,
receive: false,
},
})
);
})
.catch(error => {
/* istanbul ignore next */
Expand Down
19 changes: 19 additions & 0 deletions src/actions/pickPair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from 'axios';
import config from 'Config';

// Picks random deposit and receive coins.
export default async ({ base, quote }, params) => {
// Checks if url has params. If yes then update accordingly and if no then pick random coins.
let response;
if (base && quote) {
response = await loadPair(`${base}${quote}`, pairs).catch(console.error);
} else if (params && params.hasOwnProperty('pair')) {
response = await loadPair(params.pair).catch(console.error);
} else {
response = await mostTradedPair().catch(console.error);
}
return response.data;
};

const mostTradedPair = () => axios.get(`${config.API_BASE_URL}/pair/most_traded/`);
const loadPair = pair => axios.get(`${config.API_BASE_URL}/pair/${pair.toUpperCase()}/`);