Skip to content

Commit

Permalink
Try each federation guardian when joining
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman authored and futurepaul committed Apr 10, 2024
1 parent cdc7c88 commit 0dbcdc5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/logic/errorDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type MutinyError =
| "Failed to read or write json from the front end"
| "The given node pubkey is invalid."
| "Failed to get nostr data."
| "Error with NIP-07 extension"
| "Failed to get the bitcoin price."
| "Satoshi amount is invalid"
| "Failed to execute a dlc function"
Expand All @@ -54,6 +55,11 @@ type MutinyError =
| "Failed to create payjoin request."
| "Payjoin response error: {0}"
| "Payjoin configuration failed."
| "Error calling Cashu Mint"
| "Mint URL in token is empty"
| "Token has been already spent."
| "A federation is required"
| "Failed to connect to a federation."
| "Unknown Error";

export function matchError(e: unknown): Error {
Expand Down
37 changes: 29 additions & 8 deletions src/routes/settings/ManageFederations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function AddFederationForm(props: { refetch?: RefetchType }) {
f: FederationForm
) => {
const federation_code = f.federation_code.trim();
await onSelect(federation_code);
await onSelect([federation_code]);
};

const [federations] = createResource(async () => {
Expand All @@ -124,14 +124,36 @@ function AddFederationForm(props: { refetch?: RefetchType }) {
}
});

const onSelect = async (inviteCode: string) => {
const onSelect = async (inviteCodes: string[]) => {
setSuccess("");
setError(undefined);
setLoadingFederation(inviteCode);
try {
const newFederation =
await state.mutiny_wallet?.new_federation(inviteCode);
console.log("New federation added:", newFederation);
for (const inviteCode of inviteCodes) {
try {
console.log("Adding federation:", inviteCode);
setLoadingFederation(inviteCode);
const newFederation =
await state.mutiny_wallet?.new_federation(inviteCode);
console.log("New federation added:", newFederation);
break;
} catch (e) {
const error = eify(e);
console.log("Error adding federation:", error.message);
// if we can't connect to the guardian, try to others,
// otherwise throw the error
if (
error.message ===
"Failed to connect to a federation." ||
error.message === "Invalid Arguments were given"
) {
console.error(
"Failed to connect to guardian, trying another one"
);
} else {
throw e;
}
}
}
setSuccess(
i18n.t("settings.manage_federations.federation_added_success")
);
Expand Down Expand Up @@ -275,11 +297,10 @@ function AddFederationForm(props: { refetch?: RefetchType }) {
</div>
</KeyValue>
</Show>
{/* FIXME: do something smarter than just taking first code */}
<Button
intent="blue"
onClick={() =>
onSelect(fed.invite_codes[0])
onSelect(fed.invite_codes)
}
loading={fed.invite_codes.includes(
loadingFederation()
Expand Down

0 comments on commit 0dbcdc5

Please sign in to comment.