Skip to content

Commit

Permalink
Merge pull request #257 from dcrescimbeni/fix/page-crashes-when-recod…
Browse files Browse the repository at this point in the history
…ing-mint

Fix: page crash when trying to decode rep mint
  • Loading branch information
rossneilson authored Mar 7, 2023
2 parents daf205a + e492d51 commit a8a3544
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
9 changes: 7 additions & 2 deletions apps/davi/src/Modules/Guilds/Hooks/useTotalSupply.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { BigNumber } from 'ethers';
import { useMemo } from 'react';
import { DecodedCall } from 'components/ActionsBuilder/types';

interface IUseTotalSupply {
decodedCall: DecodedCall;
}

interface RepMintState {
toAddress: string;
amount: BigNumber;
}

export const useTotalSupply = ({ decodedCall }) => {
export const useTotalSupply = ({ decodedCall }: IUseTotalSupply) => {
const parsedData = useMemo<RepMintState>(() => {
if (!decodedCall) return null;
return {
toAddress: decodedCall.args.to,
toAddress: decodedCall.args.account,
amount: decodedCall.args.amount,
};
}, [decodedCall]);
Expand Down
28 changes: 28 additions & 0 deletions apps/davi/src/utils/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { shortenAddress } from './address';
import { ANY_ADDRESS } from './constants';

const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});

describe('shortenAddress', () => {
test('should return a shorten address if everything is correct (default digits)', () => {
const result = shortenAddress(ANY_ADDRESS);
expect(result).toBe('0xaAaA...aaAa');
});

test('should return a shorten address if everything is correct (2 digits)', () => {
const result = shortenAddress(ANY_ADDRESS, 2);
expect(result).toBe('0xaA...Aa');
});

test('should return null and log error if address is undefined', () => {
const result = shortenAddress(undefined);
expect(result).toBeNull();
expect(consoleError).toBeCalled();
});

test('should return null and log error if parameter is not an address', () => {
const result = shortenAddress('0xNotAnAddress');
expect(result).toBeNull();
expect(consoleError).toBeCalled();
});
});
6 changes: 4 additions & 2 deletions apps/davi/src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers, utils } from 'ethers';
import i18next from 'i18next';

const arbitrum = require('../configs/arbitrum/config.json');
const arbitrumTestnet = require('../configs/arbitrumTestnet/config.json');
Expand All @@ -17,8 +18,9 @@ const appConfig: AppConfig = {
};

export function shortenAddress(address: string, digits = 4) {
if (!isAddress(address)) {
throw Error(`Invalid 'address' parameter '${address}'.`);
if (!address || !isAddress(address)) {
console.error(`${i18next.t('inputValidation.invalidAddress')}: ${address}`);
return null;
}
return `${address.substring(0, digits + 2)}...${address.substring(
42 - digits
Expand Down

1 comment on commit a8a3544

@vercel
Copy link

@vercel vercel bot commented on a8a3544 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.