Skip to content

Commit

Permalink
feat: improve sdks and tests (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio committed Jan 11, 2024
1 parent 7533fae commit 1f7767f
Show file tree
Hide file tree
Showing 58 changed files with 489 additions and 265 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-terms-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@fuel-wallet/sdk': minor
---

feat: add hooks `useAddAssets`, `useAddNetwork`, `useAssets` and `useNetwork`.
feat: improve hooks `useWallet` `useConnect` and `useAccounts`.
feat: remove `FuelConnectorProvider` and consolidate into `FuelProvider`.
4 changes: 2 additions & 2 deletions examples/cra-dapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import {
useAccounts,
useDisconnect,
useFuelConnect,
useConnectUI,
useIsConnected,
} from '@fuel-wallet/react';
import './App.css';

function App() {
const { connect, error, isError, theme, setTheme, isConnecting } =
useFuelConnect();
useConnectUI();
const { disconnect } = useDisconnect();
const { isConnected } = useIsConnected();
const { accounts } = useAccounts();
Expand Down
12 changes: 7 additions & 5 deletions examples/cra-dapp/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FuelProvider, FuelConnectProvider } from '@fuel-wallet/react';
import { FuelProvider } from '@fuel-wallet/react';
import React from 'react';
import ReactDOM from 'react-dom/client';

Expand All @@ -11,10 +11,12 @@ const root = ReactDOM.createRoot(
);
root.render(
<React.StrictMode>
<FuelProvider>
<FuelConnectProvider>
<App />
</FuelConnectProvider>
<FuelProvider
fuelConfig={{
devMode: true,
}}
>
<App />
</FuelProvider>
</React.StrictMode>
);
Expand Down
15 changes: 5 additions & 10 deletions packages/app/connect/connect.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { cssObj } from '@fuel-ui/css';
import { Box, Button, Text } from '@fuel-ui/react';
import {
FuelProvider,
FuelConnectProvider,
useAccount,
useFuelConnect,
} from '@fuel-wallet/react';
import { FuelProvider, useAccount, useConnectUI } from '@fuel-wallet/react';
import { useDarkMode } from 'storybook-dark-mode';

export default {
component: FuelConnectProvider,
component: FuelProvider,
title: 'Connect',
parameters: {
layout: 'fullscreen',
Expand All @@ -18,7 +13,7 @@ export default {

const App = () => {
const { account } = useAccount();
const { connect, isConnecting } = useFuelConnect();
const { connect, isConnecting } = useConnectUI();

return (
<>
Expand All @@ -35,9 +30,9 @@ export const Usage = () => {
return (
<Box css={styles.box}>
<FuelProvider>
<FuelConnectProvider theme={isDark ? 'dark' : 'light'}>
<FuelProvider theme={isDark ? 'dark' : 'light'}>
<App />
</FuelConnectProvider>
</FuelProvider>
</FuelProvider>
</Box>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/systems/Send/machines/sendMachine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
import { BN, Provider, TransactionRequest } from 'fuels';
import { BN, Provider, TransactionRequest, bn } from 'fuels';
import { assign, createMachine, InterpreterFrom, StateFrom } from 'xstate';
import { AccountService } from '~/systems/Account';
import { FetchMachine, assignError } from '~/systems/Core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.describe('Forward Half ETH and Mint Custom Asset', () => {
}));
});

test('e2e foreward half eth and mint custom asset', async ({ page }) => {
test('e2e forward half eth and mint custom asset', async ({ page }) => {
await connect(page, fuelWalletTestHelper);

const depositAmount = '1.000';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const AssetConfigurationCard = () => {
subId: '',
decimals: '',
});
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const DepositAndMintMultiCalls = () => {
const [forwardAmount, setForwardAmount] = useState<string>('');
const [mintAmount, setMintAmount] = useState<string>('');
const { account } = useAccount();
const wallet = useWallet({ address: account });
const wallet = useWallet(account);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { depositHalf } from '../contract_interactions';

export const DepositHalfEthCard = () => {
const [amount, setAmount] = useState<string>('');
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { calculateAssetId } from '../utils';

export const ForwardCustomAssetCard = () => {
const [amount, setAmount] = useState<string>('');
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

const assetId = calculateAssetId(MAIN_CONTRACT_ID, BaseAssetId);

Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-contract-tests/src/components/ForwardEthCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { deposit } from '../contract_interactions';

export const ForwardEthCard = () => {
const [amount, setAmount] = useState<string>('');
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { depositHalfAndExternalMint } from '../contract_interactions';
export const ForwardHalfAndExternalMintCard = () => {
const [forwardAmount, setForwardAmount] = useState<string>('');
const [mintAmount, setMintAmount] = useState<string>('');
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { depositHalfAndMint } from '../contract_interactions';
export const ForwardHalfAndMintCard = () => {
const [forwardAmount, setForwardAmount] = useState<string>('');
const [mintAmount, setMintAmount] = useState<string>('');
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { calculateAssetId } from '../utils';

export const ForwardHalfCustomAssetCard = () => {
const [amount, setAmount] = useState<string>('');
const account = useAccount();
const wallet = useWallet({ address: account.account });
const { account } = useAccount();
const wallet = useWallet(account);

const assetId = calculateAssetId(MAIN_CONTRACT_ID, BaseAssetId);

Expand Down
11 changes: 3 additions & 8 deletions packages/e2e-contract-tests/src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { useConnectors, useFuelConnect } from '@fuel-wallet/react';
import { useConnectUI } from '@fuel-wallet/react';

export const Header = () => {
const { connect } = useFuelConnect();
const { connectors } = useConnectors();
const { connect } = useConnectUI();

return (
<button disabled={!connectors.length} onClick={connect}>
Connect
</button>
);
return <button onClick={connect}>Connect</button>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mint } from '../contract_interactions';
export const MintAssetCard = () => {
const [amount, setAmount] = useState<string>('');
const { account } = useAccount();
const { wallet } = useWallet({ address: account });
const { wallet } = useWallet(account);

return (
<div>
Expand Down
8 changes: 2 additions & 6 deletions packages/e2e-contract-tests/src/components/Providers.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { FuelProvider, FuelConnectProvider } from '@fuel-wallet/react';
import { FuelProvider } from '@fuel-wallet/react';
import type { ReactNode } from 'react';

type ProviderProps = {
children: ReactNode;
};

export const Providers = ({ children }: ProviderProps) => {
return (
<FuelProvider>
<FuelConnectProvider>{children}</FuelConnectProvider>
</FuelProvider>
);
return <FuelProvider>{children}</FuelProvider>;
};
3 changes: 3 additions & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
"react": "^18.2.0"
},
"dependencies": {
"@fuel-wallet/sdk": "workspace:*",
"@radix-ui/react-dialog": "^1.0.5",
"@tanstack/react-query": "^4.36.1",
"styled-components": "^6.1.1",
"compare-versions": "^6.1.0",
"events": "^3.3.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export * from './useAccount';
export * from './useAccounts';
export * from './useBalance';
export * from './useChain';
export * from './useAssets';
export * from './useAddNetwork';
export * from './useNetwork';
export * from './useAddAssets';
export * from './useConnect';
export * from './useConnectors';
export * from './useDisconnect';
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/hooks/useAccount.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query';

import { useFuel } from '../components';
import { useFuel } from '../providers';
import { QUERY_KEYS } from '../utils';

export const useAccount = () => {
Expand All @@ -17,12 +17,12 @@ export const useAccount = () => {
}
},
{
enabled: !!fuel,
initialData: null,
}
);

return {
account: data || undefined,
account: data,
...queryProps,
};
};
12 changes: 6 additions & 6 deletions packages/react/src/hooks/useAccounts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query';

import { useFuel } from '../components';
import { useFuel } from '../providers';
import { QUERY_KEYS } from '../utils';

export const useAccounts = () => {
Expand All @@ -10,19 +10,19 @@ export const useAccounts = () => {
[QUERY_KEYS.accounts],
async () => {
try {
const accounts = await fuel?.accounts();
return accounts || null;
const accounts = await fuel.accounts();
return accounts || [];
} catch (error: unknown) {
return null;
return [];
}
},
{
enabled: !!fuel,
initialData: [],
}
);

return {
accounts: data || undefined,
accounts: data,
...queryProps,
};
};
25 changes: 25 additions & 0 deletions packages/react/src/hooks/useAddAssets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Asset } from '@fuel-wallet/sdk';
import { useMutation } from '@tanstack/react-query';

import { useFuel } from '../providers';
import { MUTATION_KEYS } from '../utils';

export const useAddAssets = () => {
const { fuel } = useFuel();

const { mutate, mutateAsync, ...queryProps } = useMutation(
[MUTATION_KEYS.addAssets],
async (assets: Asset | Asset[]) => {
if (Array.isArray(assets)) {
return fuel.addAssets(assets);
}
return fuel.addAsset(assets);
}
);

return {
addAssets: (assets: Asset | Asset[]) => mutate(assets),
addAssetsAsync: (assets: Asset | Asset[]) => mutateAsync(assets),
...queryProps,
};
};
21 changes: 21 additions & 0 deletions packages/react/src/hooks/useAddNetwork.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useMutation } from '@tanstack/react-query';

import { useFuel } from '../providers';
import { MUTATION_KEYS } from '../utils';

export const useAddNetwork = () => {
const { fuel } = useFuel();

const { mutate, mutateAsync, ...queryProps } = useMutation(
[MUTATION_KEYS.addAssets],
async (networkUrl: string) => {
return fuel.addNetwork(networkUrl);
}
);

return {
addNetwork: (networkUrl: string) => mutate(networkUrl),
addNetworkAsync: (networkUrl: string) => mutateAsync(networkUrl),
...queryProps,
};
};
29 changes: 29 additions & 0 deletions packages/react/src/hooks/useAssets.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Asset } from '@fuel-wallet/sdk';
import { useQuery } from '@tanstack/react-query';

import { useFuel } from '../providers';
import { QUERY_KEYS } from '../utils';

export const useAssets = () => {
const { fuel } = useFuel();

const { data, ...queryProps } = useQuery(
[QUERY_KEYS.assets],
async () => {
try {
const assets = (await fuel.assets()) as Array<Asset>;
return assets || [];
} catch (error: unknown) {
return [];
}
},
{
initialData: [],
}
);

return {
assets: data,
...queryProps,
};
};
Loading

0 comments on commit 1f7767f

Please sign in to comment.