Skip to content

Commit

Permalink
cleanup: codegen cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 26, 2023
1 parent cdc129c commit 59ad7e6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 577 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@typescript-eslint/parser": "^5.20.0",
"@wharfkit/mock-data": "^1.0.0-beta11",
"@wharfkit/session": "^1.0.0-beta4",
"assert": "^2.0.0",
"chai": "^4.3.4",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.1.0",
Expand Down
37 changes: 17 additions & 20 deletions scripts/codegen-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {APIClient} = require('@wharfkit/session')
const fs = require('fs')
const {fetch} = require('node-fetch')

const {codegen, Contract} = require('../lib/contract.js')
const {codegen, ContractKit} = require('../lib/contract.js')

const client = new APIClient({
url: process.env.ANTELOPE_NODE_URL || 'https://eos.greymass.com',
Expand All @@ -23,31 +23,28 @@ async function codegenCli() {

log(`Fetching ABI for ${contractName}...`)

const contract = Contract.from({name: contractName, client})
const contractKit = new ContractKit({
client,
})
const contract = await contractKit.load(contractName)

const abi = await contract.getAbi()
log(`Generating Contract helper for ${contractName}...`)

if (!abi) {
log(`No ABI found for ${contractName}`)
} else {
log(`Generating Contract helper for ${contractName}...`)

const generatedCode = await codegen(contractName, abi)
const generatedCode = await codegen(contractName, contract.abi)

log(`Generated Contract helper class for ${contractName}...`)
log(`Generated Contract helper class for ${contractName}...`)

// Check if --file is present in the command line arguments
const fileFlagIndex = process.argv.indexOf('--file')
if (fileFlagIndex !== -1 && process.argv[fileFlagIndex + 1]) {
const contractFilePath = process.argv[fileFlagIndex + 1]
// Check if --file is present in the command line arguments
const fileFlagIndex = process.argv.indexOf('--file')
if (fileFlagIndex !== -1 && process.argv[fileFlagIndex + 1]) {
const contractFilePath = process.argv[fileFlagIndex + 1]

fs.writeFileSync(contractFilePath, generatedCode)
fs.writeFileSync(contractFilePath, generatedCode)

log(`Generated Contract helper for ${contractName} saved to ${contractFilePath}`)
} else {
log(`Generated Contract helper class:`)
log(generatedCode)
}
log(`Generated Contract helper for ${contractName} saved to ${contractFilePath}`)
} else {
log(`Generated Contract helper class:`)
log(generatedCode)
}
}

Expand Down
38 changes: 5 additions & 33 deletions src/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {ABI} from '@greymass/eosio'
import * as ts from 'typescript'

import {
Expand All @@ -10,8 +9,7 @@ import {
getFieldTypesFromAbi,
} from './codegen/helpers'
import {generateNamespace, generateNamespaceName} from './codegen/namespace'
import {generateActions} from './codegen/actions'
import {generateTableClass} from './codegen/table'
import {generateContractClass} from './codegen/contract'

const printer = ts.createPrinter()

Expand All @@ -34,29 +32,8 @@ export async function codegen(contractName, abi) {
'@wharfkit/contract'
)

const {methods: actionMethods, interfaces: actionsInterfaces} = generateActions(
contractName,
namespaceName,
ABI.from(abi)
)

// Generate actions namespace
const actionsNamespace = generateNamespace(namespaceName, [
generateNamespace('actions', actionMethods),
])

const tableClasses: ts.ClassDeclaration[] = []
const tableInterfaces: ts.InterfaceDeclaration[] = []

for (const table of abi.tables) {
const {classDeclaration} = await generateTableClass(contractName, namespaceName, table, abi)
tableClasses.push(classDeclaration)
}

// Generate tables namespace
const tableNamespace = generateNamespace(namespaceName, [
generateNamespace('tables', tableClasses),
])

const {classDeclaration} = await generateContractClass(namespaceName, contractName, abi)

// Extract fields from the ABI
const structs = getFieldTypesFromAbi(abi)
Expand All @@ -76,20 +53,15 @@ export async function codegen(contractName, abi) {

// Generate types namespace
const typesDeclaration = generateNamespace(namespaceName, [
generateNamespace('types', [
...actionsInterfaces,
...tableInterfaces,
...structDeclarations,
]),
generateNamespace('types', structDeclarations),
])

const sourceFile = ts.factory.createSourceFile(
[
importContractStatement,
importCoreStatement,
actionsNamespace,
tableNamespace,
typesDeclaration,
classDeclaration,
],
ts.factory.createToken(ts.SyntaxKind.EndOfFileToken),
ts.NodeFlags.None
Expand Down
213 changes: 0 additions & 213 deletions src/codegen/actions.ts

This file was deleted.

Loading

0 comments on commit 59ad7e6

Please sign in to comment.