Skip to content

Commit

Permalink
calc correct avg price for add liquidity (#10527)
Browse files Browse the repository at this point in the history
* calc correct avg price for add liquidity

* add isWinning

* change to isWinner
  • Loading branch information
bthaile authored Feb 3, 2021
1 parent f7e92e8 commit 73017ab
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/augur-simplified/src/modules/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const MILLION = THOUSAND.times(THOUSAND);
export const BILLION = MILLION.times(THOUSAND);
export const TRILLION = BILLION.times(THOUSAND);
export const SEC_IN_YEAR = createBigNumber(86400).times(createBigNumber(365));
export const NUM_TICKS_Y_N = 1000;

// # Asset Types
export const ETH = 'ETH';
Expand Down
2 changes: 2 additions & 0 deletions packages/augur-simplified/src/modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export interface MarketOutcome {
isFinalNumerator?: boolean;
payoutNumerator?: string;
name: string;
isInvalid: boolean;
isWinner: boolean;
}

export interface AmmOutcome {
Expand Down
11 changes: 5 additions & 6 deletions packages/augur-simplified/src/utils/contract-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { TransactionResponse, Web3Provider } from '@ethersproject/providers'
import { convertDisplayCashAmountToOnChainCashAmount, convertDisplayShareAmountToOnChainShareAmount, convertOnChainCashAmountToDisplayCashAmount, convertOnChainSharesToDisplayShareAmount, isSameAddress } from './format-number';
import { augurSdkLite } from './augurlitesdk';
import { ETH, NO_OUTCOME_ID, NULL_ADDRESS, USDC, YES_NO_OUTCOMES_NAMES, YES_OUTCOME_ID, INVALID_OUTCOME_ID, MARKET_STATUS } from '../modules/constants';
import { ETH, NO_OUTCOME_ID, NULL_ADDRESS, USDC, YES_NO_OUTCOMES_NAMES, YES_OUTCOME_ID, INVALID_OUTCOME_ID, MARKET_STATUS, NUM_TICKS_Y_N } from '../modules/constants';
import { getProviderOrSigner } from '../modules/ConnectAccount/utils';
import { createBigNumber } from './create-big-number';

Expand Down Expand Up @@ -878,7 +878,7 @@ const getInitPositionValues = (trades: UserTrades, amm: AmmExchange, isYesOutcom
const netShareAmounts = allInputShareAmounts.minus(sharesExited.shares);
const netCashAmounts = allInputCashAmounts.minus(sharesExited.cashAmount);
const cost = convertOnChainSharesToDisplayShareAmount(netCashAmounts, amm.cash.decimals).times(new BN(cashPrice));
const avgPrice = netShareAmounts.gt(0) ? new BN(1).minus(netCashAmounts.div(netShareAmounts)) : new BN(0);
const avgPrice = netShareAmounts.gt(0) ? netCashAmounts.div(netShareAmounts) : new BN(0);
const positionFromRemoveLiquidity = hasPositionFromRemoveLiquidity(amm.transactions, account);
return { avgPrice: String(avgPrice), initCostUsd: String(cost), positionFromLiquidity, positionFromRemoveLiquidity }
}
Expand All @@ -897,16 +897,15 @@ const accumLpSharesPrice = (transactions: AmmTransaction[], isYesOutcome: boolea
const result = transactions.filter(t => isSameAddress(t.sender, account) && (t.tx_type === TransactionTypes.ADD_LIQUIDITY || t.tx_type === TransactionTypes.REMOVE_LIQUIDITY)).reduce((p, t) => {
const yesShares = new BN(t.yesShares);
const noShares = new BN(t.noShares);
// TODO, convert cash to share cash, needed to combine with user's positions
const cashValue = new BN(t.cashValue).div(10000);
const cashValue = new BN(t.cash).minus(new BN(t.cashValue)).div(NUM_TICKS_Y_N);
if (isYesOutcome) {
const netYesShares = noShares.minus(yesShares)
if (netYesShares.lte(new BN(0))) return p;
return { shares: p.shares.plus(netYesShares), cashAmount: p.cashAmount.plus(new BN(cashValue)) }
return { shares: p.shares.plus(t.netShares), cashAmount: p.cashAmount.plus(new BN(cashValue)) }
}
const netNoShares = yesShares.minus(noShares)
if (netNoShares.lte(new BN(0))) return p;
return { shares: p.shares.plus(netNoShares), cashAmount: p.cashAmount.plus(new BN(cashValue)) }
return { shares: p.shares.plus(t.netShares), cashAmount: p.cashAmount.plus(new BN(cashValue)) }
},
{ shares: new BN(0), cashAmount: new BN(0) });

Expand Down
1 change: 1 addition & 0 deletions packages/augur-simplified/src/utils/process-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ const shapeOutcomes = (graphOutcomes: GraphMarketOutcome[]): MarketOutcome[] =>
payoutNumerator: g.payoutNumerator,
name: g.value,
isInvalid: g.id.indexOf('-0') > -1,
isWinner: Boolean(Number(g.payoutNumerator)),
}));

const shapeAmmExchange = (
Expand Down

0 comments on commit 73017ab

Please sign in to comment.