-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsolita-merkle-distributor.ts
52 lines (46 loc) · 1.78 KB
/
solita-merkle-distributor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import path from 'path'
import { spawn } from 'child_process'
import { Solita } from '@metaplex-foundation/solita'
import { writeFile, readFile } from 'fs/promises'
const PROGRAM_NAME = 'merkle_distributor'
const PROGRAM_ID = 'AZMc26abaSP7si1wtLaV5yPxTxpWd895M8YpJFFdQ8Qw'
const programDir = path.join('.', 'programs', 'merkle-distributor')
const generatedIdlDir = path.join('.', 'target', 'idl')
const generatedSDKDir = path.join('.', 'tests', 'merkle-distributor-libs')
// @ts-ignore
const anchor = spawn('anchor', ['build', '--idl', generatedIdlDir], {
cwd: programDir
})
.on('error', (err) => {
console.error(err)
// @ts-ignore this err does have a code
if (err.code === 'ENOENT') {
console.error(
'Ensure that `anchor` is installed and in your path, see:\n https://project-serum.github.io/anchor/getting-started/installation.html#install-anchor\n'
)
}
process.exit(1)
})
.on('exit', () => {
console.log(
'IDL written to: %s',
path.join(generatedIdlDir, `${PROGRAM_NAME}.json`)
)
generateTypeScriptSDK()
})
anchor.stdout.on('data', (buf) => console.log(buf.toString('utf8')))
anchor.stderr.on('data', (buf) => console.error(buf.toString('utf8')))
async function generateTypeScriptSDK() {
console.error('Generating TypeScript SDK to %s', generatedSDKDir)
const generatedIdlPath = path.join(generatedIdlDir, `${PROGRAM_NAME}.json`)
const data = await readFile(generatedIdlPath, 'utf8')
const idl = JSON.parse(data)
if (idl.metadata?.address == null) {
idl.metadata = { ...idl.metadata, address: PROGRAM_ID }
await writeFile(generatedIdlPath, JSON.stringify(idl, null, 2))
}
const gen = new Solita(idl, { formatCode: true })
await gen.renderAndWriteTo(generatedSDKDir)
console.error('Success!')
process.exit(0)
}