-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from hackdays-io/feature/balance-graph
Add fraction token balance in graph endpoint
- Loading branch information
Showing
12 changed files
with
293 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.