Skip to content

Commit

Permalink
chore: add get event params (#48)
Browse files Browse the repository at this point in the history
Added `GetEventParams` type and its `defaultEventParams` const.
_fromBlock_ is set to "latest" by default, we need to fetch all the
events so set it to "earliest".
  • Loading branch information
palace22 authored Jun 10, 2024
1 parent 30a1188 commit 99a6743
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/chains/evm/common/constants/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { EventParams } from "../types/contract.js";

export const defaultEventParams: EventParams = {
fromBlock: "earliest",
toBlock: "latest",
strict: true,
};
13 changes: 12 additions & 1 deletion src/chains/evm/common/types/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import type { Abi, GetContractReturnType, Client } from "viem";
import type {
Abi,
GetContractReturnType,
Client,
GetContractEventsParameters,
} from "viem";

type OmitWrite<T> = Omit<T, "write">;
export type GetReadContractReturnType<TAbi extends Abi> = OmitWrite<
GetContractReturnType<TAbi, Client>
>;

export type EventParams = Pick<
GetContractEventsParameters,
"strict" | "fromBlock" | "toBlock"
>;
export type GetEventParams = { eventParams: EventParams };
2 changes: 2 additions & 0 deletions src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
toUnderlyingAmount,
} from "../../../../common/utils/formulae.js";
import { compoundEverySecond } from "../../../../common/utils/math-lib.js";
import { defaultEventParams } from "../../common/constants/contract.js";
import {
buildMessageParams,
buildMessagePayload,
Expand Down Expand Up @@ -256,6 +257,7 @@ export async function getUserLoanIds(
loanManager,
accountId,
loanTypeId: loanTypeIdFilter,
eventParams: defaultEventParams,
});
}

Expand Down
9 changes: 6 additions & 3 deletions src/chains/evm/hub/types/loan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { AccountId, LoanId } from "../../../../common/types/lending.js";
import type { LoanType } from "../../../../common/types/module.js";
import type { FolksTokenId } from "../../../../common/types/token.js";
import type { GetReadContractReturnType } from "../../common/types/contract.js";
import type {
GetEventParams,
GetReadContractReturnType,
} from "../../common/types/contract.js";
import type { LoanManagerAbi } from "../constants/abi/loan-manager-abi.js";
import type { Dnum } from "dnum";

Expand Down Expand Up @@ -83,13 +86,13 @@ export type UserLoanInfo = {
liquidationMargin: Dnum;
};

export type CreateUserLoanEventParams = {
export type CreateUserLoanEventParams = GetEventParams & {
loanManager: GetReadContractReturnType<typeof LoanManagerAbi>;
accountId: AccountId;
loanTypeId?: LoanType;
};

export type DeleteUserLoanEventParams = {
export type DeleteUserLoanEventParams = GetEventParams & {
loanManager: GetReadContractReturnType<typeof LoanManagerAbi>;
accountId: AccountId;
};
8 changes: 4 additions & 4 deletions src/chains/evm/hub/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import type {
export async function fetchCreateUserLoanEvents(
params: CreateUserLoanEventParams,
) {
const { loanManager, accountId, loanTypeId } = params;
const { loanManager, accountId, loanTypeId, eventParams } = params;
const logs = await loanManager.getEvents.CreateUserLoan(
{ accountId },
{ strict: true },
eventParams,
);
return logs
.filter(
Expand All @@ -27,10 +27,10 @@ export async function fetchCreateUserLoanEvents(
export async function fetchDeleteUserLoanEvents(
params: DeleteUserLoanEventParams,
) {
const { loanManager, accountId } = params;
const { loanManager, accountId, eventParams } = params;
const logs = await loanManager.getEvents.DeleteUserLoan(
{ accountId },
{ strict: true },
eventParams,
);
return logs.map((log) => ({
blockNumber: log.blockNumber,
Expand Down

0 comments on commit 99a6743

Please sign in to comment.