Skip to content

Commit

Permalink
fix: non zero commissions split (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauthiermyr authored Oct 30, 2023
1 parent 8f8c2a1 commit 09eec53
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ type OracleAggregatorReportVariantVote @entity {
#

type Commission @entity {
id: String! # "integrationsAddress+recipient"
id: String! # "integrationsAddress+idx"
vPoolIntegration: vPoolIntegration!

recipient: Bytes!
Expand Down
63 changes: 48 additions & 15 deletions src/ERC20.mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
PoolBalance,
DepositDataEntry,
ExitDataEntry,
vExitQueue
vExitQueue,
CommissionLoader
} from '../generated/schema';
import { Stake as Stake_1_0_0_rc4 } from '../generated/templates/ERC20_1_0_0_rc4/Native20';
import {
Expand Down Expand Up @@ -283,19 +284,41 @@ export function handleNewCommissionSplit(event: NewCommissionSplit): void {
const ts = event.block.timestamp;
const blockId = event.block.number;

// clear out old commission splits
let idx = 0;
while (true) {
const commission = Commission.load(entityUUID(event, [idx.toString()]));
if (commission != null) {
commission.commission = BigInt.zero();
commission.save();
++idx;
} else {
break;
}
}

const length = idx;

for (let i = 0; i < recipients.length; i++) {
const recipient = recipients[i];
const split = splits[i];

const id = entityUUID(event, [recipient.toHexString()]);
let commission = Commission.load(id);
let commission: Commission | null = null;
for (let j = 0; j < length; j++) {
const com = Commission.load(entityUUID(event, [j.toString()]));
if (com!.recipient.equals(recipient)) {
commission = com;
break;
}
}

if (!commission) {
commission = new Commission(id);
commission = new Commission(entityUUID(event, [idx.toString()]));
commission.vPoolIntegration = erc20!.id;
commission.withdrawnCommission = BigInt.fromI32(0);
commission.withdrawnCommission = BigInt.zero();
commission.createdAt = ts;
commission.createdAtBlock = blockId;
++idx;
}

commission.commission = split;
Expand All @@ -316,16 +339,26 @@ export function handleCommissionWithdrawn(event: CommissionWithdrawn): void {
const ts = event.block.timestamp;
const blockId = event.block.number;

const id = entityUUID(event, [event.params.withdrawer.toHexString()]);
const commission = Commission.load(id);
commission!.withdrawnCommission = commission!.withdrawnCommission.plus(event.params.amountWithdrawn);
commission!.editedAt = ts;
commission!.editedAtBlock = blockId;
commission!.save();

erc20!.editedAt = ts;
erc20!.editedAtBlock = blockId;
erc20!.save();
let idx = 0;
while (true) {
const commission = Commission.load(entityUUID(event, [idx.toString()]));
if (commission != null) {
if (commission.recipient.equals(event.params.withdrawer)) {
commission.withdrawnCommission = commission.withdrawnCommission.plus(event.params.amountWithdrawn);
commission.editedAt = ts;
commission.editedAtBlock = blockId;
commission.save();

erc20!.editedAt = ts;
erc20!.editedAtBlock = blockId;
erc20!.save();
break;
}
++idx;
} else {
break;
}
}
}

export function handleStake_1_0_0_rc4(event: Stake_1_0_0_rc4): void {
Expand Down

0 comments on commit 09eec53

Please sign in to comment.