// Copyright 2017-2023 @polkadot/api-contract authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { SubmittableResult } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ApiTypes } from '@polkadot/api/types'; import type { WeightV1, WeightV2 } from '@polkadot/types/interfaces'; import type { BN } from '@polkadot/util'; import type { AbiConstructor, AbiMessage, BlueprintOptions, WeightAll } from '../types'; import type { BlueprintDeploy, ContractGeneric } from './types'; import { Bytes } from '@polkadot/types'; import { bnToBn, compactAddLength, u8aToU8a } from '@polkadot/util'; import { randomAsU8a } from '@polkadot/util-crypto'; export const EMPTY_SALT = new Uint8Array(); export function withMeta (meta: AbiMessage, creator: Omit): T { (creator as T).meta = meta; return creator as T; } export function createBluePrintTx (meta: AbiMessage, fn: (options: BlueprintOptions, params: unknown[]) => SubmittableExtrinsic): BlueprintDeploy { return withMeta(meta, (options: BlueprintOptions, ...params: unknown[]): SubmittableExtrinsic => fn(options, params) ); } export function createBluePrintWithId (fn: (constructorOrId: AbiConstructor | string | number, options: BlueprintOptions, params: unknown[]) => T): ContractGeneric { return (constructorOrId: AbiConstructor | string | number, options: BlueprintOptions, ...params: unknown[]): T => fn(constructorOrId, options, params); } export function encodeSalt (salt: Uint8Array | string | null = randomAsU8a()): Uint8Array { return salt instanceof Bytes ? salt : salt && salt.length ? compactAddLength(u8aToU8a(salt)) : EMPTY_SALT; } export function convertWeight (weight: WeightV1 | WeightV2 | bigint | string | number | BN): WeightAll { const [refTime, proofSize] = isWeightV2(weight) ? [weight.refTime.toBn(), weight.proofSize.toBn()] : [bnToBn(weight), undefined]; return { v1Weight: refTime, v2Weight: { proofSize, refTime } }; } export function isWeightV2 (weight: WeightV1 | WeightV2 | bigint | string | number | BN): weight is WeightV2 { return !!(weight as WeightV2).proofSize; }