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

Address returnsGenerated being incorrect & add tests #119

Merged
merged 12 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint:fix && git add -A

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarn lint:fix is still there; I removed git add -A because it kept including files in each commit that I did not want to commit yet. I kept running into issues where I would stage 1 or 2 files for commit, git commit them, then the commit includes every change that was in my working directory.

yarn lint:fix
11 changes: 10 additions & 1 deletion generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Value,
ValueKind,
store,
Address,
Bytes,
BigInt,
BigDecimal
Expand Down Expand Up @@ -666,6 +665,7 @@ export class VaultUpdate extends Entity {
this.set("sharesMinted", Value.fromBigInt(BigInt.zero()));
this.set("sharesBurnt", Value.fromBigInt(BigInt.zero()));
this.set("balancePosition", Value.fromBigInt(BigInt.zero()));
this.set("balanceTokens", Value.fromBigInt(BigInt.zero()));
this.set("pricePerShare", Value.fromBigInt(BigInt.zero()));
this.set("returnsGenerated", Value.fromBigInt(BigInt.zero()));
this.set("totalFees", Value.fromBigInt(BigInt.zero()));
Expand Down Expand Up @@ -780,6 +780,15 @@ export class VaultUpdate extends Entity {
this.set("balancePosition", Value.fromBigInt(value));
}

get balanceTokens(): BigInt {
let value = this.get("balanceTokens");
return value!.toBigInt();
}

set balanceTokens(value: BigInt) {
this.set("balanceTokens", Value.fromBigInt(value));
}

get pricePerShare(): BigInt {
let value = this.get("pricePerShare");
return value!.toBigInt();
Expand Down
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ type VaultUpdate @entity {
sharesBurnt: BigInt!
"The current balance position defined as: (vault.totalAssets() * (vault.pricePerShare() / 10**vault.decimals()))."
balancePosition: BigInt!
"Balance of Tokens in the Vault and its Strategies at the time of update"
balanceTokens: BigInt!

### PERFORMANCE

Expand Down
2 changes: 1 addition & 1 deletion src/utils/account/vault-position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getBalancePosition(
account: Account,
vaultContract: VaultContract
): BigInt {
log.info('GetBalancePosition account {} ', [account.id]);
log.info('[VaultPosition] GetBalancePosition account {} ', [account.id]);
let pricePerShare = vaultContract.pricePerShare();
let decimals = vaultContract.decimals();
// (vault.balanceOf(account) * (vault.pricePerShare() / 10**vault.decimals()))
Expand Down
2 changes: 1 addition & 1 deletion src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getOrCreateTransactionFromCall(
);
let transaction = _getOrCreateTransaction(
call.transaction,
call.transaction.index, // As the call hasnt the event log inde, we use the transaction index value.
call.transaction.index, // As the call hasnt the event log inde, we use the transaction index value. Will this cause an accounting error if someone deposits and withdraws from a vault in the same transaction?
rareweasel marked this conversation as resolved.
Show resolved Hide resolved
call.block,
action
);
Expand Down
76 changes: 49 additions & 27 deletions src/utils/vault/vault-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ function createVaultUpdate(
managementFees: BigInt,
performanceFees: BigInt,
balancePosition: BigInt,
totalAssets: BigInt,
rewards: Address | null = null
): VaultUpdate {
log.debug('[VaultUpdate] Creating vault update with id {}', [vault.id]);
log.debug('[VaultUpdate] Creating vault update with id {}', [id]);
let vaultUpdate = new VaultUpdate(id);
vaultUpdate.timestamp = transaction.timestamp;
vaultUpdate.blockNumber = transaction.blockNumber;
Expand All @@ -49,6 +50,7 @@ function createVaultUpdate(
vaultUpdate.tokensWithdrawn = tokensWithdrawn;
vaultUpdate.sharesMinted = sharesMinted;
vaultUpdate.sharesBurnt = sharesBurnt;
vaultUpdate.balanceTokens = totalAssets;
// Performance
vaultUpdate.pricePerShare = pricePerShare;
vaultUpdate.totalFees = totalFees;
Expand All @@ -57,13 +59,27 @@ function createVaultUpdate(
vaultUpdate.balancePosition = balancePosition;
vaultUpdate.rewards = rewards;

if (vault.balanceTokens.gt(balancePosition)) {
vaultUpdate.returnsGenerated = BIGINT_ZERO;
} else {
vaultUpdate.returnsGenerated = balancePosition.minus(vault.balanceTokens);
}

// NOTE: This returnsGenerated parameter represents the _recognized_ returns since the last VaultUpdate.
// Recognized returns are returns which the strategy has paid out to the Vault via Harvest().
// Unrecognized returns are returns which have yet to be harvested.
// The value of unrecognized returns may change between the time of query and the time of harvest, so we use
// recognized returns for the subgraph.
vaultUpdate.returnsGenerated = totalAssets
.minus(vault.balanceTokens)
.minus(tokensDeposited)
.plus(tokensWithdrawn);
vaultUpdate.save();

vault.latestUpdate = vaultUpdate.id;
vault.balanceTokens = totalAssets;
// todo: current implementation of balanceTokensIdle does not update when debt is issued to strategies
vault.balanceTokensIdle = vault.balanceTokensIdle
.plus(tokensDeposited)
.minus(tokensWithdrawn);
// todo: balanceTokensInvested
vault.sharesSupply = vault.sharesSupply.plus(sharesMinted).minus(sharesBurnt);
vault.save();

return vaultUpdate;
}

Expand All @@ -73,7 +89,8 @@ export function firstDeposit(
depositedAmount: BigInt,
sharesMinted: BigInt,
pricePerShare: BigInt,
balancePosition: BigInt
balancePosition: BigInt,
totalAssets: BigInt
): VaultUpdate {
log.debug('[VaultUpdate] First deposit', []);
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
Expand All @@ -92,7 +109,8 @@ export function firstDeposit(
BIGINT_ZERO,
BIGINT_ZERO,
BIGINT_ZERO,
balancePosition
balancePosition,
totalAssets
);
}

Expand All @@ -105,7 +123,8 @@ export function deposit(
depositedAmount: BigInt,
sharesMinted: BigInt,
pricePerShare: BigInt,
balancePosition: BigInt
balancePosition: BigInt,
totalAssets: BigInt
): VaultUpdate {
log.debug('[VaultUpdate] Deposit', []);
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
Expand All @@ -128,7 +147,8 @@ export function deposit(
latestVaultUpdate!.totalFees,
latestVaultUpdate!.managementFees,
latestVaultUpdate!.performanceFees,
balancePosition
balancePosition,
totalAssets
);
}

Expand All @@ -142,7 +162,8 @@ export function withdraw(
withdrawnAmount: BigInt,
sharesBurnt: BigInt,
transaction: Transaction,
balancePosition: BigInt
balancePosition: BigInt,
totalAssets: BigInt
): VaultUpdate {
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
let newVaultUpdate = createVaultUpdate(
Expand All @@ -157,14 +178,9 @@ export function withdraw(
latestVaultUpdate.totalFees,
latestVaultUpdate.managementFees,
latestVaultUpdate.performanceFees,
balancePosition
balancePosition,
totalAssets
);
vault.sharesSupply = vault.sharesSupply.minus(sharesBurnt);
vault.balanceTokens = vault.balanceTokens.minus(withdrawnAmount);
vault.balanceTokensIdle = vault.balanceTokensIdle.minus(withdrawnAmount);

vault.latestUpdate = newVaultUpdate.id;
vault.save();
return newVaultUpdate;
}

Expand All @@ -173,7 +189,8 @@ export function strategyReported(
latestVaultUpdate: VaultUpdate,
transaction: Transaction,
pricePerShare: BigInt,
balancePosition: BigInt
balancePosition: BigInt,
totalAssets: BigInt
): VaultUpdate {
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
let newVaultUpdate = createVaultUpdate(
Expand All @@ -188,10 +205,9 @@ export function strategyReported(
latestVaultUpdate.totalFees,
latestVaultUpdate.managementFees,
latestVaultUpdate.performanceFees,
balancePosition
balancePosition,
totalAssets
);
vault.latestUpdate = newVaultUpdate.id;
vault.save();
return newVaultUpdate;
}

Expand All @@ -200,7 +216,8 @@ export function performanceFeeUpdated(
transaction: Transaction,
latestVaultUpdate: VaultUpdate,
balancePosition: BigInt,
performanceFee: BigInt
performanceFee: BigInt,
totalAssets: BigInt
): VaultUpdate {
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
let newVaultUpdate = createVaultUpdate(
Expand All @@ -215,7 +232,8 @@ export function performanceFeeUpdated(
latestVaultUpdate.totalFees,
BIGINT_ZERO,
performanceFee,
balancePosition
balancePosition,
totalAssets
);
return newVaultUpdate;
}
Expand All @@ -225,7 +243,8 @@ export function managementFeeUpdated(
transaction: Transaction,
latestVaultUpdate: VaultUpdate,
balancePosition: BigInt,
managementFee: BigInt
managementFee: BigInt,
totalAssets: BigInt
): VaultUpdate {
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
let newVaultUpdate = createVaultUpdate(
Expand All @@ -240,7 +259,8 @@ export function managementFeeUpdated(
latestVaultUpdate.totalFees,
managementFee,
BIGINT_ZERO,
balancePosition
balancePosition,
totalAssets
);
return newVaultUpdate;
}
Expand All @@ -250,6 +270,7 @@ export function rewardsUpdated(
transaction: Transaction,
latestVaultUpdate: VaultUpdate,
balancePosition: BigInt,
totalAssets: BigInt,
rewards: Address
): VaultUpdate {
let vaultUpdateId = buildIdFromVaultAndTransaction(vault, transaction);
Expand All @@ -266,6 +287,7 @@ export function rewardsUpdated(
BIGINT_ZERO,
BIGINT_ZERO,
balancePosition,
totalAssets,
rewards
);
return newVaultUpdate;
Expand Down
44 changes: 30 additions & 14 deletions src/utils/vault/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ export function deposit(
sharesMinted: BigInt,
timestamp: BigInt
): void {
log.debug('[Vault] Deposit', []);
log.debug(
'[Vault] Deposit vault: {} receiver: {} depositAmount: {} sharesMinted: {}',
[
vaultAddress.toHexString(),
receiver.toHexString(),
depositedAmount.toString(),
sharesMinted.toString(),
]
);
let vaultContract = VaultContract.bind(vaultAddress);
let account = accountLibrary.getOrCreate(receiver);
let pricePerShare = vaultContract.pricePerShare();
Expand All @@ -202,14 +210,16 @@ export function deposit(

let vaultUpdate: VaultUpdate;
let balancePosition = getBalancePosition(vaultContract);
let totalAssets = getTotalAssets(vaultContract);
if (vault.latestUpdate == null) {
vaultUpdate = vaultUpdateLibrary.firstDeposit(
vault,
transaction,
depositedAmount,
sharesMinted,
pricePerShare,
balancePosition
balancePosition,
totalAssets
);
} else {
vaultUpdate = vaultUpdateLibrary.deposit(
Expand All @@ -218,7 +228,8 @@ export function deposit(
depositedAmount,
sharesMinted,
pricePerShare,
balancePosition
balancePosition,
totalAssets
);
}

Expand All @@ -232,13 +243,6 @@ export function deposit(
vaultUpdate.returnsGenerated,
vaultContract.decimals()
);

vault.latestUpdate = vaultUpdate.id;
vault.balanceTokens = vault.balanceTokens.plus(depositedAmount);
vault.balanceTokensIdle = vault.balanceTokensIdle.plus(depositedAmount);
vault.sharesSupply = vault.sharesSupply.plus(sharesMinted);

vault.save();
}

export function isVault(vaultAddress: Address): boolean {
Expand Down Expand Up @@ -353,7 +357,8 @@ export function withdraw(
withdrawnAmount,
sharesBurnt,
transaction,
balancePosition
balancePosition,
getTotalAssets(vaultContract)
);

updateVaultDayData(
Expand All @@ -367,6 +372,9 @@ export function withdraw(
vaultContract.decimals()
);
}
} else {
// log.critical("[Vault] latestVaultUpdate is null and someone is calling withdraw(). Vault: {}", [vault.id.toString()])
// it turns out it is happening
}
}

Expand Down Expand Up @@ -429,7 +437,8 @@ export function strategyReported(
latestVaultUpdate as VaultUpdate,
transaction,
pricePerShare,
balancePosition
balancePosition,
getTotalAssets(vaultContract)
);
}
}
Expand Down Expand Up @@ -457,7 +466,8 @@ export function performanceFeeUpdated(
ethTransaction,
latestVaultUpdate as VaultUpdate,
getBalancePosition(vaultContract),
performanceFee
performanceFee,
getTotalAssets(vaultContract)
) as VaultUpdate;
vault.latestUpdate = vaultUpdate.id;
}
Expand Down Expand Up @@ -495,7 +505,8 @@ export function managementFeeUpdated(
ethTransaction,
latestVaultUpdate as VaultUpdate,
getBalancePosition(vaultContract),
managementFee
managementFee,
getTotalAssets(vaultContract)
) as VaultUpdate;
vault.latestUpdate = vaultUpdate.id;
}
Expand Down Expand Up @@ -567,6 +578,7 @@ export function handleUpdateRewards(
ethTransaction,
latestVaultUpdate as VaultUpdate,
getBalancePosition(vaultContract),
getTotalAssets(vaultContract),
rewards
) as VaultUpdate;
vault.latestUpdate = vaultUpdate.id;
Expand Down Expand Up @@ -606,6 +618,10 @@ function getBalancePosition(vaultContract: VaultContract): BigInt {
return totalAssets.times(pricePerShare).div(BigInt.fromI32(10).pow(decimals));
}

function getTotalAssets(vaultContract: VaultContract): BigInt {
return vaultContract.totalAssets();
}

export function createCustomVaultIfNeeded(
vaultAddress: Address,
registryAddress: Address,
Expand Down
Loading