From 111ae22a4b940c4d8e062bb9df33fa1c1fafc130 Mon Sep 17 00:00:00 2001 From: Sergej Date: Fri, 28 Jun 2024 16:46:44 +0200 Subject: [PATCH 1/2] custom asset payment --- .../Modals/Orders/Contribution/index.tsx | 5 +++-- .../Modals/Orders/OrderCreation/index.tsx | 4 +++- .../Modals/Regions/Purchase/index.tsx | 12 ++++++----- src/components/Modals/Regions/Sell/index.tsx | 5 +++-- .../Regions/IsmpRegionCard/index.tsx | 5 +++-- src/contexts/apis/RegionXApi/index.tsx | 20 ++++++++++++++++++- src/contexts/apis/common.ts | 4 +++- src/hooks/submitExtrinsic.ts | 14 +++++++++---- src/models/common/consts.ts | 3 +++ src/pages/marketplace.tsx | 10 ++++++++-- src/utils/functions/api.ts | 13 +++++++++--- 11 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/components/Modals/Orders/Contribution/index.tsx b/src/components/Modals/Orders/Contribution/index.tsx index 145d9a3f..a36f9904 100644 --- a/src/components/Modals/Orders/Contribution/index.tsx +++ b/src/components/Modals/Orders/Contribution/index.tsx @@ -20,7 +20,7 @@ import { useRegionXApi, useRelayApi } from '@/contexts/apis'; import { ApiState } from '@/contexts/apis/types'; import { useOrders } from '@/contexts/orders'; import { useToast } from '@/contexts/toast'; -import { Order } from '@/models'; +import { Order, RELAY_ASSET_ID } from '@/models'; import styles from './index.module.scss'; @@ -99,7 +99,8 @@ export const ContributionModal = ({ toastError(`Failed to contribute to an order ${e}`); setWorking(false); }, - } + }, + RELAY_ASSET_ID ); } catch (e: any) { setWorking(false); diff --git a/src/components/Modals/Orders/OrderCreation/index.tsx b/src/components/Modals/Orders/OrderCreation/index.tsx index d37db411..dc0c8173 100644 --- a/src/components/Modals/Orders/OrderCreation/index.tsx +++ b/src/components/Modals/Orders/OrderCreation/index.tsx @@ -33,6 +33,7 @@ import { useSaleInfo } from '@/contexts/sales'; import { useToast } from '@/contexts/toast'; import styles from './index.module.scss'; +import { RELAY_ASSET_ID } from '@/models'; interface OrderCreationModalProps { open: boolean; @@ -138,7 +139,8 @@ export const OrderCreationModal = ({ toastError(`Failed to create a new order ${e}`); setWorking(false); }, - } + }, + RELAY_ASSET_ID ); } catch (e: any) { toastError(`Failed to create a new order. ${e.toString()}`); diff --git a/src/components/Modals/Regions/Purchase/index.tsx b/src/components/Modals/Regions/Purchase/index.tsx index 2e6a66d9..5f4618b2 100644 --- a/src/components/Modals/Regions/Purchase/index.tsx +++ b/src/components/Modals/Regions/Purchase/index.tsx @@ -13,7 +13,7 @@ import { ApiState } from '@/contexts/apis/types'; import { useMarket } from '@/contexts/market'; import { useRegions } from '@/contexts/regions'; import { useToast } from '@/contexts/toast'; -import { Listing } from '@/models'; +import { Listing, RELAY_ASSET_ID } from '@/models'; interface PurchaseModalProps { open: boolean; @@ -86,6 +86,7 @@ export const PurchaseModal = ({ toastError('Failed to purchase the region'); }, error: (e) => { + console.log(e); toastError( `Failed to purchase the region. Error: ${ e.errorMessage === 'Error' @@ -95,7 +96,8 @@ export const PurchaseModal = ({ ); setWorking(false); }, - } + }, + RELAY_ASSET_ID ); }; @@ -113,6 +115,9 @@ export const PurchaseModal = ({ pb: '1rem', }} > + purchaseRegion()} variant='contained' @@ -120,9 +125,6 @@ export const PurchaseModal = ({ > Purchase - ); diff --git a/src/components/Modals/Regions/Sell/index.tsx b/src/components/Modals/Regions/Sell/index.tsx index 8e47bbda..49b11051 100644 --- a/src/components/Modals/Regions/Sell/index.tsx +++ b/src/components/Modals/Regions/Sell/index.tsx @@ -22,7 +22,7 @@ import { ApiState } from '@/contexts/apis/types'; import { useMarket } from '@/contexts/market'; import { useRegions } from '@/contexts/regions'; import { useToast } from '@/contexts/toast'; -import { RegionMetadata } from '@/models'; +import { RELAY_ASSET_ID, RegionMetadata } from '@/models'; interface SellModalProps { open: boolean; @@ -119,7 +119,8 @@ export const SellModal = ({ ); setWorking(false); }, - } + }, + RELAY_ASSET_ID ); }; diff --git a/src/components/Regions/IsmpRegionCard/index.tsx b/src/components/Regions/IsmpRegionCard/index.tsx index 04ee9725..7b04990c 100644 --- a/src/components/Regions/IsmpRegionCard/index.tsx +++ b/src/components/Regions/IsmpRegionCard/index.tsx @@ -23,7 +23,7 @@ import { useRegionXApi } from '@/contexts/apis/RegionXApi'; import { ApiState } from '@/contexts/apis/types'; import { useRegions } from '@/contexts/regions'; import { useToast } from '@/contexts/toast'; -import { ISMPRecordStatus, RegionMetadata } from '@/models'; +import { ISMPRecordStatus, RELAY_ASSET_ID, RegionMetadata } from '@/models'; import styles from './index.module.scss'; @@ -199,7 +199,8 @@ export const IsmpRegionCard = ({ regionMetadata }: IsmpRegionProps) => { error: (e) => { toastError(`Failed to request the region record. ${e}`); }, - } + }, + RELAY_ASSET_ID ); }; diff --git a/src/contexts/apis/RegionXApi/index.tsx b/src/contexts/apis/RegionXApi/index.tsx index 37afd653..f05d6106 100644 --- a/src/contexts/apis/RegionXApi/index.tsx +++ b/src/contexts/apis/RegionXApi/index.tsx @@ -78,6 +78,16 @@ const customRpc = { }, }; +const signedExtensions = { + ChargeAssetTxPayment: { + extrinsic: { + tip: 'Compact', + assetId: 'Option', + }, + payload: {}, + }, +}; + const defaultValue = { state: { ...initialState }, disconnectRegionX: (): void => { @@ -107,7 +117,15 @@ const RegionXApiContextProvider = (props: any) => { // // For this reason we don't have different urls based on the network. However, this // should be updated once this is in production. - connect(state, WS_REGIONX_COCOS_CHAIN, dispatch, false, types, customRpc); + connect( + state, + WS_REGIONX_COCOS_CHAIN, + dispatch, + false, + types, + customRpc, + signedExtensions + ); }, [network, state]); useEffect(() => { diff --git a/src/contexts/apis/common.ts b/src/contexts/apis/common.ts index 61a6446b..6b2f5fb7 100644 --- a/src/contexts/apis/common.ts +++ b/src/contexts/apis/common.ts @@ -94,7 +94,8 @@ export const connect = ( dispatch: any, newSocket: boolean, types?: any, - customRpc?: any + customRpc?: any, + signedExtensions?: any ) => { const { apiState, jsonrpc } = state; @@ -106,6 +107,7 @@ export const connect = ( provider, rpc: { ...jsonrpc, ...customRpc }, types, + signedExtensions, }); dispatch({ type: 'CONNECT_INIT', socket }); diff --git a/src/hooks/submitExtrinsic.ts b/src/hooks/submitExtrinsic.ts index 6570e508..f6518508 100644 --- a/src/hooks/submitExtrinsic.ts +++ b/src/hooks/submitExtrinsic.ts @@ -4,7 +4,8 @@ import { useConfirm } from 'material-ui-confirm'; import { getBalanceString, sendTx } from '@/utils/functions'; -import { TxStatusHandlers } from '@/models'; +import { NATIVE_ASSET_ID, TxStatusHandlers } from '@/models'; +import { numberToU8a } from '@polkadot/util'; export const useSubmitExtrinsic = () => { const confirm = useConfirm(); @@ -15,9 +16,14 @@ export const useSubmitExtrinsic = () => { tx: SubmittableExtrinsic<'promise', ISubmittableResult>, account: AddressOrPair, signer: Signer, - handlers: TxStatusHandlers + handlers: TxStatusHandlers, + feePaymentAsset: number = NATIVE_ASSET_ID ) => { - const info = await tx.paymentInfo(account.toString()); + console.log(feePaymentAsset); + const info = await tx.paymentInfo( + account.toString(), + feePaymentAsset == NATIVE_ASSET_ID ? {} : { assetId: numberToU8a(feePaymentAsset) } + ); const { partialFee } = info.toPrimitive() as any; confirm({ description: `Estimated gas fee: ${getBalanceString( @@ -25,7 +31,7 @@ export const useSubmitExtrinsic = () => { decimals, symbol )}`, - }).then(() => sendTx(tx, account, signer, handlers)); + }).then(() => sendTx(tx, account, signer, handlers, feePaymentAsset)); }; return { submitExtrinsicWithFeeInfo }; diff --git a/src/models/common/consts.ts b/src/models/common/consts.ts index d1bfa492..e740bf33 100644 --- a/src/models/common/consts.ts +++ b/src/models/common/consts.ts @@ -25,3 +25,6 @@ export const SAFE_XCM_VERSION = 3; export const BROKER_PALLET_ID = 50; export const POOLING_TASK_ID = 0; + +export const NATIVE_ASSET_ID = 0; +export const RELAY_ASSET_ID = 1; diff --git a/src/pages/marketplace.tsx b/src/pages/marketplace.tsx index 94d0db5a..469e122e 100644 --- a/src/pages/marketplace.tsx +++ b/src/pages/marketplace.tsx @@ -29,7 +29,12 @@ import { useRegionXApi } from '@/contexts/apis/RegionXApi'; import { ApiState } from '@/contexts/apis/types'; import { useMarket } from '@/contexts/market'; import { useToast } from '@/contexts/toast'; -import { ContextStatus, Listing, MarketFilterOptions } from '@/models'; +import { + ContextStatus, + Listing, + MarketFilterOptions, + RELAY_ASSET_ID, +} from '@/models'; // eslint-disable-next-line no-unused-vars enum SortOption { @@ -130,7 +135,8 @@ const Marketplace = () => { ); setWorking(false); }, - } + }, + RELAY_ASSET_ID ); }; diff --git a/src/utils/functions/api.ts b/src/utils/functions/api.ts index b85a8c54..5048ea38 100644 --- a/src/utils/functions/api.ts +++ b/src/utils/functions/api.ts @@ -1,18 +1,25 @@ import { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'; import { ISubmittableResult, Signer } from '@polkadot/types/types'; -import { TxStatusHandlers } from '@/models'; +import { NATIVE_ASSET_ID, TxStatusHandlers } from '@/models'; +import { numberToU8a } from '@polkadot/util'; export const sendTx = async ( tx: SubmittableExtrinsic<'promise', ISubmittableResult>, account: AddressOrPair, signer: Signer, - handlers: TxStatusHandlers + handlers: TxStatusHandlers, + feePaymentAsset: number = NATIVE_ASSET_ID ) => { + const options = + feePaymentAsset == NATIVE_ASSET_ID + ? { signer } + : { signer, assetId: numberToU8a(feePaymentAsset) }; + console.log(options); try { const unsub = await tx.signAndSend( account, - { signer }, + options, ({ status, events }) => { if (status.isReady) handlers.ready(); else if (status.isInBlock) handlers.inBlock(); From f1e7600c40aecf953e81cccd291ba20d0874a546 Mon Sep 17 00:00:00 2001 From: Sergej Date: Thu, 4 Jul 2024 13:46:57 +0200 Subject: [PATCH 2/2] progress --- package.json | 6 +++--- src/components/Modals/Regions/Purchase/index.tsx | 8 +++----- src/utils/functions/api.ts | 7 ++++--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index b23c881c..9bd9a488 100644 --- a/package.json +++ b/package.json @@ -22,11 +22,11 @@ "@mui/lab": "5.0.0-alpha.134", "@mui/material": "^5.15.14", "@mui/x-date-pickers": "^6.19.5", - "@polkadot/api": "11.2.1", + "@polkadot/api": "12.0.2", "@polkadot/extension-dapp": "0.47.3", "@polkadot/extension-inject": "0.47.3", "@polkadot/react-identicon": "^3.6.6", - "@polkadot/types": "11.2.1", + "@polkadot/types": "12.0.2", "@polkadot/ui-keyring": "3.6.6", "@polkadot/util": "12.6.2", "@polkadot/util-crypto": "^12.6.2", @@ -79,4 +79,4 @@ "typescript": "^4.7.4", "webpack": "^5.81.0" } -} +} \ No newline at end of file diff --git a/src/components/Modals/Regions/Purchase/index.tsx b/src/components/Modals/Regions/Purchase/index.tsx index 5f4618b2..acdb7105 100644 --- a/src/components/Modals/Regions/Purchase/index.tsx +++ b/src/components/Modals/Regions/Purchase/index.tsx @@ -86,12 +86,10 @@ export const PurchaseModal = ({ toastError('Failed to purchase the region'); }, error: (e) => { - console.log(e); toastError( - `Failed to purchase the region. Error: ${ - e.errorMessage === 'Error' - ? 'Please check your balance.' - : e.errorMessage + `Failed to purchase the region. Error: ${e.errorMessage === 'Error' + ? 'Please check your balance.' + : e.errorMessage }` ); setWorking(false); diff --git a/src/utils/functions/api.ts b/src/utils/functions/api.ts index 5048ea38..e45b36e0 100644 --- a/src/utils/functions/api.ts +++ b/src/utils/functions/api.ts @@ -2,7 +2,7 @@ import { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types'; import { ISubmittableResult, Signer } from '@polkadot/types/types'; import { NATIVE_ASSET_ID, TxStatusHandlers } from '@/models'; -import { numberToU8a } from '@polkadot/util'; +import { numberToHex, numberToU8a } from '@polkadot/util'; export const sendTx = async ( tx: SubmittableExtrinsic<'promise', ISubmittableResult>, @@ -14,8 +14,8 @@ export const sendTx = async ( const options = feePaymentAsset == NATIVE_ASSET_ID ? { signer } - : { signer, assetId: numberToU8a(feePaymentAsset) }; - console.log(options); + : { signer, assetId: 1, withSignedTransaction: true }; + try { const unsub = await tx.signAndSend( account, @@ -37,6 +37,7 @@ export const sendTx = async ( } ); } catch (e) { + console.log(e); handlers.error(e); } finally { handlers.finally && handlers.finally();