Skip to content

Commit 553f042

Browse files
author
Damian Finkelstein
committed
feat: 🎸 migrated balance_of to v2
1 parent 24fad05 commit 553f042

File tree

8 files changed

+48
-25
lines changed

8 files changed

+48
-25
lines changed

‎dump.rdb‎

88 Bytes
Binary file not shown.

‎jelly‎

Submodule jelly updated from a45152e to 83581f9

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"@dfinity/agent": "^0.10.2",
77
"@dfinity/candid": "^0.10.2",
88
"@dfinity/identity": "^0.10.2",
9-
"@dfinity/principal": "^0.10.2",
9+
"@dfinity/principal": "^0.12.2",
1010
"@psychedelic/cap-js": "^0.0.7",
1111
"@psychedelic/icns-js": "^0.1.11",
1212
"@psychedelic/jelly-js": "0.1.0-beta.202209091032-e94a8f5",
@@ -163,4 +163,4 @@
163163
]
164164
]
165165
}
166-
}
166+
}

‎src/components/alerts/index.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useTranslation } from 'react-i18next';
2+
import { useParams } from 'react-router';
23
import {
34
AlertsContainer,
45
AlertsWrapper,
@@ -16,7 +17,9 @@ import { useAssetsToWithdraw } from '../../hooks/use-assets-to-withdraw';
1617
export const Alerts = () => {
1718
const { t } = useTranslation();
1819

19-
useAssetsToWithdraw();
20+
const { collectionId } = useParams();
21+
22+
useAssetsToWithdraw(collectionId);
2023

2124
return (
2225
<AlertsContainer>
@@ -32,3 +35,4 @@ export const Alerts = () => {
3235
</AlertsContainer>
3336
);
3437
};
38+

‎src/hooks/use-assets-to-withdraw.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
marketplaceActions,
88
} from '../store';
99

10-
export const useAssetsToWithdraw = () => {
10+
export const useAssetsToWithdraw = (collectionId?: string) => {
1111
const dispatch = useAppDispatch();
1212
const { isConnected, principalId: plugPrincipal } = usePlugStore();
1313

@@ -21,11 +21,12 @@ export const useAssetsToWithdraw = () => {
2121
);
2222

2323
useEffect(() => {
24-
if (!isConnected || !plugPrincipal) return;
24+
if (!isConnected || !plugPrincipal || !collectionId) return;
2525

2626
dispatch(
2727
marketplaceActions.getAssetsToWithdraw({
2828
userPrincipalId: plugPrincipal,
29+
collectionId,
2930
}),
3031
);
3132
}, [
@@ -34,5 +35,7 @@ export const useAssetsToWithdraw = () => {
3435
plugPrincipal,
3536
recentlyWithdrawnAssets,
3637
recentlyFailedTransactions,
38+
collectionId,
3739
]);
3840
};
41+

‎src/store/features/marketplace/async-thunks/get-assets-to-withdraw.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Principal } from '@dfinity/principal';
22
import { createAsyncThunk } from '@reduxjs/toolkit';
3+
import { JellyCollection } from '@psychedelic/jelly-js';
34
import { actorInstanceHandler } from '../../../../integrations/actor';
45
import { marketplaceSlice } from '../marketplace-slice';
56
import { AppLog } from '../../../../utils/log';
67
import { parseBalanceResponse } from '../../../../utils/parser';
78
import { settingsActions } from '../../settings';
9+
import { jellyJsInstanceHandler } from '../../../../integrations/jelly-js';
10+
import { getJellyCollection } from '../../../../utils/jelly';
811

912
export type GetAssetsToWithdrawProps = {
1013
userPrincipalId: string;
14+
collectionId: string;
1115
};
1216

1317
export const getAssetsToWithdraw = createAsyncThunk<
@@ -22,14 +26,27 @@ export const getAssetsToWithdraw = createAsyncThunk<
2226
slice: marketplaceSlice,
2327
});
2428

25-
const { userPrincipalId } = params;
29+
const { userPrincipalId, collectionId } = params;
2630

2731
try {
28-
const userPrincipalAddress = Principal.fromText(userPrincipalId);
32+
const jellyInstance = await jellyJsInstanceHandler({
33+
thunkAPI,
34+
collectionId,
35+
slice: marketplaceSlice,
36+
});
2937

30-
const balanceResponse = await actorInstance.balanceOf(
31-
userPrincipalAddress,
32-
);
38+
const collection = await getJellyCollection({
39+
jellyInstance,
40+
collectionId,
41+
});
42+
43+
if (!collection)
44+
throw Error(`Oops! collection ${collectionId} not found!`);
45+
46+
const jellyCollection: JellyCollection =
47+
await jellyInstance.getJellyCollection(collection);
48+
49+
const balanceResponse = await jellyCollection.getMyBalance();
3350

3451
const assetsToWithdraw =
3552
!Array.isArray(balanceResponse) || !balanceResponse.length
@@ -45,3 +62,4 @@ export const getAssetsToWithdraw = createAsyncThunk<
4562
AppLog.error(err);
4663
}
4764
});
65+

‎src/utils/icns.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const reverseNameActor = new ICNSReverseController();
55

66
export const getICNSName = async (plugPrincipalId: string) => {
77
const owner = Principal.fromText(plugPrincipalId);
8+
// @ts-ignore
89
const icnsName = await reverseNameActor.getReverseName(owner);
910
return icnsName;
1011
};
@@ -21,3 +22,4 @@ export const formatICNSName = (name: string) => {
2122
name.length,
2223
)}`;
2324
};
25+

‎src/utils/parser.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,13 @@ export const parseGetCollectionsResponse = (data: Array<any>) => {
336336
};
337337

338338
// TODO: update data type while using collection details
339-
export const parseBalanceResponse = (data: Array<any>) => {
340-
const parsed = data.reduce((accParent, currParent) => {
341-
const assetsToWithdraw: AssetToWithdraw = {
342-
principalId: Principal.fromUint8Array(
343-
currParent[0]._arr,
344-
).toString(),
345-
amount: parseE8SAmountToWICP(currParent[1]),
346-
};
347-
348-
return [...accParent, assetsToWithdraw];
349-
}, [] as AssetsToWithdraw);
350-
351-
return parsed;
352-
};
339+
export const parseBalanceResponse = (
340+
data: {
341+
fungibleCanisterId: Principal;
342+
balance: bigint;
343+
}[],
344+
) =>
345+
data.map((fungibleBalance) => ({
346+
principalId: fungibleBalance.fungibleCanisterId.toString(),
347+
amount: parseE8SAmountToWICP(fungibleBalance.balance),
348+
}));

0 commit comments

Comments
 (0)