Skip to content

Commit

Permalink
Merge branch 'dev' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidHaji-zada committed Jan 11, 2024
2 parents 34009eb + 647d4ca commit 1063b2d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 49 deletions.
32 changes: 4 additions & 28 deletions src/contexts/AllAddresses/AllAddresses.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { AllAddressesAction } from '@contexts';
import { CacheableAccount } from '@appTypes/CacheableAccount';
import { Cache, CacheKey } from '@lib/cache';
import { ExplorerAccount } from '@models/Explorer';
import { API } from '@api/api';
import { createContextSelector } from '@helpers/createContextSelector';
import { AirDAOEventDispatcher } from '@lib';
import {
AirDAOEventType,
AirDAONotificationReceiveEventPayload
} from '@appTypes';
import { ArrayUtils } from '@utils/array';
import { AddressUtils } from '@utils/address';

const AllAddressesContext = () => {
const [allAddresses, setAllAddresses] = useState<ExplorerAccount[]>([]);
Expand Down Expand Up @@ -91,30 +91,6 @@ const AllAddressesContext = () => {
[addAddress, addOrUpdateAddress, removeAddress, updateAddress]
);

const populateAddresses = async (
addresses: CacheableAccount[]
): Promise<ExplorerAccount[]> => {
try {
return await Promise.all(
addresses.map(async (address) => {
try {
const account = new ExplorerAccount(
await API.explorerService.searchAddress(address.address)
);
const newAccount = Object.assign({}, account);
newAccount.name = address.name;
newAccount.isOnWatchlist = Boolean(address.isOnWatchlist);
return newAccount;
} catch (error) {
throw error;
}
})
);
} catch (error) {
throw error;
}
};

// fetch all addresses on mount
const getAddresses = async () => {
setLoading(true);
Expand All @@ -124,7 +100,7 @@ const AllAddressesContext = () => {
const currentAddresses = allAddresses
.filter((address) => !!address)
.map(ExplorerAccount.toCacheable);
const populatedAddresses = await populateAddresses(
const populatedAddresses = await AddressUtils.populateAddresses(
ArrayUtils.mergeArrays('address', addresses, currentAddresses)
);
setAllAddresses(populatedAddresses);
Expand Down Expand Up @@ -153,13 +129,13 @@ const AllAddressesContext = () => {
(address) => address.address === data.from
);
if (toIdx > -1) {
const updatedSenderAddress = await populateAddresses([
const updatedSenderAddress = await AddressUtils.populateAddresses([
allAddresses[toIdx]
]);
reducer({ type: 'update', payload: updatedSenderAddress[0] });
}
if (fromIdx > -1) {
const updatedReceivingAddress = await populateAddresses([
const updatedReceivingAddress = await AddressUtils.populateAddresses([
allAddresses[fromIdx]
]);
reducer({ type: 'update', payload: updatedReceivingAddress[0] });
Expand Down
50 changes: 43 additions & 7 deletions src/contexts/ListsContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ExplorerAccount } from '@models/Explorer';
import { ExplorerAccountDTO } from '@models';
import { API } from '@api/api';
import { MULTISIG_VAULT } from '@constants/variables';
import { AddressUtils } from '@utils/address';

const ListsContext = () => {
const allAddresses = useAllAddresses();
Expand All @@ -26,18 +27,53 @@ const ListsContext = () => {
(l) =>
new AccountList({
...l,
accounts: l.addresses?.map(
(address) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
allAddresses.find(
(populatedAddress) => populatedAddress.address === address
)!
)
accounts: l.addresses
?.map(
(address) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
allAddresses.find(
(populatedAddress) => populatedAddress.address === address
)!
)
.filter((account) => account != undefined)
})
);
return populatedLists;
}, [allAddresses, listsOfAddressGroup]);

const pouplateUnknownAddresses = useCallback(async () => {
const unknownAddresses = listsOfAddressGroup
.map(
(l) =>
new AccountList({
...l,
accounts: l.addresses
?.map(
(address) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
allAddresses.find(
(populatedAddress) => populatedAddress.address === address
)!
)
.filter((account) => account == undefined)
})
)
.reduce((a, b) => a.concat(b.accounts), [] as ExplorerAccount[]);

if (unknownAddresses?.length > 0) {
const populatedUnknownAddress = await AddressUtils.populateAddresses(
unknownAddresses
);
if (!allAddresses.length) {
allAddressesReducer({ type: 'set', payload: populatedUnknownAddress });
}
}
}, [allAddresses, allAddressesReducer, listsOfAddressGroup]);

useEffect(() => {
pouplateUnknownAddresses();
}, [allAddresses, listsOfAddressGroup, pouplateUnknownAddresses]);

const updateListOfAddressGroup = async (newLists: CacheableAccountList[]) => {
setListsOfAddressGroup([...newLists]);
};
Expand Down
3 changes: 2 additions & 1 deletion src/contexts/SendCrypto/SendCrypto.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export const SEND_CRYPTO_INITIAL_STATE: SendCryptoContextState = {
currency: AirDAODictTypes.Code.AMB,
loading: false,
estimatedFee: 0,
error: null
error: null,
transactionId: ''
};
1 change: 1 addition & 0 deletions src/contexts/SendCrypto/SendCrypto.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface SendCryptoContextState {
loading: boolean;
estimatedFee: number;
error: Error | null;
transactionId: string; // random tx id for internal usage
}

type SendCryptoActionType = 'SET_DATA' | 'RESET_DATA';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/TransferDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TransferDispatcher {

constructor() {
this.web3 = new Web3(new Web3.providers.HttpProvider(WEB3_LINK));
this.web3.eth.transactionPollingTimeout = 60;
this.web3.eth.transactionPollingTimeout = 15;
}

private async prepareTransactionConfig(
Expand Down
29 changes: 19 additions & 10 deletions src/screens/SendFunds/SendFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ export const SendFunds = () => {
const navigation = useNavigation<HomeNavigationProp>();
const route = useRoute<RouteProp<HomeParamsList, 'SendFunds'>>();
const tokenFromNavigationParams = route.params?.token;
const { to: destinationAddress = '', from: senderAddress = '' } =
sendContextState;

const {
to: destinationAddress = '',
from: senderAddress = '',
transactionId
} = sendContextState;
const transactionIdRef = useRef(transactionId);
transactionIdRef.current = transactionId;
const { data: selectedAccount } = useAccountByAddress(senderAddress);
const walletHash = selectedAccount?.wallet.id || '';

Expand Down Expand Up @@ -170,7 +174,8 @@ export const SendFunds = () => {

const sendTx = () => {
hideReviewModal();
updateSendContext({ type: 'SET_DATA', loading: true });
const txId = new Date().getTime().toString();
updateSendContext({ type: 'SET_DATA', loading: true, transactionId: txId });
setTimeout(async () => {
try {
navigation.replace('SendFundsStatus');
Expand All @@ -184,13 +189,17 @@ export const SendFunds = () => {
Number(amountInCrypto),
selectedToken
);
updateSendContext({ type: 'SET_DATA', loading: false });
if (transactionIdRef.current === txId) {
updateSendContext({ type: 'SET_DATA', loading: false });
}
} catch (error: unknown) {
updateSendContext({
type: 'SET_DATA',
loading: false,
error: error as any
});
if (transactionIdRef.current === txId) {
updateSendContext({
type: 'SET_DATA',
loading: false,
error: error as any
});
}
}
}, 1000);
};
Expand Down
32 changes: 30 additions & 2 deletions src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { API } from '@api/api';
import { Permission } from '@appTypes';
import { CacheableAccount, Permission } from '@appTypes';
import { PermissionService } from '@lib';
import { ExplorerAccount } from '@models';

Expand All @@ -15,4 +15,32 @@ const addressToToken = (address: string) => {
return Buffer.from(address).toString('base64').slice(3, 11);
};

export const AddressUtils = { watchChangesOfAddress, addressToToken };
const populateAddresses = async (
addresses: CacheableAccount[]
): Promise<ExplorerAccount[]> => {
try {
return await Promise.all(
addresses.map(async (address) => {
try {
const account = new ExplorerAccount(
await API.explorerService.searchAddress(address.address)
);
const newAccount = Object.assign({}, account);
newAccount.name = address.name;
newAccount.isOnWatchlist = Boolean(address.isOnWatchlist);
return newAccount;
} catch (error) {
throw error;
}
})
);
} catch (error) {
throw error;
}
};

export const AddressUtils = {
watchChangesOfAddress,
addressToToken,
populateAddresses
};

0 comments on commit 1063b2d

Please sign in to comment.