@sentio/cli 2.4.0 → 2.5.0-rc.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.
package/src/cli.ts CHANGED
@@ -5,7 +5,7 @@ import commandLineUsage from 'command-line-usage'
5
5
  import fs from 'fs'
6
6
  import path from 'path'
7
7
 
8
- import yaml from 'js-yaml'
8
+ import yaml from 'yaml'
9
9
  import { SentioProjectConfig } from './config.js'
10
10
  import chalk from 'chalk'
11
11
  import { buildProcessor, buildProcessorWithArgs } from './commands/build.js'
@@ -41,7 +41,7 @@ if (mainOptions.command === 'login') {
41
41
  // TODO move them to their own modules
42
42
 
43
43
  // Process configs
44
- let processorConfig: SentioProjectConfig = { host: '', project: '', build: true, debug: false }
44
+ let processorConfig: SentioProjectConfig = { host: '', project: '', build: true, debug: false, contracts: [] }
45
45
  // Fist step, read from project yaml file
46
46
  try {
47
47
  console.log(chalk.blue('Loading Process config'))
@@ -59,7 +59,7 @@ if (mainOptions.command === 'login') {
59
59
  process.exit(1)
60
60
  }
61
61
 
62
- processorConfig = yaml.load(fs.readFileSync('sentio.yaml', 'utf8')) as SentioProjectConfig
62
+ processorConfig = yaml.parse(fs.readFileSync('sentio.yaml', 'utf8')) as SentioProjectConfig
63
63
  if (!processorConfig.project === undefined) {
64
64
  console.error('Config yaml must have contain a valid project identifier')
65
65
  process.exit(1)
@@ -6,6 +6,9 @@ import * as process from 'process'
6
6
  import { getPackageRoot } from '../utils.js'
7
7
  import commandLineArgs from 'command-line-args'
8
8
  import commandLineUsage from 'command-line-usage'
9
+ import yaml from 'yaml'
10
+ import { SentioProjectConfig } from '../config.js'
11
+ import { getABIFilePath, getABI, writeABIFile } from '../abi.js'
9
12
 
10
13
  export const buildOptionDefinitions = [
11
14
  {
@@ -24,6 +27,11 @@ export const buildOptionDefinitions = [
24
27
  type: Boolean,
25
28
  description: 'Skip dependency enforce.',
26
29
  },
30
+ {
31
+ name: 'example',
32
+ type: Boolean,
33
+ description: 'Generate example usage of the processor.',
34
+ },
27
35
  ]
28
36
 
29
37
  export async function buildProcessorWithArgs(argv: string[]) {
@@ -52,7 +60,7 @@ export async function buildProcessor(onlyGen: boolean, options: commandLineArgs.
52
60
  }
53
61
 
54
62
  if (!options['skip-gen']) {
55
- await codegen()
63
+ await codegen(options.example || false)
56
64
  }
57
65
 
58
66
  if (!onlyGen) {
@@ -87,7 +95,8 @@ import 'mine.js'
87
95
  }
88
96
  }
89
97
 
90
- export async function codegen() {
98
+ export async function codegen(genExample = false) {
99
+ await prepareABI()
91
100
  const outputBase = path.resolve('src', 'types')
92
101
  try {
93
102
  // @ts-ignore dynamic import
@@ -100,7 +109,7 @@ export async function codegen() {
100
109
  }
101
110
  fs.emptyDirSync(output)
102
111
  // @ts-ignore dynamic import
103
- await codegen.codegen(input, output)
112
+ await codegen.codegen(input, output, genExample)
104
113
  } catch (e) {
105
114
  console.error('code gen error', e)
106
115
  }
@@ -113,7 +122,7 @@ export async function codegen() {
113
122
  fs.emptyDirSync(output)
114
123
 
115
124
  // @ts-ignore dynamic import
116
- await codegen.codegen(path.resolve('abis', 'solana'), output)
125
+ await codegen.codegen(path.resolve('abis', 'solana'), output, genExample)
117
126
  } catch (e) {
118
127
  console.error('code gen error', e)
119
128
  }
@@ -126,7 +135,7 @@ export async function codegen() {
126
135
  fs.emptyDirSync(output)
127
136
 
128
137
  // @ts-ignore dynamic import
129
- await codegen.codegen(path.resolve('abis', 'aptos'), output)
138
+ await codegen.codegen(path.resolve('abis', 'aptos'), output, genExample)
130
139
  } catch (e) {
131
140
  console.error('code gen error', e)
132
141
  }
@@ -139,7 +148,7 @@ export async function codegen() {
139
148
  fs.emptyDirSync(output)
140
149
 
141
150
  // @ts-ignore dynamic import
142
- await codegen.codegen(path.resolve('abis', 'sui'), output)
151
+ await codegen.codegen(path.resolve('abis', 'sui'), output, genExample)
143
152
  } catch (e) {
144
153
  console.error('code gen error', e)
145
154
  }
@@ -172,3 +181,21 @@ async function execStep(cmd: string, stepName: string) {
172
181
  console.log(chalk.blue(stepName + ' success'))
173
182
  console.log()
174
183
  }
184
+
185
+ async function prepareABI() {
186
+ const processorConfig = yaml.parse(fs.readFileSync('sentio.yaml', 'utf8')) as SentioProjectConfig
187
+
188
+ if (!processorConfig.contracts) {
189
+ return
190
+ }
191
+
192
+ for (const contract of processorConfig.contracts) {
193
+ const outputPath = getABIFilePath(contract.chain, contract.name || contract.address)
194
+ if (fs.existsSync(outputPath)) {
195
+ continue
196
+ }
197
+ console.log('Download Missing ABI specified in sentio.yaml')
198
+ const res = await getABI(contract.chain, contract.address, contract.name)
199
+ writeABIFile(res.abi, outputPath)
200
+ }
201
+ }
@@ -1,15 +1,13 @@
1
1
  import commandLineArgs from 'command-line-args'
2
2
  import commandLineUsage from 'command-line-usage'
3
- import path from 'path'
4
3
  import fs from 'fs-extra'
5
4
  import chalk from 'chalk'
6
5
 
7
6
  import { codegen } from './build.js'
8
7
 
9
- // @ts-ignore no types
10
- import { init } from 'etherscan-api'
11
- import { AptosClient } from 'aptos-sdk'
12
- import { JsonRpcProvider } from '@mysten/sui.js'
8
+ import * as process from 'process'
9
+ import yaml from 'yaml'
10
+ import { getABIFilePath, getABI, writeABIFile } from '../abi.js'
13
11
 
14
12
  export async function runAdd(argv: string[]) {
15
13
  const optionDefinitions = [
@@ -23,9 +21,9 @@ export async function runAdd(argv: string[]) {
23
21
  name: 'chain',
24
22
  alias: 'c',
25
23
  type: String,
26
- defaultValue: 'homestead',
24
+ defaultValue: '1',
27
25
  description:
28
- 'Chain identifier, can be: homestead/mainnet,goerli,arbitrum,avalanche, apots, aptos/testnet and sui, sui/testnet',
26
+ 'Chain id define from here: https://raw.githubusercontent.com/sentioxyz/sentio-sdk/main/packages/sdk/src/core/chain.ts notice some chain may not support this command',
29
27
  },
30
28
  {
31
29
  name: 'address',
@@ -64,91 +62,38 @@ export async function runAdd(argv: string[]) {
64
62
  process.exit(1)
65
63
  }
66
64
 
67
- let ethApi
68
- let aptosClient: AptosClient | undefined
69
- let suiClient: JsonRpcProvider | undefined
70
- switch (chain) {
71
- case 'aptos':
72
- aptosClient = new AptosClient('https://mainnet.aptoslabs.com/')
73
- break
74
- case 'aptos/testnet':
75
- aptosClient = new AptosClient('https://testnet.aptoslabs.com/')
76
- break
77
- case 'sui':
78
- throw Error('SUI mainnet is not support yet, try sui/testnet')
79
- // suiClient = new JsonRpcProvider('https://fullnode.mainnet.sui.io/')
80
- // break
81
- case 'sui/testnet':
82
- suiClient = new JsonRpcProvider('https://fullnode.testnet.sui.io/')
83
- break
84
- case 'mainnet':
85
- case 'homestead':
86
- ethApi = init()
87
- break
88
- default:
89
- ethApi = init(undefined, chain)
90
- }
91
-
92
- const baseErrMsg = chalk.red(
93
- `Failed to automatic download contract ${address} from ${chain}, please manually download abi and put it into abis/eth directory`
94
- )
65
+ const abiRes = await getABI(chain, address, options.name)
66
+ const filename = abiRes.name || address
95
67
 
96
- const filename = options.name ? options.name : address
68
+ writeABIFile(abiRes.abi, getABIFilePath(chain, filename))
97
69
 
98
- if (aptosClient) {
99
- try {
100
- const module = await aptosClient.getAccountModules(address)
101
- writeToDirectory(module, chain, filename)
102
- } catch (e) {
103
- console.error(baseErrMsg, e)
104
- process.exit(1)
105
- }
70
+ // Write contract info to sentio.yaml
71
+ const yamlDocument: yaml.Document = yaml.parseDocument(fs.readFileSync('sentio.yaml', 'utf8'))
72
+ let contracts = yamlDocument.get('contracts') as yaml.YAMLSeq
73
+ if (!contracts) {
74
+ contracts = new yaml.YAMLSeq()
75
+ yamlDocument.set('contracts', contracts)
106
76
  }
107
- if (suiClient) {
108
- try {
109
- const module = await suiClient.getNormalizedMoveModulesByPackage(address)
110
- writeToDirectory(module, chain, filename)
111
- } catch (e) {
112
- console.error(baseErrMsg, e)
113
- process.exit(1)
77
+
78
+ let hasContract = false
79
+ for (const item of contracts.items as yaml.YAMLMap[]) {
80
+ if (item.get('chain') === chain && item.get('address') === address) {
81
+ hasContract = true
114
82
  }
115
83
  }
116
84
 
117
- if (ethApi) {
118
- try {
119
- const resp = await ethApi.contract.getabi(address)
120
- if (resp.status !== '1') {
121
- throw Error(resp.message)
122
- }
123
- writeToDirectory(resp.result, chain, filename)
124
- } catch (e) {
125
- console.error(baseErrMsg, e)
126
- process.exit(1)
85
+ if (!hasContract) {
86
+ const newContract = new yaml.YAMLMap()
87
+ newContract.set('chain', chain)
88
+ newContract.set('address', address)
89
+ if (address !== filename) {
90
+ newContract.set('name', filename)
127
91
  }
92
+ contracts.add(newContract)
93
+ fs.writeFileSync('sentio.yaml', yamlDocument.toString(), 'utf8')
128
94
  }
95
+
129
96
  // Run gen
130
97
  await codegen()
131
98
  }
132
99
  }
133
-
134
- function writeToDirectory(obj: object | string, chain: string, name: string) {
135
- if (typeof obj === 'string') {
136
- obj = JSON.parse(obj)
137
- }
138
- const data = JSON.stringify(obj, null, 2)
139
- let subpath = chain
140
- switch (chain) {
141
- case 'aptos':
142
- case 'aptos/testnet':
143
- case 'sui':
144
- case 'sui/testnet':
145
- break
146
- default:
147
- subpath = 'eth'
148
- }
149
-
150
- const output = path.join('abis', subpath, name + '.json')
151
- fs.mkdirSync(path.dirname(output), { recursive: true })
152
- fs.writeFileSync(output, data)
153
- console.log(chalk.green('ABI has been downloaded to', output))
154
- }
package/src/config.ts CHANGED
@@ -5,12 +5,17 @@ const HostMap: { [host: string]: string } = {
5
5
  prod: 'https://app.sentio.xyz',
6
6
  }
7
7
 
8
+ export interface ContractConfig {
9
+ address: string
10
+ chain: string
11
+ name: string
12
+ }
13
+
8
14
  export interface SentioProjectConfig {
9
15
  project: string
10
16
  host: string
11
- // source: string
12
17
  build: boolean
13
- // targets: Target[]
18
+ contracts: ContractConfig[]
14
19
  debug: boolean
15
20
  }
16
21