Skip to content

Commit

Permalink
feat: no limit warning (#3134)
Browse files Browse the repository at this point in the history
* feat: no limit warning

* feat: improve loading time

* fix: allow caching only when limit value don't change

* fix: types
  • Loading branch information
pavanjoshi914 authored May 14, 2024
1 parent 1c78718 commit 8435e0a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/app/context/AccountContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface AccountContextType {
avatarUrl?: AccountInfo["avatarUrl"];
connectorType?: AccountInfo["connectorType"];
lightningAddress?: AccountInfo["lightningAddress"];
nodeRequired?: AccountInfo["nodeRequired"];
} | null;
balancesDecorated: {
fiatBalance: string;
Expand Down
31 changes: 28 additions & 3 deletions src/app/screens/Home/DefaultView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ const DefaultView: FC<Props> = (props) => {
<PublisherLnData lnData={props.lnDataFromCurrentTab[0]} />
)}

<div className="p-4">
<div className="flex flex-col gap-4 p-4">
{isBlockedUrl && (
<div className="items-center dark:text-white text-sm mb-4">
<div className="items-center dark:text-white text-sm">
<Alert type="info">
<p className="pb-2">
{t("default_view.is_blocked_hint", {
Expand Down Expand Up @@ -165,8 +165,33 @@ const DefaultView: FC<Props> = (props) => {
</div>
</Alert>
)}
{account?.nodeRequired ? (
<Alert type="warn">
<div className="flex items-center gap-2">
<div className="shrink-0">
<PopiconsCircleExclamationLine className="w-5 h-5" />
</div>
<span className="text-sm">
<Trans
i18nKey={"default_view.node_required"}
t={t}
components={[
// eslint-disable-next-line react/jsx-key
<a
className="underline"
href="https://getalby.com"
target="_blank"
rel="noreferrer"
/>,
]}
/>
</span>
</div>
</Alert>
) : (
<BalanceBox />
)}

<BalanceBox />
{(accountLoading || lightningAddress) && (
<div className="flex justify-center">
<a
Expand Down
8 changes: 7 additions & 1 deletion src/common/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export interface AccountInfoRes {
connectorType: ConnectorType;
balance: { balance: string | number; currency: ACCOUNT_CURRENCIES };
currentAccountId: string;
info: { alias: string; pubkey?: string; lightning_address?: string };
info: {
alias: string;
pubkey?: string;
lightning_address?: string;
node_required?: boolean;
};
name: string;
avatarUrl?: string;
}
Expand Down Expand Up @@ -114,6 +119,7 @@ export const swrGetAccountInfo = async (
currency: currency || "BTC", // set default currency for every account
avatarUrl,
lightningAddress: response.info.lightning_address,
nodeRequired: response.info.node_required,
};
storeAccounts({
...accountsCache,
Expand Down
75 changes: 70 additions & 5 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { auth, Client } from "@getalby/sdk";
import {
CreateSwapParams,
CreateSwapResponse,
GetAccountInformationResponse,
Invoice,
RequestOptions,
SwapInfoResponse,
Token,
} from "@getalby/sdk/dist/types";
import browser from "webextension-polyfill";
import { decryptData, encryptData } from "~/common/lib/crypto";
import { Account, OAuthToken } from "~/types";
import { Account, GetAccountInformationResponses, OAuthToken } from "~/types";
import state from "../state";
import Connector, {
CheckPaymentArgs,
Expand All @@ -37,6 +36,26 @@ interface Config {
oAuthToken: OAuthToken | undefined;
}

interface UserDetails {
identifier: string;
email: string;
name: string;
avatar: string | null;
lightning_address: string;
shared_node: boolean;
node_required: boolean;
limits: {
max_send_volume: number;
max_send_amount: number;
max_receive_volume: number;
max_receive_amount: number;
max_account_balance: number;
max_volume_period_in_days: number;
};
node_type: string;
node_connection_error_count: number;
}

export default class Alby implements Connector {
private account: Account;
private config: Config;
Expand Down Expand Up @@ -116,23 +135,28 @@ export default class Alby implements Connector {
}

async getInfo(): Promise<
GetInfoResponse<WebLNNode & GetAccountInformationResponse>
GetInfoResponse<WebLNNode & GetAccountInformationResponses>
> {
const cacheKey = "getInfo";
const cacheValue = this._cache.get(cacheKey) as GetInfoResponse<
WebLNNode & GetAccountInformationResponse
WebLNNode & GetAccountInformationResponses
>;
if (cacheValue) {

const node_required = await this._isNodeRequired();

if (cacheValue && cacheValue.data.node_required === node_required) {
return cacheValue;
}

try {
const info = await this._request((client) =>
client.accountInformation({})
);

const returnValue = {
data: {
...info,
node_required: node_required,
alias: "🐝 getalby.com",
},
};
Expand Down Expand Up @@ -406,4 +430,45 @@ export default class Alby implements Connector {
throw new Error("Invalid token");
}
}

private async _isNodeRequired() {
const url = `${process.env.ALBY_API_URL}/internal/users`;

const requestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
...(await this._authUser?.getAuthHeader()),
"User-Agent": `lightning-browser-extension:${process.env.VERSION}`,
"X-User-Agent": `lightning-browser-extension:${process.env.VERSION}`,
},
};

try {
const details = await this._genericRequest<UserDetails>(
url,
requestOptions
);

return details.node_required;
} catch (error) {
console.error("Error fetching limits:", error);
throw error;
}
}

private async _genericRequest<T>(
url: RequestInfo,
init: RequestInit
): Promise<T> {
const res = await fetch(url, init);

if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}

const data: T = await res.json();

return data;
}
}
1 change: 1 addition & 0 deletions src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
"description": "Fund your account and receive via your lightning address or an invoice"
}
},
"node_required": "Your Alby Account needs a wallet. Connect a wallet to your account on <0>getalby.com</0>",
"upgrade_account": "You are using the old LNDHub setup. <0>Please re-connect</0> your Alby account to get access to the latest features."
}
},
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CreateSwapParams } from "@getalby/sdk/dist/types";
import {
CreateSwapParams,
GetAccountInformationResponse,
} from "@getalby/sdk/dist/types";
import { PaymentRequestObject } from "bolt11-signet";
import { Runtime } from "webextension-polyfill";
import { ACCOUNT_CURRENCIES, CURRENCIES } from "~/common/constants";
Expand Down Expand Up @@ -46,8 +49,13 @@ export interface AccountInfo {
currency: ACCOUNT_CURRENCIES;
avatarUrl?: string;
lightningAddress?: string;
nodeRequired?: boolean;
}

export type GetAccountInformationResponses = GetAccountInformationResponse & {
node_required?: boolean;
};

export interface MetaData {
title?: string;
description?: string;
Expand Down

0 comments on commit 8435e0a

Please sign in to comment.