diff --git a/README.md b/README.md index 012df2f..01f9853 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ type Earning @entity { type FeeReceipt @entity { id: ID! - timestamp: BigInt! + timestamp: BigInt! isInventory: Boolean! vault: Vault! amount: BigInt diff --git a/package.json b/package.json index 3e7be78..f537243 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@graphprotocol/graph-cli": "0.31.0", - "@graphprotocol/graph-ts": "0.27.0" + "@graphprotocol/graph-ts": "0.27.0", + "matchstick-as": "0.5.0" } } diff --git a/tests/inventory-staking-utils.ts b/tests/inventory-staking-utils.ts new file mode 100644 index 0000000..cadc09c --- /dev/null +++ b/tests/inventory-staking-utils.ts @@ -0,0 +1,212 @@ +import { newMockEvent } from "matchstick-as" +import { ethereum, BigInt, Address } from "@graphprotocol/graph-ts" +import { + Deposit, + OwnershipTransferred, + SetIsGuardian, + SetPaused, + Upgraded, + Withdraw, + XTokenCreated, + FeesReceived +} from "../generated/InventoryStaking/InventoryStaking" + +export function createDepositEvent( + vaultId: BigInt, + baseTokenAmount: BigInt, + xTokenAmount: BigInt, + timelockUntil: BigInt, + sender: Address +): Deposit { + let depositEvent = changetype(newMockEvent()) + + depositEvent.parameters = new Array() + + depositEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + depositEvent.parameters.push( + new ethereum.EventParam( + "baseTokenAmount", + ethereum.Value.fromUnsignedBigInt(baseTokenAmount) + ) + ) + depositEvent.parameters.push( + new ethereum.EventParam( + "xTokenAmount", + ethereum.Value.fromUnsignedBigInt(xTokenAmount) + ) + ) + depositEvent.parameters.push( + new ethereum.EventParam( + "timelockUntil", + ethereum.Value.fromUnsignedBigInt(timelockUntil) + ) + ) + depositEvent.parameters.push( + new ethereum.EventParam("sender", ethereum.Value.fromAddress(sender)) + ) + + return depositEvent +} + +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 createSetIsGuardianEvent( + addr: Address, + isGuardian: boolean +): SetIsGuardian { + let setIsGuardianEvent = changetype(newMockEvent()) + + setIsGuardianEvent.parameters = new Array() + + setIsGuardianEvent.parameters.push( + new ethereum.EventParam("addr", ethereum.Value.fromAddress(addr)) + ) + setIsGuardianEvent.parameters.push( + new ethereum.EventParam( + "isGuardian", + ethereum.Value.fromBoolean(isGuardian) + ) + ) + + return setIsGuardianEvent +} + +export function createSetPausedEvent( + lockId: BigInt, + paused: boolean +): SetPaused { + let setPausedEvent = changetype(newMockEvent()) + + setPausedEvent.parameters = new Array() + + setPausedEvent.parameters.push( + new ethereum.EventParam("lockId", ethereum.Value.fromUnsignedBigInt(lockId)) + ) + setPausedEvent.parameters.push( + new ethereum.EventParam("paused", ethereum.Value.fromBoolean(paused)) + ) + + return setPausedEvent +} + +export function createUpgradedEvent(childImplementation: Address): Upgraded { + let upgradedEvent = changetype(newMockEvent()) + + upgradedEvent.parameters = new Array() + + upgradedEvent.parameters.push( + new ethereum.EventParam( + "childImplementation", + ethereum.Value.fromAddress(childImplementation) + ) + ) + + return upgradedEvent +} + +export function createWithdrawEvent( + vaultId: BigInt, + baseTokenAmount: BigInt, + xTokenAmount: BigInt, + sender: Address +): Withdraw { + let withdrawEvent = changetype(newMockEvent()) + + withdrawEvent.parameters = new Array() + + withdrawEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + withdrawEvent.parameters.push( + new ethereum.EventParam( + "baseTokenAmount", + ethereum.Value.fromUnsignedBigInt(baseTokenAmount) + ) + ) + withdrawEvent.parameters.push( + new ethereum.EventParam( + "xTokenAmount", + ethereum.Value.fromUnsignedBigInt(xTokenAmount) + ) + ) + withdrawEvent.parameters.push( + new ethereum.EventParam("sender", ethereum.Value.fromAddress(sender)) + ) + + return withdrawEvent +} + +export function createXTokenCreatedEvent( + vaultId: BigInt, + baseToken: Address, + xToken: Address +): XTokenCreated { + let xTokenCreatedEvent = changetype(newMockEvent()) + + xTokenCreatedEvent.parameters = new Array() + + xTokenCreatedEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + xTokenCreatedEvent.parameters.push( + new ethereum.EventParam("baseToken", ethereum.Value.fromAddress(baseToken)) + ) + xTokenCreatedEvent.parameters.push( + new ethereum.EventParam("xToken", ethereum.Value.fromAddress(xToken)) + ) + + return xTokenCreatedEvent +} + +export function createFeesReceivedEvent( + vaultId: BigInt, + amount: BigInt +): FeesReceived { + let feesReceivedEvent = changetype(newMockEvent()) + + feesReceivedEvent.parameters = new Array() + + feesReceivedEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + feesReceivedEvent.parameters.push( + new ethereum.EventParam("amount", ethereum.Value.fromUnsignedBigInt(amount)) + ) + + return feesReceivedEvent +} diff --git a/tests/inventory-staking.test.ts b/tests/inventory-staking.test.ts new file mode 100644 index 0000000..b03866e --- /dev/null +++ b/tests/inventory-staking.test.ts @@ -0,0 +1,82 @@ +import { + assert, + describe, + test, + clearStore, + beforeAll, + afterAll +} from "matchstick-as/assembly/index" +import { BigInt, Address } from "@graphprotocol/graph-ts" +import { Deposit } from "../generated/InventoryStaking/InventoryStaking" +import { handleDeposit } from "../src/inventory-staking" +import { createDepositEvent } from "./inventory-staking-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 vaultId = BigInt.fromI32(234) + let baseTokenAmount = BigInt.fromI32(234) + let xTokenAmount = BigInt.fromI32(234) + let timelockUntil = BigInt.fromI32(234) + let sender = Address.fromString( + "0x0000000000000000000000000000000000000001" + ) + let newDepositEvent = createDepositEvent( + vaultId, + baseTokenAmount, + xTokenAmount, + timelockUntil, + sender + ) + handleDeposit(newDepositEvent) + }) + + afterAll(() => { + clearStore() + }) + + test("ExampleEntity created and stored", () => { + assert.entityCount("ExampleEntity", 1) + + // 0xA16081F360e3847006dB660bae1c6d1b2e17eC2A is the default address used in newMockEvent() function + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "vaultId", + "234" + ) + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "baseTokenAmount", + "234" + ) + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "xTokenAmount", + "234" + ) + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "timelockUntil", + "234" + ) + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "sender", + "0x0000000000000000000000000000000000000001" + ) + }) +}) diff --git a/tests/lp-staking-utils.ts b/tests/lp-staking-utils.ts new file mode 100644 index 0000000..7e03c41 --- /dev/null +++ b/tests/lp-staking-utils.ts @@ -0,0 +1,135 @@ +import { newMockEvent } from "matchstick-as" +import { ethereum, BigInt, Address } from "@graphprotocol/graph-ts" +import { + FeesReceived, + OwnershipTransferred, + PoolCreated, + PoolUpdated, + SetIsGuardian, + SetPaused +} from "../generated/LPStaking/LPStaking" + +export function createFeesReceivedEvent( + vaultId: BigInt, + amount: BigInt +): FeesReceived { + let feesReceivedEvent = changetype(newMockEvent()) + + feesReceivedEvent.parameters = new Array() + + feesReceivedEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + feesReceivedEvent.parameters.push( + new ethereum.EventParam("amount", ethereum.Value.fromUnsignedBigInt(amount)) + ) + + return feesReceivedEvent +} + +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 createPoolCreatedEvent( + vaultId: BigInt, + pool: Address +): PoolCreated { + let poolCreatedEvent = changetype(newMockEvent()) + + poolCreatedEvent.parameters = new Array() + + poolCreatedEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + poolCreatedEvent.parameters.push( + new ethereum.EventParam("pool", ethereum.Value.fromAddress(pool)) + ) + + return poolCreatedEvent +} + +export function createPoolUpdatedEvent( + vaultId: BigInt, + pool: Address +): PoolUpdated { + let poolUpdatedEvent = changetype(newMockEvent()) + + poolUpdatedEvent.parameters = new Array() + + poolUpdatedEvent.parameters.push( + new ethereum.EventParam( + "vaultId", + ethereum.Value.fromUnsignedBigInt(vaultId) + ) + ) + poolUpdatedEvent.parameters.push( + new ethereum.EventParam("pool", ethereum.Value.fromAddress(pool)) + ) + + return poolUpdatedEvent +} + +export function createSetIsGuardianEvent( + addr: Address, + isGuardian: boolean +): SetIsGuardian { + let setIsGuardianEvent = changetype(newMockEvent()) + + setIsGuardianEvent.parameters = new Array() + + setIsGuardianEvent.parameters.push( + new ethereum.EventParam("addr", ethereum.Value.fromAddress(addr)) + ) + setIsGuardianEvent.parameters.push( + new ethereum.EventParam( + "isGuardian", + ethereum.Value.fromBoolean(isGuardian) + ) + ) + + return setIsGuardianEvent +} + +export function createSetPausedEvent( + lockId: BigInt, + paused: boolean +): SetPaused { + let setPausedEvent = changetype(newMockEvent()) + + setPausedEvent.parameters = new Array() + + setPausedEvent.parameters.push( + new ethereum.EventParam("lockId", ethereum.Value.fromUnsignedBigInt(lockId)) + ) + setPausedEvent.parameters.push( + new ethereum.EventParam("paused", ethereum.Value.fromBoolean(paused)) + ) + + return setPausedEvent +} diff --git a/tests/lp-staking.test.ts b/tests/lp-staking.test.ts new file mode 100644 index 0000000..9f9d295 --- /dev/null +++ b/tests/lp-staking.test.ts @@ -0,0 +1,50 @@ +import { + assert, + describe, + test, + clearStore, + beforeAll, + afterAll +} from "matchstick-as/assembly/index" +import { BigInt, Address } from "@graphprotocol/graph-ts" +import { FeesReceived } from "../generated/LPStaking/LPStaking" +import { handleFeesReceived } from "../src/lp-staking" +import { createFeesReceivedEvent } from "./lp-staking-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 vaultId = BigInt.fromI32(234) + let amount = BigInt.fromI32(234) + let newFeesReceivedEvent = createFeesReceivedEvent(vaultId, amount) + handleFeesReceived(newFeesReceivedEvent) + }) + + afterAll(() => { + clearStore() + }) + + test("ExampleEntity created and stored", () => { + assert.entityCount("ExampleEntity", 1) + + // 0xA16081F360e3847006dB660bae1c6d1b2e17eC2A is the default address used in newMockEvent() function + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "vaultId", + "234" + ) + + assert.fieldEquals( + "ExampleEntity", + "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A", + "amount", + "234" + ) + }) +})