Skip to content

Commit

Permalink
Remove TezTransfers fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
serjonya-trili committed Oct 6, 2023
1 parent 0340f27 commit 76a9057
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 155 deletions.
101 changes: 51 additions & 50 deletions src/components/AccountDrawer/AccountDrawerDisplay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import multisigsSlice, { multisigActions } from "../../utils/redux/slices/multis
import tokensSlice from "../../utils/redux/slices/tokensSlice";
import { GHOSTNET, MAINNET } from "../../types/Network";
import { networksActions } from "../../utils/redux/slices/networks";
const { updateTezBalance, updateTokenBalance, updateTezTransfers, updateDelegations } =
assetsSlice.actions;
const { updateTezBalance, updateTokenBalance, updateDelegations } = assetsSlice.actions;
const { addAccount } = accountsSlice.actions;

const { setMultisigs } = multisigsSlice.actions;
Expand Down Expand Up @@ -155,35 +154,36 @@ describe("<AccountCard />", () => {
});

it("should display accounts operations under operations tab if any", () => {
store.dispatch(
updateTezTransfers([
{
pkh: selectedAccount.address.pkh,
transfers: [
mockTzktTezTransfer(
selectedAccount.address.pkh,
mockImplicitAccount(1).address.pkh,
1000000
),
mockTzktTezTransfer(
mockImplicitAccount(2).address.pkh,
selectedAccount.address.pkh,
2000000
),
],
},
{
pkh: mockImplicitAccount(1).address.pkh,
transfers: [
mockTzktTezTransfer(
mockImplicitAccount(1).address.pkh,
mockImplicitAccount(1).address.pkh,
5000000
),
],
},
])
);
// TODO: fix this test
// store.dispatch(
// updateTezTransfers([
// {
// pkh: selectedAccount.address.pkh,
// transfers: [
// mockTzktTezTransfer(
// selectedAccount.address.pkh,
// mockImplicitAccount(1).address.pkh,
// 1000000
// ),
// mockTzktTezTransfer(
// mockImplicitAccount(2).address.pkh,
// selectedAccount.address.pkh,
// 2000000
// ),
// ],
// },
// {
// pkh: mockImplicitAccount(1).address.pkh,
// transfers: [
// mockTzktTezTransfer(
// mockImplicitAccount(1).address.pkh,
// mockImplicitAccount(1).address.pkh,
// 5000000
// ),
// ],
// },
// ])
// );

render(<AccountCard account={selectedAccount} />);
expect(screen.getByTestId("account-card-operations-tab")).toBeInTheDocument();
Expand Down Expand Up @@ -293,25 +293,26 @@ describe("<AccountCard />", () => {
});

it("multisig account display operations under operations tab if any", () => {
store.dispatch(
updateTezTransfers([
{
pkh: multisigAccount.address.pkh,
transfers: [
mockTzktTezTransfer(
multisigAccount.address.pkh,
mockImplicitAccount(1).address.pkh,
1000000
),
mockTzktTezTransfer(
mockImplicitAccount(2).address.pkh,
multisigAccount.address.pkh,
2000000
),
],
},
])
);
// TODO: fix this test
// store.dispatch(
// updateTezTransfers([
// {
// pkh: multisigAccount.address.pkh,
// transfers: [
// mockTzktTezTransfer(
// multisigAccount.address.pkh,
// mockImplicitAccount(1).address.pkh,
// 1000000
// ),
// mockTzktTezTransfer(
// mockImplicitAccount(2).address.pkh,
// multisigAccount.address.pkh,
// 2000000
// ),
// ],
// },
// ])
// );

render(<AccountCard account={multisigAccount} />);
expect(screen.getByTestId("account-card-operations-tab")).toBeInTheDocument();
Expand Down
73 changes: 1 addition & 72 deletions src/utils/redux/slices/assetsSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GHOSTNET } from "../../../types/Network";
import { networksActions } from "./networks";

Check warning on line 12 in src/utils/redux/slices/assetsSlice.test.ts

View workflow job for this annotation

GitHub Actions / build

'networksActions' is defined but never used

Check warning on line 12 in src/utils/redux/slices/assetsSlice.test.ts

View workflow job for this annotation

GitHub Actions / build

'networksActions' is defined but never used

const {
actions: { updateTezBalance, updateTokenBalance, updateTezTransfers, updateTokenTransfers },
actions: { updateTezBalance, updateTokenBalance, updateTokenTransfers },
} = assetsSlice;

describe("assetsSlice", () => {
Expand Down Expand Up @@ -170,77 +170,6 @@ describe("assetsSlice", () => {
});
});

test("tez transfers are upserted", () => {
store.dispatch(
updateTezTransfers([
{
pkh: "foo",
transfers: [mockTezTransaction(1), mockTezTransaction(2)],
},
{ pkh: "bar", transfers: [mockTezTransaction(3)] },
])
);

expect(store.getState().assets).toEqual({
conversionRate: null,
balances: {
mutez: {},
tokens: {},
},
delegations: {},
bakers: [],
transfers: {
tez: {
foo: [mockTezTransaction(1), mockTezTransaction(2)],
bar: [mockTezTransaction(3)],
},
tokens: {},
},
blockLevel: null,
refetchTrigger: 0,
lastTimeUpdated: null,
isLoading: false,
latestOperations: [],
});

store.dispatch(
updateTezTransfers([
{
pkh: "foo",
transfers: [mockTezTransaction(4)],
},
{
pkh: "baz",
transfers: [mockTezTransaction(5)],
},
])
);

expect(store.getState().assets).toEqual({
conversionRate: null,
balances: {
mutez: {},
tokens: {},
},
delegations: {},
bakers: [],
transfers: {
tez: {
foo: [mockTezTransaction(4)],
bar: [mockTezTransaction(3)],
baz: [mockTezTransaction(5)],
},
tokens: {},
},
blockLevel: null,
refetchTrigger: 0,
lastTimeUpdated: null,
isLoading: false,
latestOperations: [],
});
store.dispatch(networksActions.setCurrent(GHOSTNET));
});

test("token transfers are upserted", () => {
store.dispatch(updateTokenTransfers([mockTokenTransaction(1), mockTokenTransaction(2)]));

Expand Down
15 changes: 0 additions & 15 deletions src/utils/redux/slices/assetsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ type State = {
lastTimeUpdated: string | null;
};

export type TezTransfersPayload = {
pkh: RawPkh;
transfers: TezTransfer[];
};

export type DelegationPayload = {
pkh: RawPkh;
delegation: DelegationOperation;
Expand Down Expand Up @@ -69,16 +64,6 @@ const assetsSlice = createSlice({
updateBlockLevel: (state, { payload }: { payload: number }) => {
state.blockLevel = payload;
},
updateTezTransfers: (state, { payload }: { type: string; payload: TezTransfersPayload[] }) => {
const tezOperationsPayload = payload;
const newTezTransfers = { ...state.transfers.tez };

tezOperationsPayload.forEach(op => {
const { pkh, transfers } = op;
newTezTransfers[pkh] = transfers;
});
state.transfers.tez = newTezTransfers;
},
updateTokenTransfers: (state, { payload: transfers }: { payload: TokenTransfer[] }) => {
transfers.forEach(transfer => {
state.transfers.tokens[transfer.transactionId] = transfer;
Expand Down
8 changes: 6 additions & 2 deletions src/utils/tezos/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ export const getCombinedOperations = async (
).slice(0, limit) as TzktCombinedOperation[];
};

export const getTokenTransfers = (transactionIds: number[], network: Network) =>
withRateLimit(() =>
export const getTokenTransfers = async (transactionIds: number[], network: Network) => {
if (transactionIds.length === 0) {
return [];
}
return withRateLimit(() =>
tokensGetTokenTransfers(
// tzkt doesn't work with the `in` operator correctly
// the only way is to have just one element in it and join it with a comma manually
{ transactionId: { in: [transactionIds.join(",")] as any } },
{ baseUrl: network.tzktApiUrl }
)
) as Promise<TokenTransfer[]>;
};

export const getLastDelegation = async (address: RawPkh, network: Network) =>
withRateLimit(() =>
Expand Down
17 changes: 1 addition & 16 deletions src/utils/useAssetsPolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Network } from "../types/Network";
import { useImplicitAccounts } from "./hooks/accountHooks";
import { useRefetchTrigger } from "./hooks/assetsHooks";
import { getPendingOperationsForMultisigs, getRelevantMultisigContracts } from "./multisig/helpers";
import { assetsActions, DelegationPayload, TezTransfersPayload } from "./redux/slices/assetsSlice";
import { assetsActions, DelegationPayload } from "./redux/slices/assetsSlice";
import { useAppDispatch } from "./redux/hooks";
import { multisigActions } from "./redux/slices/multisigsSlice";
import { tokensActions } from "./redux/slices/tokensSlice";
Expand All @@ -16,7 +16,6 @@ import {
getLastDelegation,
getLatestBlockLevel,
getTezosPriceInUSD,
getTezTransfers,
getTokenBalances,
getTokenTransfers,
} from "./tezos";
Expand All @@ -28,14 +27,6 @@ import { RawPkh } from "../types/Address";
import { useToast } from "@chakra-ui/react";
import { Multisig } from "./multisig/types";

const getTezTransfersPayload = async (
pkh: string,
network: Network
): Promise<TezTransfersPayload> => {
const transfers = await getTezTransfers(pkh, network);
return { pkh, transfers };
};

const getDelegationsPayload = async (
pkh: string,
network: Network
Expand All @@ -62,11 +53,6 @@ const updateTezBalances = async (dispatch: AppDispatch, network: Network, addres
dispatch(assetsActions.updateTezBalance(accountInfos.flat()));
};

const updateTezTransfers = async (dispatch: AppDispatch, network: Network, pkhs: RawPkh[]) => {
const tezTransfers = await Promise.all(pkhs.map(pkh => getTezTransfersPayload(pkh, network)));
dispatch(assetsActions.updateTezTransfers(tezTransfers));
};

const updateDelegations = async (dispatch: AppDispatch, network: Network, pkhs: RawPkh[]) => {
const delegations = await Promise.all(pkhs.map(pkh => getDelegationsPayload(pkh, network)));
dispatch(assetsActions.updateDelegations(compact(delegations)));
Expand Down Expand Up @@ -129,7 +115,6 @@ const updateAccountAssets = async (
await Promise.all([
updatePendingOperations(dispatch, network, multisigs),
updateTezBalances(dispatch, network, allAccountAddresses),
updateTezTransfers(dispatch, network, allAccountAddresses),
updateDelegations(dispatch, network, allAccountAddresses),
updateTokenBalances(dispatch, network, allAccountAddresses),
updateOperations(dispatch, network, allAccountAddresses),
Expand Down
1 change: 1 addition & 0 deletions src/views/operations/useGetOperations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const useGetOperations = (initialAddresses: RawPkh[]) => {
fetchOperationsAndUpdateTokensInfo(dispatch, network, addresses)
.then(latestOperations => {
setOperations(latestOperations);
setHasMore(latestOperations.length > 0);
setUpdatesTrigger(prev => prev + 1);
})
.finally(() => setIsLoading(false));
Expand Down

0 comments on commit 76a9057

Please sign in to comment.