diff --git a/src/helper.ts b/src/helper.ts index 11ce182..80a86a6 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -23,6 +23,7 @@ export const calculateEarningAmount = ( .div(totalStake.toBigDecimal()) .times(feeAmount.toBigDecimal()); + export function getOrCreateUser(address: Address): User { let user = User.load(address.toHexString()); if (!user) { @@ -213,13 +214,15 @@ export function updateOrCreateUserVaultFeeAggregate( userAddress: Address, isInventory: boolean ): UserVaultFeeAggregate { - let userVaultFeeAggregate = UserVaultFeeAggregate.load( - vaultId.concat("-").concat(userAddress.toHexString()) + let id = vaultId.concat("-").concat(userAddress.toHexString()).concat("-") + .concat( + isInventory + ? BigInt.fromI32(1).toHexString() + : BigInt.fromI32(0).toHexString() ); + let userVaultFeeAggregate = UserVaultFeeAggregate.load(id); if (!userVaultFeeAggregate) { - userVaultFeeAggregate = new UserVaultFeeAggregate( - vaultId.concat("-").concat(userAddress.toHexString()) - ); + userVaultFeeAggregate = new UserVaultFeeAggregate(id); userVaultFeeAggregate.aggregatedVaultFees = amount; userVaultFeeAggregate.vault = vaultId; userVaultFeeAggregate.isInventory = isInventory; diff --git a/src/inventory-staking.ts b/src/inventory-staking.ts index babe205..c608cbc 100644 --- a/src/inventory-staking.ts +++ b/src/inventory-staking.ts @@ -1,4 +1,4 @@ -import { BigInt, Address } from "@graphprotocol/graph-ts"; +import { BigInt, Address, log, store } from "@graphprotocol/graph-ts"; import { Deposit, Withdraw, @@ -32,6 +32,10 @@ export function handleFeesReceived(event: FeesReceived): void { if (vault) { if (vault.shares) { var array: string[] = vault.shares; + var newArray: string[] = []; + log.info("Inventory Fee Distributing to {} potential shareholders", [ + array.length.toString(), + ]); for (let i = 0; i < array.length; i++) { let poolShare = PoolShare.load(array[i]); if (poolShare) { @@ -56,9 +60,18 @@ export function handleFeesReceived(event: FeesReceived): void { Address.fromString(poolShare.user), true ); + newArray.push(poolShare.id); + } else { + if (poolShare.liquidityShare != BigInt.fromI32(0).toBigDecimal()) { + newArray.push(poolShare.id); + } else { + store.remove("PoolShare", poolShare.id); + } } } } + vault.shares = newArray; + vault.save(); } } } @@ -75,13 +88,12 @@ export function handleDeposit(event: Deposit): void { Address.fromBytes(vault.address), event.params.sender ); - if(poolShare){ + if (poolShare) { poolShare.inventoryShare = poolShare.inventoryShare.plus( event.params.xTokenAmount.toBigDecimal() ); poolShare.save(); - } - else { + } else { poolShare = createOrUpdatePoolShare( Address.fromBytes(vault.address), vault.id, @@ -109,22 +121,21 @@ export function handleWithdraw(event: Withdraw): void { Address.fromBytes(vault.address), event.params.sender ); - if(poolShare){ + if (poolShare) { poolShare.inventoryShare = poolShare.inventoryShare.minus( event.params.xTokenAmount.toBigDecimal() ); poolShare.save(); } - } } -export function handleXTokenCreated(event : XTokenCreated) : void { +export function handleXTokenCreated(event: XTokenCreated): void { let vault = getVaultFromId(event.params.vaultId); - if(vault){ + if (vault) { getOrCreateToken(event.params.xToken, vault.id, true); vault.xTokenAddress = event.params.xToken; vault.save(); TokenX.create(event.params.xToken); } -} \ No newline at end of file +} diff --git a/src/lp-staking.ts b/src/lp-staking.ts index 6d1bf12..60bf1a6 100644 --- a/src/lp-staking.ts +++ b/src/lp-staking.ts @@ -1,4 +1,4 @@ -import { BigInt, Address, log } from "@graphprotocol/graph-ts"; +import { BigInt, Address, log, store } from "@graphprotocol/graph-ts"; import { FeesReceived, PoolCreated, @@ -31,6 +31,10 @@ export function handleFeesReceived(event: FeesReceived): void { if (vault) { if (vault.shares) { var array: string[] = vault.shares; + var newArray: string[] = []; + log.info("LP Fee Distributing to {} potential shareholders", [ + array.length.toString(), + ]); for (let i = 0; i < array.length; i++) { let poolShare = PoolShare.load(array[i]); if (poolShare) { @@ -55,9 +59,18 @@ export function handleFeesReceived(event: FeesReceived): void { Address.fromString(poolShare.user), false ); + newArray.push(poolShare.id); + } else { + if (poolShare.inventoryShare != BigInt.fromI32(0).toBigDecimal()) { + newArray.push(poolShare.id); + } else { + store.remove("PoolShare", poolShare.id); + } } } } + vault.shares = newArray; + vault.save(); } } }