Commit 6cf4c21f authored by vicotor's avatar vicotor

add token owners

parent 91386f8b
......@@ -522,3 +522,66 @@ export class Minted extends Entity {
this.set("transactionHash", Value.fromBytes(value));
}
}
// add a entity to store token id to owner, id use token id, and owner use address.
export class TokenOwner extends Entity {
constructor(id: BigInt) {
super();
this.set("id", Value.fromBigInt(id));
}
save(): void {
let id = this.get("id");
assert(id != null, "Cannot save TokenOwner entity without an ID");
if (id) {
assert(
id.kind == ValueKind.BIGINT,
`Entities of type TokenOwner must have an ID of type BigInt but the id '${id.displayData()}' is of type ${id.displayKind()}`,
);
store.set("TokenOwner", id.toString(), this);
}
}
static load(id: BigInt): TokenOwner | null {
return changetype<TokenOwner | null>(store.get("TokenOwner", id.toString()));
}
get id(): BigInt {
let value = this.get("id");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBigInt();
}
}
set id(value: BigInt) {
this.set("id", Value.fromBigInt(value));
}
get owner(): Bytes {
let value = this.get("owner");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBytes();
}
}
set owner(value: Bytes) {
this.set("owner", Value.fromBytes(value));
}
get tokenId(): BigInt {
let value = this.get("tokenId");
if (!value || value.kind == ValueKind.NULL) {
throw new Error("Cannot return null for a required field.");
} else {
return value.toBigInt();
}
}
set tokenId(value: BigInt) {
this.set("tokenId", Value.fromBigInt(value));
}
}
\ No newline at end of file
......@@ -39,3 +39,10 @@ type ApprovalForAll @entity(immutable: true) {
blockTimestamp: BigInt!
transactionHash: Bytes!
}
// add TokenOwner entity
type TokenTransfer @entity(immutable: true) {
id: BigInt!
owner: Bytes! #address
tokenId: BigInt! # uint256
}
\ No newline at end of file
......@@ -3,7 +3,7 @@ import {
Approval as ApprovalEvent,
ApprovalForAll as ApprovalForAllEvent
} from "../generated/Drago/Drago"
import { Transfer, Approval, ApprovalForAll, Minted } from "../generated/schema"
import {Transfer, Approval, ApprovalForAll, Minted, TokenOwner} from "../generated/schema"
import {
store,
Bytes,
......@@ -24,6 +24,13 @@ export function handleTransfer(event: TransferEvent): void {
entity.transactionHash = event.transaction.hash
entity.save()
// update TokenOwner
if (true) {
let ownerEntity= new TokenOwner(event.params.tokenId)
ownerEntity.owner = event.params.to
ownerEntity.tokenId = event.params.tokenId
ownerEntity.save()
}
if (event.params.from.toHexString() == "0x0000000000000000000000000000000000000000") {
// Mint
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment