Skip to content

Commit

Permalink
Slim down marketplace subgraph into essential information only (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze authored Feb 2, 2022
1 parent 1dcb92b commit 54a6185
Show file tree
Hide file tree
Showing 22 changed files with 1,395 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/config/src/arbitrum.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
"treasure_address": "0xEBba467eCB6b21239178033189CeAE27CA12EaDf",
"treasure_start_block": 2416540,

"seed_of_life_address": "0x3956C81A51FeAed98d7A678d53F44b9166c8ed66",
"seed_of_life_start_block": 2416557,
"extra_life_address": "0x21e1969884D477afD2Afd4Ad668864a0EebD644c",
"extra_life_start_block": 2416557,
"keys_address": "0xf0a35bA261ECE4FC12870e5B7b9E7790202EF9B5",
"keys_start_block": 2416554,
"smol_brains_address": "0x6325439389E0797Ab35752B4F43a14C004f22A9c",
"smol_brains_start_block": 3163146,
"smol_brains_land_address": "0xd666d1CC3102cd03e07794A61E5F4333B4239F53",
Expand Down
17 changes: 17 additions & 0 deletions subgraphs/marketplace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "marketplace",
"version": "1.0.0",
"description": "Subgraph that powers the Treasure Marketplace",
"license": "MIT",
"scripts": {
"build": "graph build subgraph.yaml",
"codegen": "graph codegen subgraph.yaml",
"deploy:dev": "graphx deploy --product hosted-service treasureproject/bridgeworld-dev",
"deploy:prod": "graph deploy --product hosted-service treasureproject/marketplace",
"deploy:staging": "graph deploy --product hosted-service treasureproject/marketplace-next",
"prepare:arbitrum": "mustache ../../node_modules/@treasure/subgraph-config/src/arbitrum.json template.yaml > subgraph.yaml",
"prepare:arbitrum-testnet": "mustache ../../node_modules/@treasure/subgraph-config/src/arbitrum-testnet.json template.yaml > subgraph.yaml",
"prepare:mainnet": "true",
"test": "graph test"
}
}
92 changes: 92 additions & 0 deletions subgraphs/marketplace/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
enum Status {
Active
Inactive
Sold
}

enum TokenStandard {
ERC721
ERC1155
}

type Collection @entity {
id: ID!

contract: String!
floorPrice: BigInt!
listings: [Listing!]!
name: String!
tokens: [Token!]! @derivedFrom(field: "collection")
standard: TokenStandard!

totalListings: Int!
totalSales: BigInt!
totalVolume: BigInt!
}

type Listing @entity {
id: ID!

# "Track originally listed quantity, needed when staking Treasures"
# _listedQuantity: BigInt!

blockTimestamp: BigInt!
buyer: User
# contract: String!
collection: Collection!
# collectionName: String!
expires: BigInt!

# "Used to support multiple filters with metadata attributes"
# filters: [String!]

# nicePrice: String
pricePerItem: BigInt!
quantity: Int!
seller: User!
status: Status!
token: Token!
# tokenName: String
# totalPrice: String

# Sold listing
transactionLink: String
}

type Token @entity {
id: ID!

collection: Collection!
# contract: String!
"For ERC1155s, but is it needed?"
floorPrice: BigInt
listings: [Listing!] @derivedFrom(field: "token")
name: String
owners: [UserToken!]! @derivedFrom(field: "token")
tokenId: BigInt!
}

type User @entity {
id: ID!

listings: [Listing!]! @derivedFrom(field: "seller")
purchases: [Listing!]! @derivedFrom(field: "buyer")
tokens: [UserToken!]! @derivedFrom(field: "user")
staked: [StakedToken!]! @derivedFrom(field: "user")
}

type UserToken @entity {
id: ID!

quantity: Int!
token: Token!
user: User!
}

type StakedToken @entity {
id: ID!

quantity: Int!
token: Token!
user: User!
}
13 changes: 13 additions & 0 deletions subgraphs/marketplace/src/helpers/ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Address, BigInt } from "@graphprotocol/graph-ts";

export function getAddressId(address: Address, tokenId: BigInt): string {
return `${address.toHexString()}-${tokenId.toHexString()}`;
}

export function getUserAddressId(
user: Address,
address: Address,
tokenId: BigInt
): string {
return [user.toHexString(), getAddressId(address, tokenId)].join("-");
}
4 changes: 4 additions & 0 deletions subgraphs/marketplace/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./ids";
export * from "./model";
export * from "./token-id";
export * from "./utils";
44 changes: 44 additions & 0 deletions subgraphs/marketplace/src/helpers/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Address, log } from "@graphprotocol/graph-ts";
import { Collection } from "../../generated/schema";

function createCollection(
contract: Address,
name: string,
standard: string
): void {
let id = contract.toHexString();
let collection = Collection.load(id);

if (!collection) {
collection = new Collection(id);

collection.contract = id;
collection.listings = [];
collection.name = name;
collection.standard = standard;

collection.save();
}
}

export function createErc721Collection(contract: Address, name: string): void {
createCollection(contract, name, "ERC721");
}

export function createErc1155Collection(contract: Address, name: string): void {
createCollection(contract, name, "ERC1155");
}

export function getCollection(contract: Address): Collection {
let id = contract.toHexString();
let collection = Collection.load(id);

// Should never happen, famous last words
if (!collection) {
collection = new Collection(id);

log.warning("Unknown collection: {}", [contract.toHexString()]);
}

return collection;
}
Loading

0 comments on commit 54a6185

Please sign in to comment.