New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wharfkit/contract

Package Overview
Dependencies
Maintainers
3
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wharfkit/contract - npm Package Compare versions

Comparing version 0.3.3 to 0.3.4

4

lib/contract.d.ts

@@ -213,4 +213,2 @@ import { API, ABIDef, APIClient, ABI, NameType, Name, BytesType, ABISerializableObject, PermissionLevelType, Action, Blob } from '@wharfkit/antelope';

declare function codegen(contractName: any, abi: any): Promise<string>;
interface ContractKitArgs {

@@ -249,2 +247,2 @@ client: APIClient;

export { ABIDefinition, ActionArgs, ActionDataType, ActionOptions, Contract, ContractArgs, ContractKit, ContractKitArgs, ContractKitOptions, GetTableRowsOptions, PartialBy, QueryParams, Table, TableCursor, TableCursorArgs, TableRowParamsTypes, abiToBlob, blobStringToAbi, capitalize, codegen, ContractKit as default, indexPositionInWords, pascalCase, singularize, wrapIndexValue };
export { ABIDefinition, ActionArgs, ActionDataType, ActionOptions, Contract, ContractArgs, ContractKit, ContractKitArgs, ContractKitOptions, GetTableRowsOptions, PartialBy, QueryParams, Table, TableCursor, TableCursorArgs, TableRowParamsTypes, abiToBlob, blobStringToAbi, capitalize, ContractKit as default, indexPositionInWords, pascalCase, singularize, wrapIndexValue };

@@ -8,25 +8,4 @@ 'use strict';

var tslib = require('tslib');
var ts = require('typescript');
var abicache = require('@wharfkit/abicache');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var ts__namespace = /*#__PURE__*/_interopNamespace(ts);
function pascalCase(value) {

@@ -367,238 +346,2 @@ return value

const EOSIO_CORE_CLASSES = [
'Asset',
'Checksum256',
'Float64',
'Name',
'TimePoint',
'TimePointSec',
'UInt128',
'UInt16',
'UInt32',
'UInt64',
'UInt8',
];
const EOSIO_CORE_TYPES = [
'AssetType',
'Blob',
'Checksum256Type',
'Float64Type',
'NameType',
'TimePointType',
'UInt128Type',
'UInt16Type',
'UInt32Type',
'UInt64Type',
'UInt8Type',
];
function generateClassDeclaration(name, members, options = {}) {
const heritageClauses = [];
if (options.parent) {
heritageClauses.push(ts__namespace.factory.createHeritageClause(ts__namespace.SyntaxKind.ExtendsKeyword, [
ts__namespace.factory.createExpressionWithTypeArguments(ts__namespace.factory.createIdentifier(options.parent), undefined),
]));
}
const modifiers = [];
if (options.export === true) {
modifiers.push(ts__namespace.factory.createToken(ts__namespace.SyntaxKind.ExportKeyword));
}
if (options.decorator) {
modifiers.push(options.decorator);
}
const classDeclaration = ts__namespace.factory.createClassDeclaration(modifiers, ts__namespace.factory.createIdentifier(name), undefined,
heritageClauses, members);
return classDeclaration;
}
function generateImportStatement(classes, path) {
return ts__namespace.factory.createImportDeclaration(undefined,
ts__namespace.factory.createImportClause(false,
undefined,
ts__namespace.factory.createNamedImports(classes.map((className) => ts__namespace.factory.createImportSpecifier(false, undefined,
ts__namespace.factory.createIdentifier(className)
)))
), ts__namespace.factory.createStringLiteral(path)
);
}
function generateStruct(structName, isExport = false, members = []) {
const decorators = [
ts__namespace.factory.createDecorator(ts__namespace.factory.createCallExpression(ts__namespace.factory.createIdentifier('Struct.type'), undefined, [
ts__namespace.factory.createStringLiteral(structName),
])),
];
return ts__namespace.factory.createClassDeclaration(isExport
? [...decorators, ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.ExportKeyword)]
: decorators, ts__namespace.factory.createIdentifier(capitalize(structName)), undefined,
[
ts__namespace.factory.createHeritageClause(ts__namespace.SyntaxKind.ExtendsKeyword, [
ts__namespace.factory.createExpressionWithTypeArguments(ts__namespace.factory.createIdentifier('Struct'), []),
]),
],
members
);
}
function generateField(field, isExport = false, namespace, abi) {
const fieldName = field.name.toLowerCase();
const decorators = [
ts__namespace.factory.createDecorator(ts__namespace.factory.createCallExpression(ts__namespace.factory.createIdentifier('Struct.field'), undefined, [ts__namespace.factory.createStringLiteral(field.type)])),
];
return ts__namespace.factory.createPropertyDeclaration(isExport
? [...decorators, ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.DeclareKeyword)]
: decorators, ts__namespace.factory.createIdentifier(fieldName),
undefined,
ts__namespace.factory.createTypeReferenceNode(findInternalType(field.type, namespace, abi)),
undefined
);
}
function getFieldTypesFromAbi(abi) {
const structTypes = [];
if (abi && abi.structs) {
for (const struct of abi.structs) {
const fields = [];
for (const field of struct.fields) {
fields.push({
name: field.name.charAt(0).toUpperCase() + field.name.slice(1),
type: field.type,
});
}
structTypes.push({ structName: struct.name, fields });
}
}
return structTypes;
}
function findCoreClass(type) {
for (const coreType of EOSIO_CORE_CLASSES) {
if (type.split('_').join('') === coreType.toLowerCase()) {
return coreType;
}
}
}
function findInternalType(type, namespace, abi) {
let typeString = removeDecorators(type);
const relevantAbitype = findAbiType(typeString, abi);
if (relevantAbitype) {
typeString = relevantAbitype;
}
const variantType = findVariantType(typeString, namespace, abi);
if (variantType) {
typeString = variantType;
}
return formatInternalType(typeString, namespace, abi);
}
function formatInternalType(typeString, namespace, abi) {
const structNames = abi.structs.map((struct) => struct.name.toLowerCase());
if (structNames.includes(typeString.toLowerCase())) {
return `${namespace}.${capitalize(typeString)}`;
}
else {
return findCoreClass(typeString) || capitalize(typeString);
}
}
function findVariantType(typeString, namespace, abi) {
const abiVariant = abi.variants.find((variant) => variant.name.toLowerCase() === typeString.toLowerCase());
if (!abiVariant) {
return;
}
return abiVariant.types
.map((variant) => formatInternalType(variant, namespace, abi))
.join(' | ');
}
function findAbiType(typeString, abi) {
var _a;
return (_a = abi.types.find((abiType) => abiType.new_type_name.toLowerCase() === typeString.toLowerCase())) === null || _a === void 0 ? void 0 : _a.type;
}
const decorators = ['?', '[]'];
function removeDecorators(type) {
for (const decorator of decorators) {
if (type.includes(decorator)) {
type = type.replace(decorator, '');
break;
}
}
return type;
}
function generateNamespaceName(contractName) {
return contractName
.split('.')
.map((namePart) => capitalize(namePart))
.join('');
}
function generateNamespace(namespaceName, children, isExport = true) {
return ts__namespace.factory.createModuleDeclaration(isExport ? [ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.ExportKeyword)] : [],
ts__namespace.factory.createIdentifier(namespaceName), ts__namespace.factory.createModuleBlock(children), ts__namespace.NodeFlags.Namespace);
}
function generateContractClass(namespaceName, contractName) {
return tslib.__awaiter(this, void 0, void 0, function* () {
const classMembers = [];
const constructorParams = [
ts__namespace.factory.createParameterDeclaration(undefined, undefined, undefined, 'args', undefined, ts__namespace.factory.createTypeReferenceNode(ts__namespace.factory.createIdentifier('PartialBy'), [
ts__namespace.factory.createTypeReferenceNode(ts__namespace.factory.createIdentifier('ContractArgs'), undefined),
ts__namespace.factory.createUnionTypeNode([
ts__namespace.factory.createLiteralTypeNode(ts__namespace.factory.createStringLiteral('abi')),
ts__namespace.factory.createLiteralTypeNode(ts__namespace.factory.createStringLiteral('account')),
]),
]), undefined),
];
const constructorBody = ts__namespace.factory.createBlock([
ts__namespace.factory.createExpressionStatement(ts__namespace.factory.createCallExpression(ts__namespace.factory.createSuper(), undefined, [
ts__namespace.factory.createObjectLiteralExpression([
ts__namespace.factory.createPropertyAssignment('client', ts__namespace.factory.createPropertyAccessExpression(ts__namespace.factory.createIdentifier('args'), 'client')),
ts__namespace.factory.createPropertyAssignment('abi', ts__namespace.factory.createIdentifier('abi')),
ts__namespace.factory.createPropertyAssignment('account', ts__namespace.factory.createCallExpression(ts__namespace.factory.createPropertyAccessExpression(ts__namespace.factory.createIdentifier('Name'), 'from'), undefined, [ts__namespace.factory.createStringLiteral(contractName)])),
], true),
])),
], true);
const constructorMember = ts__namespace.factory.createConstructorDeclaration(undefined, undefined, constructorParams, constructorBody);
classMembers.push(constructorMember);
const classDeclaration = generateClassDeclaration('Contract', classMembers, {
parent: 'BaseContract',
export: true,
});
return { classDeclaration };
});
}
const printer = ts__namespace.createPrinter();
function codegen(contractName, abi) {
return tslib.__awaiter(this, void 0, void 0, function* () {
const namespaceName = generateNamespaceName(contractName);
const importCoreStatement = generateImportStatement([
'ABI',
'APIClient',
'Session',
'Struct',
'TransactResult',
...EOSIO_CORE_CLASSES,
...EOSIO_CORE_TYPES,
], '@wharfkit/session');
const importContractStatement = generateImportStatement(['Contract as BaseContract', 'ContractArgs', 'PartialBy', 'blobStringToAbi'], '@wharfkit/contract');
const { classDeclaration } = yield generateContractClass(namespaceName, contractName);
const structs = getFieldTypesFromAbi(abi);
const structDeclarations = [];
for (const struct of structs) {
const structMembers = [];
for (const field of struct.fields) {
structMembers.push(generateField(field, true, `${namespaceName}.Types`, abi));
}
structDeclarations.push(generateStruct(struct.structName, true, structMembers));
}
const abiBlob = abiToBlob(abi);
const abiBlobField = ts__namespace.factory.createVariableStatement([ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.ExportKeyword)], ts__namespace.factory.createVariableDeclarationList([
ts__namespace.factory.createVariableDeclaration('abiBlob', undefined, undefined, ts__namespace.factory.createCallExpression(ts__namespace.factory.createPropertyAccessExpression(ts__namespace.factory.createIdentifier('Blob'), ts__namespace.factory.createIdentifier('from')), undefined, [ts__namespace.factory.createStringLiteral(String(abiBlob))])),
], ts__namespace.NodeFlags.Const));
const abiField = ts__namespace.factory.createVariableStatement([ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.ExportKeyword)], ts__namespace.factory.createVariableDeclarationList([
ts__namespace.factory.createVariableDeclaration('abi', undefined, undefined, ts__namespace.factory.createCallExpression(ts__namespace.factory.createPropertyAccessExpression(ts__namespace.factory.createIdentifier('ABI'), ts__namespace.factory.createIdentifier('from')), undefined, [ts__namespace.factory.createIdentifier('abiBlob')])),
], ts__namespace.NodeFlags.Const));
const namespaceDeclaration = generateNamespace(namespaceName, [
abiBlobField,
abiField,
classDeclaration,
generateNamespace('Types', structDeclarations),
]);
const sourceFile = ts__namespace.factory.createSourceFile([importContractStatement, importCoreStatement, namespaceDeclaration], ts__namespace.factory.createToken(ts__namespace.SyntaxKind.EndOfFileToken), ts__namespace.NodeFlags.None);
return printer.printFile(sourceFile);
});
}
const defaultContractKitOptions = {};

@@ -643,3 +386,2 @@ class ContractKit {

exports.capitalize = capitalize;
exports.codegen = codegen;
exports["default"] = ContractKit;

@@ -646,0 +388,0 @@ exports.indexPositionInWords = indexPositionInWords;

import { isInstanceOf, UInt128, UInt64, Float64, Checksum256, Checksum160, Name, Serializer, ABI, Blob, PermissionLevel, Action } from '@wharfkit/antelope';
import { PlaceholderAuth } from '@wharfkit/signing-request';
import * as ts from 'typescript';
import { ABICache } from '@wharfkit/abicache';

@@ -322,233 +321,2 @@

const EOSIO_CORE_CLASSES = [
'Asset',
'Checksum256',
'Float64',
'Name',
'TimePoint',
'TimePointSec',
'UInt128',
'UInt16',
'UInt32',
'UInt64',
'UInt8',
];
const EOSIO_CORE_TYPES = [
'AssetType',
'Blob',
'Checksum256Type',
'Float64Type',
'NameType',
'TimePointType',
'UInt128Type',
'UInt16Type',
'UInt32Type',
'UInt64Type',
'UInt8Type',
];
function generateClassDeclaration(name, members, options = {}) {
const heritageClauses = [];
if (options.parent) {
heritageClauses.push(ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier(options.parent), undefined),
]));
}
const modifiers = [];
if (options.export === true) {
modifiers.push(ts.factory.createToken(ts.SyntaxKind.ExportKeyword));
}
if (options.decorator) {
modifiers.push(options.decorator);
}
const classDeclaration = ts.factory.createClassDeclaration(modifiers, ts.factory.createIdentifier(name), undefined,
heritageClauses, members);
return classDeclaration;
}
function generateImportStatement(classes, path) {
return ts.factory.createImportDeclaration(undefined,
ts.factory.createImportClause(false,
undefined,
ts.factory.createNamedImports(classes.map((className) => ts.factory.createImportSpecifier(false, undefined,
ts.factory.createIdentifier(className)
)))
), ts.factory.createStringLiteral(path)
);
}
function generateStruct(structName, isExport = false, members = []) {
const decorators = [
ts.factory.createDecorator(ts.factory.createCallExpression(ts.factory.createIdentifier('Struct.type'), undefined, [
ts.factory.createStringLiteral(structName),
])),
];
return ts.factory.createClassDeclaration(isExport
? [...decorators, ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)]
: decorators, ts.factory.createIdentifier(capitalize(structName)), undefined,
[
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier('Struct'), []),
]),
],
members
);
}
function generateField(field, isExport = false, namespace, abi) {
const fieldName = field.name.toLowerCase();
const decorators = [
ts.factory.createDecorator(ts.factory.createCallExpression(ts.factory.createIdentifier('Struct.field'), undefined, [ts.factory.createStringLiteral(field.type)])),
];
return ts.factory.createPropertyDeclaration(isExport
? [...decorators, ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword)]
: decorators, ts.factory.createIdentifier(fieldName),
undefined,
ts.factory.createTypeReferenceNode(findInternalType(field.type, namespace, abi)),
undefined
);
}
function getFieldTypesFromAbi(abi) {
const structTypes = [];
if (abi && abi.structs) {
for (const struct of abi.structs) {
const fields = [];
for (const field of struct.fields) {
fields.push({
name: field.name.charAt(0).toUpperCase() + field.name.slice(1),
type: field.type,
});
}
structTypes.push({ structName: struct.name, fields });
}
}
return structTypes;
}
function findCoreClass(type) {
for (const coreType of EOSIO_CORE_CLASSES) {
if (type.split('_').join('') === coreType.toLowerCase()) {
return coreType;
}
}
}
function findInternalType(type, namespace, abi) {
let typeString = removeDecorators(type);
const relevantAbitype = findAbiType(typeString, abi);
if (relevantAbitype) {
typeString = relevantAbitype;
}
const variantType = findVariantType(typeString, namespace, abi);
if (variantType) {
typeString = variantType;
}
return formatInternalType(typeString, namespace, abi);
}
function formatInternalType(typeString, namespace, abi) {
const structNames = abi.structs.map((struct) => struct.name.toLowerCase());
if (structNames.includes(typeString.toLowerCase())) {
return `${namespace}.${capitalize(typeString)}`;
}
else {
return findCoreClass(typeString) || capitalize(typeString);
}
}
function findVariantType(typeString, namespace, abi) {
const abiVariant = abi.variants.find((variant) => variant.name.toLowerCase() === typeString.toLowerCase());
if (!abiVariant) {
return;
}
return abiVariant.types
.map((variant) => formatInternalType(variant, namespace, abi))
.join(' | ');
}
function findAbiType(typeString, abi) {
return abi.types.find((abiType) => abiType.new_type_name.toLowerCase() === typeString.toLowerCase())?.type;
}
const decorators = ['?', '[]'];
function removeDecorators(type) {
for (const decorator of decorators) {
if (type.includes(decorator)) {
type = type.replace(decorator, '');
break;
}
}
return type;
}
function generateNamespaceName(contractName) {
return contractName
.split('.')
.map((namePart) => capitalize(namePart))
.join('');
}
function generateNamespace(namespaceName, children, isExport = true) {
return ts.factory.createModuleDeclaration(isExport ? [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)] : [],
ts.factory.createIdentifier(namespaceName), ts.factory.createModuleBlock(children), ts.NodeFlags.Namespace);
}
async function generateContractClass(namespaceName, contractName) {
const classMembers = [];
const constructorParams = [
ts.factory.createParameterDeclaration(undefined, undefined, undefined, 'args', undefined, ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('PartialBy'), [
ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('ContractArgs'), undefined),
ts.factory.createUnionTypeNode([
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('abi')),
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('account')),
]),
]), undefined),
];
const constructorBody = ts.factory.createBlock([
ts.factory.createExpressionStatement(ts.factory.createCallExpression(ts.factory.createSuper(), undefined, [
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment('client', ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('args'), 'client')),
ts.factory.createPropertyAssignment('abi', ts.factory.createIdentifier('abi')),
ts.factory.createPropertyAssignment('account', ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Name'), 'from'), undefined, [ts.factory.createStringLiteral(contractName)])),
], true),
])),
], true);
const constructorMember = ts.factory.createConstructorDeclaration(undefined, undefined, constructorParams, constructorBody);
classMembers.push(constructorMember);
const classDeclaration = generateClassDeclaration('Contract', classMembers, {
parent: 'BaseContract',
export: true,
});
return { classDeclaration };
}
const printer = ts.createPrinter();
async function codegen(contractName, abi) {
const namespaceName = generateNamespaceName(contractName);
const importCoreStatement = generateImportStatement([
'ABI',
'APIClient',
'Session',
'Struct',
'TransactResult',
...EOSIO_CORE_CLASSES,
...EOSIO_CORE_TYPES,
], '@wharfkit/session');
const importContractStatement = generateImportStatement(['Contract as BaseContract', 'ContractArgs', 'PartialBy', 'blobStringToAbi'], '@wharfkit/contract');
const { classDeclaration } = await generateContractClass(namespaceName, contractName);
const structs = getFieldTypesFromAbi(abi);
const structDeclarations = [];
for (const struct of structs) {
const structMembers = [];
for (const field of struct.fields) {
structMembers.push(generateField(field, true, `${namespaceName}.Types`, abi));
}
structDeclarations.push(generateStruct(struct.structName, true, structMembers));
}
const abiBlob = abiToBlob(abi);
const abiBlobField = ts.factory.createVariableStatement([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration('abiBlob', undefined, undefined, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Blob'), ts.factory.createIdentifier('from')), undefined, [ts.factory.createStringLiteral(String(abiBlob))])),
], ts.NodeFlags.Const));
const abiField = ts.factory.createVariableStatement([ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], ts.factory.createVariableDeclarationList([
ts.factory.createVariableDeclaration('abi', undefined, undefined, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('ABI'), ts.factory.createIdentifier('from')), undefined, [ts.factory.createIdentifier('abiBlob')])),
], ts.NodeFlags.Const));
const namespaceDeclaration = generateNamespace(namespaceName, [
abiBlobField,
abiField,
classDeclaration,
generateNamespace('Types', structDeclarations),
]);
const sourceFile = ts.factory.createSourceFile([importContractStatement, importCoreStatement, namespaceDeclaration], ts.factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None);
return printer.printFile(sourceFile);
}
const defaultContractKitOptions = {};

@@ -584,3 +352,3 @@ class ContractKit {

export { Contract, ContractKit, Table, TableCursor, abiToBlob, blobStringToAbi, capitalize, codegen, ContractKit as default, indexPositionInWords, pascalCase, singularize, wrapIndexValue };
export { Contract, ContractKit, Table, TableCursor, abiToBlob, blobStringToAbi, capitalize, ContractKit as default, indexPositionInWords, pascalCase, singularize, wrapIndexValue };
//# sourceMappingURL=contract.m.js.map
{
"name": "@wharfkit/contract",
"description": "ContractKit for Wharf",
"version": "0.3.3",
"version": "0.3.4",
"homepage": "https://github.com/wharfkit/contract",

@@ -45,3 +45,3 @@ "license": "BSD-3-Clause",

"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/mock-data": "^1.0.0-beta13",
"@wharfkit/mock-data": "^1.0.0",
"@wharfkit/session": "^1.0.0-beta9",

@@ -48,0 +48,0 @@ "assert": "^2.0.0",

export * from './contract'
export * from './contract/table'
export * from './contract/table-cursor'
export * from './codegen'
export * from './kit'
export * from './utils'

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc