Skip to content

Commit

Permalink
feat: refactor token wrapping to erc20 (#4)
Browse files Browse the repository at this point in the history
* feat: refactor wrapped token balance track

* feat: split code

* feat: standard treasury transfer types

* feat: custom treasury
  • Loading branch information
vibern0 authored Nov 4, 2024
1 parent a8209cc commit f8b6cc8
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 416 deletions.
190 changes: 100 additions & 90 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,108 @@ field_selection:
- hash
- transactionIndex
contracts:
# - name: Hub **COMMENTED OUT FOR NOW TO SYNC FASTER***
# handler: src/event_handlers/hubV1.ts
# events:
# - event: HubTransfer(address indexed from, address indexed to, uint256 amount)
# - event: OrganizationSignup(address indexed organization)
# - event: Signup(address indexed user, address token)
# - event: Trust(address indexed canSendTo, address indexed user, uint256 limit)
# - name: PersonalCRC
# abi_file_path: abis/PersonalCRC.json
# handler: src/event_handlers/hubV1.ts
# events:
# - event: Transfer(address indexed from, address indexed to, uint256 amount)
- name: Hub
handler: src/event_handlers/hubV1.ts
events:
- event: HubTransfer(address indexed from, address indexed to, uint256 amount)
- event: OrganizationSignup(address indexed organization)
- event: Signup(address indexed user, address token)
- event: Trust(address indexed canSendTo, address indexed user, uint256 limit)
- name: PersonalCRC
abi_file_path: abis/PersonalCRC.json
handler: src/event_handlers/hubV1.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 amount)
- name: HubV2
handler: src/event_handlers/hubV2.ts
events:
- event: RegisterHuman(address indexed avatar, address indexed inviter)
- event: RegisterOrganization(address indexed organization, string name)
- event: RegisterGroup(address indexed group, address indexed mint, address indexed treasury, string name, string symbol)
- event: Trust(address indexed truster, address indexed trustee, uint256 expiryTime)
- event: Stopped(address indexed avatar)
- event: StreamCompleted(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] amounts)
- event: PersonalMint(address indexed human, uint256 amount, uint256 period, uint256 endPeriod)
- event: TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)
- event: TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)
- event: DiscountCost(address indexed account, uint256 indexed id, uint256 discountCost)
- name: ERC20Lift
handler: src/event_handlers/hubV2.ts
events:
- event: ProxyCreation(address proxy, address masterCopy)
- event: ERC20WrapperDeployed(address indexed avatar, address indexed erc20Wrapper, uint8 circlesType)
- name: StandardTreasury
handler: src/event_handlers/treasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
- name: CustomTreasury
handler: src/event_handlers/treasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
- name: NameRegistry
handler: src/event_handlers/hubV2.ts
events:
- event: UpdateMetadataDigest(address indexed avatar, bytes32 metadataDigest)
- name: WrappedERC20
handler: src/event_handlers/wrappedERC20.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 value)
- event: DepositDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: WithdrawDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: DepositInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- event: WithdrawInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- name: SafeAccount
handler: src/event_handlers/hubV2.ts
events:
- event: ExecutionSuccess(bytes32 txHash, uint256 payment)

networks:
# - id: 10200
# start_block: 2211707
# contracts:
# - name: Hub
# address:
# - 0xdbF22D4e8962Db3b2F1d9Ff55be728A887e47710
# - name: PersonalCRC
# - name: HubV2
# address:
# - 0xb80feeDfEce647dDc709777D5094fACD157BA001
# - name: ERC20Lift
# address:
# - 0xBD2D6Fbb6A702B04B750Bc9942fBaAE81187355E
# - name: StandardTreasury
# address:
# - 0xC06ADED7950429FdF2023e139B076f6BaFf9Fe1C
# - name: NameRegistry
# address:
# - 0x24b3fDCdD9fef844fB3094ef43c0A6Ac23a6dF9E
# - name: WrappedERC20
- id: 100
# start_block: 12529458
start_block: 36486014
contracts:
- name: HubV2
handler: src/event_handlers/hubV2.ts
events:
- event: RegisterHuman(address indexed avatar, address indexed inviter)
- event: RegisterOrganization(address indexed organization, string name)
- event: RegisterGroup(address indexed group, address indexed mint, address indexed treasury, string name, string symbol)
- event: Trust(address indexed truster, address indexed trustee, uint256 expiryTime)
- event: Stopped(address indexed avatar)
- event: StreamCompleted(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] amounts)
- event: PersonalMint(address indexed human, uint256 amount, uint256 period, uint256 endPeriod)
- event: TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)
- event: TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)
- event: DiscountCost(address indexed account, uint256 indexed id, uint256 discountCost)
address:
- 0xc12C1E50ABB450d6205Ea2C3Fa861b3B834d13e8
- name: ERC20Lift
handler: src/event_handlers/hubV2.ts
events:
- event: ProxyCreation(address proxy, address masterCopy)
- event: ERC20WrapperDeployed(address indexed avatar, address indexed erc20Wrapper, uint8 circlesType)
address:
- 0x5F99a795dD2743C36D63511f0D4bc667e6d3cDB5
- name: StandardTreasury
handler: src/event_handlers/standardTreasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
address:
- 0x08F90aB73A515308f03A718257ff9887ED330C6e
- name: NameRegistry
handler: src/event_handlers/hubV2.ts
events:
- event: UpdateMetadataDigest(address indexed avatar, bytes32 metadataDigest)
- name: WrapperERC20Personal
handler: src/event_handlers/hubV2.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 value)
- event: DepositDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: WithdrawDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: DepositInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- event: WithdrawInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
address:
- 0xA27566fD89162cC3D40Cb59c87AAaA49B85F3474
- name: WrappedERC20
- name: SafeAccount
handler: src/event_handlers/hubV2.ts
events:
- event: ExecutionSuccess(bytes32 txHash, uint256 payment)

networks:
# - id: 10200
# start_block: 2211707
# contracts:
# - name: Hub
# address:
# - 0xdbF22D4e8962Db3b2F1d9Ff55be728A887e47710
# - name: PersonalCRC
# - name: HubV2
# address:
# - 0xb80feeDfEce647dDc709777D5094fACD157BA001
# - name: ERC20Lift
# address:
# - 0xBD2D6Fbb6A702B04B750Bc9942fBaAE81187355E
# - name: StandardTreasury
# address:
# - 0xC06ADED7950429FdF2023e139B076f6BaFf9Fe1C
# - name: NameRegistry
# address:
# - 0x24b3fDCdD9fef844fB3094ef43c0A6Ac23a6dF9E
# - name: WrapperERC20Personal
- id: 100
start_block: 12529458
# start_block: 36486014
contracts:
- name: HubV2
address:
- 0xc12C1E50ABB450d6205Ea2C3Fa861b3B834d13e8
- name: ERC20Lift
address:
- 0x5F99a795dD2743C36D63511f0D4bc667e6d3cDB5
- name: StandardTreasury
address:
- 0x08F90aB73A515308f03A718257ff9887ED330C6e
- name: NameRegistry
address:
- 0xA27566fD89162cC3D40Cb59c87AAaA49B85F3474
- name: WrapperERC20Personal
- name: SafeAccount
- name: CustomTreasury
7 changes: 5 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ enum TransferType {
StreamCompleted
HubTransfer
Transfer
GroupMintSingle
GroupMintBatch
GroupRedeem
GroupRedeemCollateralReturn
GroupRedeemCollateralBurn
}

type Transfer {
Expand Down Expand Up @@ -74,8 +79,6 @@ type AvatarBalance {
token: Token!
avatar: Avatar!
balance: BigInt!
version: Int!
isWrapped: Boolean!
inflationaryValue: BigInt
lastCalculated: Int
}
Expand Down
26 changes: 12 additions & 14 deletions src/common/handleTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Transfer,
HubV2_TransferSingle_eventArgs,
HubV2_TransferBatch_eventArgs,
WrapperERC20Personal_Transfer_eventArgs,
WrappedERC20_Transfer_eventArgs,
} from "generated";
import { incrementStats } from "../incrementStats";
import { TransferType_t } from "generated/src/db/Enums.gen";
Expand All @@ -26,7 +26,7 @@ export const handleTransfer = async ({
| PersonalCRC_Transfer_eventArgs */
| HubV2_TransferSingle_eventArgs
| HubV2_TransferBatch_eventArgs
| WrapperERC20Personal_Transfer_eventArgs
| WrappedERC20_Transfer_eventArgs
>;
context: handlerContext;
operator: string | undefined;
Expand Down Expand Up @@ -76,23 +76,21 @@ export const handleTransfer = async ({

await updateAvatarBalance(
context,
event.params.to,
tokens[i],
values[i],
version,
isWrapped,
undefined,
undefined
event.block.timestamp,
{
id: event.params.to,
token_id: tokens[i],
}
);
await updateAvatarBalance(
context,
event.params.from,
tokens[i],
-values[i],
version,
isWrapped,
undefined,
undefined
event.block.timestamp,
{
id: event.params.from,
token_id: tokens[i],
},
);
await incrementStats(context, "transfers");
}
Expand Down
31 changes: 14 additions & 17 deletions src/common/updateAvatarBalance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handlerContext } from "generated";
import { AvatarBalance, handlerContext } from "generated";
import { zeroAddress } from "viem";

function makeAvatarBalanceEntityId(avatarId: string, tokenId: string) {
Expand All @@ -7,30 +7,29 @@ function makeAvatarBalanceEntityId(avatarId: string, tokenId: string) {

export const updateAvatarBalance = async (
context: handlerContext,
avatarId: string,
tokenId: string,
amount: bigint,
version: number,
isWrapped: boolean,
inflationaryValue: bigint | undefined,
lastCalculated: number | undefined
blockTimestamp: number,
options: Partial<AvatarBalance>
) => {
if (avatarId === zeroAddress) {
const { id: avatarId, token_id: tokenId } = options;
if (avatarId === zeroAddress || !avatarId || !tokenId) {
return;
}
const balanceId = makeAvatarBalanceEntityId(avatarId, tokenId);
const [avatarBalance, avatar] = await Promise.all([
const [avatarBalance, avatar, token] = await Promise.all([
context.AvatarBalance.get(balanceId),
context.Avatar.get(avatarId),
context.Token.get(tokenId),
]);
if (avatarBalance) {
let updated = {
...avatarBalance,
balance: avatarBalance.balance + amount,
lastCalculated: blockTimestamp,
};
if (inflationaryValue !== undefined) {
updated.inflationaryValue =
(avatarBalance.inflationaryValue || 0n) + inflationaryValue;
if (token?.tokenType === "WrappedStaticToken") {
updated.inflationaryValue = avatarBalance.inflationaryValue! + amount;
} else {
updated.balance = avatarBalance.balance + amount;
}
context.AvatarBalance.set(updated);
} else {
Expand All @@ -39,10 +38,8 @@ export const updateAvatarBalance = async (
avatar_id: avatarId,
token_id: tokenId,
balance: amount,
version,
isWrapped,
inflationaryValue,
lastCalculated,
inflationaryValue: 0n,
lastCalculated: blockTimestamp,
});
}
if (avatar) {
Expand Down
Loading

0 comments on commit f8b6cc8

Please sign in to comment.