Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: no limit warning #3134

Merged
merged 14 commits into from
May 14, 2024
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({})
);
Comment on lines 152 to 154
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use the new endpoint instead of this one? it should contain the same information and the additional account limit info.

so we don't need to do a second request.


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
Loading