Skip to content

Commit

Permalink
feat: 🎸 migrated balance_of to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Finkelstein committed Sep 13, 2022
1 parent 24fad05 commit 87e20f4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@dfinity/agent": "^0.10.2",
"@dfinity/candid": "^0.10.2",
"@dfinity/identity": "^0.10.2",
"@dfinity/principal": "^0.10.2",
"@dfinity/principal": "^0.12.2",
"@psychedelic/cap-js": "^0.0.7",
"@psychedelic/icns-js": "^0.1.11",
"@psychedelic/jelly-js": "0.1.0-beta.202209091032-e94a8f5",
Expand Down Expand Up @@ -163,4 +163,4 @@
]
]
}
}
}
6 changes: 5 additions & 1 deletion src/components/alerts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router';
import {
AlertsContainer,
AlertsWrapper,
Expand All @@ -16,7 +17,9 @@ import { useAssetsToWithdraw } from '../../hooks/use-assets-to-withdraw';
export const Alerts = () => {
const { t } = useTranslation();

useAssetsToWithdraw();
const { collectionId } = useParams();

useAssetsToWithdraw(collectionId);

return (
<AlertsContainer>
Expand All @@ -32,3 +35,4 @@ export const Alerts = () => {
</AlertsContainer>
);
};

7 changes: 5 additions & 2 deletions src/hooks/use-assets-to-withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
marketplaceActions,
} from '../store';

export const useAssetsToWithdraw = () => {
export const useAssetsToWithdraw = (collectionId?: string) => {
const dispatch = useAppDispatch();
const { isConnected, principalId: plugPrincipal } = usePlugStore();

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

useEffect(() => {
if (!isConnected || !plugPrincipal) return;
if (!isConnected || !plugPrincipal || !collectionId) return;

dispatch(
marketplaceActions.getAssetsToWithdraw({
userPrincipalId: plugPrincipal,
collectionId,
}),
);
}, [
Expand All @@ -34,5 +35,7 @@ export const useAssetsToWithdraw = () => {
plugPrincipal,
recentlyWithdrawnAssets,
recentlyFailedTransactions,
collectionId,
]);
};

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Principal } from '@dfinity/principal';
import { createAsyncThunk } from '@reduxjs/toolkit';
import { JellyCollection } from '@psychedelic/jelly-js';
import { actorInstanceHandler } from '../../../../integrations/actor';
import { marketplaceSlice } from '../marketplace-slice';
import { AppLog } from '../../../../utils/log';
import { parseBalanceResponse } from '../../../../utils/parser';
import { settingsActions } from '../../settings';
import { jellyJsInstanceHandler } from '../../../../integrations/jelly-js';
import { getJellyCollection } from '../../../../utils/jelly';

export type GetAssetsToWithdrawProps = {
userPrincipalId: string;
collectionId: string;
};

export const getAssetsToWithdraw = createAsyncThunk<
Expand All @@ -22,14 +26,33 @@ export const getAssetsToWithdraw = createAsyncThunk<
slice: marketplaceSlice,
});

const { userPrincipalId } = params;
const { userPrincipalId, collectionId } = params;

try {
const userPrincipalAddress = Principal.fromText(userPrincipalId);
const jellyInstance = await jellyJsInstanceHandler({
thunkAPI,
collectionId,
slice: marketplaceSlice,
});

const balanceResponse = await actorInstance.balanceOf(
userPrincipalAddress,
);
const collection = await getJellyCollection({
jellyInstance,
collectionId,
});

if (!collection)
throw Error(`Oops! collection ${collectionId} not found!`);

const jellyCollection: JellyCollection =
await jellyInstance.getJellyCollection(collection);

const balanceResponse = await jellyCollection.getMyBalance();

// const userPrincipalAddress = Principal.fromText(userPrincipalId);

// const balanceResponse = await actorInstance.balanceOf(
// userPrincipalAddress,
// );

const assetsToWithdraw =
!Array.isArray(balanceResponse) || !balanceResponse.length
Expand All @@ -45,3 +68,4 @@ export const getAssetsToWithdraw = createAsyncThunk<
AppLog.error(err);
}
});

2 changes: 2 additions & 0 deletions src/utils/icns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const reverseNameActor = new ICNSReverseController();

export const getICNSName = async (plugPrincipalId: string) => {
const owner = Principal.fromText(plugPrincipalId);
// @ts-ignore
const icnsName = await reverseNameActor.getReverseName(owner);
return icnsName;
};
Expand All @@ -21,3 +22,4 @@ export const formatICNSName = (name: string) => {
name.length,
)}`;
};

24 changes: 10 additions & 14 deletions src/utils/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,13 @@ export const parseGetCollectionsResponse = (data: Array<any>) => {
};

// TODO: update data type while using collection details
export const parseBalanceResponse = (data: Array<any>) => {
const parsed = data.reduce((accParent, currParent) => {
const assetsToWithdraw: AssetToWithdraw = {
principalId: Principal.fromUint8Array(
currParent[0]._arr,
).toString(),
amount: parseE8SAmountToWICP(currParent[1]),
};

return [...accParent, assetsToWithdraw];
}, [] as AssetsToWithdraw);

return parsed;
};
export const parseBalanceResponse = (
data: {
fungibleCanisterId: Principal;
balance: bigint;
}[],
) =>
data.map((fungibleBalance) => ({
principalId: fungibleBalance.fungibleCanisterId.toString(),
amount: parseE8SAmountToWICP(fungibleBalance.balance),
}));

0 comments on commit 87e20f4

Please sign in to comment.