Skip to content

Commit

Permalink
removal and aggregate id fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GundamDweeb committed Jul 8, 2022
1 parent 1c967ba commit 0590266
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 20 additions & 9 deletions src/inventory-staking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, Address } from "@graphprotocol/graph-ts";
import { BigInt, Address, log, store } from "@graphprotocol/graph-ts";
import {
Deposit,
Withdraw,
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
15 changes: 14 additions & 1 deletion src/lp-staking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, Address, log } from "@graphprotocol/graph-ts";
import { BigInt, Address, log, store } from "@graphprotocol/graph-ts";
import {
FeesReceived,
PoolCreated,
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}
}
Expand Down

0 comments on commit 0590266

Please sign in to comment.