Skip to content

Commit 76181e8

Browse files
authored
Merge branch 'main' into pb/circle-w3s
2 parents bea4e34 + 3496ef4 commit 76181e8

File tree

6 files changed

+216
-17
lines changed

6 files changed

+216
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"prisma": "^5.14.0",
6868
"prom-client": "^15.1.3",
6969
"superjson": "^2.2.1",
70-
"thirdweb": "^5.71.0",
70+
"thirdweb": "^5.78.0",
7171
"uuid": "^9.0.1",
7272
"viem": "^2.21.54",
7373
"winston": "^3.14.1",

src/server/routes/backend-wallet/get-balance.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { type Static, Type } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { getSdk } from "../../../shared/utils/cache/get-sdk";
54
import { AddressSchema } from "../../schemas/address";
65
import {
76
currencyValueSchema,
@@ -10,6 +9,10 @@ import {
109
import { walletWithAddressParamSchema } from "../../schemas/wallet";
1110
import { getChainIdFromChain } from "../../utils/chain";
1211

12+
import { getChain } from "../../../shared/utils/chain";
13+
import { thirdwebClient } from "../../../shared/utils/sdk";
14+
import { getWalletBalance } from "thirdweb/wallets";
15+
1316
const responseSchema = Type.Object({
1417
result: Type.Object({
1518
walletAddress: AddressSchema,
@@ -49,9 +52,12 @@ export async function getBalance(fastify: FastifyInstance) {
4952
handler: async (request, reply) => {
5053
const { chain, walletAddress } = request.params;
5154
const chainId = await getChainIdFromChain(chain);
52-
const sdk = await getSdk({ chainId });
5355

54-
const balanceData = await sdk.getBalance(walletAddress);
56+
const balanceData = await getWalletBalance({
57+
client: thirdwebClient,
58+
address: walletAddress,
59+
chain: await getChain(chainId),
60+
});
5561

5662
reply.status(StatusCodes.OK).send({
5763
result: {

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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";
54
import {
65
contractEventSchema,
76
eventsQuerystringSchema,
@@ -10,7 +9,12 @@ import {
109
contractParamSchema,
1110
standardResponseSchema,
1211
} from "../../../schemas/shared-api-schemas";
12+
import { thirdwebClient } from "../../../../shared/utils/sdk";
13+
import { getChain } from "../../../../shared/utils/chain";
1314
import { getChainIdFromChain } from "../../../utils/chain";
15+
import { getContract, getContractEvents } from "thirdweb";
16+
import { maybeBigInt } from "../../../../shared/utils/primitive-types";
17+
import { toContractEventV4Schema } from "../../../schemas/event";
1418

1519
const requestSchema = contractParamSchema;
1620

@@ -82,19 +86,25 @@ export async function getAllEvents(fastify: FastifyInstance) {
8286
const { fromBlock, toBlock, order } = request.query;
8387

8488
const chainId = await getChainIdFromChain(chain);
85-
const contract = await getContract({
86-
chainId,
87-
contractAddress,
89+
90+
const contract = getContract({
91+
client: thirdwebClient,
92+
address: contractAddress,
93+
chain: await getChain(chainId),
8894
});
8995

90-
const returnData = await contract.events.getAllEvents({
91-
fromBlock,
92-
toBlock,
93-
order,
96+
const eventsV5 = await getContractEvents({
97+
contract: contract,
98+
fromBlock: maybeBigInt(fromBlock?.toString()),
99+
toBlock: maybeBigInt(toBlock?.toString()),
94100
});
95101

96102
reply.status(StatusCodes.OK).send({
97-
result: returnData,
103+
result: eventsV5.map(toContractEventV4Schema).sort((a, b) => {
104+
return order === "desc"
105+
? b.transaction.blockNumber - a.transaction.blockNumber
106+
: a.transaction.blockNumber - b.transaction.blockNumber;
107+
}),
98108
});
99109
},
100110
});

src/server/schemas/event.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { BigNumber } from "ethers";
2+
3+
export type ContractEventV4 = {
4+
eventName: string;
5+
data: Record<string, unknown>;
6+
transaction: {
7+
blockNumber: number;
8+
blockHash: string;
9+
transactionIndex: number;
10+
removed: boolean;
11+
address: string;
12+
data: string;
13+
topic: string[];
14+
transactionHash: string;
15+
logIndex: number;
16+
event: string;
17+
eventSignature?: string;
18+
};
19+
};
20+
21+
export type ContractEventV5 = {
22+
eventName: string;
23+
args: Record<string, unknown>;
24+
address: string;
25+
topic: string[];
26+
data: string;
27+
blockNumber: bigint;
28+
transactionHash: string;
29+
transactionIndex: number;
30+
blockHash: string;
31+
logIndex: number;
32+
removed: boolean;
33+
};
34+
35+
/**
36+
* Mapping of events v5 response to v4 for backward compatiblity.
37+
* Clients may be using this api and dont want to break things.
38+
*/
39+
export function toContractEventV4Schema(
40+
eventV5: ContractEventV5,
41+
): ContractEventV4 {
42+
const eventName = eventV5.eventName;
43+
44+
// backwards compatibility of BigInt(v5) to BigNumber(v4)
45+
const data: Record<string, unknown> = {};
46+
for (const key of Object.keys(eventV5.args)) {
47+
let value = eventV5.args[key];
48+
if (typeof value === "bigint") {
49+
value = BigNumber.from(value.toString());
50+
}
51+
data[key] = value;
52+
}
53+
54+
return {
55+
eventName,
56+
data,
57+
transaction: {
58+
blockNumber: Number(eventV5.blockNumber),
59+
blockHash: eventV5.blockHash,
60+
transactionIndex: eventV5.transactionIndex,
61+
removed: eventV5.removed,
62+
address: eventV5.address,
63+
data: eventV5.data,
64+
topic: eventV5.topic,
65+
transactionHash: eventV5.transactionHash,
66+
logIndex: eventV5.logIndex,
67+
event: eventV5.eventName,
68+
// todo: eventV5.eventSignature is not returned so ignoring for now
69+
},
70+
};
71+
}

tests/unit/migrationV5.test.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { getSdk } from "../../src/shared/utils/cache/get-sdk";
4+
import { getChain } from "../../src/shared/utils/chain";
5+
import { thirdwebClient } from "../../src/shared/utils/sdk";
6+
import { getWalletBalance } from "thirdweb/wallets";
7+
import { getBalance } from "thirdweb/extensions/erc20";
8+
import { getContractEvents } from "thirdweb";
9+
import { getContract as getContractV5 } from "thirdweb";
10+
import { getContract as getContractV4 } from "../../src/shared/utils/cache/get-contract";
11+
import { maybeBigInt } from "../../src/shared/utils/primitive-types";
12+
import { toContractEventV4Schema } from "../../src/server/schemas/event";
13+
14+
/**
15+
* todo: remove all dependencies including tests after everything is migrated properly.
16+
*/
17+
describe("migration from v4 to v5", () => {
18+
it("get-contract: check difference in contract interface", async () => {
19+
const chainId = 137;
20+
const contractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
21+
const walletAddress = "0xE52772e599b3fa747Af9595266b527A31611cebd";
22+
23+
// v4
24+
const sdk = await getSdk({ chainId });
25+
const contractV4 = await sdk.getContract(contractAddress);
26+
const balV4 = await contractV4.erc20.balanceOf(walletAddress);
27+
28+
/**
29+
* v5
30+
* Doesnt have nested helper functions and is separated into individual "extensions"
31+
*/
32+
const contractV5 = getContractV5({
33+
client: thirdwebClient,
34+
address: contractAddress,
35+
chain: await getChain(chainId),
36+
});
37+
const balV5 = await getBalance({
38+
contract: contractV5,
39+
address: walletAddress,
40+
});
41+
42+
expect(balV4.name).eq(balV5.name);
43+
expect(balV4.symbol).eq(balV5.symbol);
44+
expect(balV4.decimals).eq(balV5.decimals);
45+
expect(balV4.displayValue).eq(balV5.displayValue);
46+
expect(balV4.value.toString()).eq(balV5.value.toString());
47+
});
48+
49+
it("tests for get-balance(native token)", async () => {
50+
const chainId = 137;
51+
const walletAddress = "0xE52772e599b3fa747Af9595266b527A31611cebd";
52+
53+
// v4
54+
const sdk = await getSdk({ chainId });
55+
const balanceV4 = await sdk.getBalance(walletAddress);
56+
57+
// v5.
58+
const balanceV5 = await getWalletBalance({
59+
client: thirdwebClient,
60+
address: walletAddress,
61+
chain: await getChain(chainId),
62+
});
63+
64+
expect(balanceV4.name).eq(balanceV5.name);
65+
expect(balanceV4.symbol).eq(balanceV5.symbol);
66+
expect(balanceV4.decimals).eq(balanceV5.decimals);
67+
expect(balanceV4.displayValue).eq(balanceV5.displayValue);
68+
expect(balanceV4.value.toString()).eq(balanceV5.value.toString());
69+
});
70+
71+
it("tests for events/get-all", async () => {
72+
const chainId = 137;
73+
const contractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
74+
const fromBlock = 65334800;
75+
const toBlock = 65334801;
76+
const order = Math.random() > 0.5 ? "asc" : "desc";
77+
78+
// v4
79+
const contractV4 = await getContractV4({ chainId, contractAddress });
80+
const eventsV4 = await contractV4.events.getAllEvents({
81+
fromBlock,
82+
toBlock,
83+
order,
84+
});
85+
86+
// v5.
87+
const contractV5 = getContractV5({
88+
client: thirdwebClient,
89+
address: contractAddress,
90+
chain: await getChain(chainId),
91+
});
92+
const eventsV5Raw = await getContractEvents({
93+
contract: contractV5,
94+
fromBlock: maybeBigInt(fromBlock?.toString()),
95+
toBlock: maybeBigInt(toBlock?.toString()),
96+
});
97+
98+
const eventsV5 = eventsV5Raw.map(toContractEventV4Schema).sort((a, b) => {
99+
return order === "desc"
100+
? b.transaction.blockNumber - a.transaction.blockNumber
101+
: a.transaction.blockNumber - b.transaction.blockNumber;
102+
});
103+
104+
// check two array ordering is the same
105+
expect(eventsV4.length).eq(eventsV5.length);
106+
for (let i = 0; i < eventsV4.length; i++) {
107+
expect(eventsV4[i].transaction.transactionHash).eq(
108+
eventsV5[i].transaction.transactionHash,
109+
);
110+
}
111+
});
112+
});

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10176,10 +10176,10 @@ thirdweb@5.29.6:
1017610176
uqr "0.1.2"
1017710177
viem "2.13.7"
1017810178

10179-
thirdweb@^5.71.0:
10180-
version "5.77.0"
10181-
resolved "https://registry.yarnpkg.com/thirdweb/-/thirdweb-5.77.0.tgz#4a2e48ded8e07c6ded0256c1440cf5b7ad740cec"
10182-
integrity sha512-ugrrZSjy8WnY4tQ/OdFn9eXFp/XAfz8lvVF7zUcaY/WRY/x3k69n31oA87FrKsiBIcJuib2lc2sHjjstg8qUcQ==
10179+
thirdweb@^5.78.0:
10180+
version "5.78.0"
10181+
resolved "https://registry.yarnpkg.com/thirdweb/-/thirdweb-5.78.0.tgz#24ef3f773b13353797b72149e8e27e0bb3bb2bbf"
10182+
integrity sha512-S67omjLP0hmqHNEQmIFuliGF9lj0RRpqfPKKRqEMkYolQrCL1l0wtx0jLbzYRPEkopN+51qOXUsyO7AHWokM/Q==
1018310183
dependencies:
1018410184
"@coinbase/wallet-sdk" "4.2.4"
1018510185
"@emotion/react" "11.14.0"

0 commit comments

Comments
 (0)