UNPKG

@neo-one/smart-contract-codegen-esnext-esm

Version:

NEO•ONE TypeScript smart contract codegen.

44 lines (42 loc) 2.14 kB
import { toTypeScriptType } from '../utils'; import { genFunctionParameters } from './genFunctionParameters'; import { getEventName } from './getEventName'; import { hasForward } from './hasForward'; const getFunctionReturnReceipt = (name, abi) => { if (abi.claim) { return 'TransactionReceipt'; } const eventName = getEventName(name); const event = hasForward(abi) ? `TForwardOptions extends ForwardOptions<infer T> ? ${eventName} | T : ${eventName}` : eventName; return `InvokeReceipt<${toTypeScriptType(abi.returnType, { isParameter: false })}, ${event}>`; }; const getFunctionReturnTransaction = (abi) => (abi.claim ? 'ClaimTransaction' : 'InvocationTransaction'); const getFunctionReturnType = (name, abi) => `TransactionResult<${getFunctionReturnReceipt(name, abi)}, ${getFunctionReturnTransaction(abi)}>`; const getForwardType = (abi) => (hasForward(abi) ? '<TForwardOptions extends ForwardOptions<any>>' : ''); const getFunctionType = (name, abi, migration = false) => genFunctionParameters(abi, abi.parameters, { migration, }) .map((params) => `${getForwardType(abi)}(${params}): Promise<${getFunctionReturnType(name, abi)}>;`) .join(' \n'); const createConfirmedFunc = (abi, params, name, arrow = false) => `${getForwardType(abi)}(${params})${arrow ? ' =>' : ':'} Promise<${getFunctionReturnReceipt(name, abi)} & { readonly transaction: ${getFunctionReturnTransaction(abi)}}>;`; const getConfirmedType = (name, abi, migration = false) => { const paramss = genFunctionParameters(abi, abi.parameters, { withConfirmedOptions: true, migration, }); if (paramss.length === 1) { return createConfirmedFunc(abi, paramss[0], name, true); } return `{ ${paramss.map((params) => createConfirmedFunc(abi, params, name)).join(' \n')} }`; }; export const genFunction = (name, abi, options) => options.migration ? getConfirmedType(name, abi, options.migration) : `{ ${getFunctionType(name, abi, options.migration)} readonly confirmed: ${getConfirmedType(name, abi, options.migration)} }`; //# sourceMappingURL=genFunction.js.map