Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Remove calculatewinnings #1546

Merged
merged 11 commits into from
Jul 11, 2019
36 changes: 18 additions & 18 deletions src/network/graphql/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
SYNC_INFO,
TOTAL_RESULT_BETS,
ALL_STATS,
PAGINATED_MOST_BETS,
PAGINATED_BIGGEST_WINNER,
PAGINATED_LEADERBOARD_ENTRY,
} from './schema';

const QUERY_EVENTS = 'events';
Expand All @@ -33,8 +32,8 @@ const QUERY_TRANSACTIONS = 'transactions';
const QUERY_SYNC_INFO = 'syncInfo';
const QUERY_TOTAL_RESULT_BETS = 'totalResultBets';
const QUERY_ALL_STATS = 'allStats';
const QUERY_MOST_BETS = 'mostBets';
const QUERY_BIGGEST_WINNERS = 'biggestWinners';
const QUERY_EVENT_LEADERBOARD_ENTRIES = 'eventLeaderboardEntries';
const QUERY_GLOBAL_LEADERBOARD_ENTRIES = 'globalLeaderboardEntries';
/**
* Example query arguments:
* - filter: { status: 'BETTING' }
Expand Down Expand Up @@ -215,38 +214,38 @@ const QUERIES = {
}
`,

mostBets: gql`
eventLeaderboardEntries: gql`
query(
$filter: BetFilter
$filter: LeaderboardEntryFilter
$orderBy: [Order!]
$limit: Int
$skip: Int
) {
mostBets(
eventLeaderboardEntries(
filter: $filter
orderBy: $orderBy
limit: $limit
skip: $skip
) {
${PAGINATED_MOST_BETS}
${PAGINATED_LEADERBOARD_ENTRY}
}
}
`,

biggestWinners: gql`
globalLeaderboardEntries: gql`
query(
$filter: BetFilter
$filter: LeaderboardEntryFilter
$orderBy: [Order!]
$limit: Int
$skip: Int
) {
biggestWinners(
globalLeaderboardEntries(
filter: $filter
orderBy: $orderBy
limit: $limit
skip: $skip
) {
${PAGINATED_BIGGEST_WINNER}
${PAGINATED_LEADERBOARD_ENTRY}
}
}
`,
Expand Down Expand Up @@ -404,21 +403,22 @@ export async function allStats(client) {
}

/**
* Queries most bets given the filters.
* Queries the event leaderboard entry based on event address and/or user address.
* @param {ApolloClient} client Apollo Client instance.
* @param {object} args Arguments for the query.
* @return {object} Query result.
*/
export async function mostBets(client, args) {
return new GraphQuery(client, QUERY_MOST_BETS, args).execute();
export async function eventLeaderboardEntries(client, args) {
return new GraphQuery(client, QUERY_EVENT_LEADERBOARD_ENTRIES, args).execute();
}

/**
* Queries the biggest winners based on an event address.
* Queries the global leaderboard entry based on a user address.
* @param {ApolloClient} client Apollo Client instance.
* @param {object} args Arguments for the query.
* @return {object} Query result.
*/
export async function biggestWinners(client, args) {
return new GraphQuery(client, QUERY_BIGGEST_WINNERS, args).execute();
export async function globalLeaderboardEntries(client, args) {
const res = await new GraphQuery(client, QUERY_GLOBAL_LEADERBOARD_ENTRIES, args).execute();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return this. no reason to assign it first unless you were using res.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right.... this was for testing, forgot to change back.

return res;
}
24 changes: 7 additions & 17 deletions src/network/graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,16 @@ export const ALL_STATS = `
totalBets
`;

export const MOST_BET = `
export const LEADERBOARD_ENTRY = `
eventAddress
betterAddress
amount
`;

export const PAGINATED_MOST_BETS = `
totalCount
pageInfo { ${PAGE_INFO} }
items { ${MOST_BET} }
`;

export const BIGGEST_WINNER = `
eventAddress
betterAddress
amount
userAddress
investments
winnings
returnRatio
`;

export const PAGINATED_BIGGEST_WINNER = `
export const PAGINATED_LEADERBOARD_ENTRY = `
totalCount
pageInfo { ${PAGE_INFO} }
items { ${BIGGEST_WINNER} }
items { ${LEADERBOARD_ENTRY} }
`;
9 changes: 5 additions & 4 deletions src/scenes/Event/Leaderboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export default class Leaderboard extends React.Component {
}

renderEntry = (row, index) => {
const { classes, intl, store: { naka: { account } } } = this.props;
const { betterAddress, amount } = row;
if (!betterAddress) return;
const { classes, intl, store: { naka: { account }, leaderboard: { activeStep } } } = this.props;
const { userAddress, investments, winnings } = row;
if (!userAddress) return;
const amount = activeStep === 0 ? investments : winnings;
const ranking = (index <= 2 && <img src={`/images/ic_${index + 1}_cup.svg`} alt='cup' />)
|| (index === 3 && '👍') || (index >= 4 && '✊');

const address = (account && account.toLowerCase() === betterAddress && intl.formatMessage(messages.strYou)) || shortenText(betterAddress, 6);
const address = (account && account.toLowerCase() === userAddress && intl.formatMessage(messages.strYou)) || shortenText(userAddress, 6);
return (
<Grid container className={classes.grid} key={index} alignItems='center'>
<Grid item xs={1}>
Expand Down
17 changes: 6 additions & 11 deletions src/scenes/Event/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
events,
totalResultBets,
withdraws,
eventLeaderboardEntries,
} from '../../network/graphql/queries';
import { maxTransactionFee } from '../../config/app';

Expand Down Expand Up @@ -304,21 +305,15 @@ export default class EventStore {
const address = this.event && this.event.address;
if (!address || !this.app.wallet.currentAddress) return;

// If we have already called calculateWinnings before,
// only call it again every 5 blocks.
if (!isUndefined(this.nbotWinnings) && this.app.global.syncBlockNum % 5 !== 0) {
return;
}

try {
const { data } = await axios.get(API.CALCULATE_WINNINGS, {
params: {
// calcaulateWinnings working event address: 0xe272f0793b97a3606f7a4e1eed2abaded67a9376
const res = await eventLeaderboardEntries(this.app.graphqlClient, {
filter: {
eventAddress: address,
address: this.app.wallet.currentAddress,
userAddress: this.app.wallet.currentAddress,
},
});
this.nbotWinnings = satoshiToDecimal(data.result);
if (res.items.length === 0) return;
this.nbotWinnings = satoshiToDecimal(res.items[0].winnings);
this.returnRate = ((this.nbotWinnings - this.totalInvestment) / this.totalInvestment) * 100;
this.profitOrLoss = this.nbotWinnings - this.totalInvestment;
} catch (error) {
Expand Down
18 changes: 9 additions & 9 deletions src/stores/LeaderboardStore.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { observable, action, reaction, runInAction } from 'mobx';
import { isEmpty } from 'lodash';
import { Routes } from 'constants';
import { mostBets, allStats, biggestWinners } from '../network/graphql/queries';
import { Routes, SortBy } from 'constants';
import { allStats, eventLeaderboardEntries, globalLeaderboardEntries } from '../network/graphql/queries';
import { satoshiToDecimal } from '../helpers/utility';

const EVENT_LEADERBOARD_LIMIT = 10;
Expand Down Expand Up @@ -210,16 +210,16 @@ export default class {

/**
* Gets the leaderboard bets via API call.
* @return {[MostBet]} leaderboard array of the query.
* @return {[LeaderboardEntries]} leaderboard array of the query.
*/
fetchLeaderboardBets = async (filters = {}, limit = this.leaderboardLimit, skip = this.skip) => {
const { graphqlClient } = this.app;

const orderBy = { field: 'investments', direction: SortBy.DESCENDING };
let res;
if (isEmpty(filters)) {
res = await mostBets(graphqlClient, { limit, skip });
res = await globalLeaderboardEntries(graphqlClient, { orderBy, limit, skip });
} else {
res = await mostBets(graphqlClient, { filter: filters, limit, skip });
res = await eventLeaderboardEntries(graphqlClient, { filter: filters, orderBy, limit, skip });
}

const { items, pageInfo } = res;
Expand All @@ -233,14 +233,14 @@ export default class {

/**
* Gets the leaderboard winners via API call.
* @return {[BiggestWinner]} leaderboard array of the query.
* @return {[LeaderboardEntries]} leaderboard array of the query.
*/
fetchLeaderboardWinners = async (filters = {}, limit = this.leaderboardLimit, skip = this.winnerSkip) => {
// Address is required for the request filters
if (!isEmpty(filters)) {
const { graphqlClient } = this.app;

const res = await biggestWinners(graphqlClient, { filter: filters, limit, skip });
const orderBy = { field: 'winnings', direction: SortBy.DESCENDING };
const res = await eventLeaderboardEntries(graphqlClient, { filter: filters, orderBy, limit, skip });

const { items, pageInfo } = res;

Expand Down