Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions amplify/backend/function/fetchColonyBalances/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ exports.handler = async ({ source: { id: colonyAddress } }) => {
};
});

tokens.map(async ({ token }) => {
const mainChainTokens = tokens.filter(
(token) => token.token.chainMetadata.chainId === chainId,
);

mainChainTokens.map(async ({ token }) => {
const { id: tokenAddress } = token;

balances.push(async () => {
Expand Down Expand Up @@ -201,7 +205,11 @@ exports.handler = async ({ source: { id: colonyAddress } }) => {
};
});

tokens.map(async ({ token }) => {
const proxyChainTokens = tokens.filter(
(token) => token.token.chainMetadata.chainId === proxyChainId,
);

proxyChainTokens.map(async ({ token }) => {
Comment on lines +208 to +212
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't bother mapping through tokens that don't exist on this chain where the balances will definitely be 0.

const { id: tokenAddress } = token;

balances.push(async () => {
Expand Down
3 changes: 3 additions & 0 deletions amplify/backend/function/fetchTokenFromChain/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const baseToken = {
decimals: null,
type: null,
colonies: null,
chainMetadata: {
chainId: '',
},
};

const setEnvVariables = async (network) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const AmountField: FC<AmountFieldProps> = ({
if (selectedToken?.tokenAddress) {
tokenAddressController.onChange(selectedToken.tokenAddress);
}
// eslint-disable-next-line react-hooks/exhaustive-deps -- tokenAddressController deliberately omitted
}, [selectedToken?.tokenAddress]);

const handleTokenSelect = (selectedTokenAddress: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { type FormatNumeralOptions } from 'cleave-zen';
import { useMemo } from 'react';
import { useFormContext } from 'react-hook-form';

import { DEFAULT_NETWORK_INFO } from '~constants';
import { useColonyContext } from '~context/ColonyContext/ColonyContext.ts';
import { notNull } from '~utils/arrays/index.ts';
import { getTokenDecimalsWithFallback } from '~utils/tokens.ts';
import { CHAIN_FIELD_NAME } from '~v5/common/ActionSidebar/consts.ts';

export const useAmountField = (selectedTokenAddress: string | undefined) => {
const { colony } = useColonyContext();
const {
colony,
colony: { nativeToken },
} = useColonyContext();
const { watch } = useFormContext();
const chainId = watch(CHAIN_FIELD_NAME);
const chainId = watch(CHAIN_FIELD_NAME) ?? DEFAULT_NETWORK_INFO.chainId;

const colonyTokens =
colony.tokens?.items
Expand All @@ -21,7 +25,11 @@ export const useAmountField = (selectedTokenAddress: string | undefined) => {
const selectedToken =
colonyTokens.find(
(token) => token?.tokenAddress === selectedTokenAddress,
) || colonyTokens[0];
) ||
colonyTokens.find(
(token) => token?.tokenAddress === nativeToken.tokenAddress,
) ||
colonyTokens[0];

const formattingOptions: FormatNumeralOptions = useMemo(
() => ({
Expand Down
15 changes: 7 additions & 8 deletions src/utils/validation/hasEnoughFundsValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BigNumber } from 'ethers';
import moveDecimal from 'move-decimal-point';
import { type TestContext } from 'yup';

import { DEFAULT_NETWORK_INFO } from '~constants';
import { type Colony } from '~types/graphql.ts';
import { notNull } from '~utils/arrays/index.ts';
import {
Expand Down Expand Up @@ -35,20 +34,20 @@ export const hasEnoughFundsValidation = ({
return false;
}
const { parent } = context;
const { tokenAddress: tokenAddressFieldValue, chainId } = parent || {};
const { tokenAddress: tokenAddressFieldValue, chain: chainId } = parent || {};

// @TODO hook this up with actual balances
if (chainId !== DEFAULT_NETWORK_INFO.chainId) {
return true;
}
const colonyTokens =
colony.tokens?.items
.filter(notNull)
.map((colonyToken) => colonyToken.token) || [];

const selectedToken = colonyTokens.find(
({ tokenAddress: selectedTokenAddress }) =>
selectedTokenAddress === tokenAddressFieldValue || tokenAddress,
({
tokenAddress: selectedTokenAddress,
chainMetadata: { chainId: selectedChainId },
}) =>
(selectedTokenAddress === tokenAddressFieldValue || tokenAddress) &&
selectedChainId === chainId,
);

if (!selectedToken) {
Expand Down