Skip to content

Commit d46be41

Browse files
adam-majfurqanrydhanfarhanW3
authored
Add proper caching to get sdk & update nonce management (#121)
* cleaning up docs * more tweaks to intro * more info about server * readme updates * switch to pg connection string * postgres connection string * fix openapi / swagger issue, default password for postgres, remove swagger.yml * change up local docker setup * swapping ts-node for tsx * generate local wallet if none passed in * TODO * Fix local wallet generation * Update environment variables * Update README.md * Fix postgres configuration * remove dependency on wallet_keys env * updating the readme * mid commit where the high level structure is changed but need to update a bunch of files * small cleanup * Fix test cases * Update database name * create eoa mostly works but still working on write flow * gcp kms fix for getSdk * merge and updated new db ops * updating all getSDK * updated roles/permission doc (#115) * Add prisma schemas * swagger UI on Production gives a 404 (#116) * fixed nodemon & retry to not update mined status, let the other process handle it (#117) * Update database queries to use prisma --------- Co-authored-by: furqanrydhan <furqan.rydhan@gmail.com> Co-authored-by: farhanW3 <132962163+farhanW3@users.noreply.github.com>
1 parent b0d862c commit d46be41

26 files changed

+281
-345
lines changed

core/database/dbOperation.ts

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { getChainBySlug } from "@thirdweb-dev/chains";
2-
import { getSupportedChains } from "@thirdweb-dev/sdk";
1+
import { Ethereum } from "@thirdweb-dev/chains";
32
import { BigNumber } from "ethers";
43
import { FastifyInstance } from "fastify";
54
import { Knex } from "knex";
6-
import { getWalletType } from "../helpers";
75
import { WalletData } from "../interfaces";
86
import { getSDK } from "../sdk/sdk";
97
import { getWalletNonce } from "../services/blockchain";
@@ -12,7 +10,7 @@ import { connectToDatabase } from "./dbConnect";
1210
interface WalletExtraData {
1311
awsKmsKeyId?: string;
1412
awsKmsArn?: string;
15-
walletType?: string;
13+
walletType: string;
1614
gcpKmsKeyId?: string;
1715
gcpKmsKeyRingId?: string;
1816
gcpKmsLocationId?: string;
@@ -64,7 +62,7 @@ export const addWalletToDB = async (
6462
dbInstance: Knex,
6563
): Promise<void> => {
6664
try {
67-
const sdk = await getSDK(chainId);
65+
const sdk = await getSDK(chainId, walletAddress);
6866
const walletNonce = await getWalletNonce(
6967
walletAddress.toLowerCase(),
7068
sdk.getProvider(),
@@ -88,90 +86,68 @@ export const addWalletToDB = async (
8886
export const addWalletDataWithSupportChainsNonceToDB = async (
8987
server: FastifyInstance,
9088
dbInstance: Knex,
91-
isWeb3APIInitWallet?: boolean,
92-
walletAddress?: string,
93-
extraTableData?: WalletExtraData,
94-
): Promise<void> => {
89+
extraTableData: WalletExtraData,
90+
walletAddress: string,
91+
): Promise<boolean> => {
9592
try {
9693
server.log.info(
9794
`Setting up wallet Table for walletType ${extraTableData?.walletType}, walletAddress ${walletAddress}`,
9895
);
99-
const supportedChains = await getSupportedChains();
100-
const promises = supportedChains.map(async (chain) => {
101-
try {
102-
const { slug } = chain;
103-
let lastUsedNonce = -1;
104-
let walletType = extraTableData?.walletType
105-
? extraTableData?.walletType
106-
: getWalletType();
107-
const sdk = await getSDK(slug, {
108-
walletAddress,
109-
walletType,
110-
awsKmsKeyId: extraTableData?.awsKmsKeyId,
111-
gcpKmsKeyId: extraTableData?.gcpKmsKeyId,
112-
gcpKmsKeyRingId: extraTableData?.gcpKmsKeyRingId,
113-
gcpKmsLocationId: extraTableData?.gcpKmsLocationId,
114-
gcpKmsKeyVersionId: extraTableData?.gcpKmsKeyVersionId,
115-
gcpKmsResourcePath: extraTableData?.gcpKmsResourcePath,
116-
});
117-
walletAddress =
118-
(await sdk.getSigner()?.getAddress())?.toLowerCase() ?? "";
119-
server.log.debug(`Setting up wallet for chain ${slug}`);
120-
if (walletAddress.length === 0) {
121-
// server.log.warn(`Wallet address not found for chain ${slug}.`);
122-
throw new Error(`Wallet address not found for chain ${slug}.`);
123-
}
124-
const walletBlockchainNonce = await getWalletNonce(
125-
walletAddress,
126-
sdk.getProvider(),
127-
);
128-
const walletDataInDB = await getWalletDetails(
129-
walletAddress,
130-
BigNumber.from(chain.chainId).toString(),
131-
dbInstance,
132-
);
133-
134-
if (walletDataInDB) {
135-
lastUsedNonce = walletDataInDB.lastUsedNonce;
136-
}
137-
138-
// lastUsedNonce should be set to -1 if blockchainNonce is 0
139-
if (
140-
BigNumber.from(walletBlockchainNonce).eq(BigNumber.from(0)) &&
141-
BigNumber.from(lastUsedNonce).eq(BigNumber.from(0))
142-
) {
143-
lastUsedNonce = -1;
144-
}
96+
if (!walletAddress) {
97+
throw new Error(`Can't add wallet to DB without walletAddress`);
98+
}
14599

146-
if (
147-
extraTableData?.walletType &&
148-
extraTableData?.walletType.length > 0
149-
) {
150-
walletType = extraTableData.walletType;
151-
delete extraTableData.walletType;
152-
}
100+
try {
101+
let lastUsedNonce = -1;
102+
let walletType = extraTableData.walletType;
103+
server.log.debug(`Wallet type: ${walletType} `);
104+
const walletDataInDB = await getWalletDetails(
105+
walletAddress,
106+
Ethereum.chainId.toString(),
107+
dbInstance,
108+
);
109+
server.log.debug(
110+
`Got the wallet data from DB ${JSON.stringify(walletDataInDB)}}`,
111+
);
153112

154-
const walletData = {
155-
...extraTableData,
156-
walletAddress: walletAddress.toLowerCase(),
157-
chainId: getChainBySlug(slug).chainId.toString(),
158-
blockchainNonce: BigNumber.from(
159-
walletBlockchainNonce ?? 0,
160-
).toNumber(),
161-
lastSyncedTimestamp: new Date(),
162-
lastUsedNonce,
163-
slug,
164-
walletType,
165-
};
166-
return insertIntoWallets(walletData, dbInstance);
167-
} catch (error) {
168-
server.log.error((error as any).message);
169-
return Promise.resolve();
113+
if (walletDataInDB) {
114+
lastUsedNonce = walletDataInDB.lastUsedNonce;
170115
}
171-
});
172116

173-
await Promise.all(promises);
174-
server.log.info(`Wallet Table setup completed`);
117+
const walletData = {
118+
...extraTableData,
119+
walletAddress: walletAddress.toLowerCase(),
120+
chainId: Ethereum.chainId.toString(),
121+
blockchainNonce: 0,
122+
lastSyncedTimestamp: new Date(),
123+
lastUsedNonce,
124+
slug: Ethereum.slug,
125+
walletType,
126+
};
127+
server.log.info(`Wallet Table about to insert: ${walletData}}`);
128+
const insert = await insertIntoWallets(walletData, dbInstance);
129+
server.log.debug(`Inserted the wallet data into DB, ${insert}`);
130+
const sdk = await getSDK(Ethereum.slug, walletAddress);
131+
/*server.log.debug(`Got the sdk for wallet`);*/
132+
/*const walletBlockchainNonce = await getWalletNonce(*/
133+
/*walletAddress,*/
134+
/*sdk.getProvider(),*/
135+
/*);*/
136+
/*server.log.debug(*/
137+
/*`Got the ${walletBlockchainNonce.toString()} for wallet`,*/
138+
/*);*/
139+
/*// lastUsedNonce should be set to -1 if blockchainNonce is 0*/
140+
/*if (*/
141+
/*BigNumber.from(walletBlockchainNonce).eq(BigNumber.from(0)) &&*/
142+
/*BigNumber.from(lastUsedNonce).eq(BigNumber.from(0))*/
143+
/*) {*/
144+
/*lastUsedNonce = -1;*/
145+
/*}*/
146+
147+
return Promise.resolve(insert ? true : false);
148+
} catch (error) {
149+
throw error;
150+
}
175151
} catch (error) {
176152
throw error;
177153
}

core/env.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export const env = createEnv({
5454
GOOGLE_APPLICATION_PROJECT_ID: z.string().min(1).optional(),
5555
GOOGLE_KMS_KEY_RING_ID: z.string().min(1).optional(),
5656
GOOGLE_KMS_LOCATION_ID: z.string().min(1).optional(),
57-
GOOGLE_KMS_KEY_VERSION_ID: z.string().min(1).optional(),
5857
GOOGLE_APPLICATION_CREDENTIAL_EMAIL: z.string().min(1).optional(),
5958
GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY: z.string().min(1).optional(),
6059
RETRY_TX_ENABLED: boolSchema("true"),
@@ -96,7 +95,6 @@ export const env = createEnv({
9695
GOOGLE_APPLICATION_PROJECT_ID: process.env.GOOGLE_APPLICATION_PROJECT_ID,
9796
GOOGLE_KMS_KEY_RING_ID: process.env.GOOGLE_KMS_KEY_RING_ID,
9897
GOOGLE_KMS_LOCATION_ID: process.env.GOOGLE_KMS_LOCATION_ID,
99-
GOOGLE_KMS_KEY_VERSION_ID: process.env.GOOGLE_KMS_KEY_VERSION_ID,
10098
GOOGLE_APPLICATION_CREDENTIAL_EMAIL:
10199
process.env.GOOGLE_APPLICATION_CREDENTIAL_EMAIL,
102100
GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY:

0 commit comments

Comments
 (0)