legend-cli 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ import { writeFileSync, readFileSync } from 'fs'
2
+ import { glob } from 'glob'
3
+ import ora from 'ora'
4
+ import { Prompt } from '@poppinss/prompts'
5
+ // import { compileContract } from 'ton-compiler'
6
+ import { compileFunc } from '@ton-community/func-js'
7
+ import { isHashkit } from "../helpers/detection.js"
8
+ import { log, logError } from "../helpers/logger.js"
9
+ import { fixPath, isWindows } from '../helpers/os.js'
10
+ import { funcParser } from '../helpers/func-parser.js'
11
+ import { ipfsUpload } from '../helpers/ipfs.js'
12
+ import { exit } from 'process'
13
+
14
+ export const publishContract = async(contractName, ops) => {
15
+ try {
16
+ const root = process.cwd()
17
+
18
+ const spinner = ora({ color: 'magenta' })
19
+
20
+ console.log("CONTRACT", contractName, ops)
21
+
22
+ // return
23
+
24
+ let contractPath
25
+
26
+ if (!isHashkit(root)) {
27
+ throw Error('Not HashKit contract project')
28
+ }
29
+
30
+ const prompt = new Prompt()
31
+
32
+ let contracts = await glob(`contracts/*.fc`, { ignore: ['node_modules/**', 'artifacts/**'] })
33
+
34
+ if (!contractName) {
35
+ const contractNames = contracts
36
+ .map(f => f
37
+ .substring((isWindows ? f.lastIndexOf("\\") : f.lastIndexOf('/')) + 1)
38
+ .replace(`.fc`, '')
39
+ )
40
+ contractName = await prompt.choice('Select which contract to compile', contractNames)
41
+ }
42
+
43
+ contractPath = contracts.find(f => f.endsWith(`${contractName}.fc`))
44
+ log(`Selected File: ${contractName} ${contractPath}`)
45
+
46
+
47
+ const contract = funcParser(`${root}/${contractPath}`)
48
+
49
+ // const files = [
50
+ // fixPath(`${root}/contracts/imports/stdlib.fc`),
51
+ // fixPath(`${root}/contracts/${contractName}.fc`)
52
+ // ]
53
+
54
+ const sourceName = `${contractName}.fc`
55
+ const mainSource = readFileSync(fixPath(`${root}/contracts/${sourceName}`)).toString().replace(/imports\//gm, '')
56
+
57
+ const sources = contract.files.reduce((o, c) => ({
58
+ ...o,
59
+ [c]: readFileSync(fixPath(`${root}/contracts/imports/${c}`)).toString()
60
+ }), { })
61
+
62
+ const compiled = await compileFunc({ targets: [sourceName], sources: { ...sources, [sourceName]: mainSource } })
63
+
64
+ if (!compiled.status) {
65
+ console.log("Something broke", compiled)
66
+ exit()
67
+ }
68
+
69
+ // console.log("CONTRACT", contract)
70
+ // console.log("COMPILED", Object.keys(compiled))
71
+ // console.log("STATUS", compiled.status)
72
+ // console.log("BOC", compiled.codeBoc)
73
+ // console.log("SNAP", compiled.snapshot)
74
+
75
+ // const compiled = await compileContract({ files, stdlib: false, version: 'latest' })
76
+
77
+ // const b64code = compiled.output.toString('base64')
78
+ // const hexcode = compiled.output.toString('hex')
79
+
80
+ const b64code = compiled.codeBoc
81
+ const hexcode = compiled.codeHashHex
82
+
83
+ spinner.start(`Publishing contract content\n`)
84
+
85
+ const ipfs = await ipfsUpload(JSON.stringify(compiled.snapshot))
86
+
87
+ // console.log("IPFS", ipfs.Hash)
88
+
89
+ spinner.succeed(`Contract code uploaded! Preparing metadata`)
90
+
91
+ writeFileSync(fixPath(`${root}/build/${contractName}.fif`), compiled.fiftCode)
92
+
93
+ const contractData = {
94
+ name: contractName,
95
+ license: 'MIT',
96
+ params: contract.params,
97
+ getters: contract.getters,
98
+ code: `ipfs://${ipfs.Hash}`,
99
+ b64: b64code,
100
+ hex: hexcode,
101
+ size: atob(b64code).length / 1e+6,
102
+ version: 2,
103
+ publisher: 'HashKit CLI',
104
+ publisherUrl: 'https://hashkit.xyz',
105
+ }
106
+
107
+ writeFileSync(fixPath(`${root}/build/${contractName}.abi.json`), JSON.stringify(contractData))
108
+
109
+ spinner.start(`Publishing contract metadata\n`)
110
+
111
+ const meta = await ipfsUpload(JSON.stringify(contractData))
112
+
113
+ spinner.succeed(`Contract metadata uploaded ipfs://${meta.Hash}`)
114
+
115
+ // console.log("ROOT", contractData, main)
116
+
117
+ } catch (e) {
118
+ logError(e)
119
+ }
120
+ }