From abc0860da9b4eb7eef076f3e350de055c75a4f50 Mon Sep 17 00:00:00 2001 From: Sam Ruberti Date: Wed, 7 Jun 2023 17:33:58 -0400 Subject: [PATCH] feat: add helper and core functions --- core/contracts/mod.ts | 1 + core/contracts/txPaymentInfo.ts | 30 +++++++++++++++++++++++ react/hooks/contracts/useEvents.ts | 3 ++- react/hooks/contracts/useTxPaymentInfo.ts | 22 +++++++++++------ utils/mod.ts | 1 + utils/substrate/bnToBalance.ts | 7 ++++++ utils/substrate/mod.ts | 1 + 7 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 core/contracts/txPaymentInfo.ts create mode 100644 utils/substrate/bnToBalance.ts create mode 100644 utils/substrate/mod.ts diff --git a/core/contracts/mod.ts b/core/contracts/mod.ts index 07c66e0..48f9aa9 100644 --- a/core/contracts/mod.ts +++ b/core/contracts/mod.ts @@ -4,4 +4,5 @@ export * from './decodeError.ts'; export * from './getRegistryError.ts'; export * from './toContractAbiMessage.ts'; export * from './toRegistryErrorDecoded.ts'; +export * from './txPaymentInfo.ts'; export * from './types.ts'; diff --git a/core/contracts/txPaymentInfo.ts b/core/contracts/txPaymentInfo.ts new file mode 100644 index 0000000..c6b503c --- /dev/null +++ b/core/contracts/txPaymentInfo.ts @@ -0,0 +1,30 @@ +import { + AccountId, + ContractOptions, + ContractPromise, + RuntimeDispatchInfo, + SignerOptions, +} from '../mod.ts'; + +export async function txPaymentInfo( + contract: ContractPromise | undefined, + message: string, + caller: AccountId | string, + params?: unknown[], + options?: ContractOptions, + signerOptions?: Partial, +): Promise { + const tx = contract?.tx?.[message]; + if (!tx || !caller) return; + + try { + const requiresNoArguments = tx.meta.args.length === 0; + return await (requiresNoArguments + ? tx(options || {}) + : tx(options || {}, params)) + .paymentInfo(caller, signerOptions); + } catch (e: unknown) { + console.error(e); + return; + } +} diff --git a/react/hooks/contracts/useEvents.ts b/react/hooks/contracts/useEvents.ts index 7eb3a61..0c892b6 100644 --- a/react/hooks/contracts/useEvents.ts +++ b/react/hooks/contracts/useEvents.ts @@ -1,6 +1,7 @@ import { useContext, useMemo } from 'react'; import { Event, EventsContext } from '../../providers/events/mod.ts'; import { RemoveEventPayload } from '../../providers/events/model.ts'; +import { AccountId } from '../../../core/mod.ts'; export interface Events { events: Event[]; @@ -8,7 +9,7 @@ export interface Events { } export const useEvents = ( - contractAddress: string | undefined, + contractAddress: AccountId | string | undefined, filters?: string[], ): Events => { const { events, removeEvent } = useContext(EventsContext); diff --git a/react/hooks/contracts/useTxPaymentInfo.ts b/react/hooks/contracts/useTxPaymentInfo.ts index d61d303..046106f 100644 --- a/react/hooks/contracts/useTxPaymentInfo.ts +++ b/react/hooks/contracts/useTxPaymentInfo.ts @@ -1,6 +1,10 @@ import { useCallback, useState } from 'react'; import { useWallet } from '../wallets/useWallet.ts'; -import { RuntimeDispatchInfo, SignerOptions } from '../../../core/mod.ts'; +import { + RuntimeDispatchInfo, + SignerOptions, + txPaymentInfo, +} from '../../../core/mod.ts'; import { CallOptions } from './types.ts'; import { ChainContract, useDefaultCaller } from '../mod.ts'; @@ -27,21 +31,23 @@ export function useTxPaymentInfo( const defaultCaller = useDefaultCaller(); const send = useCallback(async (params, options, signerOptions) => { - const tx = chainContract?.contract?.tx?.[message]; const caller = account?.address || options?.defaultCaller ? defaultCaller : undefined; - if (!tx || !caller) return; + if (!chainContract?.contract || !caller) return; try { setIsSubmitting(true); - const requiresNoArguments = tx.meta.args.length === 0; - const paymentInfoResult = await (requiresNoArguments - ? tx(options || {}) - : tx(options || {}, params)) - .paymentInfo(caller, signerOptions); + const paymentInfoResult = await txPaymentInfo( + chainContract.contract, + message, + caller, + params, + options, + signerOptions, + ); setResult(paymentInfoResult); setIsSubmitting(false); diff --git a/utils/mod.ts b/utils/mod.ts index bb7c1e5..e0500b0 100644 --- a/utils/mod.ts +++ b/utils/mod.ts @@ -3,4 +3,5 @@ export * from '@polkadot/util'; export * from './contracts/mod.ts'; export * from './helpers/mod.ts'; export * from './pick/mod.ts'; +export * from './substrate/mod.ts'; export * from './types/mod.ts'; diff --git a/utils/substrate/bnToBalance.ts b/utils/substrate/bnToBalance.ts new file mode 100644 index 0000000..0e04b57 --- /dev/null +++ b/utils/substrate/bnToBalance.ts @@ -0,0 +1,7 @@ +import { ApiBase, Balance } from '../../core/mod.ts'; +import { BN } from '../mod.ts'; + +export const bnToBalance = ( + api: Pick, 'createType'> | undefined, + bn: BN | undefined, +): Balance | undefined => api?.createType('Balance', bn); diff --git a/utils/substrate/mod.ts b/utils/substrate/mod.ts new file mode 100644 index 0000000..e93a98d --- /dev/null +++ b/utils/substrate/mod.ts @@ -0,0 +1 @@ +export * from './bnToBalance.ts';