kopia lustrzana https://github.com/bugout-dev/moonworm
Merge 9871baf44e
into 924b9cb6ed
commit
db9933e277
|
@ -0,0 +1,17 @@
|
||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
browser: false,
|
||||||
|
es6: true,
|
||||||
|
node: true,
|
||||||
|
},
|
||||||
|
parser: "@typescript-eslint/parser",
|
||||||
|
parserOptions: {
|
||||||
|
project: `${__dirname}/tsconfig.json`,
|
||||||
|
sourceType: "module",
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
"prettier/prettier": "error",
|
||||||
|
},
|
||||||
|
plugins: ["prettier", "@typescript-eslint"],
|
||||||
|
extends: ["eslint:recommended", "plugin:prettier/recommended"],
|
||||||
|
}
|
|
@ -0,0 +1,145 @@
|
||||||
|
|
||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/node
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=node
|
||||||
|
|
||||||
|
### Node ###
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
.cache/
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
.temp
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.*
|
||||||
|
|
||||||
|
### Node Patch ###
|
||||||
|
# Serverless Webpack directories
|
||||||
|
.webpack/
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
|
||||||
|
# SvelteKit build / generate output
|
||||||
|
.svelte-kit
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/node
|
|
@ -0,0 +1,4 @@
|
||||||
|
*.d.ts
|
||||||
|
*.d.ts.map
|
||||||
|
*.js
|
||||||
|
*.js.map
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"tabWidth": 4,
|
||||||
|
"semi": false
|
||||||
|
}
|
Plik diff jest za duży
Load Diff
|
@ -0,0 +1,46 @@
|
||||||
|
{
|
||||||
|
"name": "moonworm",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "moonworm: Generate a command line interface to any Ethereum smart contract",
|
||||||
|
"main": "lib/cli.js",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc --build .",
|
||||||
|
"lint": "prettier --check src/ && eslint src/**",
|
||||||
|
"prettier": "prettier --write src/",
|
||||||
|
"test": "mocha --recursive \"tests/**/*.ts\""
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/bugout-dev/moonworm.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"moonworm",
|
||||||
|
"moonstream",
|
||||||
|
"smartcontract"
|
||||||
|
],
|
||||||
|
"author": "Moonstream",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/bugout-dev/moonworm/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/bugout-dev/moonworm#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/core": "^7.16.10",
|
||||||
|
"@babel/generator": "^7.16.8",
|
||||||
|
"@types/babel__generator": "^7.6.4",
|
||||||
|
"@types/mocha": "^9.0.0",
|
||||||
|
"@types/node": "^17.0.9",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^5.10.0",
|
||||||
|
"@typescript-eslint/parser": "^5.10.0",
|
||||||
|
"eslint": "^8.7.0",
|
||||||
|
"eslint-config-prettier": "^8.3.0",
|
||||||
|
"eslint-plugin-prettier": "^4.0.0",
|
||||||
|
"mocha": "^9.1.4",
|
||||||
|
"prettier": "^2.5.1",
|
||||||
|
"tslint": "^6.1.3",
|
||||||
|
"typescript": "^4.5.4"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"commander": "^8.3.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,89 @@
|
||||||
|
import fs from "fs"
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
|
import { Command } from "commander"
|
||||||
|
|
||||||
|
import { generateHardHatInterface } from "./generators/hardhat"
|
||||||
|
import { MOONWORM_VERSION } from "./version"
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const program = new Command()
|
||||||
|
|
||||||
|
// Handle generate-hardhat command from CLI
|
||||||
|
const handleHardHatGenerate = (
|
||||||
|
outdir: string,
|
||||||
|
name: string,
|
||||||
|
projectPath: string
|
||||||
|
) => {
|
||||||
|
const workingPath = path.resolve()
|
||||||
|
const outdirPath = path.join(workingPath, outdir)
|
||||||
|
|
||||||
|
const buildPath = path.join(projectPath, "artifacts", "contracts")
|
||||||
|
const buildFilePath = path.join(
|
||||||
|
buildPath,
|
||||||
|
`${name}.sol`,
|
||||||
|
`${name}.json`
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!fs.existsSync(path.resolve(buildFilePath))) {
|
||||||
|
console.log(
|
||||||
|
`File does not exist: ${buildFilePath}. Maybe you have to compile the smart contracts?`
|
||||||
|
)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
const build = JSON.parse(fs.readFileSync(buildFilePath, "utf8"))
|
||||||
|
const abi = build["abi"]
|
||||||
|
|
||||||
|
const contractInterface = generateHardHatInterface(abi, name)
|
||||||
|
|
||||||
|
if (!fs.existsSync(outdirPath)) {
|
||||||
|
fs.mkdirSync(outdirPath)
|
||||||
|
}
|
||||||
|
const outdirFilePath = path.join(outdir, `${name}.js`)
|
||||||
|
fs.writeFileSync(outdirFilePath, contractInterface)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
program.version(
|
||||||
|
MOONWORM_VERSION,
|
||||||
|
"-v, --version",
|
||||||
|
"Show moonworm version"
|
||||||
|
)
|
||||||
|
|
||||||
|
program
|
||||||
|
.command("generate-hardhat")
|
||||||
|
.description("Moonworm code generator for hardhat projects")
|
||||||
|
.requiredOption(
|
||||||
|
"-o, --outdir <directory>",
|
||||||
|
"Output directory where files will be generated."
|
||||||
|
)
|
||||||
|
.requiredOption(
|
||||||
|
"-n --name <contract>",
|
||||||
|
"Contract name generate interface to"
|
||||||
|
)
|
||||||
|
.requiredOption(
|
||||||
|
"-p --project <path>",
|
||||||
|
"Path to brownie project directory"
|
||||||
|
)
|
||||||
|
.action((options) => {
|
||||||
|
handleHardHatGenerate(
|
||||||
|
options.outdir,
|
||||||
|
options.name,
|
||||||
|
options.project
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
program.parse()
|
||||||
|
} catch (error) {
|
||||||
|
console.log("")
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
.then(() => process.exit(process.exitCode))
|
||||||
|
.catch((error) => {
|
||||||
|
console.log(error)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
|
@ -0,0 +1,48 @@
|
||||||
|
import { parse } from "@babel/parser";
|
||||||
|
import generate from "@babel/generator";
|
||||||
|
import {FunctionDeclaration} from "@babel/types"
|
||||||
|
import { type } from "os";
|
||||||
|
|
||||||
|
const code = `
|
||||||
|
function transfer(from, to) {
|
||||||
|
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const ast = parse(code);
|
||||||
|
|
||||||
|
console.log((ast.program.body[0] as FunctionDeclaration))
|
||||||
|
|
||||||
|
function make_function(func_name : string, args : String[], body: Object | null) {
|
||||||
|
|
||||||
|
let function_def = {
|
||||||
|
// start: null,
|
||||||
|
// end: null,
|
||||||
|
// leadingComments: null,
|
||||||
|
// trailingComments: null,
|
||||||
|
// innerComments: null,
|
||||||
|
// loc: null,
|
||||||
|
type: "FunctionDeclaration",
|
||||||
|
id: {
|
||||||
|
type: "Identifier",
|
||||||
|
name: func_name,
|
||||||
|
},
|
||||||
|
params: args.map((el => {return {
|
||||||
|
type: "Identifier",
|
||||||
|
name: el
|
||||||
|
}})),
|
||||||
|
body: {
|
||||||
|
type: "BlockStatement",
|
||||||
|
body: body ? [body] : [],
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const output = generate(function_def as unknown as FunctionDeclaration, {})
|
||||||
|
console.log(output.code)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let body = parse('console.log(`Hello ${greeting}`)')
|
||||||
|
|
||||||
|
make_function("helloWorld", ["greeting"], body)
|
|
@ -0,0 +1,43 @@
|
||||||
|
interface input {
|
||||||
|
internalType: String
|
||||||
|
name: String
|
||||||
|
type: String
|
||||||
|
}
|
||||||
|
interface abiItem {
|
||||||
|
name: string
|
||||||
|
inputs: Array<input>
|
||||||
|
outputs: Array<String>
|
||||||
|
stateMutability: String
|
||||||
|
type: String
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateHardHatInterface(
|
||||||
|
abi: Array<abiItem>,
|
||||||
|
name: string
|
||||||
|
): string {
|
||||||
|
// return `console.log("${name}")`
|
||||||
|
const contractName = name
|
||||||
|
const contractInterface = abi.map((abiItem) => {
|
||||||
|
console.log("abiItem", abiItem.name)
|
||||||
|
if (!abiItem.name) return ""
|
||||||
|
|
||||||
|
const string =
|
||||||
|
`${abiItem.name} (address, ${abiItem.inputs.map(
|
||||||
|
(inputItem) => inputItem.name
|
||||||
|
)}) {
|
||||||
|
const Contract = await ethers.getContractFactory("${contractName}");
|
||||||
|
const contract = await Contract.attach(` +
|
||||||
|
"`${address}`" +
|
||||||
|
`);
|
||||||
|
await contract.${abiItem.name}(${abiItem.inputs.map(
|
||||||
|
(inputItem) => inputItem.name
|
||||||
|
)});
|
||||||
|
};
|
||||||
|
`
|
||||||
|
return string
|
||||||
|
})
|
||||||
|
return `export class Moonworm {
|
||||||
|
constructor () {}
|
||||||
|
${contractInterface.join("")}
|
||||||
|
};`
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
export const MOONWORM_VERSION = "0.0.1"
|
|
@ -0,0 +1,33 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
/* Projects */
|
||||||
|
"composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||||
|
|
||||||
|
/* Language and Environment */
|
||||||
|
"target": "es2017", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||||
|
|
||||||
|
/* Modules */
|
||||||
|
"module": "commonjs", /* Specify what module code is generated. */
|
||||||
|
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||||
|
"typeRoots": ["./node_modules/@types"], /* Specify multiple folders that act like `./node_modules/@types`. */
|
||||||
|
|
||||||
|
/* Emit */
|
||||||
|
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||||
|
"declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||||
|
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||||
|
"outDir": "./lib", /* Specify an output folder for all emitted files. */
|
||||||
|
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||||
|
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
||||||
|
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
||||||
|
|
||||||
|
/* Type Checking */
|
||||||
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
|
|
||||||
|
/* Completeness */
|
||||||
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"./node_modules/",
|
||||||
|
"./lib/"
|
||||||
|
]
|
||||||
|
}
|
Ładowanie…
Reference in New Issue