Skip to content

Commit a328df6

Browse files
authored
Merge pull request #35 from Tianweiii/wilsonDev
Add back account entity
2 parents 34e0e9a + 8313676 commit a328df6

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed
3.16 KB
Binary file not shown.

packages/subgraph/lyra-token/build/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ type Transfer @entity(immutable: true) {
4444
blockTimestamp: BigInt!
4545
transactionHash: Bytes!
4646
}
47+
48+
type Account @entity(immutable: false) {
49+
id: Bytes! # address
50+
balance: BigInt! # latest token balance
51+
}

packages/subgraph/lyra-token/generated/schema.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,58 @@ export class Transfer extends Entity {
578578
this.set("transactionHash", Value.fromBytes(value));
579579
}
580580
}
581+
582+
export class Account extends Entity {
583+
constructor(id: Bytes) {
584+
super();
585+
this.set("id", Value.fromBytes(id));
586+
}
587+
588+
save(): void {
589+
let id = this.get("id");
590+
assert(id != null, "Cannot save Account entity without an ID");
591+
if (id) {
592+
assert(
593+
id.kind == ValueKind.BYTES,
594+
`Entities of type Account must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`,
595+
);
596+
store.set("Account", id.toBytes().toHexString(), this);
597+
}
598+
}
599+
600+
static loadInBlock(id: Bytes): Account | null {
601+
return changetype<Account | null>(
602+
store.get_in_block("Account", id.toHexString()),
603+
);
604+
}
605+
606+
static load(id: Bytes): Account | null {
607+
return changetype<Account | null>(store.get("Account", id.toHexString()));
608+
}
609+
610+
get id(): Bytes {
611+
let value = this.get("id");
612+
if (!value || value.kind == ValueKind.NULL) {
613+
throw new Error("Cannot return null for a required field.");
614+
} else {
615+
return value.toBytes();
616+
}
617+
}
618+
619+
set id(value: Bytes) {
620+
this.set("id", Value.fromBytes(value));
621+
}
622+
623+
get balance(): BigInt {
624+
let value = this.get("balance");
625+
if (!value || value.kind == ValueKind.NULL) {
626+
throw new Error("Cannot return null for a required field.");
627+
} else {
628+
return value.toBigInt();
629+
}
630+
}
631+
632+
set balance(value: BigInt) {
633+
this.set("balance", Value.fromBigInt(value));
634+
}
635+
}

packages/subgraph/lyra-token/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ type Transfer @entity(immutable: true) {
4444
blockTimestamp: BigInt!
4545
transactionHash: Bytes!
4646
}
47+
48+
type Account @entity(immutable: false) {
49+
id: Bytes! # address
50+
balance: BigInt! # latest token balance
51+
}

packages/subgraph/lyra-token/src/lyra-token.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BigInt, Bytes } from "@graphprotocol/graph-ts"
12
import {
23
Approval as ApprovalEvent,
34
GovernmentUpdated as GovernmentUpdatedEvent,
@@ -10,9 +11,20 @@ import {
1011
GovernmentUpdated,
1112
MerchantUpdated,
1213
OwnershipTransferred,
13-
Transfer
14+
Transfer,
15+
Account
1416
} from "../generated/schema"
1517

18+
function getOrCreateAccount(address: Bytes): Account {
19+
let account = Account.load(address)
20+
if (account == null) {
21+
account = new Account(address) // now it's Bytes, not string
22+
account.balance = BigInt.zero()
23+
account.save()
24+
}
25+
return account
26+
}
27+
1628
export function handleApproval(event: ApprovalEvent): void {
1729
let entity = new Approval(
1830
event.transaction.hash.concatI32(event.logIndex.toI32())
@@ -85,4 +97,19 @@ export function handleTransfer(event: TransferEvent): void {
8597
entity.transactionHash = event.transaction.hash
8698

8799
entity.save()
100+
101+
const zeroAddress = Bytes.fromHexString("0x0000000000000000000000000000000000000000");
102+
103+
// Update balances
104+
if (event.params.from != zeroAddress) {
105+
let fromAccount = getOrCreateAccount(event.params.from)
106+
fromAccount.balance = fromAccount.balance.minus(event.params.value)
107+
fromAccount.save()
108+
}
109+
110+
if (event.params.to != zeroAddress) {
111+
let toAccount = getOrCreateAccount(event.params.to)
112+
toAccount.balance = toAccount.balance.plus(event.params.value)
113+
toAccount.save()
114+
}
88115
}

0 commit comments

Comments
 (0)