Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/code: refactor schema #14

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
35468d5
chore: rename schema fields
milkyklim Oct 13, 2020
4dff309
chore: add cleaner comments
milkyklim Oct 13, 2020
b0fedf0
chore: refactor total fields
milkyklim Oct 13, 2020
0afdb4f
chore: add cleaner schema version
milkyklim Oct 14, 2020
fce6952
chore: add clean schema
milkyklim Oct 14, 2020
b69f7ae
chore: rename want token
milkyklim Oct 15, 2020
b627977
chore: replace singular names with plurals
milkyklim Oct 15, 2020
9148fa6
chore: remove "raw" part from names
milkyklim Oct 15, 2020
c8023af
chore: remove @entity from interfaces
milkyklim Oct 15, 2020
3ba7f1a
chore: add field to track current strategy balance
milkyklim Oct 15, 2020
a1a90de
chore: add name field for strategy
milkyklim Oct 15, 2020
81aee4c
chore: add strategy status
milkyklim Oct 15, 2020
6dcc96b
chore: add missing AccountBalanceUpdate
milkyklim Oct 15, 2020
3cf4c02
chore: rename fields, add comments and TODOs
milkyklim Oct 15, 2020
d1779e3
chore: refactor schema (broken)
milkyklim Oct 23, 2020
becede6
chore: rename VaultBalanceUpdate
milkyklim Oct 23, 2020
e8cfd77
chore: clean up comments and fields
milkyklim Oct 23, 2020
044bd4a
chore: refactor Vault type
milkyklim Oct 23, 2020
4c3f3c4
chore: remove dead fields and fix comments
milkyklim Oct 23, 2020
dd8b026
chore: refactor comments and fields
milkyklim Oct 23, 2020
9f946dd
chore: add comments for IDs
milkyklim Oct 23, 2020
2a8706b
style: update comments
milkyklim Oct 23, 2020
b7c5b8a
chore: use timestamp and blocknumber instead of transaction
milkyklim Oct 24, 2020
f0f92e6
docs: add main queries for revenue
milkyklim Oct 24, 2020
ea83815
chore: remove Decimal support (broken)
milkyklim Oct 24, 2020
84c7e9f
chore: refactor token creation (broken)
milkyklim Oct 24, 2020
74c18d9
chore: update schema names
milkyklim Oct 24, 2020
2c8b49d
chore: clean up helpers
milkyklim Oct 24, 2020
8d02e5d
chore: replace dash with minus in comments
milkyklim Oct 24, 2020
195eb62
chore: ditch previous code
milkyklim Oct 25, 2020
d05a899
chore: add deposit and withdrawal handlers
milkyklim Oct 25, 2020
2b04da9
chore: add transfer support
milkyklim Oct 25, 2020
f994793
chore: add token support
milkyklim Oct 26, 2020
2bc1c26
chore: simplify deposit and withdrawal
milkyklim Oct 27, 2020
823b81e
chore: create workflow for deposits and withdrawals
milkyklim Oct 27, 2020
73a3c79
chore: remove comments
milkyklim Oct 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 29 additions & 307 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,330 +23,52 @@ Subgraph to track yearn vaults metrics on Mainnet.

See `package.json` for local deployment.

## Entities
## Tracking

### Vault

The `Vault` entity is the most relevant one on this subgraph, since it holds most of the data related to the scope of the Vault, like vault balance (how much of the base token the vault has), strategy balance (how much of that base token is invested in the strategy), aggregated data for deposits, withdrawals, earnings, links to other related entities such as the Controller and Strategy, and many more.

```graphql
type Vault @entity {
id: ID!

"Amount of underlying token that a full share of <yToken> represents"
pricePerFullShare: BigDecimal!

pricePerFullShareRaw: BigInt!

totalSupply: BigDecimal!

totalSupplyRaw: BigInt!

"Balance of the Vault contract of underlying Token + balance of the Strategy contract of underlying Token"
vaultBalance: BigDecimal!

"Balance of underlying Token specifically held in the strategy"
strategyBalance: BigDecimal!

"How much the vault allows to be borrowed"
available: BigDecimal!

"Balance of the Vault contract of underlying Token + balance of the Strategy contract of underlying Token"
vaultBalanceRaw: BigInt!

"Balance of underlying Token specifically held in the strategy"
strategyBalanceRaw: BigInt!

"How much the vault allows to be borrowed"
availableRaw: BigInt!

"Token accepted as deposit on the Vault"
underlyingToken: Token!

"yToken minted as shares by the Vault"
shareToken: Token!

currentController: Controller!

currentStrategy: Strategy!

"Transaction metadata for the last update"
transaction: Transaction!

"totalDeposited - totalWithdrawn. Considers all deposits of underlying made by external accounts"
netDeposits: BigDecimal!

totalDeposited: BigDecimal!

totalWithdrawn: BigDecimal!

"totalSharesMinted - totalSharesBurned"
totalActiveShares: BigDecimal!

totalSharesMinted: BigDecimal!

totalSharesBurned: BigDecimal!

netDepositsRaw: BigInt!

totalDepositedRaw: BigInt!

totalWithdrawnRaw: BigInt!

totalActiveSharesRaw: BigInt!

totalSharesMintedRaw: BigInt!

totalSharesBurnedRaw: BigInt!

totalEarnings: BigDecimal!

totalEarningsRaw: BigInt!

totalHarvestCalls: BigInt!

transfers: [Transfer!]! @derivedFrom(field: "vault")

deposits: [Deposit!]! @derivedFrom(field: "vault")

withdrawals: [Withdrawal!]! @derivedFrom(field: "vault")

harvests: [Harvest!]! @derivedFrom(field: "vault")

balances: [AccountVaultBalance!]! @derivedFrom(field: "vault")

strategies: [Strategy!]! @derivedFrom(field: "vault")

controllers: [Controller!]! @derivedFrom(field: "vault")
}
```

#### Account

The `Acount` entity represents an ethereum address that interacted with the protocol (at least 1 Vault). This entity is only really there to centralize all the data for a specific user, such as all the user balances (one for each vault, more on that later), all the deposits, withdrawals and transfer that the account is related to.

```graphql
type Account @entity {
"Ethereum address of the user"
id: ID!

vaultBalances: [AccountVaultBalance!]! @derivedFrom(field: "account")

"Deposits that are related to this account"
deposits: [Deposit!]! @derivedFrom(field: "account")

"Withdrawals that are related to this account"
withdrawals: [Deposit!]! @derivedFrom(field: "account")

"Transfers that are sent to this account"
receivedTransfers: [Transfer!]! @derivedFrom(field: "to")

"Transfers that are sent from this account"
sentTransfers: [Transfer!]! @derivedFrom(field: "from")
}
```

#### AccountVaultBalance

This entity represent the balance (or position) of an Account within a specific Vault (hence the name). It has a lot of aggregated values that are tracked by following the minting/burning of shares of the yToken, which allow for tracking of the underlying token movement as well as the share token movements (such as deposits, withdrawals and transfers), and aggregation of those values to know the total amount of each of the possible fields. We also provide some variables that depict internal "balances" of those aggregated values, like the `netDeposits` field, which represents the balance of ingess and egress transfers ((deposits + transfersIN) - (withdrawals + transfersOUT)).

This entity does not have a real-time balance of the underlying token (with earnings included) like the `Vault` entity has, given that it's only able to be updated when ERC20 share token Transfer events occur for the specified account, and cannot be updated when earnings are calculated (Harvest), thus making it impossible to have a real-time earnings value pre-calculated. It is possible to calculate it with the values provided when you query this entity, given that it has the linked `Vault` entity which has the real-time `pricePerFullShare` value, and the `AccountVaultBalance` does keep track of the real-time `shareBalance`, and with those values you can calculate the updated underlying token balance for this specific `Account`.

```graphql
type AccountVaultBalance @entity {
id: ID!

vault: Vault!

account: Account!

"Token used on deposits/withdrawals"
underlyingToken: Token!

"Token received as shares"
shareToken: Token!

"Net deposits of a given Account within a given Vault. Transfers between accounts are taken into consideration for this metric"
netDeposits: BigDecimal!

"Total tokens deposited by this Account in Vault"
totalDeposited: BigDecimal!

"Total tokens withdrawn by this Account in Vault"
totalWithdrawn: BigDecimal!

"Total tokens sent to another account by this Account in Vault"
totalSent: BigDecimal!

"Total tokens received from another account by this Account in Vault"
totalReceived: BigDecimal!

"Shares are the token minted by the Vault"
shareBalance: BigDecimal!

totalSharesMinted: BigDecimal!

totalSharesBurned: BigDecimal!

totalSharesSent: BigDecimal!

totalSharesReceived: BigDecimal!

"Net deposits of a given Account within a given Vault. Transfers between accounts are taken into consideration for this metric"
netDepositsRaw: BigInt!

"Total tokens deposited by this Account in Vault"
totalDepositedRaw: BigInt!

"Total tokens withdrawn by this Account in Vault"
totalWithdrawnRaw: BigInt!

"Total tokens sent to another account by this Account in Vault"
totalSentRaw: BigInt!

"Total tokens received from another account by this Account in Vault"
totalReceivedRaw: BigInt!

"Shares are the token minted by the Vault"
shareBalanceRaw: BigInt!

totalSharesMintedRaw: BigInt!

totalSharesBurnedRaw: BigInt!

totalSharesSentRaw: BigInt!

totalSharesReceivedRaw: BigInt!
}
```

#### Token

This entity holds information regarding ERC20 tokens present in the subgraph.
Total revenue per vault:

```graphql
type Token @entity {
id: ID!

address: Bytes!

decimals: Int!

name: String!

symbol: String!
{
vaultUpdates(orderBy: timestamp, orderDirection: desc, first: 1) {
timestamp
blockNumber
earnings
}
}
```

#### Transfer, Deposit, Withdraw

These entities hold the specific raw data that was processed to update the other entities to their latest state when an ERC20 Transfer from the yVault triggered (depending on the type of transfer it creates one of the 3 entities). They also track the metadata for the block that triggered it (block number, transaction hash and timestamp) and are linked to the corresponding entities that they modified. (Vault, AccountVaultBalance, etc).

#### Harvest

This entity, much like the `Transfer`, `Deposit` and `Withdraw` entity, tracks data that was used to update the state of other entities as well as metadata for the block that triggered it, whenever a harvest call is made to the strategy of a yVault.

This entity also aggregates some data, and calculate earnings gained for the yVault when it was called, which are then aggregated to the total it holds as well as the strategy total. It also track balance changes before and after the harvest() call, and `pricePerFullShare` changes too.
Total performance fee per vault (see [note](https://docs.yearn.finance/faq#can-you-explain-the-5-fee-on-additional-yield)):

```graphql
type Harvest @entity {
id: ID!

vault: Vault!

strategy: Strategy!

caller: Bytes!

pricePerFullShareBefore: BigDecimal!

pricePerFullShareAfter: BigDecimal!

pricePerFullShareBeforeRaw: BigInt!

pricePerFullShareAfterRaw: BigInt!

vaultBalanceBefore: BigDecimal!

vaultBalanceAfter: BigDecimal!

strategyBalanceBefore: BigDecimal!

strategyBalanceAfter: BigDecimal!

vaultBalanceBeforeRaw: BigInt!

vaultBalanceAfterRaw: BigInt!

strategyBalanceBeforeRaw: BigInt!

strategyBalanceAfterRaw: BigInt!

earnings: BigDecimal!

earningsRaw: BigInt!

transaction: Transaction!
{
vaultUpdates(orderBy: timestamp, orderDirection: desc, first: 1) {
timestamp
blockNumber
performanceFees
}
}
```

#### Strategy and Controller

These two entities hold barely any information for each of the smart contracts that they represent (Strategy and Controller), but allow us to keep tabs of all the historical controllers and strategies that a yVault might have overtime by deriving a list of all that have a link to the yVault.

The `Strategy` entity also keeps track of all the `Harvest` entities it has created, as well as the earnings made for the Vault when it was active.
Total withdrawal fee per vault:

```graphql
type Strategy @entity {
"Ethereum address"
id: ID!

vault: Vault!

totalEarnings: BigDecimal!

totalEarningsRaw: BigInt!

harvests: [Harvest!]! @derivedFrom(field: "strategy")

activeOnVaults: [Vault!] @derivedFrom(field: "currentStrategy")
}

type Controller @entity {
"Ethereum address"
id: ID!

vault: Vault!

activeOnVaults: [Vault!] @derivedFrom(field: "currentController")
{
vaultUpdates(orderBy: timestamp, orderDirection: desc, first: 1) {
timestamp
blockNumber
withdrawalFees
}
}
```

#### Transaction

This entity tracks metadata for specific transactions, as well as some derived data for all the deposits, withdrawals, transfers, harvests performed on it, as well as tracking a list of all the vaults that were last updated on that transaction.
Total withdrawal volume per vault:

```graphql
type Transaction @entity {
"ID = Transaction Hash"
id: ID!

timestamp: BigInt!

blockNumber: BigInt!

"Duplicated field to allow for byte search with transactionHash_contains"
transactionHash: Bytes!

deposits: [Deposit!]! @derivedFrom(field: "transaction")

withdrawals: [Withdrawal!]! @derivedFrom(field: "transaction")

transfers: [Transfer!]! @derivedFrom(field: "transaction")

harvests: [Harvest!]! @derivedFrom(field: "transaction")

"List of Vaults that last updated on this transaction"
vaultsUpdated: [Vault!]! @derivedFrom(field: "transaction")
{
vaultUpdates(orderBy: timestamp, orderDirection: desc, first: 1) {
timestamp
blockNumber
withdrawals
}
}
```
File renamed without changes.
Loading