UNPKG

amethysta

Version:
174 lines (161 loc) 6.21 kB
/** * Amethysta * version: 1.0 * author: victor ratts * lisence: MIT */ import parser from "@solidity-parser/parser"; import fs from "fs"; import api from "./api/smart.api.js"; import qs from "qs"; import FormData from "form-data"; import pdf from "./controllers/pdf.controller.js"; class Amethysta { constructor(config = { api: 'v1', schedule: 'pure', protocol: 'erc20' }) { this.config = config; } GetSmartContract(filePath = '') { return new Promise(async (resolve, reject) => { try { var strem = fs.openSync(filePath); const data = fs.readFileSync(filePath, 'utf8'); var ast = parser.parse(data.toString()); fs.close(strem); return resolve(ast); } catch (e) { return reject({ status: 'error', description: e.message }) } }) } GetContractByStr(contract = '') { return new Promise(async (resolve, reject) => { try { var ast = parser.parse(contract); return resolve(ast); } catch (e) { return reject({ status: 'error', description: e.message }) } }) } CheckSmartContract(content = {}) { return new Promise(async (resolve, reject) => { try { parser.visit(content, { ImportDirective: function (node) { return resolve(node) } }) } catch (e) { return reject(e.message) } }) } SendForRemix(SmartContract) { return new Promise(async (resolve, reject) => { var fd = new FormData(); fd.append("path", JSON.stringify(SmartContract)) api.post('/api/v0/add?pin=true', fd, { headers: fd.getHeaders() }).then(data => { return resolve(data.data) }).catch(e => { return reject(e) }) }) } createPdfOutput(smartJson = {}, path = './smartsample.pdf') { return new Promise(async (resolve, reject) => { if (smartJson.children.length > 0) { var smart = smartJson.children; var date = new Date(); var patern = { title: '401-system', description: 'smartcontract validation', main: `Details of the contract issued on the date: ${date.toISOString()}`, parts: [], clauses: [], end: 'The information in this smartcontract is generated through a validation algorithm that reads the document using open source libraries. This document is eligible for release on the etherium smartchain.' }; for (var smt of smart) { patern.parts.push({ line: smt.type, name: smt.name }) if(smt.subNodes !== undefined){ for(var sub of smt.subNodes){ patern.parts.push({ line: sub.type, name: sub.name, }) if(sub.members !== undefined && sub.members.length > 0){ for(var mb of sub.members){ patern.clauses.push({ line: mb.type, name: mb.name, }) } } else { patern.clauses.push({ line: 'no', name: 'external: '+ btoa(sub.name), }) } if(sub.body !== undefined && sub.body.statements !== undefined && sub.body.statements.length > 0){ for(var bd of sub.body.statements){ patern.clauses.push({ line: bd.type, name: bd.name ?? (bd.expression == undefined ? 'Case: '+btoa(bd.type + Math.random()) : bd.expression.type), }) // if(bd.expression.arguments !== undefined){ // for(var arg of bd.expression.arguments){ // } // } } } else { patern.clauses.push({ line: 'no', name: 'no', }) } } } } pdf(patern, path) return resolve(patern) } else { return reject({ status: 'error', description: 'falure to create Output' }) } }) } CheckContractStatus(SmartContract = {}) { return new Promise(async (resolve, reject) => { var pragma = { status: '', name: '', contract: '', pragma: '', kind: '', metaBase: '', extraInfo: [] }; for (var smart of SmartContract.children ?? []) { if (smart.name == 'solidity') { pragma.status = 'success' pragma.name = 'solidity SmartContract (.sol)' } } }) } } export default Amethysta;