Skip to content
Open

Ed #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion coffee-subgraph/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coffee-subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"dependencies": {
"@graphprotocol/graph-cli": "0.33.1",
"@graphprotocol/graph-ts": "0.27.0",
"dotenv": "^16.0.3"
"dotenv": "^16.0.3",
"matchstick": "^1.2.1"
},
"devDependencies": {
"matchstick-as": "0.5.0"
Expand Down
18 changes: 10 additions & 8 deletions coffee-subgraph/src/coffee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ export function handleworkerCheckedIn(event: workerCheckedIn): void {
}

export function handleworkerPaid(event: workerPaid): void {
for (var i = 0; i< event.params.worker.length; i++) {

for (let i = 0; i< event.params.worker.length; i++) {
let thisWorker = event.params.worker[i];
let thisPayment = event.params.amount[i];

Expand All @@ -201,13 +202,14 @@ export function handleworkerPaid(event: workerPaid): void {
// We are assuming that a worker will never be paid before being
// checked in, so the worker should already exist.
if(!worker){
worker = new Worker(thisWorker.toHex());
worker.daysWorked = 1;
worker.daysUnpaid = 1;
// worker.payments = [];
worker.hasForeman = [];
// worker.checkIns = [];
log.critical("Worker was paid before being checked in", []);
continue;
// worker = new Worker(thisWorker.toHex());
// worker.daysWorked = 1;
// worker.daysUnpaid = 1;
// // worker.payments = [];
// worker.hasForeman = [];
// // worker.checkIns = [];
// log.critical("Worker was paid before being checked in", []);
}
// Worker is now paid for all their previous days unpaid
let oldDaysUnpaid = worker.daysUnpaid;
Expand Down
13 changes: 7 additions & 6 deletions coffee-subgraph/tests/coffee-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function createnewForemanEvent(

export function createworkerCheckedInEvent(
foreman: Address,
worker: Address,
worker: Array<Address>,
date: string
): workerCheckedIn {
let workerCheckedInEvent = changetype<workerCheckedIn>(newMockEvent())
Expand All @@ -80,7 +80,7 @@ export function createworkerCheckedInEvent(
new ethereum.EventParam("foreman", ethereum.Value.fromAddress(foreman))
)
workerCheckedInEvent.parameters.push(
new ethereum.EventParam("worker", ethereum.Value.fromAddress(worker))
new ethereum.EventParam("worker", ethereum.Value.fromAddressArray(worker))
)
workerCheckedInEvent.parameters.push(
new ethereum.EventParam("date", ethereum.Value.fromString(date))
Expand All @@ -91,8 +91,8 @@ export function createworkerCheckedInEvent(

export function createworkerPaidEvent(
farm: Address,
worker: Address,
amount: BigInt,
worker: Array<Address>,
amount: Array<BigInt>,
date: string
): workerPaid {
let workerPaidEvent = changetype<workerPaid>(newMockEvent())
Expand All @@ -103,10 +103,11 @@ export function createworkerPaidEvent(
new ethereum.EventParam("farm", ethereum.Value.fromAddress(farm))
)
workerPaidEvent.parameters.push(
new ethereum.EventParam("worker", ethereum.Value.fromAddress(worker))
new ethereum.EventParam("worker", ethereum.Value.fromAddressArray(worker))

)
workerPaidEvent.parameters.push(
new ethereum.EventParam("amount", ethereum.Value.fromUnsignedBigInt(amount))
new ethereum.EventParam("amount", ethereum.Value.fromUnsignedBigIntArray(amount))
)
workerPaidEvent.parameters.push(
new ethereum.EventParam("date", ethereum.Value.fromString(date))
Expand Down
197 changes: 151 additions & 46 deletions coffee-subgraph/tests/coffee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,162 @@ import {
test,
clearStore,
beforeAll,
afterAll
afterAll,

} from "matchstick-as/assembly/index"
import { Address, BigInt } from "@graphprotocol/graph-ts"
import { ExampleEntity } from "../generated/schema"
// import { ExampleEntity } from "../generated/schema"
import { OwnershipTransferred } from "../generated/coffee/coffee"
import { handleOwnershipTransferred } from "../src/coffee"
import { createOwnershipTransferredEvent } from "./coffee-utils"
import { handleOwnershipTransferred, handleworkerCheckedIn, handleworkerPaid } from "../src/coffee"
import { createnewForemanEvent, createOwnershipTransferredEvent } from "./coffee-utils"
import {Farm, Foreman} from "../generated/schema"
import {createnewFarmEvent, createworkerPaidEvent, createworkerCheckedInEvent} from "./coffee-utils"
import {handlenewFarm,handlenewForeman} from "../src/coffee"

// 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 previousOwner = Address.fromString(
"0x0000000000000000000000000000000000000001"
)
let newOwner = Address.fromString(
"0x0000000000000000000000000000000000000001"
)
let newOwnershipTransferredEvent = createOwnershipTransferredEvent(
previousOwner,
newOwner
)
handleOwnershipTransferred(newOwnershipTransferredEvent)
})

afterAll(() => {
clearStore()
})

// For more test scenarios, see:
// https://thegraph.com/docs/en/developer/matchstick/#write-a-unit-test

test("ExampleEntity created and stored", () => {
assert.entityCount("ExampleEntity", 1)

// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
assert.fieldEquals(
"ExampleEntity",
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a",
"previousOwner",
"0x0000000000000000000000000000000000000001"
)
assert.fieldEquals(
"ExampleEntity",
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a",
"newOwner",
"0x0000000000000000000000000000000000000001"
)

// More assert options:
// https://thegraph.com/docs/en/developer/matchstick/#asserts
})

beforeAll(()=>{
//create farm to be used in tests below
let farmAddress = Address.fromString("0x0000000000000000000000000000000000000001");
let farm = createnewFarmEvent (farmAddress);
handlenewFarm (farm);

//create forman to be used below
let foremanAddress = Address.fromString("0x0000000000000000000000000000000000000002");
let foreman = createnewForemanEvent(farmAddress,foremanAddress);
handlenewForeman(foreman);


//Check in a worker to be used below
let workerAddress = Address.fromString("0x0000000000000000000000000000000000000005");
let workerArray : Array<Address> = [workerAddress];
let checkin = createworkerCheckedInEvent(foremanAddress, workerArray, "2023/02/23" );
handleworkerCheckedIn(checkin);



})

//after all tests, clear
afterAll(()=>{
clearStore()
})

//creates farm and tests that it exists and its id is the wallet address
test ("Creating farm",()=>{

let address = Address.fromString("0x0000000000000000000000000000000000000003");

let anotherFarm = createnewFarmEvent (address);

handlenewFarm (anotherFarm);

assert.fieldEquals("Farm", "0x0000000000000000000000000000000000000003", "id", "0x0000000000000000000000000000000000000003");


})

//create foremand and tests that its id is its address
test("Creating a foreman",()=>{
let farmAddress = Address.fromString("0x0000000000000000000000000000000000000001");
let foremanAddress = Address.fromString("0x0000000000000000000000000000000000000004");


let foreman = createnewForemanEvent(farmAddress,foremanAddress);
handlenewForeman(foreman);
assert.fieldEquals("Foreman","0x0000000000000000000000000000000000000004", "id", "0x0000000000000000000000000000000000000004");
})

//checks in a worker
test("Worker Check in", ()=>{
let workerAddress = Address.fromString("0x0000000000000000000000000000000000000005");
let foremanAddress = Address.fromString("0x0000000000000000000000000000000000000002");
let workerArray : Array<Address> = [workerAddress];
let checkin = createworkerCheckedInEvent(foremanAddress, workerArray, "2023/02/23" );
handleworkerCheckedIn(checkin);
}
)


test("Paying a worker before first check in ", ()=>{
let farmAddress = Address.fromString("0x0000000000000000000000000000000000000001");
let workerAddress1 = Address.fromString("0x0000000000000000000000000000000000000005");
let workerAddress2 = Address.fromString("0x0000000000000000000000000000000000000006");
let workerArray : Array<Address> = [workerAddress1, workerAddress2];
let payment = BigInt.fromI32(1);
let paymentArray : Array<BigInt> = [payment,payment];
let day = "2023/02/23";
let paid = createworkerPaidEvent(farmAddress,workerArray,paymentArray, day);

handleworkerPaid(paid);

}
)

test("Checking in a worker from a non-forman address", ()=>{

//this forman address is not a logged forman address
let foremanAddress = Address.fromString("0x0000000000000000000000000000000000000995")

//valid worker address from beforeAll()
let workerAddress = Address.fromString("0x0000000000000000000000000000000000000005");
let workerArray : Array<Address> = [workerAddress];

//create checkin event and handle it
let checkin = createworkerCheckedInEvent(foremanAddress, workerArray, "2023/03/03");
handleworkerCheckedIn(checkin);



}
)





// describe("Describe entity assertions", () => {
// beforeAll(() => {
// let previousOwner = Address.fromString(
// "0x0000000000000000000000000000000000000001"
// )
// let newOwner = Address.fromString(
// "0x0000000000000000000000000000000000000001"
// )
// let newOwnershipTransferredEvent = createOwnershipTransferredEvent(
// previousOwner,
// newOwner
// )
// handleOwnershipTransferred(newOwnershipTransferredEvent)
// })

// afterAll(() => {
// clearStore()
// })

// // For more test scenarios, see:
// // https://thegraph.com/docs/en/developer/matchstick/#write-a-unit-test

// test("ExampleEntity created and stored", () => {
// assert.entityCount("ExampleEntity", 1)

// // 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
// assert.fieldEquals(
// "ExampleEntity",
// "0xa16081f360e3847006db660bae1c6d1b2e17ec2a",
// "previousOwner",
// "0x0000000000000000000000000000000000000001"
// )
// assert.fieldEquals(
// "ExampleEntity",
// "0xa16081f360e3847006db660bae1c6d1b2e17ec2a",
// "newOwner",
// "0x0000000000000000000000000000000000000001"
// )

// // More assert options:
// // https://thegraph.com/docs/en/developer/matchstick/#asserts
// })
// })
5 changes: 5 additions & 0 deletions coffee-subgraph/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,11 @@
"assemblyscript" "^0.19.20"
"wabt" "1.0.24"

"matchstick@^1.2.1":
"integrity" "sha512-shyh9IWHF0XbWZ3zK4pcIWTkick+qcNIV+ZWNcFHgwFG3fxUxksdYkA46FlkTpKltG3ug/b1ZKq/tsNMfQyv4A=="
"resolved" "https://registry.npmjs.org/matchstick/-/matchstick-1.2.1.tgz"
"version" "1.2.1"

"md5.js@^1.3.4":
"integrity" "sha512-xitP+WxNPcTTOgnTJcrhM0xvdPepipPSf3I8EIpGKeFLjt3PlJLIDG3u8EX53ZIubkb+5U2+3rELYpEhHhzdkg=="
"resolved" "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz"
Expand Down
Loading