---
# System prepended metadata

title: BAYC

---

# BAYC

https://bit.ly/subgraphcode

### Creating Subgraph on the Subgraph Studio
[Subgraph Studio](https://thegraph.com/studio/)


```bash
graph init --contract-name BoredApeYachtClub --index-events --product subgraph-studio --from-contract 0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D
```


#### schema.graphql
```
type Ape @entity {
  id: ID!
  apeID: BigInt
  contentURI: String!
  createAtTimestamp: BigInt!
  creator: User!
  owner: User!
}

type User @entity {
  id: ID!
  apes: [Ape!]! @derivedFrom(field: "owner")
  created: [Ape!]! @derivedFrom (field:"creator")
}
```


#### subgraph.yaml
```yaml
      entities:
        - Ape
        - User
```
```yaml
      eventHandlers:
        - event: Transfer(indexed address,indexed address,indexed uint256)
          handler: handleTransfer
```


#mappings.ts
```typescript
import {Transfer as TransferEvent, BoredApeYachtClub as BoredApeYachtClubContract} from '../generated/BoredApeYachtClub/BoredApeYachtClub'
import {Ape, User} from '../generated/schema'

export function handleTransfer(event: TransferEvent): void {
 let ape = Ape.load(event.params.tokenId.toString());
    if(!ape) {
       ape = new Ape(event.params.tokenId.toString());
       ape.creator = event.params.to.toHexString();
       ape.createAtTimestamp = event.block.timestamp;
      

      let apeContract = BoredApeYachtClubContract.bind(event.address);
      ape.contentURI = apeContract.tokenURI(event.params.tokenId)
    }
  ape.owner = event.params.to.toHexString();
  ape.save();

  let user = User.load(event.params.to.toHexString());
  if(!user) {
    user = new User(event.params.to.toHexString());
    user.save();

  }

}
```