Skip to content

Commit

Permalink
Add USD amount in FPMMTrade entity
Browse files Browse the repository at this point in the history
Add a new field `collateralAmountUSD` as BigDecimal type to calculate
the value of `collateralAmount` in USD value at the moment of the trade.

Resolves: protofire#72
  • Loading branch information
davidalbela committed Oct 20, 2020
1 parent 5a76d03 commit 48c4675
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type FpmmTrade @entity {
creator: Account!
creationTimestamp: BigInt!
collateralAmount: BigInt!
collateralAmountUSD: BigDecimal!
feeAmount: BigInt!
outcomeIndex: BigInt!
outcomeTokensTraded: BigInt!
Expand Down
16 changes: 13 additions & 3 deletions src/FixedProductMarketMakerMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function requireAccount(accountAddress: string): Account | null {
}

function recordTrade(fpmm: FixedProductMarketMaker,
traderAddress: string, collateralAmount: BigInt,
traderAddress: string,
collateralAmount: BigInt, collateralAmountUSD: BigDecimal,
feeAmount: BigInt, outcomeIndex: BigInt,
outcomeTokensTraded: BigInt, tradeType: string,
creationTimestamp: BigInt): void {
Expand All @@ -54,6 +55,7 @@ function recordTrade(fpmm: FixedProductMarketMaker,
fpmmTrade.creator = traderAddress;
fpmmTrade.creationTimestamp = creationTimestamp;
fpmmTrade.collateralAmount = collateralAmount;
fpmmTrade.collateralAmountUSD = collateralAmountUSD;
fpmmTrade.feeAmount = feeAmount;
fpmmTrade.outcomeIndex = outcomeIndex;
fpmmTrade.outcomeTokensTraded = outcomeTokensTraded;
Expand Down Expand Up @@ -272,6 +274,9 @@ export function handleBuy(event: FPMMBuy): void {
let collateralUSDPrice = ethPerCollateral != null && usdPerEth != null ?
ethPerCollateral.times(usdPerEth as BigDecimal) :
zeroDec;
let collateralAmountUSD = (collateralUSDPrice != zeroDec) ?
collateralUSDPrice.times(event.params.investmentAmount.divDecimal(collateralScaleDec)) :
zeroDec;

setLiquidity(fpmm as FixedProductMarketMaker, newAmounts, collateralScaleDec, collateralUSDPrice);
increaseVolume(
Expand All @@ -290,7 +295,8 @@ export function handleBuy(event: FPMMBuy): void {
event.params.buyer.toHexString());

recordTrade(fpmm as FixedProductMarketMaker,
event.params.buyer.toHexString(), event.params.investmentAmount,
event.params.buyer.toHexString(),
event.params.investmentAmount, collateralAmountUSD,
event.params.feeAmount, event.params.outcomeIndex,
event.params.outcomeTokensBought, TRADE_TYPE_BUY,
event.block.timestamp);
Expand Down Expand Up @@ -325,6 +331,9 @@ export function handleSell(event: FPMMSell): void {
let collateralUSDPrice = ethPerCollateral != null && usdPerEth != null ?
ethPerCollateral.times(usdPerEth as BigDecimal) :
zeroDec;
let collateralAmountUSD = (collateralUSDPrice != zeroDec) ?
collateralUSDPrice.times(event.params.returnAmount.divDecimal(collateralScaleDec)) :
zeroDec;

setLiquidity(fpmm as FixedProductMarketMaker, newAmounts, collateralScaleDec, collateralUSDPrice);
increaseVolume(
Expand All @@ -343,7 +352,8 @@ export function handleSell(event: FPMMSell): void {
event.params.seller.toHexString());

recordTrade(fpmm as FixedProductMarketMaker,
event.params.seller.toHexString(), event.params.returnAmount,
event.params.seller.toHexString(),
event.params.returnAmount, collateralAmountUSD,
event.params.feeAmount, event.params.outcomeIndex,
event.params.outcomeTokensSold, TRADE_TYPE_SELL,
event.block.timestamp);
Expand Down

0 comments on commit 48c4675

Please sign in to comment.