Skip to content

Commit

Permalink
passing transfer test
Browse files Browse the repository at this point in the history
  • Loading branch information
GundamDweeb committed May 17, 2022
1 parent b4e5bb9 commit f9dfc70
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 27 deletions.
17 changes: 12 additions & 5 deletions generated/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export class Collectible extends Entity {
this.set("owner", Value.fromString(""));
this.set("creator", Value.fromString(""));
this.set("tokenId", Value.fromBigInt(BigInt.zero()));
this.set("descriptorURI", Value.fromString(""));
}

save(): void {
Expand Down Expand Up @@ -129,13 +128,21 @@ export class Collectible extends Entity {
this.set("tokenId", Value.fromBigInt(value));
}

get descriptorURI(): string {
get descriptorURI(): string | null {
let value = this.get("descriptorURI");
return value!.toString();
if (!value || value.kind == ValueKind.NULL) {
return null;
} else {
return value.toString();
}
}

set descriptorURI(value: string) {
this.set("descriptorURI", Value.fromString(value));
set descriptorURI(value: string | null) {
if (!value) {
this.unset("descriptorURI");
} else {
this.set("descriptorURI", Value.fromString(<string>value));
}
}

get modified(): BigInt | null {
Expand Down
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Collectible @entity {
creator: Account!

tokenId: BigInt!
descriptorURI: String!
descriptorURI: String

#timestamps
modified: BigInt
Expand Down
16 changes: 8 additions & 8 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export function handleTransfer(event: Transfer): void {
collectible.creator = to.id;
collectible.created = event.block.timestamp;

let erc721Contract = ERC721.bind(event.address);
let descriptorUri = erc721Contract.try_tokenURI(event.params.tokenId);
if(!descriptorUri.reverted)
{
collectible.descriptorURI = descriptorUri.value;
}else{
collectible.descriptorURI = "";
}
// let erc721Contract = ERC721.bind(event.address);
// let descriptorUri = erc721Contract.try_tokenURI(event.params.tokenId);
// if(!descriptorUri.reverted)
// {
// collectible.descriptorURI = descriptorUri.value;
// }else{
// collectible.descriptorURI = "";
// }
collectible.save()
log.info("MINT EVENT - TOKENID {}, TxHash {}", [collectibleId, event.transaction.hash.toHexString()])
}
Expand Down
Binary file added tests/.bin/erc721.wasm
Binary file not shown.
21 changes: 21 additions & 0 deletions tests/.docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM --platform=linux/x86_64 ubuntu:20.04

ARG DEBIAN_FRONTEND=noninteractive

ENV ARGS=""

RUN apt update \
&& apt install -y sudo curl postgresql \
&& curl -fsSL https://deb.nodesource.com/setup_16.x | sudo bash - \
&& sudo apt install -y nodejs

RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/0.4.4/binary-linux-20 \
&& chmod a+x binary-linux-20

RUN mkdir matchstick
WORKDIR /matchstick

# Commenting out for now as it seems there's no need to copy when using bind mount
# COPY ./ .

CMD ../binary-linux-20 ${ARGS}
26 changes: 18 additions & 8 deletions tests/erc721.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Address, ethereum, BigInt } from "@graphprotocol/graph-ts";
import { clearStore, test, assert, describe, beforeEach, newMockEvent } from "matchstick-as/assembly/index";
import { Collection } from "../generated/schema";
import { createCollection, createNewMockTransferEvent } from "./helper";
import {
Transfer
} from "../generated/ERC721/ERC721"
import { Address, BigInt } from "@graphprotocol/graph-ts";
import { test, describe, beforeEach, assert } from "matchstick-as/assembly/index";
import { ADDRESS_ZERO, colllectionAddress1, createCollection, createNewMockTransferEvent } from "./helper";
import {handleTransfer} from "../src/mapping"

describe("ERC721 Tests", () => {
beforeEach(() => {
createCollection();
})
test("Transfer - Mint", () => {
let from = Address.fromString("0x28c6c06298d514db089934071355e5743bf21d60");
// Arrange
let from = ADDRESS_ZERO;
let to = Address.fromString("0x0dda28d190b17ba5a8774064de4386347b33220e");
let tokenId = BigInt.fromString("1");
let mockTransferEvent = createNewMockTransferEvent(from, to, tokenId);

assert.entityCount("Collection", 1);

// Act
handleTransfer(mockTransferEvent);

let expectedCollectibleId = colllectionAddress1.toHex() + "-" + tokenId.toHex();

// Assert
assert.entityCount("Account", 2);
assert.fieldEquals("Collectible", expectedCollectibleId, "owner", to.toHexString());
assert.fieldEquals("Collectible", expectedCollectibleId, "creator", to.toHexString());
})
})

10 changes: 5 additions & 5 deletions tests/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { newMockEvent } from "matchstick-as";
import { Transfer } from "../generated/ERC721/ERC721";
import { Collection } from "../generated/schema";

export const ADDRESS_ZERO = Address.fromString("0x0000000000000000000000000000000000000000");
export const colllectionAddress1 = Address.fromString("0xba5bde662c17e2adff1075610382b9b691296350");

const colllectionAddress1 = Address.fromString("0xba5bde662c17e2adff1075610382b9b691296350");

export function createCollection() {
export function createCollection() : void {
let collection = new Collection(colllectionAddress1.toHex())
collection.collectionAddress = colllectionAddress1;
collection.collectionName = "SUPER";
collection.collectionSymbol = "SPR";
collection.save();
}

export function createNewMockTransferEvent(from: Address, to: Address, tokenId: BigInt) {
export function createNewMockTransferEvent(from: Address, to: Address, tokenId: BigInt) : Transfer {
let mockEvent = newMockEvent();
let fromParam = new ethereum.EventParam("from", ethereum.Value.fromAddress(from));
let toParam = new ethereum.EventParam("to", ethereum.Value.fromAddress(to));
let tokenIdParam = new ethereum.EventParam("tokenId", ethereum.Value.fromSignedBigInt(tokenId));

let mockTransferEvent = new Transfer(
mockEvent.address,
colllectionAddress1,
mockEvent.logIndex,
mockEvent.transactionLogIndex,
mockEvent.logType,
Expand Down

0 comments on commit f9dfc70

Please sign in to comment.