Skip to content

Commit

Permalink
Merge pull request #9 from TokenUnion/develop
Browse files Browse the repository at this point in the history
Initial version deployed to Graph hosted service
  • Loading branch information
TomAFrench authored Feb 17, 2021
2 parents 7598213 + 3597c55 commit b8c7386
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 176 deletions.
3 changes: 0 additions & 3 deletions networks.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mainnet:
networkName: mainnet
contracts:
ConditionalTokens:
address: '0xC59b0e4De5F1248C1140964E0fF287B192407E0C'
Expand All @@ -10,7 +9,6 @@ mainnet:
transactionHash: '0xc21edc19c4f4249f047cec4cb13052e4ce39446766f723b9a94b3748a433e9b6'
startBlock: 9969340
matic:
networkName: matic
contracts:
ConditionalTokens:
address: '0x4D97DCd97eC945f40cF65F87097ACe5EA0476045'
Expand All @@ -21,7 +19,6 @@ matic:
transactionHash: '0x1af1b88e36bb0289f6e037e00d01b26bed997ce7a0406ebafce07dcf766ce7c6'
startBlock: 4023693
mumbai:
networkName: matic
contracts:
ConditionalTokens:
address: '0x7D8610E9567d2a6C9FBf66a5A13E9Ba8bb120d43'
Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
"create-local": "graph create --node http://localhost:8020/ TokenUnion/polymarket",
"remove-local": "graph remove --node http://localhost:8020/ TokenUnion/polymarket",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 TokenUnion/polymarket",
"deploy:mainnet": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ TokenUnion/polymarket",
"deploy:mainnet:test": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ tomafrench/polymarket",
"deploy:matic": "graph deploy --node http://ec2-35-160-238-58.us-west-2.compute.amazonaws.com:8020 --ipfs https://ipfs.infura.io:5001 TokenUnion/polymarket",
"deploy:mumbai": "graph deploy --node https://mumbai-graph.matic.today --ipfs https://ipfs.infura.io:5001 TokenUnion/polymarket",
"deploy": "graph deploy $SUBGRAPH --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy:matic": "SUBGRAPH=TokenUnion/polymarket-matic yarn deploy",
"deploy:matic-staging": "SUBGRAPH=TokenUnion/polymarket-matic-staging yarn deploy",
"deploy:mumbai": "SUBGRAPH=TokenUnion/polymarket-mumbai yarn deploy",
"codegen": "graph codegen --debug --output-dir src/types/",
"prepare:mainnet": "NETWORK_NAME=mainnet ts-node ./templatify.ts && yarn codegen",
"prepare:matic": "NETWORK_NAME=matic ts-node ./templatify.ts && yarn codegen",
"prepare:mumbai": "NETWORK_NAME=mumbai ts-node ./templatify.ts && yarn codegen",
"publish-graph:mainnet": "yarn prepare:mainnet && yarn deploy:mainnet",
"publish-graph:mainnet:test": "yarn prepare:mainnet && yarn deploy:mainnet:test",
"publish-graph:matic": "yarn prepare:matic && yarn deploy:matic",
"publish-graph:matic-staging": "yarn prepare:matic && yarn deploy:matic-staging",
"publish-graph:mumbai": "yarn prepare:mumbai && yarn deploy:mumbai"
},
"repository": {
Expand Down Expand Up @@ -57,4 +55,4 @@
"dependencies": {
"@graphprotocol/graph-ts": "^0.18.1"
}
}
}
53 changes: 53 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type Global @entity {
type Account @entity {
"User address"
id: ID!
"Timestamp at which account first interacted with Polymarket"
creationTimestamp: BigInt!
"Timestamp at which account most recently interacted with Polymarket"
lastSeenTimestamp: BigInt!
"Total volume of this user's trades in USDC base units"
collateralVolume: BigInt!
"Total volume of this user's trades in USDC scaled by 10^6"
Expand All @@ -51,6 +55,10 @@ type Account @entity {
marketPositions: [MarketPosition!] @derivedFrom(field: "user")
"Purchases and sales of shares by the user"
transactions: [Transaction!] @derivedFrom(field: "user")
"Split of collateral / outcome tokens into multiple positions"
splits: [Split!] @derivedFrom(field: "stakeholder")
"Merge of more specific outcome tokens into collateral / more general outcome tokens"
merges: [Merge!] @derivedFrom(field: "stakeholder")
"Redemption of underlying collateral after a market has resolved"
redemptions: [Redemption!] @derivedFrom(field: "redeemer")
}
Expand All @@ -69,8 +77,11 @@ type Condition @entity {
id: ID!
"Address which can resolve this condition"
oracle: Bytes!
"Question ID which corresponds to this condition"
questionId: Bytes!
"Number of possible outcomes for this condition"
outcomeSlotCount: Int!
"Timestamp at which this condition was resolved"
resolutionTimestamp: BigInt
"Fraction of collateral assigned to each outcome"
payouts: [BigDecimal!]
Expand All @@ -83,37 +94,52 @@ type Condition @entity {
type Split @entity {
"Transaction Hash"
id: ID!
"Timestamp at which split occurred"
timestamp: BigInt!
"Address which is performing this split"
stakeholder: Account!
"Token which is collateralising positions being split"
collateralToken: Collateral!
parentCollectionId: Bytes!
"Condition on which split is occuring"
condition: Condition!
partition: [BigInt!]!
"The amount of collateral/outcome tokens being split"
amount: BigInt!
}

type Merge @entity {
"Transaction Hash"
id: ID!
"Timestamp at which merge occurred"
timestamp: BigInt!
"Address which is performing this merge"
stakeholder: Account!
"Token which is collateralising positions being merged"
collateralToken: Collateral!
parentCollectionId: Bytes!
"Condition on which merge is occuring"
condition: Condition!
partition: [BigInt!]!
"The amount of outcome tokens being merged"
amount: BigInt!
}

type Redemption @entity {
"Transaction Hash"
id: ID!
"Timestamp at which redemption occurred"
timestamp: BigInt!
"Address which is redeeming these outcomes"
redeemer: Account!
"Token which is being claimed in return for outcome tokens"
collateralToken: Collateral!
parentCollectionId: Bytes!
"Condition on which redemption is occuring"
condition: Condition!
"Outcomes which are being redeemed"
indexSets: [BigInt!]!
"The amount of collateral being claimed"
payout: BigInt!
}

Expand All @@ -127,6 +153,8 @@ type FixedProductMarketMaker @entity {
creator: Bytes!
"Time at which this market was deployed"
creationTimestamp: BigInt!
"Hash of deployment transactions"
creationTransactionHash: Bytes!

"Token which is colleralising this market"
collateralToken: Collateral!
Expand Down Expand Up @@ -204,29 +232,42 @@ type MarketPosition @entity {
valueSold: BigInt!
"Total value paid by the user to enter this position"
netValue: BigInt!
"Total amount of fees paid by user in relation to this position"
feesPaid: BigInt!
}

# Market Actions

type FpmmFundingAddition @entity {
"Transaction Hash"
id: ID!
"Timestamp at which funding addition occurred"
timestamp: BigInt!
"FPMM to which funding is being added"
fpmm: FixedProductMarketMaker!
"Account adding funding"
funder: Account!
"Outcome tokens amounts added to FPMM"
amountsAdded: [BigInt!]!
"Outcome tokens amounts refunded to funder"
amountsRefunded: [BigInt!]!
"Liquidity shares minted to funder"
sharesMinted: BigInt!
}

type FpmmFundingRemoval @entity {
"Transaction Hash"
id: ID!
"Timestamp at which funding removal occurred"
timestamp: BigInt!
"FPMM to which funding is being removed"
fpmm: FixedProductMarketMaker!
"Account removing funding"
funder: Account!
"Outcome tokens amounts removed from FPMM"
amountsRemoved: [BigInt!]!
collateralRemoved: BigInt!
"Liquidity shares burned by funder"
sharesBurnt: BigInt!
}

Expand All @@ -238,19 +279,31 @@ enum TradeType {
type Transaction @entity {
"Transaction Hash"
id: ID!
"Buy or Sell transaction"
type: TradeType!
"Timestamp at which transaction occurred"
timestamp: BigInt!
"Market which transaction is interacting with"
market: FixedProductMarketMaker!
"Account performing transaction"
user: Account!
"Amount of collateral in trade"
tradeAmount: BigInt!
"Amount of collateral paid in fees"
feeAmount: BigInt!
"Index of outcome token being bought or sold"
outcomeIndex: BigInt!
"Amount of outcome tokens being bought or sold"
outcomeTokensAmount: BigInt!
}

type FpmmPoolMembership @entity {
"funder address + pool address"
id: ID!
"Market to which funder is providing funding"
pool: FixedProductMarketMaker!
"Account which is providing funding"
funder: Account!
"Amount of liquidity tokens owned by funder"
amount: BigInt!
}
20 changes: 16 additions & 4 deletions src/ConditionalTokensMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { partitionCheck } from './utils/conditional-utils';
import { bigZero } from './utils/constants';
import { getCollateralDetails } from './utils/collateralTokens';
import { requireAccount } from './utils/account-utils';
import { markAccountAsSeen, requireAccount } from './utils/account-utils';

export function handlePositionSplit(event: PositionSplit): void {
if (
Expand All @@ -34,9 +34,14 @@ export function handlePositionSplit(event: PositionSplit): void {
}

getCollateralDetails(event.params.collateralToken);
requireAccount(event.params.stakeholder.toHexString());
requireAccount(event.params.stakeholder.toHexString(), event.block.timestamp);
markAccountAsSeen(
event.params.stakeholder.toHexString(),
event.block.timestamp,
);

let split = new Split(event.transaction.hash.toHexString());
split.timestamp = event.block.timestamp;
split.stakeholder = event.params.stakeholder.toHexString();
split.collateralToken = event.params.collateralToken.toHexString();
split.parentCollectionId = event.params.parentCollectionId;
Expand Down Expand Up @@ -71,9 +76,14 @@ export function handlePositionsMerge(event: PositionsMerge): void {
// We don't track merges within the market makers
return;
}
requireAccount(event.params.stakeholder.toHexString());
requireAccount(event.params.stakeholder.toHexString(), event.block.timestamp);
markAccountAsSeen(
event.params.stakeholder.toHexString(),
event.block.timestamp,
);

let merge = new Merge(event.transaction.hash.toHexString());
merge.timestamp = event.block.timestamp;
merge.stakeholder = event.params.stakeholder.toHexString();
merge.collateralToken = event.params.collateralToken.toHexString();
merge.parentCollectionId = event.params.parentCollectionId;
Expand Down Expand Up @@ -102,9 +112,11 @@ export function handlePositionsMerge(event: PositionsMerge): void {
}

export function handlePayoutRedemption(event: PayoutRedemption): void {
requireAccount(event.params.redeemer.toHexString());
requireAccount(event.params.redeemer.toHexString(), event.block.timestamp);
markAccountAsSeen(event.params.redeemer.toHexString(), event.block.timestamp);

let redemption = new Redemption(event.transaction.hash.toHexString());
redemption.timestamp = event.block.timestamp;
redemption.redeemer = event.params.redeemer.toHexString();
redemption.collateralToken = event.params.collateralToken.toHexString();
redemption.parentCollectionId = event.params.parentCollectionId;
Expand Down
1 change: 1 addition & 0 deletions src/FixedProductMarketMakerFactoryMapping.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function handleFixedProductMarketMakerCreation(

fixedProductMarketMaker.creator = event.params.creator;
fixedProductMarketMaker.creationTimestamp = event.block.timestamp;
fixedProductMarketMaker.creationTransactionHash = event.transaction.hash;

getCollateralDetails(event.params.collateralToken);
fixedProductMarketMaker.collateralToken = event.params.collateralToken.toHexString();
Expand Down
28 changes: 18 additions & 10 deletions src/FixedProductMarketMakerMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ import {
} from './utils/constants';
import { getCollateralScale } from './utils/collateralTokens';
import { updateGlobalVolume } from './utils/global-utils';
import { max } from './utils/maths';
import { requireAccount, updateUserVolume } from './utils/account-utils';
import { increment, max } from './utils/maths';
import {
markAccountAsSeen,
requireAccount,
updateUserVolume,
} from './utils/account-utils';

function recordBuy(event: FPMMBuy): void {
let buy = new Transaction(event.transaction.hash.toHexString());
Expand Down Expand Up @@ -145,8 +149,9 @@ export function handleFundingAdded(event: FPMMFundingAdded): void {
fpmm.outcomeTokenPrices = calculatePrices(newAmounts);
}

fpmm.liquidityAddQuantity = fpmm.liquidityAddQuantity.plus(bigOne);
fpmm.liquidityAddQuantity = increment(fpmm.liquidityAddQuantity);
fpmm.save();
markAccountAsSeen(event.params.funder.toHexString(), event.block.timestamp);
recordFundingAddition(event);
updateMarketPositionFromLiquidityAdded(event);
}
Expand Down Expand Up @@ -186,8 +191,9 @@ export function handleFundingRemoved(event: FPMMFundingRemoved): void {
fpmm.outcomeTokenPrices = calculatePrices(newAmounts);
}

fpmm.liquidityRemoveQuantity = fpmm.liquidityRemoveQuantity.plus(bigOne);
fpmm.liquidityRemoveQuantity = increment(fpmm.liquidityRemoveQuantity);
fpmm.save();
markAccountAsSeen(event.params.funder.toHexString(), event.block.timestamp);
recordFundingRemoval(event);
updateMarketPositionFromLiquidityRemoved(event);
}
Expand Down Expand Up @@ -245,8 +251,8 @@ export function handleBuy(event: FPMMBuy): void {
collateralScaleDec,
);

fpmm.tradesQuantity = fpmm.tradesQuantity.plus(bigOne);
fpmm.buysQuantity = fpmm.buysQuantity.plus(bigOne);
fpmm.tradesQuantity = increment(fpmm.tradesQuantity);
fpmm.buysQuantity = increment(fpmm.buysQuantity);
fpmm.save();

updateUserVolume(
Expand All @@ -255,6 +261,7 @@ export function handleBuy(event: FPMMBuy): void {
collateralScaleDec,
event.block.timestamp,
);
markAccountAsSeen(event.params.buyer.toHexString(), event.block.timestamp);
recordBuy(event);
updateGlobalVolume(
event.params.investmentAmount,
Expand Down Expand Up @@ -318,8 +325,8 @@ export function handleSell(event: FPMMSell): void {
collateralScaleDec,
);

fpmm.tradesQuantity = fpmm.tradesQuantity.plus(bigOne);
fpmm.sellsQuantity = fpmm.sellsQuantity.plus(bigOne);
fpmm.tradesQuantity = increment(fpmm.tradesQuantity);
fpmm.sellsQuantity = increment(fpmm.sellsQuantity);
fpmm.save();

updateUserVolume(
Expand All @@ -328,6 +335,7 @@ export function handleSell(event: FPMMSell): void {
collateralScaleDec,
event.block.timestamp,
);
markAccountAsSeen(event.params.seller.toHexString(), event.block.timestamp);
recordSell(event);
updateGlobalVolume(
event.params.returnAmount,
Expand All @@ -344,8 +352,8 @@ export function handlePoolShareTransfer(event: Transfer): void {
let toAddress = event.params.to.toHexString();
let sharesAmount = event.params.value;

requireAccount(fromAddress);
requireAccount(toAddress);
requireAccount(fromAddress, event.block.timestamp);
requireAccount(toAddress, event.block.timestamp);

if (fromAddress != AddressZero) {
let fromMembership = loadPoolMembership(fpmmAddress, fromAddress);
Expand Down
Loading

0 comments on commit b8c7386

Please sign in to comment.