Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request_region_record fix for new release #214

Merged
merged 3 commits into from
Aug 11, 2024
Merged
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
54 changes: 20 additions & 34 deletions src/components/Regions/IsmpRegionCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import TimeAgo from 'javascript-time-ago';
import en from 'javascript-time-ago/locale/en';
import { useEffect, useState } from 'react';

import { useSubmitExtrinsic } from '@/hooks/submitExtrinsic';
import { timesliceToTimestamp } from '@/utils/functions';
import { sendUnsignedTx, timesliceToTimestamp } from '@/utils/functions';
import { makeResponse, makeTimeout, queryRequest } from '@/utils/ismp';

import { useAccounts } from '@/contexts/account';
Expand Down Expand Up @@ -48,12 +47,7 @@ export const IsmpRegionCard = ({
timeslicePeriod,
} = useCoretimeApi();
const {
state: {
api: regionxApi,
apiState: regionxApiState,
symbol: regionxSymbol,
decimals: regionxDecimals,
},
state: { api: regionxApi, apiState: regionxApiState },
} = useRegionXApi();
const {
state: { activeAccount, activeSigner },
Expand All @@ -67,7 +61,6 @@ export const IsmpRegionCard = ({

const { region, coreOccupancy, status } = regionMetadata;
const { toastWarning, toastSuccess, toastInfo, toastError } = useToast();
const { submitExtrinsicWithFeeInfo } = useSubmitExtrinsic();
const [working, setWorking] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -183,31 +176,24 @@ export const IsmpRegionCard = ({
region.getRegionId()
);
setWorking(true);
submitExtrinsicWithFeeInfo(
regionxSymbol,
regionxDecimals,
request,
activeAccount.address,
activeSigner,
{
ready: () => toastInfo('Transaction was initiated'),
inBlock: () => toastInfo('In Block'),
finalized: () => {
/** */
},
success: () => {
toastSuccess('Transaction successful');
setWorking(false);
fetchRegions();
},
fail: () => {
toastError(`Failed to request region record`);
},
error: (e) => {
toastError(`Failed to request the region record. ${e}`);
},
}
);
sendUnsignedTx(request, {
ready: () => toastInfo('Transaction was initiated'),
inBlock: () => toastInfo('In Block'),
finalized: () => {
/** */
},
success: () => {
toastSuccess('Transaction successful');
setWorking(false);
fetchRegions();
},
fail: () => {
toastError(`Failed to request region record`);
},
error: (e) => {
toastError(`Failed to request the region record. ${e}`);
},
});
};

return (
Expand Down
27 changes: 27 additions & 0 deletions src/utils/functions/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ export const sendTx = async (
}
};

export const sendUnsignedTx = async (
tx: SubmittableExtrinsic<'promise', ISubmittableResult>,
handlers: TxStatusHandlers
) => {
try {
const unsub = await tx.send(({ status, events }) => {
if (status.isReady) handlers.ready();
else if (status.isInBlock) handlers.inBlock();
else if (status.isFinalized) {
handlers.finalized();
events.forEach(({ event: { method } }) => {
if (method === 'ExtrinsicSuccess') {
handlers.success();
} else if (method === 'ExtrinsicFailed') {
handlers.fail();
}
});
unsub();
}
});
} catch (e) {
handlers.error(e);
} finally {
handlers.finally && handlers.finally();
}
};

export const enableRegionX = (network: NetworkType): boolean => {
return network === NetworkType.ROCOCO || EXPERIMENTAL;
};
Loading