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
1 change: 1 addition & 0 deletions src/languageProvider/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"leaderboard.biggestWinners": "Biggest Winners",
"leaderboard.mostNBOT": "Who bet the most NBOT",
"leaderboard.ranking": "Ranking",
"leaderboard.returnRatio": "Return Ratio",
"leaderboard.title": "Leaderboard",
"leaderboard.totalBets": "Total bets in NBOT",
"leaderboard.totalEvents": "Total Events",
Expand Down
1 change: 1 addition & 0 deletions src/languageProvider/locales/ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"leaderboard.biggestWinners": "가장 큰 승자",
"leaderboard.mostNBOT": "누가 가장 BOT를 걸 었는지",
"leaderboard.ranking": "순위",
"leaderboard.returnRatio": "도박의 신",
"leaderboard.title": "리더 보드",
"leaderboard.totalBets": "전체 베팅NBOT",
"leaderboard.totalEvents": "총 이벤트",
Expand Down
1 change: 1 addition & 0 deletions src/languageProvider/locales/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"leaderboard.biggestWinners": "大赢家",
"leaderboard.mostNBOT": "投注最多NBOT",
"leaderboard.ranking": "排名",
"leaderboard.returnRatio": "赌神榜",
"leaderboard.title": "排行榜",
"leaderboard.totalBets": "总NBOT投注数",
"leaderboard.totalEvents": "总事件数",
Expand Down
35 changes: 17 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,21 @@ 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) {
return new GraphQuery(client, QUERY_GLOBAL_LEADERBOARD_ENTRIES, args).execute();
}
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} }
`;
41 changes: 31 additions & 10 deletions src/scenes/Event/Leaderboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ const messages = defineMessages({
id: 'leaderboard.biggestWinners',
defaultMessage: 'Biggest Winners',
},
returnRatio: {
id: 'leaderboard.returnRatio',
defaultMessage: 'Return Ratio',
},
strYou: {
id: 'str.you',
defaultMessage: 'You',
},
});
const tabs = [messages.mostNBOT, messages.biggestWinner];

@withStyles(styles, { withTheme: true })
@injectIntl
Expand Down Expand Up @@ -59,13 +62,17 @@ 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 }, ui: { location } } } = this.props;
const { userAddress, investments, winnings, returnRatio } = row;
if (!userAddress) return;
let amount;
if (activeStep === 0) amount = toFixed(satoshiToDecimal(investments), true);
else if (location === Routes.LEADERBOARD) amount = `${(returnRatio * 100).toFixed(2)}%`;
else amount = toFixed(satoshiToDecimal(winnings), true);
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 All @@ -78,18 +85,32 @@ export default class Leaderboard extends React.Component {
</Grid>
<Grid item xs={3}>
<Typography>
{toFixed(satoshiToDecimal(amount), true)}
{amount}
</Typography>
</Grid>
</Grid>
);
}

getLeaderboardTitle = (intl, location, activeStep) => {
if (activeStep === 0) return intl.formatMessage(messages.mostNBOT);
else if (location === Routes.LEADERBOARD) return intl.formatMessage(messages.returnRatio);
return intl.formatMessage(messages.biggestWinner);
}

getLeaderboardLoadMore = (location, activeStep) => {
const { store: { leaderboard: { loadMoreLeaderboardBets, loadMoreLeaderboardBiggestWinners, loadMoreLeaderboardReturnRatio } } } = this.props;
if (activeStep === 0) return loadMoreLeaderboardBets;
else if (location === Routes.LEADERBOARD) return loadMoreLeaderboardReturnRatio;
return loadMoreLeaderboardBiggestWinners;
}

render() {
const { classes, theme, intl, maxSteps, store: { eventPage: { event }, ui: { location },
leaderboard: { leaderboardDisplay, activeStep, loadMoreLeaderboardBets, loadMoreLeaderboardBiggestWinners, loadingMore, leaderboardLimit, diaplayHasMore } } } = this.props;
leaderboard: { leaderboardDisplay, activeStep, loadingMore, leaderboardLimit, diaplayHasMore } } } = this.props;
const url = event ? `/event_leaderboard/${event.address}` : undefined;

const leaderboardTitle = this.getLeaderboardTitle(intl, location, activeStep);
const leaderboardLoadMore = this.getLeaderboardLoadMore(location, activeStep);
const displays = leaderboardDisplay.map((row, index) => this.renderEntry(row, index));
return (
<Card>
Expand All @@ -104,7 +125,7 @@ export default class Leaderboard extends React.Component {
position="static"
activeStep={activeStep}
className={classes.mobileStepper}
currentValue={intl.formatMessage(tabs[activeStep])}
currentValue={leaderboardTitle}
nextButton={
<Button size="small" onClick={this.handleNext} disabled={activeStep === maxSteps - 1}>
{theme.direction === 'rtl' ? <KeyboardArrowLeft /> : <KeyboardArrowRight />}
Expand All @@ -121,7 +142,7 @@ export default class Leaderboard extends React.Component {
<InfiniteScroll
spacing={0}
data={displays}
loadMore={activeStep === 0 ? loadMoreLeaderboardBets : loadMoreLeaderboardBiggestWinners}
loadMore={leaderboardLoadMore}
loadingMore={loadingMore}
noEmptyPlaceholder
/>
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
2 changes: 1 addition & 1 deletion src/scenes/Leaderboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class Leaderboard extends Component {
<Card title={messages.totalBets} value={totalBets} />
</SidebarContainer>
<ContentContainer>
<_Leaderboard maxSteps={1} />
<_Leaderboard maxSteps={2} />
</ContentContainer>
</PageContainer>
);
Expand Down
Loading