Skip to content

Commit

Permalink
no subscriptions for balances. only fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Apr 16, 2024
1 parent fa73fe3 commit db019b7
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/components/TransferForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const TransferForm: FC<Props> = ({ onSubmit, loading }) => {
const encointerApi = createApiInstanceForNode('Encointer')
const kusamaApi = createApiInstanceForNode('Kusama')

const [encointerBalance, setEncointerBalance] = useState<string | undefined>(undefined);
const [kusamaBalance, setKusamaBalance] = useState<string | undefined>(undefined);
const [encointerBalance, setEncointerBalance] = useState<string | undefined>('loading...');
const [kusamaBalance, setKusamaBalance] = useState<string | undefined>('loading...');

formatBalance.setDefaults({
decimals: 12,
Expand All @@ -40,34 +40,28 @@ const TransferForm: FC<Props> = ({ onSubmit, loading }) => {

useEffect(() => {
encointerApi.then((api) => {
let unsubscribe: AccountInfo | undefined;

// If the user has selected an address, create a new subscription
selectedAccount &&
api.query.system
.account<AccountInfo>(selectedAccount.address, (balance: AccountInfo) =>
.account<AccountInfo>(selectedAccount.address).then((balance: AccountInfo) =>
{
setEncointerBalance(formatBalance(balance.data.free))
form.values.amount = Math.max(0, Number(balance.data.free) * Math.pow(10, -12) - 0.001)
})
.then(unsub => (unsubscribe = unsub))
.catch(console.error)
return () => unsubscribe
})
}, [selectedAccount, encointerApi]);

useEffect(() => {
if (selectedAccount) form.values.address = selectedAccount.address;
let unsubscribe: AccountInfo | undefined;
kusamaApi.then((api) => {
// If the user has selected an address, create a new subscription
selectedAccount &&
api.query.system
.account<AccountInfo>(selectedAccount.address, (balance: AccountInfo) =>
.account<AccountInfo>(selectedAccount.address).then((balance: AccountInfo) =>
setKusamaBalance(formatBalance(balance.data.free))
)
.then(unsub => (unsubscribe = unsub))
.catch(console.error)
return () => unsubscribe
})
}, [selectedAccount, kusamaApi]);

Expand Down

0 comments on commit db019b7

Please sign in to comment.