Skip to content

Commit

Permalink
display KSM balances and set max reasonable amount to send
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Apr 16, 2024
1 parent eb21065 commit d230610
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
61 changes: 57 additions & 4 deletions src/components/TransferForm.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useForm } from '@mantine/form';
import { isValidWalletAddress } from '../utils';
import {FC, useEffect} from 'react';
import { Button, Stack, TextInput } from '@mantine/core';
import { TNodeWithRelayChains } from '@paraspell/sdk';
import {FC, useEffect, useState} from 'react';
import {Button, Grid, Stack, TextInput} from '@mantine/core';
import {createApiInstanceForNode, TNodeWithRelayChains} from '@paraspell/sdk';
import {useWallet} from "../providers/WalletProvider.tsx";
import {formatBalance} from "@polkadot/util";

export type FormValues = {
from: TNodeWithRelayChains;
Expand All @@ -21,11 +22,55 @@ type Props = {

const TransferForm: FC<Props> = ({ onSubmit, loading }) => {
const { selectedAccount } = useWallet();
const encointerApi = createApiInstanceForNode('Encointer')
const kusamaApi = createApiInstanceForNode('Kusama')

const [encointerBalance, setEncointerBalance] = useState();
const [kusamaBalance, setKusamaBalance] = useState();

formatBalance.setDefaults({
decimals: 12,
unit: 'KSM',
});

useEffect(() => {
if (selectedAccount) form.values.address = selectedAccount.address;
}, [selectedAccount]);

useEffect(() => {
let unsubscribe

Check failure on line 41 in src/components/TransferForm.tsx

View workflow job for this annotation

GitHub Actions / ci

Variable 'unsubscribe' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 41 in src/components/TransferForm.tsx

View workflow job for this annotation

GitHub Actions / build-and-deploy

Variable 'unsubscribe' implicitly has type 'any' in some locations where its type cannot be determined.
encointerApi.then((api) => {
// If the user has selected an address, create a new subscription
selectedAccount &&
api.query.system
.account(selectedAccount.address, balance =>

Check failure on line 46 in src/components/TransferForm.tsx

View workflow job for this annotation

GitHub Actions / ci

Parameter 'balance' implicitly has an 'any' type.

Check failure on line 46 in src/components/TransferForm.tsx

View workflow job for this annotation

GitHub Actions / build-and-deploy

Parameter 'balance' implicitly has an 'any' type.
{
setEncointerBalance(formatBalance(balance.data.free))
form.values.amount = Math.max(0, balance.data.free * Math.pow(10, -12) - 0.001)
}
)
.then(unsub => (unsubscribe = unsub))
.catch(console.error)
return () => unsubscribe && unsubscribe()
})
}, [selectedAccount, encointerApi]);

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

const form = useForm<FormValues>({
initialValues: {
from: 'Encointer',
Expand All @@ -44,7 +89,15 @@ const TransferForm: FC<Props> = ({ onSubmit, loading }) => {
return (
<form onSubmit={form.onSubmit(onSubmit)}>
<Stack>
<TextInput label="Amount KSM to send to same account on relaychain" placeholder="0.1" required {...form.getInputProps('amount')} />
<p>transferable KSM balance</p>
<Grid>
<Grid.Col span={6}>Encointer</Grid.Col>
<Grid.Col span={6}>{encointerBalance}</Grid.Col>
<Grid.Col span={6}>Kusama</Grid.Col>
<Grid.Col span={6}>{kusamaBalance}</Grid.Col>
</Grid>
<TextInput label="Amount KSM to send to same account on relaychain" placeholder="0.1"
required {...form.getInputProps('amount')} />
<Button type="submit" loading={loading}>
Submit transaction
</Button>
Expand Down
3 changes: 2 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import WalletProvider from './providers/WalletProvider';
import WalletProvider, {useWallet} from './providers/WalletProvider';
import {createApiInstanceForNode} from "@paraspell/sdk";

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
Expand Down

0 comments on commit d230610

Please sign in to comment.