Skip to content

Commit

Permalink
request_region_record fix for new release (#214)
Browse files Browse the repository at this point in the history
* request_region_record fix

* fix para id

* lint
  • Loading branch information
Szegoo authored Aug 11, 2024
1 parent 7174de3 commit 90d82c3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
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;
};

0 comments on commit 90d82c3

Please sign in to comment.