Skip to content

Commit 42cf2de

Browse files
committed
maw: get-all-events almost done
1 parent c68a440 commit 42cf2de

File tree

2 files changed

+62
-56
lines changed

2 files changed

+62
-56
lines changed

src/server/routes/contract/events/get-all-events.ts

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Static, Type } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { getContract } from "../../../../shared/utils/cache/get-contract";
4+
import { BigNumber } from "ethers";
55
import {
66
contractEventSchema,
77
eventsQuerystringSchema,
@@ -10,7 +10,10 @@ import {
1010
contractParamSchema,
1111
standardResponseSchema,
1212
} from "../../../schemas/shared-api-schemas";
13+
import { thirdwebClient } from "../../../../shared/utils/sdk";
14+
import { getChain } from "../../../../shared/utils/chain";
1315
import { getChainIdFromChain } from "../../../utils/chain";
16+
import { getContract, getContractEvents } from "thirdweb";
1417

1518
const requestSchema = contractParamSchema;
1619

@@ -82,20 +85,67 @@ export async function getAllEvents(fastify: FastifyInstance) {
8285
const { fromBlock, toBlock, order } = request.query;
8386

8487
const chainId = await getChainIdFromChain(chain);
85-
const contract = await getContract({
86-
chainId,
87-
contractAddress,
88-
});
8988

90-
let returnData = await contract.events.getAllEvents({
91-
fromBlock,
92-
toBlock,
93-
order,
89+
const contract = getContract({
90+
client: thirdwebClient,
91+
address: contractAddress,
92+
chain: await getChain(chainId),
9493
});
94+
const returnData = mapEventsV4ToV5(
95+
await getContractEvents({
96+
contract: contract,
97+
fromBlock: BigInt(fromBlock),
98+
toBlock: BigInt(toBlock),
99+
}),
100+
order,
101+
);
95102

96103
reply.status(StatusCodes.OK).send({
97104
result: returnData,
98105
});
99106
},
100107
});
101108
}
109+
110+
/**
111+
* Mapping of events v5 response to v4 for backward compatiblity.
112+
* Clients may be using this api and dont want to break things.
113+
* @param eventsV5 events data returned by v5 sdk
114+
* @param order asc or desc
115+
* @returns {Type.Array(contractEventSchema)}
116+
*/
117+
export function mapEventsV4ToV5(eventsV5 = [], order = "desc") {
118+
if (!eventsV5?.length) return [];
119+
120+
return eventsV5
121+
.map((event) => {
122+
const eventName = event.eventName;
123+
const data = {};
124+
125+
// backwards compatibility of BigInt(v5) to BigNumber(v4)
126+
Object.keys(event.args).forEach((key) => {
127+
let value = event.args[key];
128+
if (typeof value == "bigint") {
129+
value = BigNumber.from(value.toString());
130+
}
131+
data[key] = value;
132+
});
133+
134+
delete event.eventName;
135+
delete event.args;
136+
const transaction = event;
137+
transaction.blockNumber = parseInt(transaction.blockNumber);
138+
transaction.event = eventName;
139+
140+
return {
141+
eventName,
142+
data,
143+
transaction,
144+
};
145+
})
146+
.sort((a, b) => {
147+
return order === "desc"
148+
? b.transaction.blockNumber - a.transaction.blockNumber
149+
: a.transaction.blockNumber - b.transaction.blockNumber;
150+
});
151+
}

tests/unit/migrationV5.test.ts

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ import { thirdwebClient } from "../../src/shared/utils/sdk";
66
import { getWalletBalance } from "thirdweb/wallets";
77
import { getBalance } from "thirdweb/extensions/erc20";
88
import { getContractEvents } from "thirdweb";
9-
import {
10-
getContract as getContractV5,
11-
GetContractEventsResult,
12-
} from "thirdweb";
9+
import { getContract as getContractV5 } from "thirdweb";
1310
import { getContract as getContractV4 } from "../../src/shared/utils/cache/get-contract";
14-
15-
import superjson from "superjson";
16-
import { BigNumber } from "ethers";
11+
import { mapEventsV4ToV5 } from "../../src/server/routes/contract/events/get-all-events";
1712

1813
/**
1914
* need to pass THIRDWEB_API_SECRET_KEY as env when running test case
@@ -106,51 +101,12 @@ describe("migration from v4 to v5", () => {
106101
order,
107102
);
108103

104+
// check two array ordering is the same
109105
expect(eventsV4.length).eq(eventsV5.length);
110106
for (let i = 0; i < eventsV4.length; i++) {
111107
expect(eventsV4[i].transaction.transactionHash).eq(
112108
eventsV5[i].transaction.transactionHash,
113109
);
114110
}
115111
});
116-
117-
/**
118-
* Mapping of events v5 response to v4 for backward compatiblity.
119-
* Clients may be using this api and dont want to break things.
120-
*/
121-
const mapEventsV4ToV5 = (eventsV5, order) => {
122-
if (!eventsV5?.length) return [];
123-
124-
return eventsV5
125-
.map((event) => {
126-
const eventName = event.eventName;
127-
const data = {};
128-
129-
// backwards compatibility of BigInt(v5) to BigNumber (v4)
130-
Object.keys(event.args).forEach((key) => {
131-
let value = event.args[key];
132-
if (typeof value == "bigint") {
133-
value = BigNumber.from(value.toString());
134-
}
135-
data[key] = value;
136-
});
137-
138-
delete event.eventName;
139-
delete event.args;
140-
const transaction = event;
141-
transaction.blockNumber = parseInt(transaction.blockNumber);
142-
transaction.event = eventName;
143-
144-
return {
145-
eventName,
146-
data,
147-
transaction,
148-
};
149-
})
150-
.sort((a, b) => {
151-
return order === "desc"
152-
? b.transaction.blockNumber - a.transaction.blockNumber
153-
: a.transaction.blockNumber - b.transaction.blockNumber;
154-
});
155-
};
156112
});

0 commit comments

Comments
 (0)