UNPKG

@gear-js/api

Version:

A JavaScript library that provides functionality to connect GEAR Component APIs.

50 lines (47 loc) 1.91 kB
import { u8aToBigInt, BN } from '@polkadot/util'; import { u128, u64 } from '@polkadot/types'; import { ValidationError } from '../errors/validation.errors.js'; function validateValue(value, api) { if (!value) return; const existentialDeposit = api.existentialDeposit; const bigintValue = value instanceof Uint8Array ? u8aToBigInt(value) : value instanceof u128 || value instanceof BN ? BigInt(value.toString()) : BigInt(value); if (bigintValue > 0 && bigintValue < existentialDeposit.toBigInt()) { throw new ValidationError(`Value less than minimal. Minimal value: ${existentialDeposit.toHuman()}`); } } function validateGasLimit(gas, api) { if (gas === undefined) throw new ValidationError("Gas limit doesn't specified"); const bigintGas = gas instanceof Uint8Array ? u8aToBigInt(gas) : gas instanceof u64 || gas instanceof BN ? BigInt(gas.toString()) : BigInt(gas); if (bigintGas > api.blockGasLimit.toBigInt()) { throw new ValidationError(`GasLimit too high. Maximum gasLimit value is ${api.blockGasLimit.toHuman()}`); } } async function validateCodeId(codeId, api) { if (await api.code.exists(codeId)) { throw new ValidationError('Code already exists'); } } async function validateProgramId(programId, api) { const isExist = await api.program.exists(programId); if (!isExist) { throw new ValidationError(`Program with id ${programId} doesn't exist`); } } async function validateMailboxItem(account, messageId, api) { const mailbox = await api.mailbox.read(account, messageId); if (!mailbox) { throw new Error(`There is no message with id ${messageId} in the mailbox`); } return mailbox[0]; } export { validateCodeId, validateGasLimit, validateMailboxItem, validateProgramId, validateValue };