-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
63 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
getAddressFromAddress, | ||
getAddressFromId, | ||
getDelegationsTo, | ||
} from './sql/wallet-delegation.queries.js'; | ||
import type { PoolClient } from 'pg'; | ||
|
||
export type WalletDelegate = { address: string; id: number }; | ||
export const NO_USER_ID = -1; | ||
|
||
/** | ||
* TODO | ||
* This is a temporal fix as lru-cache module was | ||
* not correctly packaged into a single file. | ||
*/ | ||
export const addressCache = new Map<string, WalletDelegate>(); | ||
|
||
// Get Main Wallet and ID for address. | ||
// If wallet does not exist, It will NOT be created in address, table. | ||
export async function getMainAddress(address: string, DBConn: PoolClient): Promise<WalletDelegate> { | ||
let addressMapping: WalletDelegate | undefined = addressCache.get(address); | ||
if (addressMapping) return addressMapping; | ||
|
||
// get main address. | ||
// const addressResult = await this.getOrCreateNewAddress(address); | ||
const [addressResult] = await getAddressFromAddress.run({ address }, DBConn); | ||
if (!addressResult) { | ||
// This wallet has never been used before. | ||
// This value will get updated before sent to the STF. | ||
return { address, id: NO_USER_ID }; | ||
} | ||
|
||
// if exists we have to check if it is a delegation. | ||
const [delegate] = await getDelegationsTo.run({ set_id: addressResult.id }, DBConn); | ||
if (!delegate) { | ||
// is main address or has no delegations. | ||
addressMapping = { address: addressResult.address, id: addressResult.id }; | ||
addressCache.set(address, addressMapping); | ||
return addressMapping; | ||
} | ||
|
||
// if is delegation, get main address. | ||
const [mainAddress] = await getAddressFromId.run({ id: delegate.from_id }, DBConn); | ||
addressMapping = { address: mainAddress.address, id: mainAddress.id }; | ||
addressCache.set(address, addressMapping); | ||
return addressMapping; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters