Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 1525283

Browse files
Merge pull request #38 from decentraland/fix/polygon-batch-memory
fix: polygon tracking collectionIds in memory
2 parents 7e10d7e + 65d73c6 commit 1525283

File tree

12 files changed

+117
-49
lines changed

12 files changed

+117
-49
lines changed

run.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/sh
2+
3+
# Load environment variables from .env file if it exists
4+
if [ -f .env ]; then
5+
export $(grep -v '^#' .env | xargs)
6+
fi
7+
8+
# Generate a unique schema name and user credentials using a timestamp
9+
CURRENT_TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
10+
NEW_SCHEMA_NAME="marketplace_squid_${CURRENT_TIMESTAMP}"
11+
NEW_DB_USER="marketplace_squid_user_${CURRENT_TIMESTAMP}"
12+
13+
# Check if required environment variables are set
14+
if [ -z "$DB_USER" ] || [ -z "$DB_NAME" ] || [ -z "$DB_PASSWORD" ] || [ -z "$DB_HOST" ] || [ -z "$DB_PORT" ]; then
15+
echo "Error: Required environment variables are not set."
16+
echo "Ensure DB_USER, DB_NAME, DB_PASSWORD, DB_HOST, and DB_PORT are set."
17+
exit 1
18+
fi
19+
20+
# Log the generated variables
21+
echo "Generated schema name: $NEW_SCHEMA_NAME"
22+
echo "Generated user: $NEW_DB_USER"
23+
24+
# Set PGPASSWORD to handle password prompt
25+
export PGPASSWORD=$DB_PASSWORD
26+
27+
# Connect to the database and create the new schema and user
28+
psql -v ON_ERROR_STOP=1 --username "$DB_USER" --dbname "$DB_NAME" --host "$DB_HOST" --port "$DB_PORT" <<-EOSQL
29+
CREATE SCHEMA $NEW_SCHEMA_NAME;
30+
CREATE USER $NEW_DB_USER WITH PASSWORD '$DB_PASSWORD';
31+
GRANT ALL PRIVILEGES ON SCHEMA $NEW_SCHEMA_NAME TO $NEW_DB_USER;
32+
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $NEW_DB_USER;
33+
ALTER USER $NEW_DB_USER SET search_path TO $NEW_SCHEMA_NAME;
34+
EOSQL
35+
36+
# Unset PGPASSWORD
37+
unset PGPASSWORD
38+
39+
# Construct the DB_URL with the new user
40+
export DB_URL=postgresql://$NEW_DB_USER:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME
41+
export DB_SCHEMA=$NEW_SCHEMA_NAME
42+
43+
# Log the constructed DB_URL
44+
echo "Exported DB_SCHEMA: $DB_SCHEMA"
45+
46+
# Start the processor service and the GraphQL server, and write logs to a file
47+
LOG_FILE="sqd_run_log_${CURRENT_TIMESTAMP}.txt"
48+
echo "Starting squid services..."
49+
sqd run . > "$LOG_FILE" 2>&1
50+
51+
echo "Logs are being written to $LOG_FILE"

src/common/modules/count.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export function buildCount(
1212

1313
if (!count) {
1414
console.log("count not found, creating new one");
15-
console.log("id: ", id);
1615
count = new Count({ id });
1716
count.orderTotal = 0;
1817
count.orderParcel = 0;

src/eth/handlers/bid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export async function handleBidAccepted(
128128
counts
129129
);
130130
} else {
131-
console.log("NFT not found for bid in accepted: ", id);
131+
console.log("ERROR: NFT not found for bid in accepted: ", id);
132132
}
133133
}
134134

src/eth/handlers/parcel.ts

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ export function handleUpdate(
3636
}
3737
parcel.rawData = data;
3838
const parcelData = buildData(id, data, DataType.PARCEL);
39-
if (id === "estate-0x959e104e1a4db6317fa58f8295f586e1a978c297-10") {
40-
console.log("parcelData: ", parcelData);
41-
console.log("parcelId: ", parcelId);
42-
}
4339
if (parcelData) {
4440
parcel.data = parcelData;
4541
datas.set(parcelData.id, parcelData);

src/eth/modules/analytics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function trackSale(
3434
): void {
3535
// ignore zero price sales
3636
if (price === BigInt(0)) {
37-
console.log("ignoring zero price sale");
37+
console.log("INFO: ignoring zero price sale");
3838
return;
3939
}
4040

@@ -58,7 +58,7 @@ export function trackSale(
5858
sale.searchContractAddress = nft.contractAddress;
5959
sale.searchCategory = nft.category;
6060
} else {
61-
console.log("NFT not found for sale", nftId);
61+
console.log("ERROR: NFT not found for sale", nftId);
6262
}
6363
sale.timestamp = timestamp;
6464
sale.txHash = txHash;

src/polygon/handlers/collection.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function handleSetGlobalMinter(
115115

116116
if (!collection) {
117117
console.log(
118-
`Error: Collection not found in handleSetGlobalMinter: ${collectionAddress}`
118+
`ERROR: Collection not found in handleSetGlobalMinter: ${collectionAddress}`
119119
);
120120
return;
121121
}
@@ -147,7 +147,7 @@ export function handleSetGlobalMinter(
147147
item.firstListedAt = BigInt(block.timestamp / 1000);
148148
}
149149
} else {
150-
console.log(`Item not found: ${itemId}`);
150+
console.log(`ERROR: Item not found: ${itemId}`);
151151
}
152152
}
153153
}
@@ -180,7 +180,7 @@ export function handleSetGlobalMinter(
180180
// set flag only if store is item minter, otherwise unset it
181181
item.searchIsStoreMinter = isStoreItemMinter;
182182
} else {
183-
console.log(`Item not found: ${itemId}`);
183+
console.log(`ERROR: Item not found: ${itemId}`);
184184
}
185185
}
186186
}
@@ -199,7 +199,7 @@ export function handleSetGlobalManager(
199199

200200
if (!collection) {
201201
console.log(
202-
`Error: Collection not found in handleSetGlobalManager: ${collectionAddress}`
202+
`ERROR: Collection not found in handleSetGlobalManager: ${collectionAddress}`
203203
);
204204
return;
205205
}
@@ -237,7 +237,7 @@ export function handleSetItemMinter(
237237

238238
const item = items.get(id);
239239
if (!item) {
240-
console.log(`Error: Item not found in handleSetItemMinter: ${id}`);
240+
console.log(`ERROR: Item not found in handleSetItemMinter: ${id}`);
241241
return;
242242
}
243243

@@ -261,7 +261,7 @@ export function handleSetItemMinter(
261261
const collection = collections.get(item.collection.id);
262262
if (!collection) {
263263
console.log(
264-
`Error: Collection not found in handleSetItemMinter: ${item.collection.id}`
264+
`ERROR: Collection not found in handleSetItemMinter: ${item.collection.id}`
265265
);
266266
}
267267
if (
@@ -285,7 +285,7 @@ export function handleSetItemManager(
285285

286286
let item = items.get(id);
287287
if (!item) {
288-
console.log(`Error: Item not found in handleSetItemManager: ${id}`);
288+
console.log(`ERROR: Item not found in handleSetItemManager: ${id}`);
289289
return;
290290
}
291291

@@ -334,7 +334,7 @@ export async function handleAddItem(
334334
// Skip it, collection will be set up once the proxy event is created
335335
// The ProxyCreated event is emitted right after the collection's event
336336
console.log(
337-
`Error: Collection not found in handleAddItem: ${collectionAddress}`
337+
`ERROR: Collection not found in handleAddItem: ${collectionAddress}`
338338
);
339339
return;
340340
}
@@ -357,18 +357,18 @@ export async function handleAddItem(
357357
const rarity = rarities.get(contractItem.rarity);
358358
// console.log("rarity inside Add Item: ", rarity);
359359
if (!rarity) {
360-
console.log(`Error: Rarity not found: ${contractItem.rarity}`);
360+
console.log(`ERROR: Rarity not found: ${contractItem.rarity}`);
361361
// return;
362362
}
363363

364364
let creationFee = BigInt(0);
365365

366366
if (!rarity) {
367-
console.log("Undefined rarity {} for collection {} and item {}", [
368-
contractItem.rarity,
369-
collectionAddress,
370-
itemId.toString(),
371-
]);
367+
console.log(
368+
`ERROR: Undefined rarity ${
369+
contractItem.rarity
370+
} for collection ${collectionAddress} and item ${itemId.toString()}`
371+
);
372372
} else {
373373
creationFee = rarity.price;
374374

@@ -707,7 +707,7 @@ export function handleSetApproved(
707707

708708
if (!collection) {
709709
console.log(
710-
`Error: Collection not found in handleSetApproved: ${collectionAddress}`
710+
`ERROR: Collection not found in handleSetApproved: ${collectionAddress}`
711711
);
712712
return;
713713
}
@@ -805,7 +805,7 @@ export function handleSetEditable(
805805
collection.isEditable = event._newValue;
806806
} else {
807807
console.log(
808-
`Error: Collection not found in handleSetEditable: ${collectionAddress}`
808+
`ERROR: Collection not found in handleSetEditable: ${collectionAddress}`
809809
);
810810
}
811811
}
@@ -820,7 +820,7 @@ export function handleCompleteCollection(
820820
collection.isCompleted = true;
821821
} else {
822822
console.log(
823-
`Error: Collection not found in handleCompleteCollection: ${collectionAddress}`
823+
`ERROR: Collection not found in handleCompleteCollection: ${collectionAddress}`
824824
);
825825
}
826826
}
@@ -852,7 +852,7 @@ export function handleTransferCreatorship(
852852
}
853853
} else {
854854
console.log(
855-
`Error: Collection not found in handleTransferCreatorship: ${collectionAddress}`
855+
`ERROR: Collection not found in handleTransferCreatorship: ${collectionAddress}`
856856
);
857857
}
858858
}
@@ -868,7 +868,7 @@ export function handleTransferOwnership(
868868
collection.owner = event.newOwner;
869869
} else {
870870
console.log(
871-
`Error: Collection not found in handleTransferOwnership: ${collectionAddress}`
871+
`ERROR: Collection not found in handleTransferOwnership: ${collectionAddress}`
872872
);
873873
}
874874
}

src/polygon/handlers/collectionManager.ts

-8
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@ export async function handleRaritiesSet(
1414
): Promise<void> {
1515
const addresses = getAddresses(Network.MATIC);
1616
const rarityAddress = addresses.Rarity;
17-
console.log("rarityAddress: ", rarityAddress);
1817
const raritiesWithOracleAddress = addresses.RaritiesWithOracle;
19-
console.log("raritiesWithOracleAddress: ", raritiesWithOracleAddress);
20-
// let raritiesWithOracleAddress = getRaritiesWithOracleAddress();
21-
// let rarityAddress = getRarityAddress();
2218
const newRaritiesAddress = event._newRarities;
23-
console.log("newRaritiesAddress: ", newRaritiesAddress);
2419

2520
if (newRaritiesAddress === raritiesWithOracleAddress) {
2621
let raritiesWithOracle = new RaritiesWithOracleABI.Contract(
@@ -29,7 +24,6 @@ export async function handleRaritiesSet(
2924
raritiesWithOracleAddress
3025
);
3126
let raritiesCount = await raritiesWithOracle.raritiesCount();
32-
console.log("raritiesCount: ", raritiesCount);
3327

3428
for (let i = 0; i < raritiesCount; i++) {
3529
const blockchainRarity = await raritiesWithOracle.rarities(BigInt(i));
@@ -50,9 +44,7 @@ export async function handleRaritiesSet(
5044
}
5145
} else if (newRaritiesAddress === rarityAddress) {
5246
const rarityContract = new RarityABI.Contract(ctx, block, rarityAddress);
53-
// let rarityContract = RarityABI.bind(rarityAddress);
5447
const raritiesCount = await rarityContract.raritiesCount();
55-
console.log("raritiesCount: ", raritiesCount);
5648

5749
for (let i = 0; i < raritiesCount; i++) {
5850
const blockchainRarity = await rarityContract.rarities(BigInt(i));

src/polygon/handlers/marketplace.ts

-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ export function handleOrderCreated(
6464
order.marketplaceAddress = contractAddress;
6565
order.status = OrderStatus.open;
6666
order.category = category ? (category as Category) : Category.wearable;
67-
if (!order.category) {
68-
console.log("order.category: ", order.category);
69-
}
7067
order.nft = nft;
7168
order.network = isEthereum ? NetworkModel.ethereum : NetworkModel.polygon;
7269
if (!isEthereum) {

src/polygon/main.ts

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { In, Not } from "typeorm";
12
import { TypeormDatabase } from "@subsquid/typeorm-store";
23
import { Network } from "@dcl/schemas";
3-
import { Order, Rarity, Transfer, Network as ModelNetwork } from "../model";
4+
import {
5+
Order,
6+
Rarity,
7+
Transfer,
8+
Network as ModelNetwork,
9+
Collection,
10+
} from "../model";
411
import * as CollectionFactoryABI from "./abi/CollectionFactory";
512
import * as CollectionFactoryV3ABI from "./abi/CollectionFactoryV3";
613
import * as CollectionV2ABI from "./abi/CollectionV2";
@@ -57,10 +64,12 @@ import {
5764
} from "./handlers/marketplace";
5865
import { getNFTId } from "../common/utils";
5966
import { handleRaritiesSet } from "./handlers/collectionManager";
67+
import { loadCollections } from "./utils/loaders";
6068

6169
const schemaName = process.env.DB_SCHEMA;
6270
const addresses = getAddresses(Network.MATIC);
6371
let bytesRead = 0; // amount of bytes received
72+
const preloadedCollections = loadCollections().addresses;
6473
const collectionsCreatedByFactory = new Set<string>();
6574

6675
processor.run(
@@ -81,6 +90,15 @@ processor.run(
8190
.find(Rarity)
8291
.then((q) => new Map(q.map((i) => [i.id, i])));
8392

93+
const collectionIdsNotIncludedInPreloaded = await ctx.store
94+
.find(Collection, {
95+
where: {
96+
id: Not(In(preloadedCollections)),
97+
},
98+
})
99+
.then((q) => new Set(q.map((c) => c.id)));
100+
101+
const collectionIdsCreatedInBatch = new Set<string>();
84102
const inMemoryData = getBatchInMemoryState();
85103
const {
86104
sales,
@@ -101,7 +119,11 @@ processor.run(
101119
committeeEvents,
102120
} = inMemoryData;
103121

104-
ctx.log.info(`blocks, ${ctx.blocks.length}`);
122+
ctx.log.info(
123+
`blocks, amount: ${ctx.blocks.length}, from: ${
124+
ctx.blocks[0].header.height
125+
} to: ${ctx.blocks[ctx.blocks.length - 1].header.height}`
126+
);
105127
for (let block of ctx.blocks) {
106128
for (let log of block.logs) {
107129
const topic = log.topics[0];
@@ -124,7 +146,8 @@ processor.run(
124146
? CollectionFactoryABI.events.ProxyCreated.decode(log)
125147
: CollectionFactoryV3ABI.events.ProxyCreated.decode(log);
126148

127-
collectionsCreatedByFactory.add(event._address.toLowerCase());
149+
// collectionsCreatedByFactory.add(event._address.toLowerCase());
150+
collectionIdsCreatedInBatch.add(event._address); // add the Id to the list of collections to be processed
128151

129152
const collectionContract = new CollectionV2ABI.Contract(
130153
ctx,
@@ -359,7 +382,9 @@ processor.run(
359382
.map((c) => c.toLowerCase())
360383
.includes(log.address)
361384
) {
362-
console.log("Committee event found not from committee contract");
385+
console.log(
386+
"ERROR: Committee event found not from committee contract"
387+
);
363388
break;
364389
}
365390
const event = CommitteeABI.events.MemberSet.decode(log);
@@ -382,7 +407,13 @@ processor.run(
382407
case CollectionV2ABI.events.OwnershipTransferred.topic:
383408
case CollectionV2ABI.events.Transfer.topic: {
384409
// @TODO check addresses
385-
if (!collectionsCreatedByFactory.has(log.address)) {
410+
if (
411+
![
412+
...preloadedCollections, // collections already pre-calculated
413+
...collectionIdsNotIncludedInPreloaded, // collections not included in the preloaded list but yes in the db (newest ones)
414+
...collectionIdsCreatedInBatch, // collections created in the current batch, will later by saved in the db
415+
].includes(log.address)
416+
) {
386417
// ctx.log.warn(
387418
// `CollectionV2 event found not from collection contract, address: ${log.address}, topic: ${topic}, block: ${block.header.height}`
388419
// );
@@ -487,7 +518,7 @@ processor.run(
487518
),
488519
});
489520
} else {
490-
console.log("Error: Event not decoded correctly");
521+
console.log("ERROR: Event not decoded correctly");
491522
}
492523
break;
493524
}

src/polygon/modules/analytics.ts

-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ export function trackSale(
7575
item = nft.item; // set the item coming out from the NFT
7676
}
7777
if (!item || !nft) {
78-
console.log("item: ", item);
79-
console.log("nft: ", nft);
8078
console.log(`ERROR: NFT or Item not found for sale ${nftId} ${itemId}`);
8179
return;
8280
}

0 commit comments

Comments
 (0)