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

Add fraction token balance in graph endpoint #286

Merged
merged 1 commit into from
Feb 1, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ contract HatsTimeFrameModule is HatsModule, Ownable, IHatsTimeFrameModule {
_setWoreTime(wearer, hatId, time);
isActive[hatId][wearer] = true;
HATS().mintHat(hatId, wearer);

emit HatMinted(hatId, wearer, time == 0 ? block.timestamp : time);
}

/**
Expand All @@ -99,6 +101,8 @@ contract HatsTimeFrameModule is HatsModule, Ownable, IHatsTimeFrameModule {
totalActiveTime[hatId][wearer] +=
block.timestamp -
woreTime[hatId][wearer];

emit HatDeactivated(hatId, wearer);
}

/**
Expand All @@ -115,6 +119,8 @@ contract HatsTimeFrameModule is HatsModule, Ownable, IHatsTimeFrameModule {
);
isActive[hatId][wearer] = true;
woreTime[hatId][wearer] = block.timestamp;

emit HatReactivated(hatId, wearer);
}

function renounce(uint256 hatId, address wearer) external {
Expand All @@ -129,6 +135,8 @@ contract HatsTimeFrameModule is HatsModule, Ownable, IHatsTimeFrameModule {
isActive[hatId][wearer] = false;
deactivatedTime[hatId][wearer] = 0;
totalActiveTime[hatId][wearer] = 0;

emit HatRenounced(hatId, wearer);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ interface IHatsTimeFrameModule {
* @notice Emitted when hat creation authority is revoked
*/
event OperationAuthorityRevoked(address indexed authority);

/**
* @notice Emitted when a hat is minted
*/
event HatMinted(
uint256 indexed hatId,
address indexed wearer,
uint256 timestamp
);

/**
* @notice Emitted when a hat is deactivated
*/
event HatDeactivated(uint256 indexed hatId, address indexed wearer);

/**
* @notice Emitted when a hat is Reactivated
*/
event HatReactivated(uint256 indexed hatId, address indexed wearer);

/**
* @notice Emitted when a hat is renounced
*/
event HatRenounced(uint256 indexed hatId, address indexed wearer);
}
82 changes: 82 additions & 0 deletions pkgs/subgraph/abis/HatsTimeFrameModule.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,88 @@
"name": "OwnableUnauthorizedAccount",
"type": "error"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "hatId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "wearer",
"type": "address"
}
],
"name": "HatDeactivated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "hatId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "wearer",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "timestamp",
"type": "uint256"
}
],
"name": "HatMinted",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "hatId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "wearer",
"type": "address"
}
],
"name": "HatReactivated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "hatId",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "wearer",
"type": "address"
}
],
"name": "HatRenounced",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
9 changes: 8 additions & 1 deletion pkgs/subgraph/config/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{
"address": "0x3E70d10aCdcC14B6C31DA26DcC195a6EDf1C2c16",
"file": "BigBang",
"mappingFile": "bigbangMapping",
"entities": [{ "name": "Executed" }],
"handlers": [
{
Expand All @@ -16,6 +17,7 @@
{
"address": "0x54889278bf4F16ACAa3CC1402C987A6C42a5308B",
"file": "FractionToken",
"mappingFile": "fractionTokenMapping",
"entities": [{ "name": "InitialMint" }, { "name": "TransferSignle" }],
"handlers": [
{
Expand Down Expand Up @@ -51,7 +53,8 @@
"file": "HatsTimeFrameModule",
"entities": [
{ "name": "OperationAuthorityGranted" },
{ "name": "OperationAuthorityRevoked" }
{ "name": "OperationAuthorityRevoked" },
{ "name": "HatMinted" }
],
"handlers": [
{
Expand All @@ -61,6 +64,10 @@
{
"event": "OperationAuthorityRevoked(indexed address)",
"handler": "handleOperationAuthorityRevoked"
},
{
"event": "HatMinted(indexed uint256,indexed address,uint256)",
"handler": "handleHatMinted"
}
]
}
Expand Down
3 changes: 1 addition & 2 deletions pkgs/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"dependencies": {
"@graphprotocol/graph-cli": "0.51.0",
"@graphprotocol/graph-ts": "0.31.0",
"@hatsprotocol/sdk-v1-core": "^0.10.0",
"viem": "^2.21.55"
"@hatsprotocol/sdk-v1-core": "^0.10.0"
},
"devDependencies": {
"@types/node": "^22.10.2",
Expand Down
17 changes: 14 additions & 3 deletions pkgs/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ type TransferFractionToken @entity {
to: String!
tokenId: BigInt!
amount: BigInt!
workspaceId: ID!
hatId: BigInt!
wearer: String!
workspaceId: ID
hatId: BigInt
wearer: String
blockTimestamp: BigInt!
blockNumber: BigInt!
}

type BalanceOfFractionToken @entity {
id: ID!
owner: String!
tokenId: BigInt!
balance: BigInt!
workspaceId: ID
hatId: BigInt
wearer: String
updatedAt: BigInt!
}
45 changes: 45 additions & 0 deletions pkgs/subgraph/src/bigbangMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Executed } from "../generated/BigBang/BigBang";
import {
HatsHatCreatorModule,
HatsTimeFrameModule,
Workspace,
} from "../generated/schema";
import {
HatsHatCreatorModule as HatsHatCreatorModuleTemplate,
HatsTimeFrameModule as HatsTimeFrameModuleTemplate,
} from "../generated/templates";
import { hatIdToTreeId } from "./helper/hat";

export function handleExecuted(ev: Executed): void {
const treeId = hatIdToTreeId(ev.params.topHatId.toHexString());
let workspace = new Workspace(treeId);

workspace.topHatId = ev.params.topHatId;
workspace.creator = ev.params.creator.toHex();
workspace.topHatId = ev.params.topHatId;
workspace.hatterHatId = ev.params.hatterHatId;
workspace.hatsTimeFrameModule = ev.params.hatsTimeFrameModule.toHex();
workspace.hatsHatCreatorModule = ev.params.hatsHatCreatorModule.toHex();
workspace.splitCreator = ev.params.splitCreator.toHex();
workspace.blockTimestamp = ev.block.timestamp;
workspace.blockNumber = ev.block.number;

workspace.save();

// Create new index from template for HatsModules
const newHatsHatCreatorModule = new HatsHatCreatorModule(
ev.params.hatsHatCreatorModule.toHex(),
);
newHatsHatCreatorModule.workspaceId = treeId;
newHatsHatCreatorModule.save();

HatsHatCreatorModuleTemplate.create(ev.params.hatsHatCreatorModule);

const newHatsTimeFrameModule = new HatsTimeFrameModule(
ev.params.hatsTimeFrameModule.toHex(),
);
newHatsTimeFrameModule.workspaceId = treeId;
newHatsTimeFrameModule.save();

HatsTimeFrameModuleTemplate.create(ev.params.hatsTimeFrameModule);
}
98 changes: 98 additions & 0 deletions pkgs/subgraph/src/fractionTokenMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Address, BigInt as GraphBigInt } from "@graphprotocol/graph-ts";
import {
InitialMint,
TransferSingle,
} from "../generated/FractionToken/FractionToken";
import {
BalanceOfFractionToken,
InitializedFractionToken,
TransferFractionToken,
} from "../generated/schema";
import { hatIdToTreeId } from "./helper/hat";

export function handleInitialMint(ev: InitialMint): void {
const initializedFractionToken = new InitializedFractionToken(
ev.params.tokenId.toHex(),
);
const treeId = hatIdToTreeId(ev.params.hatId.toHexString());
initializedFractionToken.hatId = ev.params.hatId;
initializedFractionToken.wearer = ev.params.wearer.toHex();
initializedFractionToken.workspaceId = treeId;
initializedFractionToken.blockTimestamp = ev.block.timestamp;
initializedFractionToken.blockNumber = ev.block.number;

initializedFractionToken.save();
}

export function handleTransferSingle(ev: TransferSingle): void {
const id = `${ev.transaction.hash.toHex()}${ev.params.id.toHex()}${ev.params.from.toHex()}${ev.params.to.toHex()}`;
const initializedFractionToken = InitializedFractionToken.load(
ev.params.id.toHex(),
);

// Save Transfer History
const transfer = new TransferFractionToken(id);
transfer.from = ev.params.from.toHex();
transfer.to = ev.params.to.toHex();
transfer.tokenId = ev.params.id;
transfer.amount = ev.params.value;
transfer.workspaceId = initializedFractionToken
? initializedFractionToken.workspaceId
: "";
transfer.hatId = initializedFractionToken
? initializedFractionToken.hatId
: GraphBigInt.fromString("0");
transfer.wearer = initializedFractionToken
? initializedFractionToken.wearer
: "";
transfer.blockNumber = ev.block.number;
transfer.blockTimestamp = ev.block.timestamp;

// Update Balance of Tokens
updateBalance(
ev.params.id,
ev.params.from,
ev.params.value.neg(),
initializedFractionToken,
ev.block.timestamp,
);
updateBalance(
ev.params.id,
ev.params.to,
ev.params.value,
initializedFractionToken,
ev.block.timestamp,
);

transfer.save();
}

function updateBalance(
tokenId: GraphBigInt,
account: Address,
amount: GraphBigInt,
initializedFractionToken: InitializedFractionToken | null,
timestamp: GraphBigInt,
): void {
let balance = BalanceOfFractionToken.load(`${tokenId}${account.toHex()}`);

if (balance) {
balance.balance = balance.balance.plus(amount);
} else if (account.toHex() != "0x0000000000000000000000000000000000000000") {
balance = new BalanceOfFractionToken(`${tokenId}${account.toHex()}`);
balance.owner = account.toHex();
balance.tokenId = tokenId;
balance.balance = amount;
balance.updatedAt = timestamp;
}

if (balance && initializedFractionToken) {
balance.workspaceId = initializedFractionToken.workspaceId;
balance.hatId = initializedFractionToken.hatId;
balance.wearer = initializedFractionToken.wearer;
}

if (balance) {
balance.save();
}
}
8 changes: 8 additions & 0 deletions pkgs/subgraph/src/helper/hat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function hatIdToTreeId(hatId: string): string {
const id = Number.parseInt(
`0x${hatId.slice(2).padStart(64, "0").substring(0, 8)}`,
)
.toString()
.split(".")[0];
return id;
}
Loading
Loading