Skip to content

Commit

Permalink
fix loans count (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Vieira authored Jun 1, 2023
1 parent 73fac4d commit 9179464
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export class Borrower extends Entity {
set loans(value: Array<string>) {
this.set("loans", Value.fromStringArray(value));
}

get loansCount(): i32 {
let value = this.get("loansCount");
return value!.toI32();
}

set loansCount(value: i32) {
this.set("loansCount", Value.fromI32(value));
}
}

export class Loan extends Entity {
Expand Down
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Borrower @entity {
id: ID!
# borrower loans
loans: [Loan!]! @derivedFrom(field: "borrower")
# number of loans
loansCount: Int!
}

"""
Expand Down
4 changes: 3 additions & 1 deletion src/mappings/micro-credit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export function handleLoanAdded(event: LoanAdded): void {
if (!borrower) {
// create borrower entity
borrower = new Borrower(event.params.userAddress.toHex());
borrower.loansCount = 0;
}

loan.borrower = event.params.userAddress.toHex();
Expand Down Expand Up @@ -216,7 +217,8 @@ export function handleLoanClaimed(event: LoanClaimed): void {
// update loan entity data
loan.claimed = event.block.timestamp.toI32();

if (borrower.loans.length === 1) {
borrower.loansCount += 1;
if (borrower.loansCount === 1) {
loanManager.borrowers += 1;
}
loanManager.loans += 1;
Expand Down

0 comments on commit 9179464

Please sign in to comment.