diff --git a/subgraphs/chamber/matchstick.yaml b/subgraphs/chamber/matchstick.yaml new file mode 100644 index 0000000..8851578 --- /dev/null +++ b/subgraphs/chamber/matchstick.yaml @@ -0,0 +1 @@ +libsFolder: ../../node_modules diff --git a/subgraphs/chamber/networks.json b/subgraphs/chamber/networks.json new file mode 100644 index 0000000..81a1552 --- /dev/null +++ b/subgraphs/chamber/networks.json @@ -0,0 +1,8 @@ +{ + "sepolia": { + "Registry": { + "address": "0x62996E7041C0A434B8d0268EF7F6BBCf2d668dA7", + "startBlock": 5281759 + } + } +} \ No newline at end of file diff --git a/subgraphs/chamber/package.json b/subgraphs/chamber/package.json new file mode 100644 index 0000000..c39bbf5 --- /dev/null +++ b/subgraphs/chamber/package.json @@ -0,0 +1,18 @@ +{ + "name": "chamberj", + "license": "MIT", + "scripts": { + "codegen": "graph codegen", + "build": "graph build", + "deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ chamberj", + "create-local": "graph create --node http://localhost:8020/ chamberj", + "remove-local": "graph remove --node http://localhost:8020/ chamberj", + "deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 chamberj", + "test": "graph test" + }, + "dependencies": { + "@graphprotocol/graph-cli": "0.67.4", + "@graphprotocol/graph-ts": "0.32.0" + }, + "devDependencies": { "matchstick-as": "0.5.0" } +} diff --git a/subgraphs/chamber/schema.graphql b/subgraphs/chamber/schema.graphql new file mode 100644 index 0000000..d8bdd0a --- /dev/null +++ b/subgraphs/chamber/schema.graphql @@ -0,0 +1,115 @@ +type ChamberDeployed @entity(immutable: true) { + id: ID! + chamber: Bytes! # address + serial: BigInt! # uint256 + deployer: Bytes! # address + memberToken: Bytes! # address + govToken: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type OwnershipTransferred @entity(immutable: true) { + id: ID! + previousOwner: Bytes! # address + newOwner: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + + +type ChangedGuard @entity(immutable: true) { + id: ID! + guard: Bytes! # address + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type Demoted @entity(immutable: true) { + id: ID! + demoter: Bytes! # address + amt: BigInt! # uint256 + tokenId: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type Initialized @entity(immutable: true) { + id: ID! + version: Int! # uint8 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type Promoted @entity(immutable: true) { + id: ID! + promoter: Bytes! # address + amt: BigInt! # uint256 + tokenId: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type ProposalApproved @entity(immutable: true) { + id: ID! + proposalId: BigInt! # uint256 + tokenId: BigInt! # uint256 + approvals: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type ProposalCreated @entity(immutable: true) { + id: ID! + proposalId: BigInt! # uint256 + target: [Bytes!]! # address[] + value: [BigInt!]! # uint256[] + data: [Bytes!]! # bytes[] + voters: [BigInt!]! # uint256[5] + nonce: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type ProposalExecuted @entity(immutable: true) { + id: ID! + proposalId: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type ReceivedEther @entity(immutable: true) { + id: ID! + sender: Bytes! # address + value: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type ReceivedFallback @entity(immutable: true) { + id: ID! + sender: Bytes! # address + value: BigInt! # uint256 + contractAddress: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} diff --git a/subgraphs/chamber/schema.grqphql b/subgraphs/chamber/schema.grqphql deleted file mode 100644 index e69de29..0000000 diff --git a/subgraphs/chamber/src/chamber.ts b/subgraphs/chamber/src/chamber.ts index e69de29..f71c45a 100644 --- a/subgraphs/chamber/src/chamber.ts +++ b/subgraphs/chamber/src/chamber.ts @@ -0,0 +1,157 @@ +import { createId } from "../../../helpers/utils" +import { Bytes } from "@graphprotocol/graph-ts" + +import { + ChangedGuard as ChangedGuardEvent, + Demoted as DemotedEvent, + Initialized as InitializedEvent, + Promoted as PromotedEvent, + ProposalApproved as ProposalApprovedEvent, + ProposalCreated as ProposalCreatedEvent, + ProposalExecuted as ProposalExecutedEvent, + ReceivedEther as ReceivedEtherEvent, + ReceivedFallback as ReceivedFallbackEvent + } from "../generated/templates/Chamber/Chamber" + + import { + ChangedGuard, + Demoted, + Initialized, + Promoted, + ProposalApproved, + ProposalCreated, + ProposalExecuted, + ReceivedEther, + ReceivedFallback + } from "../generated/schema" + + export function handleChangedGuard(event: ChangedGuardEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const changedGuardTx = new ChangedGuard(id); + changedGuardTx.guard = event.params.guard + changedGuardTx.contractAddress = changetype(event.transaction.to) + + changedGuardTx.blockNumber = event.block.number + changedGuardTx.blockTimestamp = event.block.timestamp + changedGuardTx.transactionHash = event.transaction.hash + + changedGuardTx.save() + } + + export function handleDemoted(event: DemotedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const demotedTx = new Demoted(id); + demotedTx.demoter = event.params.demoter + demotedTx.amt = event.params.amt + demotedTx.tokenId = event.params.tokenId + demotedTx.contractAddress = changetype(event.transaction.to) + + demotedTx.blockNumber = event.block.number + demotedTx.blockTimestamp = event.block.timestamp + demotedTx.transactionHash = event.transaction.hash + + demotedTx.save() + } + + export function handleInitialized(event: InitializedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const initializedTx = new Initialized(id); + initializedTx.version = event.params.version + initializedTx.contractAddress = changetype(event.transaction.to) + + initializedTx.blockNumber = event.block.number + initializedTx.blockTimestamp = event.block.timestamp + initializedTx.transactionHash = event.transaction.hash + + initializedTx.save() + } + + export function handlePromoted(event: PromotedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const promotedTx = new Promoted(id) + promotedTx.promoter = event.params.promoter + promotedTx.amt = event.params.amt + promotedTx.tokenId = event.params.tokenId + promotedTx.contractAddress = changetype(event.transaction.to) + + promotedTx.blockNumber = event.block.number + promotedTx.blockTimestamp = event.block.timestamp + promotedTx.transactionHash = event.transaction.hash + + promotedTx.save() + } + + export function handleProposalApproved(event: ProposalApprovedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const proposalApprovedTx = new ProposalApproved(id) + proposalApprovedTx.proposalId = event.params.proposalId + proposalApprovedTx.tokenId = event.params.tokenId + proposalApprovedTx.approvals = event.params.approvals + proposalApprovedTx.contractAddress = changetype(event.transaction.to) + + proposalApprovedTx.blockNumber = event.block.number + proposalApprovedTx.blockTimestamp = event.block.timestamp + proposalApprovedTx.transactionHash = event.transaction.hash + + proposalApprovedTx.save() + } + + export function handleProposalCreated(event: ProposalCreatedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const proposalCreatedTx = new ProposalCreated(id) + proposalCreatedTx.proposalId = event.params.proposalId + proposalCreatedTx.target = changetype(event.params.target) + proposalCreatedTx.value = event.params.value + proposalCreatedTx.data = event.params.data + proposalCreatedTx.voters = event.params.voters + proposalCreatedTx.nonce = event.params.nonce + proposalCreatedTx.contractAddress = changetype(event.transaction.to) + + proposalCreatedTx.blockNumber = event.block.number + proposalCreatedTx.blockTimestamp = event.block.timestamp + proposalCreatedTx.transactionHash = event.transaction.hash + + proposalCreatedTx.save() + } + + export function handleProposalExecuted(event: ProposalExecutedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const proposalExecutedTx = new ProposalExecuted(id) + proposalExecutedTx.proposalId = event.params.proposalId + proposalExecutedTx.contractAddress = changetype(event.transaction.to) + + proposalExecutedTx.blockNumber = event.block.number + proposalExecutedTx.blockTimestamp = event.block.timestamp + proposalExecutedTx.transactionHash = event.transaction.hash + + proposalExecutedTx.save() + } + + export function handleReceivedEther(event: ReceivedEtherEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const receivedEtherTx = new ReceivedEther(id) + receivedEtherTx.sender = event.params.sender + receivedEtherTx.value = event.params.value + receivedEtherTx.contractAddress = changetype(event.transaction.to) + + receivedEtherTx.blockNumber = event.block.number + receivedEtherTx.blockTimestamp = event.block.timestamp + receivedEtherTx.transactionHash = event.transaction.hash + + receivedEtherTx.save() + } + + export function handleReceivedFallback(event: ReceivedFallbackEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const receivedFallbackTx = new ReceivedFallback(id) + receivedFallbackTx.sender = event.params.sender + receivedFallbackTx.value = event.params.value + receivedFallbackTx.contractAddress = changetype(event.transaction.to) + + receivedFallbackTx.blockNumber = event.block.number + receivedFallbackTx.blockTimestamp = event.block.timestamp + receivedFallbackTx.transactionHash = event.transaction.hash + + receivedFallbackTx.save() + } + \ No newline at end of file diff --git a/subgraphs/chamber/src/registry.ts b/subgraphs/chamber/src/registry.ts index e69de29..80f3e44 100644 --- a/subgraphs/chamber/src/registry.ts +++ b/subgraphs/chamber/src/registry.ts @@ -0,0 +1,43 @@ +import { createId } from "../../../helpers/utils" +import { + ChamberDeployed as ChamberDeployedEvent, + OwnershipTransferred as OwnershipTransferredEvent + } from "../generated/Registry/Registry" + import { + ChamberDeployed, + OwnershipTransferred + } from "../generated/schema" + + import { Chamber } from "../generated/templates" + + export function handleChamberDeployed(event: ChamberDeployedEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const chamberDeployedTx = new ChamberDeployed(id); + chamberDeployedTx.chamber = event.params.chamber + chamberDeployedTx.serial = event.params.serial + chamberDeployedTx.deployer = event.params.deployer + chamberDeployedTx.memberToken = event.params.memberToken + chamberDeployedTx.govToken = event.params.govToken + + chamberDeployedTx.blockNumber = event.block.number + chamberDeployedTx.blockTimestamp = event.block.timestamp + chamberDeployedTx.transactionHash = event.transaction.hash + + chamberDeployedTx.save() + + Chamber.create(event.params.chamber) + } + + export function handleOwnershipTransferred(event: OwnershipTransferredEvent): void { + const id = createId(event.transaction.hash, event.logIndex); + const ownershipTransferredTx = new OwnershipTransferred(id); + ownershipTransferredTx.previousOwner = event.params.previousOwner + ownershipTransferredTx.newOwner = event.params.newOwner + + ownershipTransferredTx.blockNumber = event.block.number + ownershipTransferredTx.blockTimestamp = event.block.timestamp + ownershipTransferredTx.transactionHash = event.transaction.hash + + ownershipTransferredTx.save() + } + \ No newline at end of file diff --git a/subgraphs/chamber/subgraph.yaml b/subgraphs/chamber/subgraph.yaml new file mode 100644 index 0000000..4dd0da7 --- /dev/null +++ b/subgraphs/chamber/subgraph.yaml @@ -0,0 +1,75 @@ +specVersion: 1.0.0 +indexerHints: + prune: auto +schema: + file: ./schema.graphql +dataSources: + - kind: ethereum + name: Registry + network: sepolia + source: + address: "0x62996E7041C0A434B8d0268EF7F6BBCf2d668dA7" + abi: Registry + startBlock: 5281759 + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - ChamberDeployed + - Initialized + - OwnershipTransferred + abis: + - name: Registry + file: ./abis/Registry.json + eventHandlers: + - event: ChamberDeployed(indexed address,indexed uint256,indexed address,address,address) + handler: handleChamberDeployed + - event: Initialized(uint8) + handler: handleRegistryInitialized + - event: OwnershipTransferred(indexed address,indexed address) + handler: handleOwnershipTransferred + file: ./src/registry.ts +templates: + - kind: ethereum + name: Chamber + network: sepolia + source: + abi: Chamber + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - ChangedGuard + - Demoted + - Initialized + - Promoted + - ProposalApproved + - ProposalCreated + - ProposalExecuted + - ReceivedEther + - ReceivedFallback + abis: + - name: Chamber + file: ./abis/Chamber.json + eventHandlers: + - event: ChangedGuard(indexed address) + handler: handleChangedGuard + - event: Demoted(address,uint256,uint256) + handler: handleDemoted + - event: Initialized(uint8) + handler: handleInitialized + - event: Promoted(address,uint256,uint256) + handler: handlePromoted + - event: ProposalApproved(uint256,uint256,uint256) + handler: handleProposalApproved + - event: ProposalCreated(uint256,address[],uint256[],bytes[],uint256[5],uint256) + handler: handleProposalCreated + - event: ProposalExecuted(uint256) + handler: handleProposalExecuted + - event: ReceivedEther(indexed address,uint256) + handler: handleReceivedEther + - event: ReceivedFallback(indexed address,uint256) + handler: handleReceivedFallback + file: ./src/chamber.ts \ No newline at end of file diff --git a/subgraphs/chamber/tests/chamber.test.ts b/subgraphs/chamber/tests/chamber.test.ts new file mode 100644 index 0000000..eaa7283 --- /dev/null +++ b/subgraphs/chamber/tests/chamber.test.ts @@ -0,0 +1,212 @@ +import{ + describe, + test, +} from "matchstick-as" + +import { + ChangedGuard, + Demoted, + Initialized, + Promoted, + ProposalApproved, + ProposalCreated, + ProposalExecuted, + ReceivedEther, + ReceivedFallback, +} from "../generated/templates/Chamber/Chamber" + +import { + handleChangedGuard, + handleDemoted, + handleInitialized, + handlePromoted, + handleProposalApproved, + handleProposalCreated, + handleProposalExecuted, + handleReceivedEther, + handleReceivedFallback, +} from "../src/chamber" + +import { + createChangedGuardEvent, + createDemotedEvent, + createInitializedEvent, + createPromotedEvent, + createProposalApprovedEvent, + createProposalCreatedEvent, + createProposalExecutedEvent, + createReceivedEtherEvent, + createReceivedFallbackEvent, +} from "./helpers/utils" + +import { + expectChangedGuardAdded, + expectDemotedAdded, + expectInitializedAdded, + expectPromotedAdded, + expectProposalApprovedAdded, + expectProposalCreatedAdded, + expectProposalExecutedAdded, + expectReceivedEtherAdded, + expectReceivedFallbackAdded, +} from "./helpers/assertions" + +describe ("Chamber Test", () => { + describe ("Test ChangedGuard", () => { + const createMockChangedGuardEvent = ( + guard : string = '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf', + ) : ChangedGuard => { + const event = createChangedGuardEvent( guard ); + return event; + }; + test ("Handel ChangedGuard Tx", () => { + const event = createMockChangedGuardEvent(); + handleChangedGuard(event); + expectChangedGuardAdded(event); + }) + }) + + describe ("Test Demoted", () => { + const createMockDemotedEvent = ( + demoter : string = '0x8e1aa1674d9fc9f0dca4a4d31db85e65d216666c', + amt : i32 = 100, + tokenId : i32 = 1, + ) : Demoted => { + const event = createDemotedEvent( demoter, amt, tokenId ); + return event; + }; + test ("Handel Demoted Tx", () => { + const event = createMockDemotedEvent(); + handleDemoted(event); + expectDemotedAdded(event); + }) + }) + + describe ("Test Initialized", () => { + const createMockInitializedEvent = ( + version: i32 = 1, + ) : Initialized => { + const event = createInitializedEvent( version ); + return event; + }; + test ("Handel Initialized Tx", () => { + const event = createMockInitializedEvent(); + handleInitialized(event); + expectInitializedAdded(event); + }) + }) + + describe ("Test Promoted", () => { + const createMockPromotedEvent = ( + promoter : string = '0x8e1aa1674d9fc9f0dca4a4d31db85e65d216666c', + amt : i32 = 100, + tokenId : i32 = 1, + ) : Promoted => { + const event = createPromotedEvent( promoter, amt, tokenId ); + return event; + }; + test ("Handel Promoted Tx", () => { + const event = createMockPromotedEvent(); + handlePromoted(event); + expectPromotedAdded(event); + }) + }) + + describe ("Test ProposalApproved", () => { + const createMockProposalApprovedEvent = ( + proposalId: i32 = 1, + tokenId: i32 = 1, + approvals: i32 = 1, + ) : ProposalApproved => { + const event = createProposalApprovedEvent( + proposalId, + tokenId, + approvals, + ); + return event + }; + test ("Handel ProposalApproved Tx", () => { + const event = createMockProposalApprovedEvent(); + handleProposalApproved(event); + expectProposalApprovedAdded(event); + }) + }) + + describe ("Test ProposalCreated", () => { + const createMockProposalCreatedEvent = ( + proposalId: i32 = 1, + target: string[] = ['0x2e0049b05217290087BA613290BaCC761d7adD04','0x8E1aA1674D9Fc9f0dca4a4D31db85E65D216666c'] , + value: i32[] = [1,1], + data: string[] = ['',''], + voters: i32[] = [1,2,3,4,5], + nonce: i32 = 1, + ) : ProposalCreated => { + const event = createProposalCreatedEvent( + proposalId, + target, + value, + data, + voters, + nonce, + ); + return event; + }; + test ("Handel ProposalCreated Tx", () => { + const event = createMockProposalCreatedEvent(); + handleProposalCreated(event); + expectProposalCreatedAdded(event); + }) + }) + + describe ("Test ProposalExecuted", () => { + const createMockProposalExecutedEvent = ( + proposalId: i32 = 1, + ) : ProposalExecuted => { + const event = createProposalExecutedEvent( + proposalId, + ); + return event; + }; + test ("Handel ProposalExecuted Tx", () => { + const event = createMockProposalExecutedEvent(); + handleProposalExecuted(event); + expectProposalExecutedAdded(event); + }) + }) + + describe ("Test ReceivedEther", () => { + const createMockReceivedEtherEvent = ( + sender: string = '0x8E1aA1674D9Fc9f0dca4a4D31db85E65D216666c', + value: i32 = 1000, + ) : ReceivedEther => { + const event = createReceivedEtherEvent( + sender, + value, + ); + return event; + }; + test ("Handel ReceivedEther Tx", () => { + const event = createMockReceivedEtherEvent(); + handleReceivedEther(event); + expectReceivedEtherAdded(event); + }) + }) + + describe ("Test ReceivedFallback", () => { + const createMockReceivedFallbackEvent = ( + sender: string = '0x8E1aA1674D9Fc9f0dca4a4D31db85E65D216666c', + value: i32 = 1000, + ) : ReceivedFallback => { + const event = createReceivedFallbackEvent( + sender, + value, + ); + return event; + }; + test ("Handel ReceivedFallback Tx", () => { + const event = createMockReceivedFallbackEvent(); + handleReceivedFallback(event); + expectReceivedFallbackAdded(event); + }) + }) +}) \ No newline at end of file diff --git a/subgraphs/chamber/tests/helpers/assertion.ts b/subgraphs/chamber/tests/helpers/assertion.ts deleted file mode 100644 index e69de29..0000000 diff --git a/subgraphs/chamber/tests/helpers/assertions.ts b/subgraphs/chamber/tests/helpers/assertions.ts new file mode 100644 index 0000000..0ecf5c3 --- /dev/null +++ b/subgraphs/chamber/tests/helpers/assertions.ts @@ -0,0 +1,135 @@ +import { + ChamberDeployed as ChamberDeployedTx, + OwnershipTransferred as OwnershipTransferredTx, + ChangedGuard as ChangedGuardTx, + Demoted as DemotedTx, + Initialized as InitializeTx, + Promoted as PromotedTx, + ProposalApproved as ProposalApprovedTx, + ProposalCreated as ProposalCreatedTx, + ProposalExecuted as ProposalExecutedTx, + ReceivedEther as ReceivedEtherTx, + ReceivedFallback as ReceivedFallbackTX, +} from "../../generated/schema" + +import { + ChamberDeployed, + OwnershipTransferred +} from "../../generated/Registry/Registry" + +import { + ChangedGuard, + Demoted, + Initialized, + Promoted, + ProposalApproved, + ProposalCreated, + ProposalExecuted, + ReceivedEther, + ReceivedFallback, +} from "../../generated/templates/Chamber/Chamber" + +import { assert } from "matchstick-as"; +import { Bytes , BigInt, Address } from "@graphprotocol/graph-ts"; +import { createId } from "../../../../helpers/utils"; + +export const expectChamberDeployedAdded = (event : ChamberDeployed): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const chamberDeployedTx = ChamberDeployedTx.load(id); + assert.assertNotNull(chamberDeployedTx); + if (!chamberDeployedTx) return; + assert.bytesEquals(event.params.chamber, chamberDeployedTx.chamber) + assert.bigIntEquals(event.params.serial, chamberDeployedTx.serial) + assert.bytesEquals(event.params.deployer, chamberDeployedTx.deployer) + assert.bytesEquals(event.params.govToken, chamberDeployedTx.govToken) + assert.bytesEquals(event.params.memberToken, chamberDeployedTx.memberToken) +} + +export const expectOwnershipTransferredAdded = (event : OwnershipTransferred): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const ownershipTransferredTx = OwnershipTransferredTx.load(id); + assert.assertNotNull(ownershipTransferredTx); + if (!ownershipTransferredTx) return; + assert.bytesEquals(event.params.previousOwner, ownershipTransferredTx.previousOwner) + assert.bytesEquals(event.params.newOwner, ownershipTransferredTx.newOwner) +} + +export const expectChangedGuardAdded = (event : ChangedGuard): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const changedGuardTx = ChangedGuardTx.load(id); + assert.assertNotNull(changedGuardTx); + if (!changedGuardTx) return; + assert.bytesEquals(event.params.guard, changedGuardTx.guard) +} + +export const expectDemotedAdded = (event : Demoted): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const demotedTx = DemotedTx.load(id); + assert.assertNotNull(demotedTx); + if (!demotedTx) return; + assert.bytesEquals(event.params.demoter, demotedTx.demoter); + assert.bigIntEquals(event.params.amt, demotedTx.amt); + assert.bigIntEquals(event.params.tokenId, demotedTx.tokenId); +} + +export const expectInitializedAdded = (event : Initialized): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const initializedTx = InitializeTx.load(id); + assert.assertNotNull(initializedTx); + if (!initializedTx) return; + assert.i32Equals(event.params.version, initializedTx.version); +} + +export const expectPromotedAdded = (event : Promoted): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const promotedTx = PromotedTx.load(id); + assert.assertNotNull(promotedTx); + if (!promotedTx) return; + assert.bytesEquals(event.params.promoter, promotedTx.promoter); + assert.bigIntEquals(event.params.amt, promotedTx.amt); + assert.bigIntEquals(event.params.tokenId, promotedTx.tokenId); +} + +export const expectProposalApprovedAdded = (event : ProposalApproved): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const proposalApprovedTx = ProposalApprovedTx.load(id); + assert.assertNotNull(proposalApprovedTx); + if (!proposalApprovedTx) return; + assert.bigIntEquals(event.params.proposalId,proposalApprovedTx.proposalId); + assert.bigIntEquals(event.params.tokenId,proposalApprovedTx.tokenId); + assert.bigIntEquals(event.params.approvals,proposalApprovedTx.approvals); +} + +export const expectProposalCreatedAdded = (event : ProposalCreated): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const proposalCreatedTx = ProposalCreatedTx.load(id); + assert.assertNotNull(proposalCreatedTx); + if (!proposalCreatedTx) return; + assert.bytesEquals(changetype
(event.params.target), changetype
(proposalCreatedTx.target)) +} + +export const expectProposalExecutedAdded = (event : ProposalExecuted): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const proposalExecutedTx = ProposalExecutedTx.load(id); + assert.assertNotNull(proposalExecutedTx); + if (!proposalExecutedTx) return; + assert.bigIntEquals(event.params.proposalId,proposalExecutedTx.proposalId); +} + +export const expectReceivedEtherAdded = (event : ReceivedEther): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const receivedEtherTx = ReceivedEtherTx.load(id); + assert.assertNotNull(receivedEtherTx); + if (!receivedEtherTx) return; + assert.bytesEquals(event.params.sender, receivedEtherTx.sender); + assert.bigIntEquals(event.params.value, receivedEtherTx.value); +} + +export const expectReceivedFallbackAdded = (event : ReceivedFallback): void => { + const id = createId(event.transaction.hash, event.transactionLogIndex); + const receivedFallbackTx = ReceivedFallbackTX.load(id); + assert.assertNotNull(receivedFallbackTx); + if (!receivedFallbackTx) return; + assert.bytesEquals(event.params.sender, receivedFallbackTx.sender); + assert.bigIntEquals(event.params.value, receivedFallbackTx.value); +} \ No newline at end of file diff --git a/subgraphs/chamber/tests/helpers/utils.ts b/subgraphs/chamber/tests/helpers/utils.ts index e69de29..124ffdf 100644 --- a/subgraphs/chamber/tests/helpers/utils.ts +++ b/subgraphs/chamber/tests/helpers/utils.ts @@ -0,0 +1,260 @@ +import { Address, BigInt, Bytes, ethereum, Wrapped } from "@graphprotocol/graph-ts"; +import { newMockEvent } from "matchstick-as"; + +import { + ChamberDeployed, + OwnershipTransferred, +} from "../../generated/Registry/Registry" + +import { + ChangedGuard, + Demoted, + Initialized, + Promoted, + ProposalApproved, + ProposalCreated, + ProposalExecuted, + ReceivedEther, + ReceivedFallback, +} from "../../generated/templates/Chamber/Chamber" + + +const addressEventParam = (key: string, value: string): ethereum.EventParam => + new ethereum.EventParam(key, ethereum.Value.fromAddress(Address.fromString(value))); + +const uintEventParam = (key: string, value: i32): ethereum.EventParam => + new ethereum.EventParam(key, ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(value))); + +export function createChamberDeployedEvent( + chamber: string, + serial: i32, + deployer: string, + memberToken: string, + govToken: string, +): ChamberDeployed { + const mockEvent = newMockEvent(); + const chamberDeployedEvent = new ChamberDeployed( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + chamberDeployedEvent.parameters.push(addressEventParam("chamber",chamber)) + chamberDeployedEvent.parameters.push(uintEventParam("serial",serial)) + chamberDeployedEvent.parameters.push(addressEventParam("deployer",deployer)) + chamberDeployedEvent.parameters.push(addressEventParam("memberToken", memberToken)) + chamberDeployedEvent.parameters.push(addressEventParam("govToken",govToken)) + return chamberDeployedEvent; +} + +export function createOwnershipTransferredEvent ( + previousOwner: string, + newOwner: string, +): OwnershipTransferred { + const mockEvent = newMockEvent(); + const ownershipTransferredEvent = new OwnershipTransferred ( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + ownershipTransferredEvent.parameters.push(addressEventParam("previousOwner",previousOwner)); + ownershipTransferredEvent.parameters.push(addressEventParam("newOwner",newOwner)); + return ownershipTransferredEvent; +} + +export function createChangedGuardEvent ( + guard: string, +) : ChangedGuard { + const mockEvent = newMockEvent(); + const changedGuardEvent = new ChangedGuard ( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + changedGuardEvent.parameters.push(addressEventParam("guard",guard)); + return changedGuardEvent; +} + +export function createDemotedEvent ( + demoter : string, + amt : i32, + tokenId : i32, +) : Demoted { + const mockEvent = newMockEvent(); + const demotedEvent = new Demoted ( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + demotedEvent.parameters.push(addressEventParam("demoter",demoter)); + demotedEvent.parameters.push(uintEventParam("amt",amt)); + demotedEvent.parameters.push(uintEventParam("tokenId",tokenId)); + return demotedEvent; +} + +export function createInitializedEvent ( + version: i32, +) : Initialized { + const mockEvent = newMockEvent(); + const initializedEvent = new Initialized ( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + initializedEvent.parameters.push(uintEventParam("version",version)); + return initializedEvent; +} + +export function createPromotedEvent ( + promoter : string, + amt : i32, + tokenId : i32, +) : Promoted { + const mockEvent = newMockEvent(); + const promotedEvent = new Promoted ( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + promotedEvent.parameters.push(addressEventParam("demoter",promoter)); + promotedEvent.parameters.push(uintEventParam("amt",amt)); + promotedEvent.parameters.push(uintEventParam("tokenId",tokenId)); + return promotedEvent; +} + +export function createProposalApprovedEvent( + proposalId: i32, + tokenId: i32, + approvals: i32, +) : ProposalApproved { + const mockEvent = newMockEvent(); + const proposalApprovedEvent = new ProposalApproved( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + proposalApprovedEvent.parameters.push(uintEventParam("proposalId",proposalId)); + proposalApprovedEvent.parameters.push(uintEventParam("tokenId",tokenId)); + proposalApprovedEvent.parameters.push(uintEventParam("approvals",approvals)); + return proposalApprovedEvent; +} + +export function createProposalCreatedEvent( + proposalId: i32 , + target: string[], + value: i32[], + data: string[], + voters: i32[], + nonce: i32 , +) : ProposalCreated { + const mockEvent = newMockEvent(); + const proposalCreatedEvent = new ProposalCreated ( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + proposalCreatedEvent.parameters.push(uintEventParam("proposalId",proposalId)); + proposalCreatedEvent.parameters.push((new ethereum.EventParam("target", ethereum.Value.fromAddressArray(changetype(target))))); + proposalCreatedEvent.parameters.push((new ethereum.EventParam("value", ethereum.Value.fromI32Array(value)))); + proposalCreatedEvent.parameters.push((new ethereum.EventParam("data", ethereum.Value.fromStringArray(data)))); + proposalCreatedEvent.parameters.push((new ethereum.EventParam("voters", ethereum.Value.fromI32Array(voters)))); + proposalCreatedEvent.parameters.push(uintEventParam("nonce",nonce)); + return proposalCreatedEvent +} + +export function createProposalExecutedEvent( + proposalId: i32, +) : ProposalExecuted { + const mockEvent = newMockEvent(); + const proposalExecutedEvent = new ProposalExecuted( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + proposalExecutedEvent.parameters.push(uintEventParam("proposalId",proposalId)); + return proposalExecutedEvent; +} + +export function createReceivedEtherEvent( + sender: string, + value: i32, +) : ReceivedEther { + const mockEvent = newMockEvent(); + const receivedEtherEvent = new ReceivedEther( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + receivedEtherEvent.parameters.push(addressEventParam("sender",sender)); + receivedEtherEvent.parameters.push(uintEventParam("value",value)); + return receivedEtherEvent; +} + +export function createReceivedFallbackEvent( + sender: string, + value: i32, +) : ReceivedFallback { + const mockEvent = newMockEvent(); + const receivedFallbackEvent = new ReceivedFallback( + mockEvent.address, + mockEvent.logIndex, + mockEvent.transactionLogIndex, + mockEvent.logType, + mockEvent.block, + mockEvent.transaction, + [], + mockEvent.receipt + ); + receivedFallbackEvent.parameters.push(addressEventParam("sender",sender)); + receivedFallbackEvent.parameters.push(uintEventParam("value",value)); + return receivedFallbackEvent; +} \ No newline at end of file diff --git a/subgraphs/chamber/tests/registry.test.ts b/subgraphs/chamber/tests/registry.test.ts new file mode 100644 index 0000000..09e7993 --- /dev/null +++ b/subgraphs/chamber/tests/registry.test.ts @@ -0,0 +1,66 @@ +import{ + describe, + test, +} from "matchstick-as" + +import { + ChamberDeployed, + OwnershipTransferred, +} from "../generated/Registry/Registry" + +import { + handleChamberDeployed, + handleOwnershipTransferred, +} from "../src/registry" + +import { + createChamberDeployedEvent, + createOwnershipTransferredEvent +} from "./helpers/utils" + +import { + expectChamberDeployedAdded, + expectOwnershipTransferredAdded +} from "./helpers/assertions" + + +describe ("Registry Test", () => { + describe("Test ChamberDeployed", () => { + const createMockChamberDeployedEvent = ( + chamber: string = '0x66905a82fb9e3e267f5be6b30a4ac7794f70763f', + serial: i32 = 1, + deployer: string = '0x8e1aa1674d9fc9f0dca4a4d31db85e65d216666c', + memberToken: string = '0x699ADd01337F25E538EB060D97c70d1BeC77dA57', + govToken: string = '0xf8234C14Eb6B34AA23172111CB407fb5361f95f2', + ) : ChamberDeployed => { + const event = createChamberDeployedEvent( + chamber, + serial, + deployer, + memberToken, + govToken, + ); + return event; + }; + test ("Handel ChamberDeployed Tx",()=>{ + const event = createMockChamberDeployedEvent(); + handleChamberDeployed(event); + expectChamberDeployedAdded(event); + }) + }) + + describe("Test OwnershipTransferred", () => { + const createMockOwnershipTransferredEvent = ( + previousOwner: string = '0x0000000000000000000000000000000000000000', + newOwner: string = '0x8e1aa1674d9fc9f0dca4a4d31db85e65d216666c', + ) : OwnershipTransferred => { + const event = createOwnershipTransferredEvent( previousOwner, newOwner); + return event + }; + test("Handel OwnershipTransferred Tx",() => { + const event = createMockOwnershipTransferredEvent(); + handleOwnershipTransferred(event); + expectOwnershipTransferredAdded(event); + }) + }) +}) \ No newline at end of file diff --git a/subgraphs/chamber/tsconfig.json b/subgraphs/chamber/tsconfig.json new file mode 100644 index 0000000..4e86672 --- /dev/null +++ b/subgraphs/chamber/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "@graphprotocol/graph-ts/types/tsconfig.base.json", + "include": ["src", "tests"] +}