Skip to content

Commit

Permalink
Add FPMM Liquidity entity
Browse files Browse the repository at this point in the history
Add a new `FPMMLiquidity` entity with the record of every
`FPMMFundingAdded` and `FPMMFundingRemoved` events from all
FixedProductMarketMakert contracts.

Add a new `fpmm.calculateLiquidityParameter` method to calculate with the
`nthRoot` the collateral liquidity parameter.

Resolves: protofire#71
  • Loading branch information
davidalbela committed Oct 20, 2020
1 parent 48c4675 commit 3ce9930
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
17 changes: 17 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ enum TradeType {
Sell
}

enum LiquidityType {
Add
Remove
}

type FpmmTrade @entity {
id: ID!
fpmm: FixedProductMarketMaker!
Expand All @@ -205,6 +210,18 @@ type FpmmTrade @entity {
outcomeTokensTraded: BigInt!
}

type FpmmLiquidity @entity {
id: ID!
fpmm: FixedProductMarketMaker!
type: LiquidityType!
outcomeTokenAmounts: [BigInt!]
collateralTokenAmounts: BigInt!
funder: Account!
sharesAmount: BigInt!
collateralRemovedFromFeePool: BigInt
creationTimestamp: BigInt!
}

type Account @entity {
id: ID!
tradeNonce: BigInt!
Expand Down
52 changes: 51 additions & 1 deletion src/FixedProductMarketMakerMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FpmmParticipation,
Global,
FpmmTrade,
FpmmLiquidity,
} from "../generated/schema"
import {
FPMMFundingAdded,
Expand All @@ -17,12 +18,14 @@ import {
} from "../generated/templates/FixedProductMarketMaker/FixedProductMarketMaker"
import { secondsPerHour, hoursPerDay, zero, zeroDec } from './utils/constants';
import { joinDayAndVolume } from './utils/day-volume';
import { updateScaledVolumes, setLiquidity } from './utils/fpmm';
import { updateScaledVolumes, calculateLiquidityParameter, setLiquidity } from './utils/fpmm';
import { requireToken } from './utils/token';
import { requireGlobal } from './utils/global';

const TRADE_TYPE_BUY = "Buy";
const TRADE_TYPE_SELL = "Sell";
const LIQUIDITY_TYPE_ADD = "Add";
const LIQUIDITY_TYPE_REMOVE = "Remove";

function requireAccount(accountAddress: string): Account | null {
let account = Account.load(accountAddress);
Expand Down Expand Up @@ -65,6 +68,37 @@ function recordTrade(fpmm: FixedProductMarketMaker,

}

function recordFPMMLiquidity(fpmm: FixedProductMarketMaker,
liquidityType: string,
outcomeTokenAmounts: BigInt[],
funder: string,
sharesAmount: BigInt,
collateralRemovedFromFeePool: BigInt,
creationTimestamp: BigInt): void {
let account = requireAccount(funder);
account.tradeNonce = account.tradeNonce.plus(BigInt.fromI32(1));
account.save();

let fpmmLiquidityId = fpmm.id.concat(funder).concat(account.tradeNonce.toHexString());
let fpmmLiquidity = FpmmLiquidity.load(fpmmLiquidityId);
if (fpmmLiquidity == null) {
fpmmLiquidity = new FpmmLiquidity(fpmmLiquidityId);
fpmmLiquidity.fpmm = fpmm.id;
fpmmLiquidity.type = liquidityType;
fpmmLiquidity.funder = funder;
fpmmLiquidity.creationTimestamp = creationTimestamp;

fpmmLiquidity.outcomeTokenAmounts = outcomeTokenAmounts;
fpmmLiquidity.collateralTokenAmounts = calculateLiquidityParameter(outcomeTokenAmounts);

fpmmLiquidity.sharesAmount = sharesAmount;
fpmmLiquidity.collateralRemovedFromFeePool = collateralRemovedFromFeePool;

fpmmLiquidity.save();
}

}

function recordParticipation(fpmm: FixedProductMarketMaker, participantAddress: string): void {
requireAccount(participantAddress);

Expand Down Expand Up @@ -214,6 +248,14 @@ export function handleFundingAdded(event: FPMMFundingAdded): void {
setLiquidity(fpmm as FixedProductMarketMaker, newAmounts, collateralScaleDec, collateralUSDPrice)

fpmm.save();

recordFPMMLiquidity(fpmm as FixedProductMarketMaker,
LIQUIDITY_TYPE_ADD,
event.params.amountsAdded,
event.params.funder.toHexString(),
event.params.sharesMinted,
new BigInt(0),
event.block.timestamp);
}

export function handleFundingRemoved(event: FPMMFundingRemoved): void {
Expand Down Expand Up @@ -243,6 +285,14 @@ export function handleFundingRemoved(event: FPMMFundingRemoved): void {
setLiquidity(fpmm as FixedProductMarketMaker, newAmounts, collateralScaleDec, collateralUSDPrice);

fpmm.save();

recordFPMMLiquidity(fpmm as FixedProductMarketMaker,
LIQUIDITY_TYPE_REMOVE,
event.params.amountsRemoved,
event.params.funder.toHexString(),
event.params.sharesBurnt,
event.params.collateralRemovedFromFeePool,
event.block.timestamp);
}

export function handleBuy(event: FPMMBuy): void {
Expand Down
14 changes: 9 additions & 5 deletions src/utils/fpmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ export function updateScaledVolumes(
);
}

export function calculateLiquidityParameter(outcomeTokenAmounts: BigInt[]): BigInt {
let amountsProduct = one;
for(let i = 0; i < outcomeTokenAmounts.length; i++) {
amountsProduct = amountsProduct.times(outcomeTokenAmounts[i]);
}
return nthRoot(amountsProduct, outcomeTokenAmounts.length);
}

export function setLiquidity(
fpmm: FixedProductMarketMaker,
outcomeTokenAmounts: BigInt[],
Expand All @@ -174,11 +182,7 @@ export function setLiquidity(
): void {
fpmm.outcomeTokenAmounts = outcomeTokenAmounts;

let amountsProduct = one;
for(let i = 0; i < outcomeTokenAmounts.length; i++) {
amountsProduct = amountsProduct.times(outcomeTokenAmounts[i]);
}
let liquidityParameter = nthRoot(amountsProduct, outcomeTokenAmounts.length);
let liquidityParameter = calculateLiquidityParameter(outcomeTokenAmounts);
fpmm.liquidityParameter = liquidityParameter;
let scaledLiquidityParameter = liquidityParameter.divDecimal(collateralScaleDec);
fpmm.scaledLiquidityParameter = scaledLiquidityParameter;
Expand Down

0 comments on commit 3ce9930

Please sign in to comment.