Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update thirdweb SDK #856

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@
"knex": "^3.1.0",
"mnemonist": "^0.39.8",
"node-cron": "^3.0.2",
"ox": "^0.6.9",
"ox": "0.6.9",
"pg": "^8.11.3",
"prisma": "^5.14.0",
"prom-client": "^15.1.3",
"superjson": "^2.2.1",
"thirdweb": "^5.83.0",
"thirdweb": "5.89.0",
"undici": "^6.20.1",
"uuid": "^9.0.1",
"viem": "^2.21.54",
"viem": "2.22.17",
"winston": "^3.14.1",
"zod": "^3.23.8"
},
Expand Down Expand Up @@ -103,7 +103,9 @@
"cookie": ">=0.7.0",
"elliptic": ">=6.6.0",
"micromatch": ">=4.0.8",
"ox": "0.6.9",
"secp256k1": ">=4.0.4",
"viem": "2.22.17",
"ws": ">=8.17.1",
"cross-spawn": ">=7.0.6"
},
Expand Down
5 changes: 5 additions & 0 deletions src/abitype.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "abitype" {
export interface Config {
AddressType: string;
}
}
22 changes: 12 additions & 10 deletions src/server/routes/contract/events/get-all-events.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import {
type GetContractEventsResult,
type PreparedEvent,
getContract,
getContractEvents,
} from "thirdweb";
import { getChain } from "../../../../shared/utils/chain";
import { prettifyError } from "../../../../shared/utils/error";
import { thirdwebClient } from "../../../../shared/utils/sdk";
import { createCustomError } from "../../../middleware/error";
import {
contractEventSchema,
eventsQuerystringSchema,
} from "../../../schemas/contract";
import { toContractEventV4Schema } from "../../../schemas/event";
import {
contractParamSchema,
standardResponseSchema,
} from "../../../schemas/shared-api-schemas";
import { thirdwebClient } from "../../../../shared/utils/sdk";
import { getChain } from "../../../../shared/utils/chain";
import { getChainIdFromChain } from "../../../utils/chain";
import { getContract, getContractEvents } from "thirdweb";
import {
type ContractEventV5,
toContractEventV4Schema,
} from "../../../schemas/event";
import { createCustomError } from "../../../middleware/error";
import { prettifyError } from "../../../../shared/utils/error";

const requestSchema = contractParamSchema;

Expand Down Expand Up @@ -97,7 +99,7 @@ export async function getAllEvents(fastify: FastifyInstance) {
chain: await getChain(chainId),
});

let eventsV5: ContractEventV5[];
let eventsV5: GetContractEventsResult<PreparedEvent<never>[], true>;
try {
eventsV5 = await getContractEvents({
contract: contract,
Expand Down
16 changes: 8 additions & 8 deletions src/server/routes/contract/read/read-batch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type, type Static } from "@sinclair/typebox";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import SuperJSON from "superjson";
Expand All @@ -10,14 +10,14 @@ import {
resolveMethod,
} from "thirdweb";
import { prepareMethod } from "thirdweb/contract";
import { decodeAbiParameters } from "viem/utils";
import type { AbiFunction } from "viem";
import { createCustomError } from "../../../middleware/error";
import { getChainIdFromChain } from "../../../utils/chain";
import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
import { decodeAbiParameters } from "viem/utils";
import { getChain } from "../../../../shared/utils/chain";
import { thirdwebClient } from "../../../../shared/utils/sdk";
import { prettifyError } from "../../../../shared/utils/error";
import { thirdwebClient } from "../../../../shared/utils/sdk";
import { createCustomError } from "../../../middleware/error";
import { standardResponseSchema } from "../../../schemas/shared-api-schemas";
import { getChainIdFromChain } from "../../../utils/chain";

const MULTICALL3_ADDRESS = "0xcA11bde05977b3631167028862bE2a173976CA11";

Expand Down Expand Up @@ -118,7 +118,7 @@ export async function readBatchRoute(fastify: FastifyInstance) {
);

// Get Multicall3 contract
const multicall = await getContract({
const multicall = getContract({
chain,
address: multicallAddress,
client: thirdwebClient,
Expand All @@ -132,7 +132,7 @@ export async function readBatchRoute(fastify: FastifyInstance) {
});

// Process results
const processedResults = results.map((result: unknown, i) => {
const processedResults = results.map((result: unknown, i: number) => {
const { success, returnData } = result as {
success: boolean;
returnData: unknown;
Expand Down
7 changes: 4 additions & 3 deletions src/server/routes/contract/write/write.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Type, type Static } from "@sinclair/typebox";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import type { AbiParameters } from "ox";
import { prepareContractCall, resolveMethod } from "thirdweb";
import { parseAbiParams, type AbiFunction } from "thirdweb/utils";
import { type AbiFunction, parseAbiParams } from "thirdweb/utils";
import { getContractV5 } from "../../../../shared/utils/cache/get-contractv5";
import { prettifyError } from "../../../../shared/utils/error";
import { queueTransaction } from "../../../../shared/utils/transaction/queue-transation";
Expand Down Expand Up @@ -98,7 +99,7 @@ export async function writeToContract(fastify: FastifyInstance) {
const functionNameOrSignature = sanitizeFunctionName(functionName);
method = await resolveMethod(functionNameOrSignature)(contract);
params = parseAbiParams(
method.inputs.map((i) => i.type),
method.inputs.map((i: AbiParameters.Parameter) => i.type),
args,
);
} catch (e) {
Expand Down
8 changes: 4 additions & 4 deletions src/server/routes/transaction/blockchain/get-logs.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Type, type Static } from "@sinclair/typebox";
import type { AbiEvent } from "abitype";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import type { AbiEvent } from "ox";
import superjson from "superjson";
import {
type Hex,
eth_getTransactionReceipt,
getContract,
getRpcClient,
parseEventLogs,
prepareEvent,
type Hex,
} from "thirdweb";
import { resolveContractAbi } from "thirdweb/contract";
import type { TransactionReceipt } from "thirdweb/transaction";
Expand Down Expand Up @@ -204,7 +204,7 @@ export async function getTransactionLogs(fastify: FastifyInstance) {
client: thirdwebClient,
});

const abi: AbiEvent[] = await resolveContractAbi(contract);
const abi: AbiEvent.AbiEvent[] = await resolveContractAbi(contract);
const eventSignatures = abi.filter((item) => item.type === "event");
if (eventSignatures.length === 0) {
throw createCustomError(
Expand Down
24 changes: 8 additions & 16 deletions src/server/schemas/event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BigNumber } from "ethers";
import type { AbiEvent } from "ox";
import type { GetContractEventsResult, PreparedEvent } from "thirdweb";

export type ContractEventV4 = {
eventName: string;
Expand All @@ -18,32 +20,22 @@ export type ContractEventV4 = {
};
};

export type ContractEventV5 = {
eventName: string;
args: Record<string, unknown>;
address: string;
topic: string[];
data: string;
blockNumber: bigint;
transactionHash: string;
transactionIndex: number;
blockHash: string;
logIndex: number;
removed: boolean;
};

/**
* Mapping of events v5 response to v4 for backward compatiblity.
* Clients may be using this api and dont want to break things.
*/
export function toContractEventV4Schema(
eventV5: ContractEventV5,
eventV5: GetContractEventsResult<
PreparedEvent<AbiEvent.AbiEvent>[],
true
>[number],
): ContractEventV4 {
const eventName = eventV5.eventName;

// backwards compatibility of BigInt(v5) to BigNumber(v4)
const data: Record<string, unknown> = {};
for (const key of Object.keys(eventV5.args)) {
// @ts-expect-error - FIXME this is kinda hacky
let value = eventV5.args[key];
if (typeof value === "bigint") {
value = BigNumber.from(value.toString());
Expand All @@ -61,7 +53,7 @@ export function toContractEventV4Schema(
removed: eventV5.removed,
address: eventV5.address,
data: eventV5.data,
topic: eventV5.topic,
topic: eventV5.topics,
transactionHash: eventV5.transactionHash,
logIndex: eventV5.logIndex,
event: eventV5.eventName,
Expand Down
8 changes: 4 additions & 4 deletions src/worker/tasks/process-event-logs-worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Prisma, Webhooks } from "@prisma/client";
import type { AbiEvent } from "abitype";
import type { AbiEvent } from "ox";
import { Worker, type Job, type Processor } from "bullmq";
import superjson from "superjson";
import {
Expand Down Expand Up @@ -144,9 +144,9 @@ const getLogs = async ({

// Get events to filter by, if any.
// Resolve the event name, "Transfer", to event signature, "Transfer(address to, uint256 quantity)".
const events: PreparedEvent<AbiEvent>[] = [];
const events: PreparedEvent<AbiEvent.AbiEvent>[] = [];
if (f.events.length > 0) {
const abi = await resolveContractAbi<AbiEvent[]>(contract);
const abi = await resolveContractAbi<AbiEvent.AbiEvent[]>(contract);
for (const signature of abi) {
if (f.events.includes(signature.name)) {
events.push(prepareEvent({ signature }));
Expand Down Expand Up @@ -224,7 +224,7 @@ const formatDecodedLog = async (args: {
}): Promise<Record<string, Prisma.InputJsonObject> | undefined> => {
const { contract, eventName, logArgs } = args;

const abi = await resolveContractAbi<AbiEvent[]>(contract);
const abi = await resolveContractAbi<AbiEvent.AbiEvent[]>(contract);
const eventSignature = abi.find((a) => a.name === eventName);
if (!eventSignature) {
return;
Expand Down
14 changes: 7 additions & 7 deletions src/worker/tasks/process-transaction-receipts-worker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Prisma } from "@prisma/client";
import type { AbiEvent } from "abitype";
import { Worker, type Job, type Processor } from "bullmq";
import { type Job, type Processor, Worker } from "bullmq";
import type { AbiEvent } from "ox";
import superjson from "superjson";
import {
type Address,
type ThirdwebContract,
eth_getBlockByNumber,
eth_getTransactionReceipt,
getContract,
getRpcClient,
type Address,
type ThirdwebContract,
} from "thirdweb";
import { resolveContractAbi } from "thirdweb/contract";
import { decodeFunctionData, type Abi, type Hash } from "viem";
import { type Abi, type Hash, decodeFunctionData } from "viem";
import { bulkInsertContractTransactionReceipts } from "../../shared/db/contract-transaction-receipts/create-contract-transaction-receipts";
import { WebhooksEventTypes } from "../../shared/schemas/webhooks";
import { getChain } from "../../shared/utils/chain";
Expand All @@ -20,8 +20,8 @@ import { normalizeAddress } from "../../shared/utils/primitive-types";
import { redis } from "../../shared/utils/redis/redis";
import { thirdwebClient } from "../../shared/utils/sdk";
import {
ProcessTransactionReceiptsQueue,
type EnqueueProcessTransactionReceiptsData,
ProcessTransactionReceiptsQueue,
} from "../queues/process-transaction-receipts-queue";
import { logWorkerExceptions } from "../queues/queues";
import { SendWebhookQueue } from "../queues/send-webhook-queue";
Expand Down Expand Up @@ -211,7 +211,7 @@ const getFunctionName = async (args: {
contract: ThirdwebContract;
data: Hash;
}) => {
const abi = await resolveContractAbi<AbiEvent[]>(args.contract);
const abi = await resolveContractAbi<AbiEvent.AbiEvent[]>(args.contract);
const decoded = decodeFunctionData<Abi>({
abi,
data: args.data,
Expand Down
8 changes: 4 additions & 4 deletions src/worker/tasks/send-transaction-worker.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Worker, type Job, type Processor } from "bullmq";
import assert from "node:assert";
import { type Job, type Processor, Worker } from "bullmq";
import superjson from "superjson";
import {
type Hex,
getAddress,
getContract,
readContract,
toSerializableTransaction,
toTokens,
type Hex,
} from "thirdweb";
import { getChainMetadata } from "thirdweb/chains";
import { isZkSyncChain, stringify } from "thirdweb/utils";
import type { Account } from "thirdweb/wallets";
import {
type UserOperation,
bundleUserOp,
createAndSignUserOp,
smartWallet,
type UserOperation,
} from "thirdweb/wallets/smart";
import { getContractAddress } from "viem";
import { TransactionDB } from "../../shared/db/transactions/db";
Expand Down Expand Up @@ -56,8 +56,8 @@ import { reportUsage } from "../../shared/utils/usage";
import { MineTransactionQueue } from "../queues/mine-transaction-queue";
import { logWorkerExceptions } from "../queues/queues";
import {
SendTransactionQueue,
type SendTransactionData,
SendTransactionQueue,
} from "../queues/send-transaction-queue";

/**
Expand Down
Loading
Loading