Skip to content

Commit 6217c50

Browse files
authored
fix: remove legacy package; update withdraw endpoint (#465)
* fix: use thirdweb sdk for gas estimation * dummy value when estimating cost * remove unneeded try/catch
1 parent bc01d7d commit 6217c50

File tree

6 files changed

+649
-254
lines changed

6 files changed

+649
-254
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
},
3131
"dependencies": {
3232
"@aws-sdk/client-kms": "^3.398.0",
33-
"@eth-optimism/sdk": "^3.1.6",
3433
"@fastify/cookie": "^8.3.0",
3534
"@fastify/cors": "^8.2.1",
3635
"@fastify/express": "^2.3.0",
@@ -68,7 +67,7 @@
6867
"pino": "^8.15.1",
6968
"pino-pretty": "^10.0.0",
7069
"prisma": "^5.2.0",
71-
"thirdweb": "^1.0.0-beta-0e947333cefd127c2ccda059c775d2975021be33-20240229054327",
70+
"thirdweb": "^5.0.0-beta-ca68bc77e74f594360b4da1e9e77793b66cfb12a-20240323045412",
7271
"uuidv4": "^6.2.13",
7372
"viem": "^1.14.0",
7473
"zod": "^3.21.4"

src/utils/env.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import * as dotenv from "dotenv";
33
import type { ZodError } from "zod";
44
import { z } from "zod";
55

6-
dotenv.config({
7-
debug: true,
8-
override: false,
9-
});
6+
dotenv.config();
107

118
export const JsonSchema = z.string().refine(
129
(value) => {

src/utils/sdk.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { sha256HexSync } from "@thirdweb-dev/crypto";
2+
import { createThirdwebClient } from "thirdweb";
23
import { env } from "./env";
34

45
export const thirdwebClientId = sha256HexSync(
56
env.THIRDWEB_API_SECRET_KEY,
67
).slice(0, 32);
8+
9+
export const thirdwebClient = createThirdwebClient({
10+
clientId: thirdwebClientId,
11+
});

src/worker/tasks/processTx.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
sendWebhooks,
3535
} from "../../utils/webhook";
3636
import { randomNonce } from "../utils/nonce";
37-
import { getWithdrawalValue } from "../utils/withdraw";
37+
import { getWithdrawValue } from "../utils/withdraw";
3838

3939
type RpcResponseData = {
4040
tx: Transactions;
@@ -161,13 +161,12 @@ export const processTx = async () => {
161161
try {
162162
let value = BigNumber.from(tx.value ?? 0);
163163
if (tx.extension === "withdraw") {
164-
value = await getWithdrawalValue({
165-
provider,
164+
const withdrawValue = await getWithdrawValue({
166165
chainId,
167166
fromAddress: tx.fromAddress!,
168167
toAddress: tx.toAddress!,
169-
gasOverrides,
170168
});
169+
value = BigNumber.from(withdrawValue.toString());
171170
}
172171

173172
const txRequest = await signer.populateTransaction({

src/worker/utils/withdraw.ts

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1-
import { asL2Provider } from "@eth-optimism/sdk";
2-
import { Base, Optimism, Zora } from "@thirdweb-dev/chains";
3-
import { getDefaultGasOverrides, toUnits } from "@thirdweb-dev/sdk";
4-
import { BigNumber, ethers } from "ethers";
1+
import { defineChain, prepareTransaction } from "thirdweb";
2+
import { ethers5Adapter } from "thirdweb/adapters/ethers5";
3+
import { estimateGasCost } from "thirdweb/transaction";
4+
import { getWalletBalance } from "thirdweb/wallets";
5+
import { getWallet } from "../../utils/cache/getWallet";
6+
import { thirdwebClient } from "../../utils/sdk";
57

6-
interface GetWithdrawalValueParams {
7-
provider: ethers.providers.Provider;
8+
interface GetWithdrawValueParams {
89
chainId: number;
910
fromAddress: string;
1011
toAddress: string;
11-
gasOverrides: Awaited<ReturnType<typeof getDefaultGasOverrides>>;
1212
}
1313

14-
export const getWithdrawalValue = async ({
15-
provider,
14+
export const getWithdrawValue = async ({
1615
chainId,
1716
fromAddress,
1817
toAddress,
19-
gasOverrides,
20-
}: GetWithdrawalValueParams): Promise<BigNumber> => {
21-
const balance = await provider.getBalance(fromAddress);
18+
}: GetWithdrawValueParams): Promise<bigint> => {
19+
const chain = defineChain(chainId);
2220

23-
let transferCost = BigNumber.from(
24-
gasOverrides.maxFeePerGas || gasOverrides.gasPrice || toUnits(1, 9),
25-
).mul(21000);
21+
// Get wallet balance.
22+
const wallet = await getWallet({ chainId, walletAddress: fromAddress });
23+
const signer = await wallet.getSigner();
24+
const account = await ethers5Adapter.signer.fromEthers(signer);
25+
const { value: balanceWei } = await getWalletBalance({
26+
account,
27+
client: thirdwebClient,
28+
chain,
29+
});
2630

27-
if (
28-
chainId === Base.chainId ||
29-
chainId === Optimism.chainId ||
30-
chainId === Zora.chainId
31-
) {
32-
const l2Provider = asL2Provider(provider);
33-
transferCost = await l2Provider.estimateTotalGasCost({
34-
to: toAddress,
35-
value: 1,
36-
});
37-
}
31+
// Estimate gas for a transfer.
32+
const transferTx = prepareTransaction({
33+
value: BigInt(1),
34+
to: toAddress,
35+
chain,
36+
client: thirdwebClient,
37+
});
38+
const { wei: transferCostWei } = await estimateGasCost({
39+
transaction: transferTx,
40+
});
3841

39-
transferCost = transferCost.mul(120).div(100); // +20% in all cases for safety
40-
return BigNumber.from(balance).sub(transferCost);
42+
// Add a 20% buffer for gas variance.
43+
const buffer = BigInt(Math.round(Number(transferCostWei) * 0.2));
44+
45+
return balanceWei - transferCostWei - buffer;
4146
};

0 commit comments

Comments
 (0)