Skip to content

Commit

Permalink
fix for adding wallets dynamically for a new chain (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanW3 authored Sep 14, 2023
1 parent f38658f commit 868c26d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
22 changes: 20 additions & 2 deletions core/database/dbOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export const getWalletDetails = async (

export const addWalletToDB = async (
chainId: string,
dbInstance: Knex,
walletAddress: string,
slug: string,
dbInstance: Knex,
walletType: string,
): Promise<void> => {
try {
const sdk = await getSDK(chainId);
Expand All @@ -76,7 +77,8 @@ export const addWalletToDB = async (
blockchainNonce: BigNumber.from(walletNonce ?? 0).toNumber(),
lastSyncedTimestamp: new Date(),
lastUsedNonce: -1,
walletType: slug,
walletType,
slug,
};

await insertIntoWallets(walletData, dbInstance);
Expand Down Expand Up @@ -176,3 +178,19 @@ export const addWalletDataWithSupportChainsNonceToDB = async (
throw error;
}
};

export const getWalletDetailsWithoutChain = async (
walletAddress: string,
database: Knex,
): Promise<any> => {
try {
const walletDetails = await database("wallets")
.select("*")
.where({ walletAddress: walletAddress.toLowerCase() })
.first();

return walletDetails;
} catch (error) {
throw error;
}
};
32 changes: 22 additions & 10 deletions server/helpers/dbOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { FastifyInstance, FastifyRequest } from "fastify";
import { StatusCodes } from "http-status-codes";
import { Knex } from "knex";
import { v4 as uuid } from "uuid";
import { connectToDatabase, getWalletDetails } from "../../core";
import {
addWalletToDB,
connectToDatabase,
getWalletDetails,
getWalletDetailsWithoutChain,
} from "../../core";
import { createCustomError } from "../../core/error/customError";
import {
TransactionSchema,
Expand Down Expand Up @@ -63,15 +68,22 @@ export const queueTransaction = async (
);

if (!walletDetails) {
// await addWalletToDB(
// chainId,
// dbInstance,
// walletAddress,
// chainData.slug,
// getWalletType(),
// );
throw new Error(
`Import Wallet Address ${walletAddress} to DB using /wallet/import end-point`,
const walletData = await getWalletDetailsWithoutChain(
walletAddress.toLowerCase(),
dbInstance,
);

if (!walletData) {
throw new Error(
`Import Wallet Address ${walletAddress} to DB using /wallet/import end-point.`,
);
}
await addWalletToDB(
chainId,
dbInstance,
walletAddress,
chainData.slug,
walletData.walletType,
);
}
// encode tx
Expand Down

0 comments on commit 868c26d

Please sign in to comment.