From 868c26dd1d24679a706b70e82dfb451e4b14ecb7 Mon Sep 17 00:00:00 2001 From: farhanW3 <132962163+farhanW3@users.noreply.github.com> Date: Wed, 13 Sep 2023 17:50:49 -0700 Subject: [PATCH] fix for adding wallets dynamically for a new chain (#128) --- core/database/dbOperation.ts | 22 ++++++++++++++++++++-- server/helpers/dbOperations.ts | 32 ++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/core/database/dbOperation.ts b/core/database/dbOperation.ts index 42b4db5ce..ac10207c5 100644 --- a/core/database/dbOperation.ts +++ b/core/database/dbOperation.ts @@ -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 => { try { const sdk = await getSDK(chainId); @@ -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); @@ -176,3 +178,19 @@ export const addWalletDataWithSupportChainsNonceToDB = async ( throw error; } }; + +export const getWalletDetailsWithoutChain = async ( + walletAddress: string, + database: Knex, +): Promise => { + try { + const walletDetails = await database("wallets") + .select("*") + .where({ walletAddress: walletAddress.toLowerCase() }) + .first(); + + return walletDetails; + } catch (error) { + throw error; + } +}; diff --git a/server/helpers/dbOperations.ts b/server/helpers/dbOperations.ts index 616a5db3f..94f79ce63 100644 --- a/server/helpers/dbOperations.ts +++ b/server/helpers/dbOperations.ts @@ -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, @@ -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