Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crc breakdown displayed wrong #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .cache/cache.db
Binary file not shown.
100,970 changes: 50,485 additions & 50,485 deletions config.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"typescript": "5.2.2"
},
"dependencies": {
"envio": "2.7.0",
"envio": "2.7.3",
"viem": "2.21.0",
"multihashes": "^4.0.3",
"sqlite3": "^5.1.7"
Expand Down
45 changes: 24 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 30 additions & 5 deletions src/common/handleTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@ import {
WrappedERC20_Transfer_eventArgs,
} from "generated";
import { incrementStats } from "../incrementStats";
import { TransferType_t } from "generated/src/db/Enums.gen";
import {
AvatarType_t,
TokenType_t,
TransferType_t,
} from "generated/src/db/Enums.gen";
import { updateAvatarBalance } from "./updateAvatarBalance";
import { getAddress, toHex } from "viem";

const mapAvatarTypeToTokenType = (avatarType: AvatarType_t): TokenType_t => {
switch (avatarType) {
case "Signup":
case "OrganizationSignup":
return "Signup";
case "RegisterGroup":
return "RegisterGroup";
default:
return "RegisterHuman";
}
};

export const handleTransfer = async ({
event,
Expand All @@ -19,6 +36,7 @@ export const handleTransfer = async ({
values,
tokens,
transferType,
avatarType,
version,
}: {
event: eventLog<
Expand All @@ -33,11 +51,20 @@ export const handleTransfer = async ({
values: bigint[];
tokens: string[];
transferType: TransferType_t;
avatarType: AvatarType_t;
version: number;
}) => {
for (let i = 0; i < tokens.length; i++) {
const token = await context.Token.get(tokens[i]);
if (!token) {
let tokenOwner_id: string;
try {
tokenOwner_id = tokens[i].startsWith("0x")
? event.params.to
: getAddress(toHex(BigInt(tokens[i])));
} catch (_) {
tokenOwner_id = event.params.to;
}
context.Token.set({
id: tokens[i],
blockNumber: event.block.number,
Expand All @@ -46,10 +73,8 @@ export const handleTransfer = async ({
logIndex: event.logIndex,
transactionHash: event.transaction.hash,
version,
// TODO: fix
tokenType: "RegisterHuman",
// TODO: fix
tokenOwner_id: event.params.to,
tokenType: mapAvatarTypeToTokenType(avatarType),
tokenOwner_id,
});
}

Expand Down
44 changes: 24 additions & 20 deletions src/event_handlers/hubV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ PersonalCRC.Transfer.handler(
values: [event.params.amount],
tokens: [event.srcAddress],
transferType: "Transfer",
avatarType: "Signup",
version: 1,
})
);
Expand All @@ -120,6 +121,7 @@ Hub.HubTransfer.handler(
values: [event.params.amount],
tokens: [event.srcAddress],
transferType: "HubTransfer",
avatarType: "Unknown",
version: 1,
})
);
Expand All @@ -131,20 +133,20 @@ Hub.HubTransfer.handler(
Hub.Trust.handler(async ({ event, context }) => {
const trustId = `${event.params.user}${event.params.canSendTo}`;
const oppositeTrustId = `${event.params.canSendTo}${event.params.user}`;
const trustRelation = await context.TrustRelation.get(trustId);
const oppositeTrustRelation = await context.TrustRelation.get(
oppositeTrustId
);
if (event.params.limit === 0n) {
// this is untrust
const trustRelation = await context.TrustRelation.get(trustId);
if (trustRelation) {
if (trustRelation && trustRelation.version === 1) {
context.TrustRelation.set({
...trustRelation,
expiryTime: 0n,
limit: 0n,
});
}
if (oppositeTrustRelation) {
if (oppositeTrustRelation && oppositeTrustRelation.version === 1) {
context.TrustRelation.set({
...oppositeTrustRelation,
isMutual: false,
Expand All @@ -153,26 +155,28 @@ Hub.Trust.handler(async ({ event, context }) => {
return;
}
const isMutual = oppositeTrustRelation !== undefined;
if (isMutual) {
if (isMutual && oppositeTrustRelation.version === 1) {
context.TrustRelation.set({
...oppositeTrustRelation,
isMutual: true,
});
}
const entity: TrustRelation = {
id: trustId,
blockNumber: event.block.number,
timestamp: event.block.timestamp,
transactionIndex: event.transaction.transactionIndex,
logIndex: event.logIndex,
version: 1,
trustee_id: event.params.canSendTo,
truster_id: event.params.user,
expiryTime: maxUint256,
limit: event.params.limit,
isMutual,
};

context.TrustRelation.set(entity);
await incrementStats(context, "trusts");
if (!trustRelation) {
const entity: TrustRelation = {
id: trustId,
blockNumber: event.block.number,
timestamp: event.block.timestamp,
transactionIndex: event.transaction.transactionIndex,
logIndex: event.logIndex,
version: 1,
trustee_id: event.params.canSendTo,
truster_id: event.params.user,
expiryTime: maxUint256,
limit: event.params.limit,
isMutual,
};

context.TrustRelation.set(entity);
await incrementStats(context, "trusts");
}
});
18 changes: 14 additions & 4 deletions src/event_handlers/hubV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ HubV2.RegisterHuman.handler(async ({ event, context }) => {
avatarType: "RegisterHuman",
version: 2,
tokenId: bytesToBigInt(toBytes(event.params.avatar)).toString(),
trustedByN: 0,
});
} else {
const avatarEntity: Avatar = {
Expand Down Expand Up @@ -312,18 +313,26 @@ HubV2.StreamCompleted.handlerWithLoader({
},
});

HubV2.TransferSingle.handler(
async ({ event, context }) =>
HubV2.TransferSingle.handlerWithLoader({
loader: async ({ event, context }) => {
let avatar = await context.Avatar.get(event.params.to);

return { avatar };
},
handler: async ({ event, context, loaderReturn }) => {
const { avatar } = loaderReturn;
await handleTransfer({
event,
context,
operator: event.params.operator,
values: [event.params.value],
tokens: [event.params.id.toString()],
transferType: "TransferSingle",
avatarType: avatar?.avatarType ?? "Unknown",
version: 2,
})
);
});
},
});

HubV2.TransferBatch.handler(
async ({ event, context }) =>
Expand All @@ -334,6 +343,7 @@ HubV2.TransferBatch.handler(
values: event.params.values,
tokens: event.params.ids.map((id) => id.toString()),
transferType: "TransferSingle",
avatarType: "Unknown",
version: 2,
})
);
Expand Down
14 changes: 11 additions & 3 deletions src/event_handlers/wrappedERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ import {
return `${avatarId}-${tokenId}`;
}

WrappedERC20.Transfer.handler(
async ({ event, context }) =>
WrappedERC20.Transfer.handlerWithLoader({
loader: async ({ event, context }) => {
let avatar = await context.Avatar.get(event.params.to);

return { avatar };
},
handler: async ({ event, context, loaderReturn }) => {
const { avatar } = loaderReturn;
await handleTransfer({
event,
context,
operator: undefined,
values: [event.params.value],
tokens: [event.srcAddress],
transferType: "Erc20WrapperTransfer",
avatarType: avatar?.avatarType ?? "Unknown",
version: 2,
})
);
}
});

async function updateDemurragedAvatarBalance(
event: eventLog<
Expand Down