Skip to content

Commit

Permalink
test helpers and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
GundamDweeb committed Jul 1, 2022
1 parent 183a23d commit e874029
Show file tree
Hide file tree
Showing 6 changed files with 482 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type Earning @entity {
type FeeReceipt @entity {
id: ID!
timestamp: BigInt!
timestamp: BigInt!
isInventory: Boolean!
vault: Vault!
amount: BigInt
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
212 changes: 212 additions & 0 deletions tests/inventory-staking-utils.ts
Original file line number Diff line number Diff line change
@@ -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<Deposit>(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<OwnershipTransferred>(
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<SetIsGuardian>(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<SetPaused>(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<Upgraded>(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<Withdraw>(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<XTokenCreated>(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<FeesReceived>(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
}
82 changes: 82 additions & 0 deletions tests/inventory-staking.test.ts
Original file line number Diff line number Diff line change
@@ -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"
)
})
})
Loading

0 comments on commit e874029

Please sign in to comment.