From 7256dccea05537d9d5a64b997e2d939952055545 Mon Sep 17 00:00:00 2001 From: syahirAmali Date: Wed, 30 Oct 2024 13:35:43 +0800 Subject: [PATCH] Initial commit --- .gitignore | 34 +++ abis/RaidGeld.json | 487 +++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 50 ++++ networks.json | 8 + package.json | 18 ++ schema.graphql | 63 +++++ src/raid-geld.ts | 112 +++++++++ subgraph.yaml | 41 ++++ tests/raid-geld-utils.ts | 193 ++++++++++++++++ tests/raid-geld.test.ts | 62 +++++ tsconfig.json | 4 + 11 files changed, 1072 insertions(+) create mode 100644 .gitignore create mode 100644 abis/RaidGeld.json create mode 100644 docker-compose.yml create mode 100644 networks.json create mode 100644 package.json create mode 100644 schema.graphql create mode 100644 src/raid-geld.ts create mode 100644 subgraph.yaml create mode 100644 tests/raid-geld-utils.ts create mode 100644 tests/raid-geld.test.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b542c60 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Graph CLI generated artifacts +build/ +generated/ + +# Dependency directories +node_modules/ +jspm_packages/ + +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# dotenv environment variables file +.env + +# Testing +coverage +coverage.json + +# Typechain +typechain +typechain-types + +# Hardhat files +cache diff --git a/abis/RaidGeld.json b/abis/RaidGeld.json new file mode 100644 index 0000000..07434a6 --- /dev/null +++ b/abis/RaidGeld.json @@ -0,0 +1,487 @@ +[ + { "type": "constructor", "inputs": [], "stateMutability": "nonpayable" }, + { "type": "fallback", "stateMutability": "payable" }, + { "type": "receive", "stateMutability": "payable" }, + { + "type": "function", + "name": "BUY_IN_AMOUNT", + "inputs": [], + "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "INITIAL_GELD", + "inputs": [], + "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "MANTISSA", + "inputs": [], + "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "addUnit", + "inputs": [ + { "name": "unit", "type": "uint8", "internalType": "uint8" }, + { "name": "n_units", "type": "uint16", "internalType": "uint16" } + ], + "outputs": [], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "allowance", + "inputs": [ + { "name": "owner", "type": "address", "internalType": "address" }, + { "name": "spender", "type": "address", "internalType": "address" } + ], + "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "approve", + "inputs": [ + { "name": "spender", "type": "address", "internalType": "address" }, + { "name": "value", "type": "uint256", "internalType": "uint256" } + ], + "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "balanceOf", + "inputs": [ + { "name": "account", "type": "address", "internalType": "address" } + ], + "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "decimals", + "inputs": [], + "outputs": [{ "name": "", "type": "uint8", "internalType": "uint8" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "getArmy", + "inputs": [ + { "name": "addr", "type": "address", "internalType": "address" } + ], + "outputs": [ + { + "name": "", + "type": "tuple", + "internalType": "struct Army", + "components": [ + { + "name": "moloch_denier", + "type": "tuple", + "internalType": "struct Raider", + "components": [ + { "name": "level", "type": "uint16", "internalType": "uint16" } + ] + }, + { + "name": "apprentice", + "type": "tuple", + "internalType": "struct Raider", + "components": [ + { "name": "level", "type": "uint16", "internalType": "uint16" } + ] + }, + { + "name": "anointed", + "type": "tuple", + "internalType": "struct Raider", + "components": [ + { "name": "level", "type": "uint16", "internalType": "uint16" } + ] + }, + { + "name": "champion", + "type": "tuple", + "internalType": "struct Raider", + "components": [ + { "name": "level", "type": "uint16", "internalType": "uint16" } + ] + }, + { + "name": "profit_per_second", + "type": "uint256", + "internalType": "uint256" + } + ] + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "getPlayer", + "inputs": [ + { "name": "addr", "type": "address", "internalType": "address" } + ], + "outputs": [ + { + "name": "", + "type": "tuple", + "internalType": "struct Player", + "components": [ + { + "name": "total_minted", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "created_at", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "last_raided_at", + "type": "uint256", + "internalType": "uint256" + } + ] + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "isRegistered", + "inputs": [ + { "name": "addr", "type": "address", "internalType": "address" } + ], + "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "name", + "inputs": [], + "outputs": [{ "name": "", "type": "string", "internalType": "string" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "owner", + "inputs": [], + "outputs": [{ "name": "", "type": "address", "internalType": "address" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "raid", + "inputs": [], + "outputs": [], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "register", + "inputs": [], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "renounceOwnership", + "inputs": [], + "outputs": [], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "symbol", + "inputs": [], + "outputs": [{ "name": "", "type": "string", "internalType": "string" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "totalSupply", + "inputs": [], + "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], + "stateMutability": "view" + }, + { + "type": "function", + "name": "transfer", + "inputs": [ + { "name": "to", "type": "address", "internalType": "address" }, + { "name": "value", "type": "uint256", "internalType": "uint256" } + ], + "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "transferFrom", + "inputs": [ + { "name": "from", "type": "address", "internalType": "address" }, + { "name": "to", "type": "address", "internalType": "address" }, + { "name": "value", "type": "uint256", "internalType": "uint256" } + ], + "outputs": [{ "name": "", "type": "bool", "internalType": "bool" }], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "transferOwnership", + "inputs": [ + { "name": "newOwner", "type": "address", "internalType": "address" } + ], + "outputs": [], + "stateMutability": "nonpayable" + }, + { + "type": "function", + "name": "withdraw", + "inputs": [], + "outputs": [], + "stateMutability": "nonpayable" + }, + { + "type": "event", + "name": "Approval", + "inputs": [ + { + "name": "owner", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "spender", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "value", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "OwnershipTransferred", + "inputs": [ + { + "name": "previousOwner", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "newOwner", + "type": "address", + "indexed": true, + "internalType": "address" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "PlayerRegistered", + "inputs": [ + { + "name": "player", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "initialGeld", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "RaidPerformed", + "inputs": [ + { + "name": "player", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "totalMinted", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + }, + { + "name": "geldBalance", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "Transfer", + "inputs": [ + { + "name": "from", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "to", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "value", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "UnitAdded", + "inputs": [ + { + "name": "player", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "unitType", + "type": "uint8", + "indexed": false, + "internalType": "uint8" + }, + { + "name": "nUnits", + "type": "uint16", + "indexed": false, + "internalType": "uint16" + }, + { + "name": "cost", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + }, + { + "name": "geldBalance", + "type": "uint256", + "indexed": false, + "internalType": "uint256" + }, + { + "name": "molochDenierLevel", + "type": "uint16", + "indexed": false, + "internalType": "uint16" + }, + { + "name": "apprenticeLevel", + "type": "uint16", + "indexed": false, + "internalType": "uint16" + }, + { + "name": "anointedLevel", + "type": "uint16", + "indexed": false, + "internalType": "uint16" + }, + { + "name": "championLevel", + "type": "uint16", + "indexed": false, + "internalType": "uint16" + } + ], + "anonymous": false + }, + { + "type": "error", + "name": "ERC20InsufficientAllowance", + "inputs": [ + { "name": "spender", "type": "address", "internalType": "address" }, + { "name": "allowance", "type": "uint256", "internalType": "uint256" }, + { "name": "needed", "type": "uint256", "internalType": "uint256" } + ] + }, + { + "type": "error", + "name": "ERC20InsufficientBalance", + "inputs": [ + { "name": "sender", "type": "address", "internalType": "address" }, + { "name": "balance", "type": "uint256", "internalType": "uint256" }, + { "name": "needed", "type": "uint256", "internalType": "uint256" } + ] + }, + { + "type": "error", + "name": "ERC20InvalidApprover", + "inputs": [ + { "name": "approver", "type": "address", "internalType": "address" } + ] + }, + { + "type": "error", + "name": "ERC20InvalidReceiver", + "inputs": [ + { "name": "receiver", "type": "address", "internalType": "address" } + ] + }, + { + "type": "error", + "name": "ERC20InvalidSender", + "inputs": [ + { "name": "sender", "type": "address", "internalType": "address" } + ] + }, + { + "type": "error", + "name": "ERC20InvalidSpender", + "inputs": [ + { "name": "spender", "type": "address", "internalType": "address" } + ] + }, + { + "type": "error", + "name": "OwnableInvalidOwner", + "inputs": [ + { "name": "owner", "type": "address", "internalType": "address" } + ] + }, + { + "type": "error", + "name": "OwnableUnauthorizedAccount", + "inputs": [ + { "name": "account", "type": "address", "internalType": "address" } + ] + } +] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a008fc9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +version: "3" +services: + graph-node: + image: graphprotocol/graph-node + ports: + - "8000:8000" + - "8001:8001" + - "8020:8020" + - "8030:8030" + - "8040:8040" + depends_on: + - ipfs + - postgres + extra_hosts: + - host.docker.internal:host-gateway + environment: + postgres_host: postgres + postgres_user: graph-node + postgres_pass: let-me-in + postgres_db: graph-node + ipfs: "ipfs:5001" + ethereum: "mainnet:http://host.docker.internal:8545" + GRAPH_LOG: info + ipfs: + image: ipfs/kubo:v0.17.0 + ports: + - "5001:5001" + volumes: + - ./data/ipfs:/data/ipfs + postgres: + image: postgres:14 + ports: + - "5432:5432" + command: + [ + "postgres", + "-cshared_preload_libraries=pg_stat_statements", + "-cmax_connections=200", + ] + environment: + POSTGRES_USER: graph-node + POSTGRES_PASSWORD: let-me-in + POSTGRES_DB: graph-node + # FIXME: remove this env. var. which we shouldn't need. Introduced by + # , maybe as a + # workaround for https://github.com/docker/for-mac/issues/6270? + PGDATA: "/var/lib/postgresql/data" + POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C" + volumes: + - ./data/postgres:/var/lib/postgresql/data diff --git a/networks.json b/networks.json new file mode 100644 index 0000000..4bd7bda --- /dev/null +++ b/networks.json @@ -0,0 +1,8 @@ +{ + "base-sepolia": { + "RaidGeld": { + "address": "0xC912823B231f5d052eF334a8f8B73F6354909cd0", + "startBlock": 17213030 + } + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..4ff74dd --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "slay-the-moloch-base-sepolia", + "license": "UNLICENSED", + "scripts": { + "codegen": "graph codegen", + "build": "graph build", + "deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ slay-the-moloch-base-sepolia", + "create-local": "graph create --node http://localhost:8020/ slay-the-moloch-base-sepolia", + "remove-local": "graph remove --node http://localhost:8020/ slay-the-moloch-base-sepolia", + "deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 slay-the-moloch-base-sepolia", + "test": "graph test" + }, + "dependencies": { + "@graphprotocol/graph-cli": "0.87.0", + "@graphprotocol/graph-ts": "0.32.0" + }, + "devDependencies": { "matchstick-as": "0.5.0" } +} diff --git a/schema.graphql b/schema.graphql new file mode 100644 index 0000000..57d58cc --- /dev/null +++ b/schema.graphql @@ -0,0 +1,63 @@ +type Approval @entity(immutable: true) { + id: Bytes! + owner: Bytes! # address + spender: Bytes! # address + value: BigInt! # uint256 + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type OwnershipTransferred @entity(immutable: true) { + id: Bytes! + previousOwner: Bytes! # address + newOwner: Bytes! # address + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type PlayerRegistered @entity(immutable: true) { + id: Bytes! + player: Bytes! # address + initialGeld: BigInt! # uint256 + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type RaidPerformed @entity(immutable: true) { + id: Bytes! + player: Bytes! # address + totalMinted: BigInt! # uint256 + geldBalance: BigInt! # uint256 + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type Transfer @entity(immutable: true) { + id: Bytes! + from: Bytes! # address + to: Bytes! # address + value: BigInt! # uint256 + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} + +type UnitAdded @entity(immutable: true) { + id: Bytes! + player: Bytes! # address + unitType: Int! # uint8 + nUnits: Int! # uint16 + cost: BigInt! # uint256 + geldBalance: BigInt! # uint256 + molochDenierLevel: Int! # uint16 + apprenticeLevel: Int! # uint16 + anointedLevel: Int! # uint16 + championLevel: Int! # uint16 + blockNumber: BigInt! + blockTimestamp: BigInt! + transactionHash: Bytes! +} diff --git a/src/raid-geld.ts b/src/raid-geld.ts new file mode 100644 index 0000000..49c11b2 --- /dev/null +++ b/src/raid-geld.ts @@ -0,0 +1,112 @@ +import { + Approval as ApprovalEvent, + OwnershipTransferred as OwnershipTransferredEvent, + PlayerRegistered as PlayerRegisteredEvent, + RaidPerformed as RaidPerformedEvent, + Transfer as TransferEvent, + UnitAdded as UnitAddedEvent +} from "../generated/RaidGeld/RaidGeld" +import { + Approval, + OwnershipTransferred, + PlayerRegistered, + RaidPerformed, + Transfer, + UnitAdded +} from "../generated/schema" + +export function handleApproval(event: ApprovalEvent): void { + let entity = new Approval( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ) + entity.owner = event.params.owner + entity.spender = event.params.spender + entity.value = event.params.value + + entity.blockNumber = event.block.number + entity.blockTimestamp = event.block.timestamp + entity.transactionHash = event.transaction.hash + + entity.save() +} + +export function handleOwnershipTransferred( + event: OwnershipTransferredEvent +): void { + let entity = new OwnershipTransferred( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ) + entity.previousOwner = event.params.previousOwner + entity.newOwner = event.params.newOwner + + entity.blockNumber = event.block.number + entity.blockTimestamp = event.block.timestamp + entity.transactionHash = event.transaction.hash + + entity.save() +} + +export function handlePlayerRegistered(event: PlayerRegisteredEvent): void { + let entity = new PlayerRegistered( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ) + entity.player = event.params.player + entity.initialGeld = event.params.initialGeld + + entity.blockNumber = event.block.number + entity.blockTimestamp = event.block.timestamp + entity.transactionHash = event.transaction.hash + + entity.save() +} + +export function handleRaidPerformed(event: RaidPerformedEvent): void { + let entity = new RaidPerformed( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ) + entity.player = event.params.player + entity.totalMinted = event.params.totalMinted + entity.geldBalance = event.params.geldBalance + + entity.blockNumber = event.block.number + entity.blockTimestamp = event.block.timestamp + entity.transactionHash = event.transaction.hash + + entity.save() +} + +export function handleTransfer(event: TransferEvent): void { + let entity = new Transfer( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ) + entity.from = event.params.from + entity.to = event.params.to + entity.value = event.params.value + + entity.blockNumber = event.block.number + entity.blockTimestamp = event.block.timestamp + entity.transactionHash = event.transaction.hash + + entity.save() +} + +export function handleUnitAdded(event: UnitAddedEvent): void { + let entity = new UnitAdded( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ) + entity.player = event.params.player + entity.unitType = event.params.unitType + entity.nUnits = event.params.nUnits + entity.cost = event.params.cost + entity.geldBalance = event.params.geldBalance + entity.molochDenierLevel = event.params.molochDenierLevel + entity.apprenticeLevel = event.params.apprenticeLevel + entity.anointedLevel = event.params.anointedLevel + entity.championLevel = event.params.championLevel + + entity.blockNumber = event.block.number + entity.blockTimestamp = event.block.timestamp + entity.transactionHash = event.transaction.hash + + entity.save() +} diff --git a/subgraph.yaml b/subgraph.yaml new file mode 100644 index 0000000..d144e63 --- /dev/null +++ b/subgraph.yaml @@ -0,0 +1,41 @@ +specVersion: 1.0.0 +indexerHints: + prune: auto +schema: + file: ./schema.graphql +dataSources: + - kind: ethereum + name: RaidGeld + network: base-sepolia + source: + address: "0xC912823B231f5d052eF334a8f8B73F6354909cd0" + abi: RaidGeld + startBlock: 17213030 + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - Approval + - OwnershipTransferred + - PlayerRegistered + - RaidPerformed + - Transfer + - UnitAdded + abis: + - name: RaidGeld + file: ./abis/RaidGeld.json + eventHandlers: + - event: Approval(indexed address,indexed address,uint256) + handler: handleApproval + - event: OwnershipTransferred(indexed address,indexed address) + handler: handleOwnershipTransferred + - event: PlayerRegistered(indexed address,uint256) + handler: handlePlayerRegistered + - event: RaidPerformed(indexed address,uint256,uint256) + handler: handleRaidPerformed + - event: Transfer(indexed address,indexed address,uint256) + handler: handleTransfer + - event: UnitAdded(indexed address,uint8,uint16,uint256,uint256,uint16,uint16,uint16,uint16) + handler: handleUnitAdded + file: ./src/raid-geld.ts diff --git a/tests/raid-geld-utils.ts b/tests/raid-geld-utils.ts new file mode 100644 index 0000000..b6cd9ea --- /dev/null +++ b/tests/raid-geld-utils.ts @@ -0,0 +1,193 @@ +import { newMockEvent } from "matchstick-as" +import { ethereum, Address, BigInt } from "@graphprotocol/graph-ts" +import { + Approval, + OwnershipTransferred, + PlayerRegistered, + RaidPerformed, + Transfer, + UnitAdded +} from "../generated/RaidGeld/RaidGeld" + +export function createApprovalEvent( + owner: Address, + spender: Address, + value: BigInt +): Approval { + let approvalEvent = changetype(newMockEvent()) + + approvalEvent.parameters = new Array() + + approvalEvent.parameters.push( + new ethereum.EventParam("owner", ethereum.Value.fromAddress(owner)) + ) + approvalEvent.parameters.push( + new ethereum.EventParam("spender", ethereum.Value.fromAddress(spender)) + ) + approvalEvent.parameters.push( + new ethereum.EventParam("value", ethereum.Value.fromUnsignedBigInt(value)) + ) + + return approvalEvent +} + +export function createOwnershipTransferredEvent( + previousOwner: Address, + newOwner: Address +): OwnershipTransferred { + let ownershipTransferredEvent = changetype( + newMockEvent() + ) + + ownershipTransferredEvent.parameters = new Array() + + ownershipTransferredEvent.parameters.push( + new ethereum.EventParam( + "previousOwner", + ethereum.Value.fromAddress(previousOwner) + ) + ) + ownershipTransferredEvent.parameters.push( + new ethereum.EventParam("newOwner", ethereum.Value.fromAddress(newOwner)) + ) + + return ownershipTransferredEvent +} + +export function createPlayerRegisteredEvent( + player: Address, + initialGeld: BigInt +): PlayerRegistered { + let playerRegisteredEvent = changetype(newMockEvent()) + + playerRegisteredEvent.parameters = new Array() + + playerRegisteredEvent.parameters.push( + new ethereum.EventParam("player", ethereum.Value.fromAddress(player)) + ) + playerRegisteredEvent.parameters.push( + new ethereum.EventParam( + "initialGeld", + ethereum.Value.fromUnsignedBigInt(initialGeld) + ) + ) + + return playerRegisteredEvent +} + +export function createRaidPerformedEvent( + player: Address, + totalMinted: BigInt, + geldBalance: BigInt +): RaidPerformed { + let raidPerformedEvent = changetype(newMockEvent()) + + raidPerformedEvent.parameters = new Array() + + raidPerformedEvent.parameters.push( + new ethereum.EventParam("player", ethereum.Value.fromAddress(player)) + ) + raidPerformedEvent.parameters.push( + new ethereum.EventParam( + "totalMinted", + ethereum.Value.fromUnsignedBigInt(totalMinted) + ) + ) + raidPerformedEvent.parameters.push( + new ethereum.EventParam( + "geldBalance", + ethereum.Value.fromUnsignedBigInt(geldBalance) + ) + ) + + return raidPerformedEvent +} + +export function createTransferEvent( + from: Address, + to: Address, + value: BigInt +): Transfer { + let transferEvent = changetype(newMockEvent()) + + transferEvent.parameters = new Array() + + transferEvent.parameters.push( + new ethereum.EventParam("from", ethereum.Value.fromAddress(from)) + ) + transferEvent.parameters.push( + new ethereum.EventParam("to", ethereum.Value.fromAddress(to)) + ) + transferEvent.parameters.push( + new ethereum.EventParam("value", ethereum.Value.fromUnsignedBigInt(value)) + ) + + return transferEvent +} + +export function createUnitAddedEvent( + player: Address, + unitType: i32, + nUnits: i32, + cost: BigInt, + geldBalance: BigInt, + molochDenierLevel: i32, + apprenticeLevel: i32, + anointedLevel: i32, + championLevel: i32 +): UnitAdded { + let unitAddedEvent = changetype(newMockEvent()) + + unitAddedEvent.parameters = new Array() + + unitAddedEvent.parameters.push( + new ethereum.EventParam("player", ethereum.Value.fromAddress(player)) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "unitType", + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(unitType)) + ) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "nUnits", + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(nUnits)) + ) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam("cost", ethereum.Value.fromUnsignedBigInt(cost)) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "geldBalance", + ethereum.Value.fromUnsignedBigInt(geldBalance) + ) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "molochDenierLevel", + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(molochDenierLevel)) + ) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "apprenticeLevel", + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(apprenticeLevel)) + ) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "anointedLevel", + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(anointedLevel)) + ) + ) + unitAddedEvent.parameters.push( + new ethereum.EventParam( + "championLevel", + ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(championLevel)) + ) + ) + + return unitAddedEvent +} diff --git a/tests/raid-geld.test.ts b/tests/raid-geld.test.ts new file mode 100644 index 0000000..dd5751e --- /dev/null +++ b/tests/raid-geld.test.ts @@ -0,0 +1,62 @@ +import { + assert, + describe, + test, + clearStore, + beforeAll, + afterAll +} from "matchstick-as/assembly/index" +import { Address, BigInt } from "@graphprotocol/graph-ts" +import { Approval } from "../generated/schema" +import { Approval as ApprovalEvent } from "../generated/RaidGeld/RaidGeld" +import { handleApproval } from "../src/raid-geld" +import { createApprovalEvent } from "./raid-geld-utils" + +// Tests structure (matchstick-as >=0.5.0) +// https://thegraph.com/docs/en/developer/matchstick/#tests-structure-0-5-0 + +describe("Describe entity assertions", () => { + beforeAll(() => { + let owner = Address.fromString("0x0000000000000000000000000000000000000001") + let spender = Address.fromString( + "0x0000000000000000000000000000000000000001" + ) + let value = BigInt.fromI32(234) + let newApprovalEvent = createApprovalEvent(owner, spender, value) + handleApproval(newApprovalEvent) + }) + + afterAll(() => { + clearStore() + }) + + // For more test scenarios, see: + // https://thegraph.com/docs/en/developer/matchstick/#write-a-unit-test + + test("Approval created and stored", () => { + assert.entityCount("Approval", 1) + + // 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function + assert.fieldEquals( + "Approval", + "0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1", + "owner", + "0x0000000000000000000000000000000000000001" + ) + assert.fieldEquals( + "Approval", + "0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1", + "spender", + "0x0000000000000000000000000000000000000001" + ) + assert.fieldEquals( + "Approval", + "0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1", + "value", + "234" + ) + + // More assert options: + // https://thegraph.com/docs/en/developer/matchstick/#asserts + }) +}) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4e86672 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "@graphprotocol/graph-ts/types/tsconfig.base.json", + "include": ["src", "tests"] +}