@@ -85,11 +96,11 @@ export function ScheduleForm() {
parseUnits(amount, scheduleInfo.asset.decimals),
);
}}
- disabled={!connectedAccount}
+ disabled={formDisabled}
/>
-
+
@@ -99,7 +110,7 @@ export function ScheduleForm() {
onAddressChange={(address) => {
setScheduleInfo('recipient', address);
}}
- disabled={!connectedAccount}
+ disabled={formDisabled}
/>
@@ -109,7 +120,7 @@ export function ScheduleForm() {
onRecurrenceChange={(value: bigint) => {
setScheduleInfo('interval', BigInt(value));
}}
- disabled={!connectedAccount}
+ disabled={formDisabled}
/>
@@ -119,7 +130,7 @@ export function ScheduleForm() {
onValueChange={(value: string) => {
setScheduleInfo('occurrences', BigInt(value));
}}
- disabled={!connectedAccount}
+ disabled={formDisabled}
/>
diff --git a/front/src/components/ScheduleTable.tsx b/front/src/components/ScheduleTable.tsx
index 023155e..647d370 100644
--- a/front/src/components/ScheduleTable.tsx
+++ b/front/src/components/ScheduleTable.tsx
@@ -4,10 +4,10 @@ import { truncateAddress } from '@/utils/address';
import { formatAmount, toast } from '@massalabs/react-ui-kit';
import CheckBox from './CheckBox';
import useSchedule from '@/hooks/useSchedule';
-import { MasToken, supportedTokens } from '../const/assets';
+import { MasToken } from '../const/assets';
import ScheduleHistory from '@/components/ScheduleHistory';
-import { getTokenInfo } from '@/utils/assets';
import { getRecurrenceFromPeriods } from './Recurrence';
+import { useTokenStore } from '@/store/token';
interface ScheduleTableProps {
schedules: Schedule[];
@@ -64,9 +64,9 @@ const TableRow: React.FC = ({
isSelected,
onCheckboxChange,
}) => {
+ const { tokens } = useTokenStore();
const asset =
- supportedTokens.find((token) => token.address === schedule.tokenAddress) ||
- MasToken;
+ tokens.find((token) => token.address === schedule.tokenAddress) || MasToken;
const formattedAmount = formatAmount(schedule.amount, asset.decimals);
const isMas = schedule.tokenAddress === '';
return (
@@ -95,7 +95,7 @@ const TableRow: React.FC = ({
)}
diff --git a/front/src/components/SelectAsset.tsx b/front/src/components/SelectAsset.tsx
index c1481b8..446fb98 100644
--- a/front/src/components/SelectAsset.tsx
+++ b/front/src/components/SelectAsset.tsx
@@ -1,9 +1,9 @@
import { useState } from 'react';
-import { supportedTokens, MasToken } from '../const/assets';
import { Asset } from '@massalabs/react-ui-kit/src/lib/token/models/AssetModel';
import { AssetSelector } from '@massalabs/react-ui-kit/src/lib/token/AssetSelector';
import useSchedule from '../hooks/useSchedule';
import { InputLabel } from './InputLabel';
+import { useTokenStore } from '@/store/token';
export default function SelectAsset({
isVesting,
@@ -12,7 +12,8 @@ export default function SelectAsset({
isVesting: boolean;
disabled: boolean;
}): JSX.Element {
- const tokens = isVesting ? [MasToken] : supportedTokens;
+ const { tokens: supportedTokens, mas } = useTokenStore.getState();
+ const tokens = isVesting ? [mas] : supportedTokens;
const [selectedAsset, setSelectedAsset] = useState(tokens[0]);
const { setScheduleInfo } = useSchedule();
diff --git a/front/src/const/assets.ts b/front/src/const/assets.ts
index 1d4ae11..597013e 100644
--- a/front/src/const/assets.ts
+++ b/front/src/const/assets.ts
@@ -1,48 +1,45 @@
-import { Asset } from '@massalabs/react-ui-kit/src/lib/token/models/AssetModel';
-
-// buildnet
-export const supportedTokens: Asset[] = [
- {
- name: 'tDAI.s',
- address: 'AS12LpYyAjYRJfYhyu7fkrS224gMdvFHVEeVWoeHZzMdhis7UZ3Eb',
- symbol: 'tDAI.s',
- decimals: 18,
- balance: 0n,
- allowance: 0n,
- chainId: 0,
- },
- {
- name: 'Wrapped MASSA',
- address: 'AS12FW5Rs5YN2zdpEnqwj4iHUUPt9R4Eqjq2qtpJFNKW3mn33RuLU',
- symbol: 'WMAS',
- decimals: 9,
- balance: 0n,
- allowance: 0n,
- chainId: 0,
- },
-];
-
-// mainnet
-// export const supportedTokens: Asset[] = [
-// {
-// name: 'DAI.e',
-// address: 'AS1ZGF1upwp9kPRvDKLxFAKRebgg7b3RWDnhgV7VvdZkZsUL7Nuv',
-// symbol: 'DAI.e',
-// decimals: 18,
-// balance: 0n,
-// allowance: 0n,
-// chainId: 0,
-// },
-// {
-// name: 'Wrapped MASSA',
-// address: 'AS12U4TZfNK7qoLyEERBBRDMu8nm5MKoRzPXDXans4v9wdATZedz9',
-// symbol: 'WMAS',
-// decimals: 9,
-// balance: 0n,
-// allowance: 0n,
-// chainId: 0,
-// },
-// ];
+export const supportedTokens = {
+ buildnet: [
+ {
+ name: 'tDAI.s',
+ address: 'AS12LpYyAjYRJfYhyu7fkrS224gMdvFHVEeVWoeHZzMdhis7UZ3Eb',
+ symbol: 'tDAI.s',
+ decimals: 18,
+ balance: 0n,
+ allowance: 0n,
+ chainId: 0,
+ },
+ {
+ name: 'Wrapped MASSA',
+ address: 'AS12FW5Rs5YN2zdpEnqwj4iHUUPt9R4Eqjq2qtpJFNKW3mn33RuLU',
+ symbol: 'WMAS',
+ decimals: 9,
+ balance: 0n,
+ allowance: 0n,
+ chainId: 0,
+ },
+ ],
+ mainnet: [
+ {
+ name: 'DAI.e',
+ address: 'AS1ZGF1upwp9kPRvDKLxFAKRebgg7b3RWDnhgV7VvdZkZsUL7Nuv',
+ symbol: 'DAI.e',
+ decimals: 18,
+ balance: 0n,
+ allowance: 0n,
+ chainId: 0,
+ },
+ {
+ name: 'Wrapped MASSA',
+ address: 'AS12U4TZfNK7qoLyEERBBRDMu8nm5MKoRzPXDXans4v9wdATZedz9',
+ symbol: 'WMAS',
+ decimals: 9,
+ balance: 0n,
+ allowance: 0n,
+ chainId: 0,
+ },
+ ],
+};
export const MasToken = {
name: 'MAS',
diff --git a/front/src/hooks/useAccountAsync.tsx b/front/src/hooks/useAccountAsync.tsx
index 8bd1a6f..9d5321d 100644
--- a/front/src/hooks/useAccountAsync.tsx
+++ b/front/src/hooks/useAccountAsync.tsx
@@ -9,7 +9,12 @@ type SavedAccount = {
};
const useAccountSync = () => {
- const { connectedAccount, setConnectedAccount } = useAccountStore();
+ const {
+ connectedAccount,
+ setConnectedAccount,
+ setCurrentWallet,
+ setWallets,
+ } = useAccountStore();
const [savedAccount, setSavedAccount] = useLocalStorage(
'saved-account',
@@ -36,17 +41,25 @@ const useAccountSync = () => {
if (connectedAccount) return;
getWallets().then((wallets) => {
+ setWallets(wallets);
for (const wallet of wallets) {
wallet.accounts().then((accounts) => {
const acc = accounts.find((a) => a.address === address);
if (acc) {
setConnectedAccount(acc);
+ setCurrentWallet(wallet);
return;
}
});
}
});
- }, [savedAccount, connectedAccount, setConnectedAccount]);
+ }, [
+ savedAccount,
+ connectedAccount,
+ setConnectedAccount,
+ setCurrentWallet,
+ setWallets,
+ ]);
};
export default useAccountSync;
diff --git a/front/src/hooks/useInit.ts b/front/src/hooks/useInit.ts
index ce8be4d..708915d 100644
--- a/front/src/hooks/useInit.ts
+++ b/front/src/hooks/useInit.ts
@@ -1,7 +1,8 @@
import { useEffect } from 'react';
-import { useAccountStore } from '@massalabs/react-ui-kit';
+import { toast, useAccountStore } from '@massalabs/react-ui-kit';
import { initApp } from '@/store/store';
import { useSchedulerStore } from '@/store/scheduler';
+import { useNetworkStore } from '@/store/network';
export const useInit = () => {
const { connectedAccount } = useAccountStore();
@@ -20,4 +21,14 @@ export const useInit = () => {
}
};
}, [eventPollerStop]);
+
+ const { network: n } = useAccountStore();
+ const { network } = useNetworkStore();
+
+ useEffect(() => {
+ if (!n) return;
+ if (n !== network) {
+ toast.error(`Please switch to ${network} network`);
+ }
+ }, [network, n]);
};
diff --git a/front/src/store/scheduler.ts b/front/src/store/scheduler.ts
index 8237234..48feec0 100644
--- a/front/src/store/scheduler.ts
+++ b/front/src/store/scheduler.ts
@@ -3,7 +3,7 @@ import { Args, Slot } from '@massalabs/massa-web3';
import { Asset } from '@massalabs/react-ui-kit/src/lib/token/models/AssetModel';
import { Schedule } from '@/serializable/Schedule';
-import { supportedTokens } from '@/const/assets';
+import { MasToken } from '@/const/assets';
import { useAccountStore } from '@massalabs/react-ui-kit';
export type ScheduleInfo = {
@@ -44,7 +44,7 @@ const defaultScheduleInfo: ScheduleInfo = {
interval: 5400n,
recipient: '',
spender: '',
- asset: supportedTokens[0],
+ asset: MasToken,
occurrences: 4n,
tolerance: 3n,
};
diff --git a/front/src/store/store.ts b/front/src/store/store.ts
index e3e106f..d9420d1 100644
--- a/front/src/store/store.ts
+++ b/front/src/store/store.ts
@@ -6,26 +6,32 @@ import { useAccountStore, formatAmount, toast } from '@massalabs/react-ui-kit';
import { useSchedulerStore } from './scheduler';
import { useTokenStore } from './token';
import { getTokenInfo } from '@/utils/assets';
+import { useNetworkStore } from './network';
export async function initApp() {
const { connectedAccount } = useAccountStore.getState();
if (!connectedAccount) return;
- await Promise.all([
- initTokens(),
- initSchedules(connectedAccount),
- initPollEvent(connectedAccount),
- ]);
+ await initTokens();
+ await initSchedules(connectedAccount);
+ await initPollEvent(connectedAccount);
}
async function initTokens() {
- const { refreshBalances } = useTokenStore.getState();
- await refreshBalances();
+ const { init } = useTokenStore.getState();
+ await init();
}
async function initSchedules(connectedAccount: Provider) {
- const { setSchedulerAddress, getBySpender } = useSchedulerStore.getState();
- setSchedulerAddress(schedulerAddress);
+ const { tokens } = useTokenStore.getState();
+ const { setSchedulerAddress, getBySpender, scheduleInfo, setScheduleInfo } =
+ useSchedulerStore.getState();
+ const { network } = useNetworkStore.getState();
+ setSchedulerAddress(schedulerAddress[network]);
+
+ scheduleInfo.asset = tokens[0];
+ setScheduleInfo('asset', tokens[0]);
+
await getBySpender(connectedAccount.address);
}
@@ -57,6 +63,7 @@ async function initPollEvent(connectedAccount: Provider) {
function handleTransferEvents(data: SCEvent[], schedules: Schedule[]) {
const { refreshBalances } = useTokenStore.getState();
+ const { tokens } = useTokenStore.getState();
for (const event of data) {
const match = event.data?.match(/Transfer:([^]+)/);
@@ -67,7 +74,7 @@ function handleTransferEvents(data: SCEvent[], schedules: Schedule[]) {
const schedule = schedules.find((s) => s.id === BigInt(scheduleId));
if (schedule) {
- const { decimals, symbol } = getTokenInfo(schedule.tokenAddress);
+ const { decimals, symbol } = getTokenInfo(schedule.tokenAddress, tokens);
const formattedAmount = formatAmount(schedule.amount, decimals).preview;
toast.success(
diff --git a/front/src/store/token.ts b/front/src/store/token.ts
index eef172e..ac1e052 100644
--- a/front/src/store/token.ts
+++ b/front/src/store/token.ts
@@ -6,18 +6,27 @@ import { MRC20 } from '@massalabs/massa-web3';
import { MasToken, supportedTokens } from '@/const/assets';
import { Asset } from '@massalabs/react-ui-kit/src/lib/token/models/AssetModel';
import { useSchedulerStore } from './scheduler';
+import { useNetworkStore } from './network';
export interface TokenStoreState {
selectedToken?: Asset;
tokens: Asset[];
mas: Asset;
+ init: () => void;
refreshBalances: () => void;
}
export const useTokenStore = create((set, get) => ({
- tokens: supportedTokens,
+ tokens: [],
mas: MasToken,
+ init: async () => {
+ const { network } = useNetworkStore.getState();
+ set({ tokens: supportedTokens[network] });
+ const { refreshBalances } = get();
+ refreshBalances();
+ },
+
refreshBalances: async () => {
const { connectedAccount } = useAccountStore.getState();
@@ -31,14 +40,14 @@ export const useTokenStore = create((set, get) => ({
return;
}
- const { tokens: supportedTokens, mas } = get();
+ const { tokens: sTokens, mas } = get();
- if (!supportedTokens.length) {
+ if (!sTokens.length) {
return;
}
const tokens = await Promise.all(
- supportedTokens.map(async (token) => {
+ sTokens.map(async (token) => {
const mrc20 = new MRC20(connectedAccount, token.address);
const [allowance, balance] = await Promise.all([
mrc20.allowance(connectedAccount.address, schedulerAddress),
diff --git a/front/src/utils/assets.ts b/front/src/utils/assets.ts
index 11d98f2..e59e78c 100644
--- a/front/src/utils/assets.ts
+++ b/front/src/utils/assets.ts
@@ -1,9 +1,10 @@
-import { MasToken, supportedTokens } from '@/const/assets';
+import { MasToken } from '@/const/assets';
+import { Asset } from '@massalabs/react-ui-kit/src/lib/token/models/AssetModel';
-export function getTokenInfo(tokenAddress: string | null) {
+export function getTokenInfo(tokenAddress: string | null, tokens: Asset[]) {
if (!tokenAddress) return MasToken;
- const token = supportedTokens.find((t) => t.address === tokenAddress);
+ const token = tokens.find((t) => t.address === tokenAddress);
if (!token) {
return {