Skip to content

Commit a66a396

Browse files
committed
Basic state change
1 parent 1b5514d commit a66a396

File tree

7 files changed

+3063
-70
lines changed

7 files changed

+3063
-70
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
build
3+
generated

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"scripts": {
55
"codegen": "graph codegen",
66
"build": "graph build",
7-
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ avolabs-io/async-subgraph_v2",
7+
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ avolabs-io/async_art_v2",
88
"create-local": "graph create --node http://localhost:8020/ avolabs-io/async-subgraph_v2",
99
"remove-local": "graph remove --node http://localhost:8020/ avolabs-io/async-subgraph_v2",
1010
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 avolabs-io/async-subgraph_v2"

schema.graphql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,37 @@ type ExampleEntity @entity {
44
owner: Bytes! # address
55
approved: Bytes! # address
66
}
7+
8+
type EventParam @entity {
9+
id: ID!
10+
index: Int!
11+
param: String!
12+
paramName: String!
13+
paramType: String!
14+
}
15+
type EventParams @entity {
16+
id: ID!
17+
index: Int!
18+
eventName: String!
19+
params: [EventParam!]!
20+
}
21+
22+
# For every transaction, list the changes, and stat
23+
type StateChange @entity {
24+
id: ID! # tx
25+
timestamp: BigInt!
26+
blockNumber: BigInt!
27+
contractVersion: Int!
28+
txEventList: [String!]! #call event logs or something
29+
txEventParamListDeprecated: [String!]!
30+
txEventParamList: [EventParams!]
31+
ownerChanges: [Owner!]!
32+
artworkChanges: [Artwork!]!
33+
}
34+
35+
type Owner @entity {
36+
id: ID!
37+
}
38+
type Artwork @entity {
39+
id: ID!
40+
}

src/mapping.ts

Lines changed: 27 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,35 @@ import {
1616
Transfer
1717
} from "../generated/Contract/Contract"
1818
import { ExampleEntity } from "../generated/schema"
19+
import {
20+
saveEventToStateChange,
21+
} from "./util";
1922

2023
export function handleApproval(event: Approval): void {
21-
// Entities can be loaded from the store using a string ID; this ID
22-
// needs to be unique across all entities of the same type
23-
let entity = ExampleEntity.load(event.transaction.from.toHex())
24-
25-
// Entities only exist after they have been saved to the store;
26-
// `null` checks allow to create entities on demand
27-
if (entity == null) {
28-
entity = new ExampleEntity(event.transaction.from.toHex())
29-
30-
// Entity fields can be set using simple assignments
31-
entity.count = BigInt.fromI32(0)
32-
}
33-
34-
// BigInt and BigDecimal math are supported
35-
entity.count = entity.count + BigInt.fromI32(1)
36-
37-
// Entity fields can be set based on event parameters
38-
entity.owner = event.params.owner
39-
entity.approved = event.params.approved
40-
41-
// Entities can be written to the store with `.save()`
42-
entity.save()
43-
44-
// Note: If a handler doesn't require existing field values, it is faster
45-
// _not_ to load the entity from the store. Instead, create it fresh with
46-
// `new Entity(...)`, set the fields that should be updated and save the
47-
// entity back to the store. Fields that were not set or unset remain
48-
// unchanged, allowing for partial updates to be applied.
49-
50-
// It is also possible to access smart contracts from mappings. For
51-
// example, the contract that has emitted the event can be connected to
52-
// with:
53-
//
54-
// let contract = Contract.bind(event.address)
55-
//
56-
// The following functions can then be called on this contract to access
57-
// state variables and other data:
58-
//
59-
// - contract.artistSecondSalePercentage(...)
60-
// - contract.balanceOf(...)
61-
// - contract.buyPrices(...)
62-
// - contract.controlTokenMapping(...)
63-
// - contract.creatorWhitelist(...)
64-
// - contract.expectedTokenSupply(...)
65-
// - contract.failedTransferCredits(...)
66-
// - contract.getApproved(...)
67-
// - contract.isApprovedForAll(...)
68-
// - contract.minBidIncreasePercent(...)
69-
// - contract.name(...)
70-
// - contract.ownerOf(...)
71-
// - contract.pendingBids(...)
72-
// - contract.permissionedControllers(...)
73-
// - contract.platformAddress(...)
74-
// - contract.platformFirstSalePercentages(...)
75-
// - contract.platformSecondSalePercentages(...)
76-
// - contract.supportsInterface(...)
77-
// - contract.symbol(...)
78-
// - contract.tokenByIndex(...)
79-
// - contract.tokenDidHaveFirstSale(...)
80-
// - contract.tokenOfOwnerByIndex(...)
81-
// - contract.tokenURI(...)
82-
// - contract.tokenURILocked(...)
83-
// - contract.totalSupply(...)
84-
// - contract.uniqueTokenCreators(...)
85-
// - contract.upgraderAddress(...)
86-
// - contract.getNumRemainingControlUpdates(...)
87-
// - contract.getControlToken(...)
24+
25+
let owner = event.params.owner;
26+
let ownerString = owner.toHex();
27+
let txTimestamp = event.block.timestamp;
28+
let blockNumber = event.block.number;
29+
30+
let eventParamValues: Array<string> = [
31+
ownerString,
32+
];
33+
let eventParamNames: Array<string> = ["owner"];
34+
let eventParamTypes: Array<string> = ["address"];
35+
36+
saveEventToStateChange(
37+
event.transaction.hash,
38+
txTimestamp,
39+
blockNumber,
40+
"Approval",
41+
eventParamValues,
42+
eventParamNames,
43+
eventParamTypes,
44+
[],
45+
[],
46+
0
47+
);
8848
}
8949

9050
export function handleApprovalForAll(event: ApprovalForAll): void {}

src/util/index.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { Address, BigInt, log, Bytes } from "@graphprotocol/graph-ts";
2+
import {
3+
StateChange,
4+
EventParam,
5+
EventParams,
6+
Owner,
7+
Artwork,
8+
} from "../../generated/schema";
9+
10+
export function getOrInitialiseStateChange(txId: string): StateChange | null {
11+
let stateChange = StateChange.load(txId);
12+
if (stateChange == null) {
13+
stateChange = new StateChange(txId);
14+
stateChange.txEventParamList = [];
15+
stateChange.ownerChanges = [];
16+
stateChange.artworkChanges = [];
17+
18+
return stateChange;
19+
} else {
20+
return stateChange;
21+
}
22+
}
23+
function getEventIndex(txHash: Bytes): i32 {
24+
let stateChange = StateChange.load(txHash.toHex());
25+
if (stateChange == null) {
26+
return 0;
27+
}
28+
return stateChange.txEventParamList.length;
29+
}
30+
function createEventParams(
31+
txHash: Bytes,
32+
argValues: Array<string>,
33+
argNames: Array<string>,
34+
argTypes: Array<string>
35+
): Array<string> {
36+
let eventIndex: i32 = getEventIndex(txHash);
37+
let eventParamsArr: Array<string> = [];
38+
for (let index = 0; index < argValues.length; index++) {
39+
let eventParamFund = new EventParam(
40+
txHash.toHex() + "-" + eventIndex.toString() + "-" + index.toString()
41+
);
42+
eventParamFund.index = index;
43+
eventParamFund.param = argValues[index];
44+
eventParamFund.paramName = argNames[index];
45+
eventParamFund.paramType = argTypes[index];
46+
eventParamFund.save();
47+
eventParamsArr.push(eventParamFund.id);
48+
}
49+
return eventParamsArr;
50+
}
51+
function txEventParamsHelper(
52+
eventName: string,
53+
eventIndex: i32,
54+
eventTxHash: Bytes,
55+
eventParamsArr: Array<string>
56+
): EventParams {
57+
let eventParams = new EventParams(
58+
eventTxHash.toHex() + "-" + eventIndex.toString()
59+
);
60+
eventParams.index = eventIndex;
61+
eventParams.eventName = eventName;
62+
eventParams.params = eventParamsArr;
63+
eventParams.save();
64+
return eventParams;
65+
}
66+
function txStateChangeHelper(
67+
txHash: Bytes,
68+
timeStamp: BigInt,
69+
blockNumber: BigInt,
70+
eventName: string,
71+
eventParamArray: Array<string>,
72+
changedOwners: string[],
73+
changedArtworks: string[],
74+
contractVersion: i32
75+
): void {
76+
let stateChange = getOrInitialiseStateChange(txHash.toHex());
77+
if (stateChange == null) {
78+
stateChange = new StateChange(txHash.toHex());
79+
stateChange.txEventParamList = [];
80+
}
81+
let eventIndex: i32 = getEventIndex(txHash);
82+
// create EventParams
83+
let eventParams = txEventParamsHelper(
84+
eventName,
85+
eventIndex,
86+
txHash,
87+
eventParamArray
88+
);
89+
stateChange.timestamp = timeStamp;
90+
stateChange.blockNumber = blockNumber;
91+
stateChange.txEventParamList = stateChange.txEventParamList.concat([
92+
eventParams.id,
93+
]);
94+
for (let i = 0, len = changedOwners.length; i < len; i++) {
95+
stateChange.ownerChanges =
96+
stateChange.ownerChanges.indexOf(changedOwners[i]) === -1
97+
? stateChange.ownerChanges.concat([changedOwners[i]])
98+
: stateChange.ownerChanges;
99+
}
100+
for (let i = 0, len = changedArtworks.length; i < len; i++) {
101+
stateChange.artworkChanges =
102+
stateChange.artworkChanges.indexOf(changedArtworks[i]) === -1
103+
? stateChange.artworkChanges.concat([changedArtworks[i]])
104+
: stateChange.artworkChanges;
105+
}
106+
stateChange.contractVersion = contractVersion;
107+
stateChange.save();
108+
}
109+
export function saveEventToStateChange(
110+
txHash: Bytes,
111+
timestamp: BigInt,
112+
blockNumber: BigInt,
113+
eventName: string,
114+
parameterValues: Array<string>,
115+
parameterNames: Array<string>,
116+
parameterTypes: Array<string>,
117+
changedOwners: string[],
118+
changedArtworks: string[],
119+
version: i32
120+
): void {
121+
let eventParamsArr: Array<string> = createEventParams(
122+
txHash,
123+
parameterValues,
124+
parameterNames,
125+
parameterTypes
126+
);
127+
txStateChangeHelper(
128+
txHash,
129+
timestamp,
130+
blockNumber,
131+
eventName,
132+
eventParamsArr,
133+
changedOwners,
134+
changedArtworks,
135+
version
136+
);
137+
}

subgraph.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
specVersion: 0.0.1
1+
specVersion: 0.0.2
22
schema:
33
file: ./schema.graphql
44
dataSources:
@@ -8,9 +8,10 @@ dataSources:
88
source:
99
address: "0xb6dAe651468E9593E4581705a09c10A76AC1e0c8"
1010
abi: Contract
11+
startBlock: 8752679
1112
mapping:
1213
kind: ethereum/events
13-
apiVersion: 0.0.2
14+
apiVersion: 0.0.3
1415
language: wasm/assemblyscript
1516
entities:
1617
- Approval

0 commit comments

Comments
 (0)