Skip to content

Commit

Permalink
amountless on-chain, fix receive warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed May 8, 2024
1 parent 694e1c3 commit b6bdd14
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion public/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"error_creating_address": "Something went wrong when creating the on-chain address.",
"amount_editable": {
"receive_too_small": "Your first receive needs to be {{amount}} SATS or greater.",
"setup_fee_lightning": "A lightning setup fee will be charged if paid over lightning.",
"setup_fee_lightning": "A lightning setup fee will be charged.",
"more_than_21m": "There are only 21 million bitcoin.",
"set_amount": "Set amount",
"max": "MAX",
Expand Down
22 changes: 12 additions & 10 deletions src/components/ReceiveWarnings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ export function ReceiveWarnings(props: {
if (state.federations?.length !== 0 && props.from_fedi_to_ln !== true) {
return undefined;
}
if (
(state.balance?.lightning || 0n) === 0n &&
!state.settings?.lsps_connection_string
) {
return i18n.t("receive.amount_editable.receive_too_small", {
amount: "100,000"
});
}
if (props.flavor === "lightning") {
if (
(state.balance?.lightning || 0n) === 0n &&
!state.settings?.lsps_connection_string
) {
return i18n.t("receive.amount_editable.receive_too_small", {
amount: "100,000"
});
}

if (props.amountSats > (inboundCapacity() || 0n)) {
return i18n.t("receive.amount_editable.setup_fee_lightning");
if (props.amountSats > (inboundCapacity() || 0n)) {
return i18n.t("receive.amount_editable.setup_fee_lightning");
}
}

return undefined;
Expand Down
74 changes: 36 additions & 38 deletions src/routes/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,24 @@ export function Receive() {
}
}

async function getOnchainReceiveString(amount: bigint) {
async function getOnchainReceiveString(amount?: bigint) {
try {
if (amount < 546n) {
if (amount && amount < 546n) {
throw new Error(i18n.t("receive.error_under_min_onchain"));
}
const raw = await sw.get_new_address(receiveTags());

const btc_amount = await sw.convert_sats_to_btc(amount);

const params = objectToSearchParams({
amount: btc_amount.toString()
});

const address = raw?.address;
setRawReceiveStrings({ address });
return `bitcoin:${address}?${params}`;

if (amount && amount > 0n) {
const btc_amount = await sw.convert_sats_to_btc(amount);
const params = objectToSearchParams({
amount: btc_amount.toString()
});
setRawReceiveStrings({ address });
return `bitcoin:${address}?${params}`;
} else {
return `bitcoin:${address}`;
}
} catch (e) {
console.error(e);
}
Expand All @@ -275,37 +277,31 @@ export function Receive() {
}

async function getQr() {
if (amount()) {
setLoading(true);
try {
if (flavor() === "lightning") {
const lightning = await getLightningReceiveString(amount());
setReceiveStrings({ lightning });
}

if (flavor() === "onchain") {
const onchain = await getOnchainReceiveString(amount());
setReceiveStrings({ onchain });
}
setLoading(true);
try {
if (flavor() === "lightning") {
const lightning = await getLightningReceiveString(amount());
setReceiveStrings({ lightning });
}

if (
!receiveStrings()?.lightning &&
!receiveStrings()?.onchain
) {
throw new Error(i18n.t("receive.receive_strings_error"));
}
if (flavor() === "onchain") {
const onchain = await getOnchainReceiveString(amount());
setReceiveStrings({ onchain });
}

// TODO: THOUGHTS?
if (!error()) {
setReceiveState("show");
}
} catch (e) {
console.error(e);
showToast(eify(e));
if (!receiveStrings()?.lightning && !receiveStrings()?.onchain) {
throw new Error(i18n.t("receive.receive_strings_error"));
}

setLoading(false);
if (!error()) {
setReceiveState("show");
}
} catch (e) {
console.error(e);
showToast(eify(e));
}

setLoading(false);
}

const qrString = createMemo(() => {
Expand Down Expand Up @@ -442,7 +438,9 @@ export function Receive() {
/>
</form>
<Button
disabled={!amount()}
disabled={
!amount() && !(flavor() === "onchain")
}
intent="green"
onClick={onSubmit}
loading={loading()}
Expand Down

0 comments on commit b6bdd14

Please sign in to comment.