@neo-one/smart-contract-codegen-esnext-esm
Version:
NEO•ONE TypeScript smart contract codegen.
44 lines (42 loc) • 1.87 kB
JavaScript
import { utils } from '@neo-one/utils-esnext-esm';
export const toTypeScriptType = (abi, { isParameter, includeOptional = true, migration = false }) => {
const addOptional = (value) => (includeOptional && abi.optional ? `${value} | undefined` : value);
const addMigration = (value) => (migration ? `(${value} | Promise<${value}>)` : value);
const addOptions = (value) => addMigration(addOptional(value));
switch (abi.type) {
case 'Signature':
return addOptions('SignatureString');
case 'Boolean':
return addOptions('boolean');
case 'Address':
return addOptions('AddressString');
case 'Hash256':
return addOptions('Hash256String');
case 'Buffer':
return addOptions('BufferString');
case 'PublicKey':
return addOptions('PublicKeyString');
case 'String':
return addOptions('string');
case 'Array':
return addOptions(`Array<${toTypeScriptType(abi.value, { isParameter })}>`);
case 'Map':
return addOptions(`Map<${toTypeScriptType(abi.key, { isParameter })}, ${toTypeScriptType(abi.value, { isParameter })}>`);
case 'Object':
return addOptions(`{
${Object.entries(abi.properties)
.reduce((acc, [key, val]) => acc.concat([`readonly '${key}': ${toTypeScriptType(val, { isParameter })}`]), [])
.join('\n')}
}`);
case 'Void':
return 'undefined';
case 'Integer':
return addOptions('BigNumber');
case 'ForwardValue':
return isParameter ? addOptions('ForwardValue') : addOptions('ContractParameter');
default:
utils.assertNever(abi);
throw new Error('Something went wrong');
}
};
//# sourceMappingURL=toTypeScriptType.js.map