Skip to content

Commit

Permalink
fix: update the vault state entity structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Wagalidoom committed Sep 10, 2024
1 parent aaa0f07 commit 1bf2d52
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type User @entity {
type VaultState @entity {
id: ID!
profitAccrued: BigInt!
treasuryShare: BigInt!
treasuryAsset: BigInt!
_totalShares: BigInt!
_totalAssets: BigInt!
lastUpdatedTimeStamp: BigInt!
avgRewardPerSecond: BigInt!
feePercent: Int!
Expand Down
3 changes: 3 additions & 0 deletions src/gno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ export function handleTransfer(event: Transfer): void {
const userTo = loadOrCreateUser(to);

if (userTo.deposit > ZERO_BI) {
// To avoid a negative result that could throw an error
if (userTo.deposit <= event.params.value) {
userTo.voteWeight = userTo.voteWeight.minus(userTo.deposit);
userTo.voteWeight = userTo.voteWeight.plus(event.params.value);
userTo.deposit = ZERO_BI;
} else {
userTo.voteWeight = userTo.voteWeight.minus(event.params.value);
userTo.deposit = userTo.deposit.minus(event.params.value);
}
userTo.gno = userTo.gno.plus(event.params.value);
saveOrRemoveUser(userTo);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/osgnoVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export function loadOrCreateVault(): VaultState {
if (vaultState == null) {
vaultState = new VaultState("VAULT_STATE");
vaultState.profitAccrued = BigInt.fromI32(0);
vaultState.treasuryShare = BigInt.fromI32(0);
vaultState.treasuryAsset = BigInt.fromI32(0);
vaultState._totalShares = BigInt.fromI32(0);
vaultState._totalAssets = BigInt.fromI32(0);
vaultState.lastUpdatedTimeStamp = BigInt.fromString("55776895775");
vaultState.avgRewardPerSecond = BigInt.fromI32(0);
vaultState.feePercent = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/osgno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function handleTransfer(event: Transfer): void {

function convertToAssets(shares: BigInt, timestamp: BigInt): BigInt {
const vaultState = loadOrCreateVault();
const _totalShares = vaultState.treasuryShare;
return _convertToAssets(shares, _totalShares, totalAssets(timestamp, vaultState.lastUpdatedTimeStamp, vaultState.avgRewardPerSecond, vaultState.treasuryAsset, BigInt.fromI32(vaultState.feePercent)));
const _totalShares = vaultState._totalShares;
return _convertToAssets(shares, _totalShares, totalAssets(timestamp, vaultState.lastUpdatedTimeStamp, vaultState.avgRewardPerSecond, vaultState._totalAssets, BigInt.fromI32(vaultState.feePercent)));
}

function _convertToAssets(shares: BigInt, totalShares_: BigInt, totalAssets_: BigInt): BigInt {
Expand Down
17 changes: 13 additions & 4 deletions src/osgnoVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ export function handleMint(event: Mint): void {

export function handleStateUpdated(event: StateUpdated): void {
let vaultState = loadOrCreateVault();
vaultState.profitAccrued = event.params.profitAccrued;
vaultState.treasuryShare = event.params.treasuryShares;
vaultState.treasuryAsset = event.params.treasuryAssets;

const profitAccrued = event.params.profitAccrued;
const treasuryShares = event.params.treasuryShares;
const treasuryAssets = event.params.treasuryAssets;

const newTotalAssets = vaultState._totalAssets.plus(profitAccrued);

vaultState.profitAccrued = profitAccrued;
vaultState._totalAssets = newTotalAssets;
vaultState._totalShares = vaultState._totalShares.plus(treasuryShares);
vaultState.lastUpdatedTimeStamp = event.block.timestamp;

vaultState.save();
}
}


export function handleAvgRewardPerSecondUpdated(event: AvgRewardPerSecondUpdated): void {
let vaultState = loadOrCreateVault();
Expand Down

0 comments on commit 1bf2d52

Please sign in to comment.