Skip to content

Commit 07c3ee4

Browse files
Merge pull request #53 from aavegotchi/feat/alchemicaToHarvest
feat: alchemica to harvest
2 parents 32b222f + ca75080 commit 07c3ee4

File tree

6 files changed

+164
-1
lines changed

6 files changed

+164
-1
lines changed

abis/RealmDiamond.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,5 +536,27 @@
536536
],
537537
"name": "ParcelWhitelistSet",
538538
"type": "event"
539+
},
540+
{
541+
"anonymous": false,
542+
"inputs": [
543+
{
544+
"indexed": false,
545+
"name": "_tokenId",
546+
"type": "uint256"
547+
},
548+
{
549+
"indexed": false,
550+
"name": "_round",
551+
"type": "uint256"
552+
},
553+
{
554+
"indexed": false,
555+
"name": "_alchemicas",
556+
"type": "uint256[]"
557+
}
558+
],
559+
"name": "SurveyParcel",
560+
"type": "event"
539561
}
540562
]

schema.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ type Parcel @entity {
416416
size: BigInt
417417

418418
accessRights: [ParcelAccessRight!]! @derivedFrom(field: "parcel")
419+
420+
remainingAlchemica: [BigInt!]!
421+
surveyRound: Int
419422
}
420423

421424
type ParcelAccessRight @entity {

src/helper/realm.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
NFTDisplayStatusUpdated,
1414
ParcelAccessRightSet,
1515
RealmDiamond,
16+
SurveyParcel,
1617
Transfer,
1718
UnequipInstallation,
1819
UnequipTile,
@@ -39,7 +40,7 @@ import {
3940
UnequipInstallationEvent,
4041
UnequipTileEvent,
4142
} from "../../generated/schema";
42-
import { REALM_DIAMOND, StatCategory } from "./constants";
43+
import { BIGINT_ZERO, REALM_DIAMOND, StatCategory } from "./constants";
4344
import { getStat } from "./stats";
4445

4546
export const getOrCreateParcel = (realmId: BigInt): Parcel => {
@@ -49,6 +50,13 @@ export const getOrCreateParcel = (realmId: BigInt): Parcel => {
4950
parcel = new Parcel(id);
5051
parcel.equippedInstallations = new Array<string>();
5152
parcel.equippedTiles = new Array<string>();
53+
parcel.remainingAlchemica = [
54+
BIGINT_ZERO,
55+
BIGINT_ZERO,
56+
BIGINT_ZERO,
57+
BIGINT_ZERO,
58+
];
59+
parcel.surveyRound = 0;
5260
parcel = updateParcelInfo(parcel);
5361
}
5462
return parcel;

src/mappings/realm.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
EventCancelled,
1919
EventPriorityAndDurationUpdated,
2020
ParcelWhitelistSet,
21+
SurveyParcel,
2122
} from "../../generated/RealmDiamond/RealmDiamond";
2223
import { ParcelWhitelistSetEvent } from "../../generated/schema";
2324
import { BIGINT_ONE, REALM_DIAMOND, StatCategory } from "../helper/constants";
@@ -158,6 +159,14 @@ export function handleAlchemicaClaimed(event: AlchemicaClaimed): void {
158159
// set last claim alchemica
159160
let parcel = getOrCreateParcel(event.params._realmId);
160161
parcel.lastClaimedAlchemica = event.block.timestamp;
162+
163+
let alchemicas = parcel.remainingAlchemica;
164+
let entry = alchemicas[event.params._alchemicaType.toI32()];
165+
alchemicas[event.params._alchemicaType.toI32()] = entry.minus(
166+
event.params._amount
167+
);
168+
parcel.remainingAlchemica = alchemicas;
169+
161170
parcel.save();
162171

163172
// stats
@@ -540,3 +549,16 @@ export function handleParcelWhitelistSet(event: ParcelWhitelistSet): void {
540549
parEntity.whitelistId = event.params._whitelistId.toI32();
541550
parEntity.save();
542551
}
552+
553+
export function handleSurveyParcel(event: SurveyParcel): void {
554+
let entity = getOrCreateParcel(event.params._tokenId);
555+
556+
let alchemica = entity.remainingAlchemica;
557+
for (let i = 0; i < event.params._alchemicas.length; i++) {
558+
alchemica[i] = alchemica[i].plus(event.params._alchemicas[i]);
559+
}
560+
561+
entity.surveyRound = entity.surveyRound + 1;
562+
entity.remainingAlchemica = alchemica;
563+
entity.save();
564+
}

subgraph.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ dataSources:
6464
handler: handleBounceGateEventPriorityAndDurationUpdated
6565
- event: ParcelWhitelistSet(uint256,uint256,uint256)
6666
handler: handleParcelWhitelistSet
67+
- event: SurveyParcel(uint256,uint256,uint256[])
68+
handler: handleSurveyParcel
6769
file: ./src/mappings/realm.ts
6870
- kind: ethereum/contract
6971
name: TileDiamond
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { ethereum } from "@graphprotocol/graph-ts";
2+
import {
3+
afterAll,
4+
assert,
5+
beforeAll,
6+
clearStore,
7+
createMockedFunction,
8+
describe,
9+
newMockEvent,
10+
test,
11+
} from "matchstick-as";
12+
import {
13+
MintParcel,
14+
SurveyParcel,
15+
} from "../../generated/RealmDiamond/RealmDiamond";
16+
import {
17+
BIGINT_EIGHT,
18+
BIGINT_FIVE,
19+
BIGINT_FOUR,
20+
BIGINT_ONE,
21+
BIGINT_SEVEN,
22+
BIGINT_SIX,
23+
BIGINT_THREE,
24+
BIGINT_TWO,
25+
REALM_DIAMOND,
26+
} from "../../src/helper/constants";
27+
import { handleMintParcel, handleSurveyParcel } from "../../src/mappings/realm";
28+
29+
let mockEvent = newMockEvent();
30+
let realmId = BIGINT_ONE;
31+
let round = BIGINT_TWO;
32+
describe("handleSurveyParcel", () => {
33+
beforeAll(() => {
34+
// prepare event
35+
let event = new SurveyParcel(
36+
mockEvent.address,
37+
mockEvent.logIndex,
38+
mockEvent.transactionLogIndex,
39+
mockEvent.logType,
40+
mockEvent.block,
41+
mockEvent.transaction,
42+
mockEvent.parameters,
43+
null
44+
);
45+
46+
event.parameters.push(
47+
new ethereum.EventParam(
48+
"_tokenId",
49+
ethereum.Value.fromUnsignedBigInt(realmId)
50+
)
51+
);
52+
53+
event.parameters.push(
54+
new ethereum.EventParam(
55+
"_round",
56+
ethereum.Value.fromUnsignedBigInt(round)
57+
)
58+
);
59+
60+
event.parameters.push(
61+
new ethereum.EventParam(
62+
"_alchemicas",
63+
ethereum.Value.fromUnsignedBigIntArray([
64+
BIGINT_TWO,
65+
BIGINT_TWO,
66+
BIGINT_TWO,
67+
BIGINT_TWO,
68+
])
69+
)
70+
);
71+
72+
// mock getParcelInfo
73+
let tuple: ethereum.Tuple = changetype<ethereum.Tuple>([
74+
ethereum.Value.fromString("A"),
75+
ethereum.Value.fromString("B"),
76+
ethereum.Value.fromAddress(REALM_DIAMOND),
77+
ethereum.Value.fromUnsignedBigInt(BIGINT_ONE),
78+
ethereum.Value.fromUnsignedBigInt(BIGINT_TWO),
79+
ethereum.Value.fromUnsignedBigInt(BIGINT_THREE),
80+
ethereum.Value.fromUnsignedBigInt(BIGINT_FOUR),
81+
ethereum.Value.fromUnsignedBigIntArray([
82+
BIGINT_FIVE,
83+
BIGINT_SIX,
84+
BIGINT_SEVEN,
85+
BIGINT_EIGHT,
86+
]),
87+
]);
88+
createMockedFunction(
89+
REALM_DIAMOND,
90+
"getParcelInfo",
91+
"getParcelInfo(uint256):((string,string,address,uint256,uint256,uint256,uint256,uint256[4]))"
92+
)
93+
.withArgs([ethereum.Value.fromUnsignedBigInt(realmId)])
94+
.returns([ethereum.Value.fromTuple(tuple)]);
95+
96+
handleSurveyParcel(event);
97+
});
98+
99+
test("it should has remainingAlchemica field", () => {
100+
assert.fieldEquals("Parcel", "1", "remainingAlchemica", "[2, 2, 2, 2]");
101+
});
102+
103+
afterAll(() => {
104+
clearStore();
105+
});
106+
});

0 commit comments

Comments
 (0)