Skip to content

Commit

Permalink
fix: improve input checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Dec 5, 2024
1 parent 7e372ec commit b44d34d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 260 deletions.
2 changes: 1 addition & 1 deletion packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@polkadot/api-derive": "^12.1.1",
"@polkadot/apps-config": "^0.141.1",
"@polkadot/extension-inject": "^0.48.2",
"@polkagate/extension-dapp": "../../../polkadot-js-extension/packages/extension-dapp/build",
"@polkagate/extension-dapp": "^0.48.2",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down
1 change: 1 addition & 0 deletions packages/dapp/setDappPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const packageJson = require('./package.json');

const isProduction = process.env.NODE_ENV === 'production';
console.log(`set path says isProduction is ${isProduction}`)
packageJson.dependencies['@polkagate/extension-dapp'] = isProduction
? '^0.48.2'
: '../../../polkadot-js-extension/packages/extension-dapp/build';
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/polkagate/snap.git"
},
"source": {
"shasum": "DQnCLJLh1R+vr3YG+b2R0WNQya1/ibMwh5nFDBlOu0s=",
"shasum": "DDrta0szZSiMMKzpnkStW2D//DKsT5I8JjgyC0CDi5Y=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
1 change: 1 addition & 0 deletions packages/snap/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const NOT_LISTED_CHAINS = [
'0xcdedc8eadbfa209d3f207bba541e57c3c58a667b05a2e1d1e86353c9000758da', // Integritee
'0xfe1b4c55fd4d668101126434206571a7838a8b6b93a6d1b95d607e78e6c53763', // Vara
'0x4a587bf17a404e3572747add7aab7bbe56e805a5479c6c436f07f36fcc8d3ae1', // Frequency
'0xb3db41421702df9a7fcac62b53ffeac85f7853cc4e689e0b93aeb3db18c09d82', // Centrifuge
]

export const PRICE_VALIDITY_PERIOD = 2 * 60 * 1000;
2 changes: 1 addition & 1 deletion packages/snap/src/listeners/onUserInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const onUserInput: OnUserInputHandler = async ({ id, event, context }) =>
const clearAddress = event.name === 'clear';
const displayClearIcon = !clearAddress && sendForm && Boolean(sendForm.to) && sendForm.to !== '';

await send(id, sendForm?.tokenSelector, sendForm?.amount, sendForm?.to, formErrors, displayClearIcon, clearAddress);
await send(id, sendForm?.amount, formErrors, sendForm?.to, sendForm?.tokenSelector, displayClearIcon, clearAddress);
break;

case 'sendReview':
Expand Down
10 changes: 8 additions & 2 deletions packages/snap/src/ui/send/SendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ export const SendForm: SnapComponent<SendFormProps> = ({
<Box>
<Icon name='send-2' color='muted' />
</Box>
<Input name='amount' type='number' placeholder='Enter amount to send' />
<Input
name='amount'
type='number'
placeholder='Enter amount to send'
/>
<Box direction='horizontal' center>
<Text color='alternative'>{_selectedToken.token}</Text>
<Text color='alternative'>
{_selectedToken.token}
</Text>
</Box>
</Field>
<Field label='To account' error={formErrors?.to}>
Expand Down
20 changes: 12 additions & 8 deletions packages/snap/src/ui/send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ import { FlowHeader } from "./FlowHeader";

export async function send(
id: string,
selectedTokenGenesisHash: string | undefined,
amount: string | undefined,
recipient: string | undefined,
formErrors: SendFormErrors,
recipient: string | undefined,
selectedTokenGenesisHash: string | undefined,
displayClearIcon?: boolean,
clearAddress?: boolean
clearAddress?: boolean,
) {

const { address, balancesAll, logos, pricesInUsd } = await handleBalancesAll();
const { address, balancesAll, logos, pricesInUsd } = await handleBalancesAll(true);
const nonZeroBalances = balancesAll.filter(({ total }) => !total.isZero());
const tokenGenesis = selectedTokenGenesisHash?.split(',');
const maybeSelectedToken = tokenGenesis && balancesAll.find(({ token, genesisHash }) => tokenGenesis[0] === token && tokenGenesis[1] === genesisHash);
const selectedToken = maybeSelectedToken || nonZeroBalances[0];
const noError = !formErrors || Object.keys(formErrors).length === 0;
const formIsFilledOut = amount && recipient;
const fee: Balance | undefined = tokenGenesis && formIsFilledOut && noError ? await getTransferFee(address, amount, tokenGenesis[1] as HexString, recipient) : undefined;
const formIsFilledOut = amount && Number(amount)!==0 && recipient;

const fee: Balance | undefined = tokenGenesis && formIsFilledOut && noError && !clearAddress
? await getTransferFee(address, amount, tokenGenesis[1] as HexString, recipient)
: undefined;

const total: number = (fee ? Number(amountToHuman(fee, selectedToken.decimal)) : 0) + (amount ? Number(amount) : 0);
const selectedTokenPrice = pricesInUsd.find((price) => price.genesisHash === selectedToken.genesisHash)?.price?.value || 0;

Expand All @@ -37,8 +41,8 @@ export async function send(
id,
ui: ui(fee, nonZeroBalances, logos, pricesInUsd, selectedToken, formErrors, displayClearIcon, clearAddress, selectedTokenPrice, total),
context: {
transferable: selectedToken.transferable.toString(),
decimal: selectedToken.decimal
decimal: selectedToken.decimal,
transferable: selectedToken.transferable.toString()
}
},
});
Expand Down
15 changes: 7 additions & 8 deletions packages/snap/src/util/handleBalancesAll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DEFAULT_CHAIN_NAME, NOT_LISTED_CHAINS, PRICE_VALIDITY_PERIOD } from '..
import { updateTokenPrices } from './getCurrentChainTokenPrice';
import { isHexToBn } from '../utils';

export const handleBalancesAll = async () => {
export const handleBalancesAll = async (savedOnly?: boolean) => {
const options = getChainOptions()
const selectedOptions = options.filter(({ value }) => !NOT_LISTED_CHAINS.includes(value))//.slice(0, 3);

Expand All @@ -21,13 +21,7 @@ export const handleBalancesAll = async () => {
let balancesAll: Balances[];
const savedBalancesAll = await getSnapState();

const logoList = await Promise.all(selectedOptions.map(({ value }) => getLogoByGenesisHash(value as HexString)));

const logos = selectedOptions.map(({ value }, index) => {
return { genesisHash: value, logo: logoList[index] }
});

if (savedBalancesAll.balancesAll && Date.now() - Number(savedBalancesAll.balancesAll.date) < PRICE_VALIDITY_PERIOD) {
if (savedBalancesAll.balancesAll && (savedOnly || Date.now() - Number(savedBalancesAll.balancesAll.date) < PRICE_VALIDITY_PERIOD)) {
const temp = JSON.parse(savedBalancesAll.balancesAll.data);

temp.forEach((item) => {
Expand All @@ -46,6 +40,11 @@ export const handleBalancesAll = async () => {
await updateSnapState('balancesAll', { date: Date.now(), data: JSON.stringify(balancesAll) });
}

const logoList = await Promise.all(selectedOptions.map(({ value }) => getLogoByGenesisHash(value as HexString)));
const logos = selectedOptions.map(({ value }, index) => {
return { genesisHash: value, logo: logoList[index] }
});

await updateTokenPrices();
const pricesInUsd = await Promise.all(selectedOptions.map(({ value }) => getNativeTokenPrice(value as HexString)));

Expand Down
Loading

0 comments on commit b44d34d

Please sign in to comment.