Skip to content

Commit

Permalink
Replace deprecated js-stellar-wallets + update @stellar/stellar-sdk (#…
Browse files Browse the repository at this point in the history
…347)

* Replaced deprecated js-stellar-wallets package

* Replaced deprecated js-stellar-wallets package

* Replace stellar-sdk with @stellar/stellar-sdk
  • Loading branch information
quietbits authored May 15, 2024
1 parent 3ecdbe9 commit 6e1c8ee
Show file tree
Hide file tree
Showing 47 changed files with 517 additions and 539 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
},
"dependencies": {
"demo-wallet-shared": "^1.0.0",
"npm-run-all": "^4.1.5",
"stellar-sdk": "^11.0.1"
"npm-run-all": "^4.1.5"
}
}
3 changes: 1 addition & 2 deletions packages/demo-wallet-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@stellar/design-system": "^0.8.1",
"@stellar/frontend-helpers": "^2.1.4",
"@stellar/prettier-config": "^1.0.1",
"@stellar/wallet-sdk": "^0.11.0-beta.1",
"@stellar/stellar-sdk": "^12.0.0-rc.2",
"@svgr/webpack": "^8.1.0",
"@testing-library/jest-dom": "^6.0.0",
"@testing-library/react": "^14.0.0",
Expand All @@ -46,7 +46,6 @@
"react-router-dom": "^6.15.0",
"redux": "^4.2.1",
"sass": "^1.69.7",
"stellar-sdk": "^11.0.1",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"styled-components": "^6.0.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from "react";
import { Checkbox, Input } from "@stellar/design-system";
import { useDispatch } from "react-redux";
import { Memo, StrKey, Keypair } from "stellar-sdk";
import { Memo, StrKey, Keypair } from "@stellar/stellar-sdk";

import { updateCustodialAction, resetCustodialAction } from "ducks/custodial";
import "./styles.scss";
Expand Down
14 changes: 5 additions & 9 deletions packages/demo-wallet-client/src/components/SendPayment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
TextLink,
Modal,
} from "@stellar/design-system";
import { DataProvider } from "@stellar/wallet-sdk";
import { StrKey } from "stellar-sdk";
import { StrKey } from "@stellar/stellar-sdk";

import { ErrorMessage } from "components/ErrorMessage";
import { fetchAccountAction } from "ducks/account";
Expand All @@ -18,6 +17,7 @@ import { getNetworkConfig } from "demo-wallet-shared/build/helpers/getNetworkCon
import { useRedux } from "hooks/useRedux";
import { AppDispatch } from "config/store";
import { ActionStatus, Asset, AssetType } from "types/types";
import { fetchAccountDetails } from "helpers/fetchAccountDetails";

export const SendPayment = ({
asset,
Expand Down Expand Up @@ -75,13 +75,9 @@ export const SendPayment = ({
return;
}

const dataProvider = new DataProvider({
serverUrl: getNetworkConfig().url,
accountOrKey: destination,
networkPassphrase: getNetworkConfig().network,
});

setIsDestinationFunded(await dataProvider.isAccountFunded());
setIsDestinationFunded(
Boolean(await fetchAccountDetails(getNetworkConfig().url, destination)),
);
};

const handleSubmit = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { StrKey } from "stellar-sdk";
import { StrKey } from "@stellar/stellar-sdk";
import {
Button,
InfoBlock,
Input,
TextLink,
Modal,
} from "@stellar/design-system";
import { DataProvider } from "@stellar/wallet-sdk";
import { ErrorMessage } from "components/ErrorMessage";
import { CSS_MODAL_PARENT_ID } from "demo-wallet-shared/build/constants/settings";
import {
sep8ClearErrorAction,
sep8ReviseTransactionAction,
} from "ducks/sep8Send";
import { getNetworkConfig } from "demo-wallet-shared/build/helpers/getNetworkConfig";
import { fetchAccountDetails } from "helpers/fetchAccountDetails";
import { useRedux } from "hooks/useRedux";
import { AppDispatch } from "config/store";
import { ActionStatus, Sep8Step } from "types/types";
Expand Down Expand Up @@ -71,13 +71,9 @@ export const Sep8Approval = ({ onClose }: { onClose: () => void }) => {
return;
}

const dataProvider = new DataProvider({
serverUrl: getNetworkConfig().url,
accountOrKey: destination,
networkPassphrase: getNetworkConfig().network,
});

setIsDestinationFunded(await dataProvider.isAccountFunded());
setIsDestinationFunded(
Boolean(await fetchAccountDetails(getNetworkConfig().url, destination)),
);
};

const renderApprovePayment = () => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { Transaction, TransactionBuilder } from "stellar-sdk";
import { Transaction, TransactionBuilder } from "@stellar/stellar-sdk";
import { Button, Checkbox, Modal, Heading3 } from "@stellar/design-system";
import { ErrorMessage } from "components/ErrorMessage";
import { Json } from "components/Json";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { Keypair } from "stellar-sdk";
import { Keypair } from "@stellar/stellar-sdk";
import { fetchAccountAction } from "ducks/account";
import { fetchClaimableBalancesAction } from "ducks/claimableBalances";
import { updateSettingsAction } from "ducks/settings";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Networks } from "stellar-sdk";
import { Networks } from "@stellar/stellar-sdk";
import { StatusBar } from "@stellar/design-system";
import { getNetworkConfig } from "demo-wallet-shared/build/helpers/getNetworkConfig";

Expand Down
70 changes: 32 additions & 38 deletions packages/demo-wallet-client/src/ducks/account.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { DataProvider, Types } from "@stellar/wallet-sdk";
import { getCatchError } from "@stellar/frontend-helpers";
import { Keypair } from "stellar-sdk";
import { Keypair } from "@stellar/stellar-sdk";

import { RootState } from "config/store";
import { getErrorMessage } from "demo-wallet-shared/build/helpers/getErrorMessage";
import { getErrorString } from "demo-wallet-shared/build/helpers/getErrorString";
import { getNetworkConfig } from "demo-wallet-shared/build/helpers/getNetworkConfig";
import { log } from "demo-wallet-shared/build/helpers/log";
import { ActionStatus, RejectMessage, AccountInitialState } from "types/types";

interface UnfundedAccount extends Types.AccountDetails {
import { fetchAccountDetails } from "helpers/fetchAccountDetails";
import {
ActionStatus,
RejectMessage,
AccountInitialState,
AccountDetails,
} from "types/types";

interface UnfundedAccount extends AccountDetails {
id: string;
}

Expand All @@ -20,18 +24,14 @@ interface AccountKeyPair {
}

interface AccountActionBaseResponse {
data: Types.AccountDetails | UnfundedAccount;
data: AccountDetails | UnfundedAccount;
isUnfunded: boolean;
}

interface FetchAccountActionResponse extends AccountActionBaseResponse {
secretKey: string;
}

interface ResponseError extends Error {
isUnfunded?: boolean;
}

export const fetchAccountAction = createAsyncThunk<
FetchAccountActionResponse,
AccountKeyPair,
Expand All @@ -41,13 +41,7 @@ export const fetchAccountAction = createAsyncThunk<
async ({ publicKey, secretKey }, { rejectWithValue }) => {
const networkConfig = getNetworkConfig();

const dataProvider = new DataProvider({
serverUrl: networkConfig.url,
accountOrKey: publicKey,
networkPassphrase: networkConfig.network,
});

let stellarAccount: Types.AccountDetails | null = null;
let stellarAccount: AccountDetails | null = null;
let isUnfunded = false;

log.request({
Expand All @@ -56,28 +50,26 @@ export const fetchAccountAction = createAsyncThunk<
});

try {
stellarAccount = await dataProvider.fetchAccountDetails();
} catch (e) {
const error: ResponseError = getCatchError(e);
stellarAccount = await fetchAccountDetails(networkConfig.url, publicKey);

if (error.isUnfunded) {
if (!stellarAccount) {
log.instruction({ title: `Account is not funded` });

stellarAccount = {
id: publicKey,
} as UnfundedAccount;

isUnfunded = true;
} else {
const errorMessage = getErrorString(error);
log.error({
title: `Fetching account \`${publicKey}\` failed`,
body: errorMessage,
});
return rejectWithValue({
errorString: errorMessage,
});
}
} catch (e) {
const errorMessage = getErrorString(e);
log.error({
title: `Fetching account \`${publicKey}\` failed`,
body: errorMessage,
});
return rejectWithValue({
errorString: errorMessage,
});
}

log.response({
Expand Down Expand Up @@ -122,15 +114,17 @@ export const fundTestnetAccount = createAsyncThunk<

const networkConfig = getNetworkConfig();

const dataProvider = new DataProvider({
serverUrl: networkConfig.url,
accountOrKey: publicKey,
networkPassphrase: networkConfig.network,
});

try {
await fetch(`https://friendbot.stellar.org?addr=${publicKey}`);
const stellarAccount = await dataProvider.fetchAccountDetails();

const stellarAccount = await fetchAccountDetails(
networkConfig.url,
publicKey,
);

if (!stellarAccount) {
throw Error(`Error fetching account: ${publicKey}`);
}

log.response({
title: "The friendbot funded account",
Expand Down
8 changes: 5 additions & 3 deletions packages/demo-wallet-client/src/ducks/allAssets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { Types } from "@stellar/wallet-sdk";

import { RootState } from "config/store";
import { accountSelector } from "ducks/account";
Expand All @@ -11,7 +10,10 @@ import { log } from "demo-wallet-shared/build/helpers/log";
import { getAssetData } from "demo-wallet-shared/build/helpers/getAssetData";
import { searchKeyPairStringToArray } from "demo-wallet-shared/build/helpers/searchKeyPairStringToArray";
import { getNetworkConfig } from "demo-wallet-shared/build/helpers/getNetworkConfig";
import { XLM_NATIVE_ASSET } from "demo-wallet-shared/build/types/types";
import {
BalanceMap,
XLM_NATIVE_ASSET,
} from "demo-wallet-shared/build/types/types";
import {
ActionStatus,
RejectMessage,
Expand All @@ -22,7 +24,7 @@ import {

type IncludeAssetOverridesProps = {
assets: Asset[];
balances?: Types.BalanceMap;
balances?: BalanceMap;
assetCategory: AssetCategory;
assetOverrides: Asset[];
};
Expand Down
2 changes: 1 addition & 1 deletion packages/demo-wallet-client/src/ducks/claimableBalances.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { Horizon } from "stellar-sdk";
import { Horizon } from "@stellar/stellar-sdk";
import { RootState } from "config/store";
import { getErrorMessage } from "demo-wallet-shared/build/helpers/getErrorMessage";
import { getNetworkConfig } from "demo-wallet-shared/build/helpers/getNetworkConfig";
Expand Down
2 changes: 1 addition & 1 deletion packages/demo-wallet-client/src/ducks/sendPayment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { Horizon } from "stellar-sdk";
import { Horizon } from "@stellar/stellar-sdk";
import { RootState } from "config/store";
import { settingsSelector } from "ducks/settings";
import { getErrorString } from "demo-wallet-shared/build/helpers/getErrorString";
Expand Down
2 changes: 1 addition & 1 deletion packages/demo-wallet-client/src/ducks/sep8Send.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Horizon } from "stellar-sdk";
import { Horizon } from "@stellar/stellar-sdk";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { RootState } from "config/store";
import { settingsSelector } from "ducks/settings";
Expand Down
39 changes: 39 additions & 0 deletions packages/demo-wallet-client/src/helpers/fetchAccountDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { makeDisplayableBalances } from "./makeDisplayableBalances";
import { AccountDetails } from "types/types";

export const fetchAccountDetails = async (
networkUrl: string,
publicKey: string,
): Promise<AccountDetails | null> => {
try {
const url = `${networkUrl}/accounts/${publicKey}`;

const request = await fetch(url);
const accountSummary = await request.json();

if (!accountSummary.account_id) {
return null;
}

const balances = makeDisplayableBalances(accountSummary);
const sponsor = accountSummary.sponsor
? { sponsor: accountSummary.sponsor }
: {};

return {
...sponsor,
id: accountSummary.id,
subentryCount: accountSummary.subentry_count,
sponsoredCount: accountSummary.num_sponsored,
sponsoringCount: accountSummary.num_sponsoring,
inflationDestination: accountSummary.inflation_destination,
thresholds: accountSummary.thresholds,
signers: accountSummary.signers,
flags: accountSummary.flags,
sequenceNumber: accountSummary.sequence,
balances,
};
} catch (err) {
throw err;
}
};
Loading

0 comments on commit 6e1c8ee

Please sign in to comment.