Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
feat: add helper and core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleOTheven authored and Sam Ruberti committed Jun 7, 2023
1 parent 28cc585 commit abc0860
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions core/contracts/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
30 changes: 30 additions & 0 deletions core/contracts/txPaymentInfo.ts
Original file line number Diff line number Diff line change
@@ -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<SignerOptions>,
): Promise<RuntimeDispatchInfo | undefined> {
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;
}
}
3 changes: 2 additions & 1 deletion react/hooks/contracts/useEvents.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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[];
removeEvent: (p: RemoveEventPayload) => void;
}

export const useEvents = (
contractAddress: string | undefined,
contractAddress: AccountId | string | undefined,
filters?: string[],
): Events => {
const { events, removeEvent } = useContext(EventsContext);
Expand Down
22 changes: 14 additions & 8 deletions react/hooks/contracts/useTxPaymentInfo.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -27,21 +31,23 @@ export function useTxPaymentInfo(
const defaultCaller = useDefaultCaller();

const send = useCallback<Send>(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);
Expand Down
1 change: 1 addition & 0 deletions utils/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
7 changes: 7 additions & 0 deletions utils/substrate/bnToBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ApiBase, Balance } from '../../core/mod.ts';
import { BN } from '../mod.ts';

export const bnToBalance = (
api: Pick<ApiBase<'promise'>, 'createType'> | undefined,
bn: BN | undefined,
): Balance | undefined => api?.createType('Balance', bn);
1 change: 1 addition & 0 deletions utils/substrate/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './bnToBalance.ts';

0 comments on commit abc0860

Please sign in to comment.