-
Notifications
You must be signed in to change notification settings - Fork 96
/
update-management-idl.ts
113 lines (90 loc) · 2.86 KB
/
update-management-idl.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { $, cd, fetch } from 'zx';
import path from 'path';
const main = async () => {
const res = await fetch(
'https://raw.githubusercontent.com/dfinity/interface-spec/master/spec/_attachments/ic.did',
);
res.text().then(async text => {
const root = path.resolve(__dirname, '..');
// TODO: remove this function once the bitcoin queries are removed from the candid spec
const candid = stripBitcoinQueries(text);
await cd(`${root}/packages/agent/src/canisters`);
await $`echo ${candid} > management.did`;
// Format the candid file
await $`npx prettier --write --plugin=prettier-plugin-motoko **/*.did`;
// Generate the idl and interface files
let ts = (await $`didc bind management.did -t ts`).toString();
let js = (await $`didc bind management.did -t js`).toString();
const didcVersion = await $`didc --version`;
const prefix = `/*
* This file is generated from the candid for asset management.
* didc version: ${didcVersion.toString().split(' ')[1].trim()}
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
`;
// replace first line of service
ts = ts.replace(`export interface _SERVICE {`, `export default interface _SERVICE {`);
js = js.replace(`export const idlFactory = ({ IDL }) => {`, `export default ({ IDL }) => {`);
// remove init function
js = js.split('export const init = ({ IDL }) => {')[0];
ts = prefix + ts;
js = prefix + js;
// write the files
await $`echo ${js} > management_idl.ts`;
await $`echo ${ts} > management_service.ts`;
await cd(`${root}`);
// Format the generated files
await $`npm run prettier:format`;
console.log('Done!');
});
};
function stripBitcoinQueries(text: string): string {
// get_utxos_query
let newText = text.replace(
`type bitcoin_get_utxos_query_args = record {
address : bitcoin_address;
network : bitcoin_network;
filter : opt variant {
min_confirmations : nat32;
page : blob;
};
};`,
'',
);
newText = newText.replace(
`
type bitcoin_get_utxos_query_result = record {
utxos : vec utxo;
tip_block_hash : block_hash;
tip_height : nat32;
next_page : opt blob;
};`,
'',
);
newText = newText.replace(
`bitcoin_get_utxos_query : (bitcoin_get_utxos_query_args) -> (bitcoin_get_utxos_query_result) query;`,
'',
);
// bitcoin_get_balance_query
newText = newText.replace(
`bitcoin_get_balance_query : (bitcoin_get_balance_query_args) -> (bitcoin_get_balance_query_result) query;`,
'',
);
newText = newText.replace(
`
type bitcoin_get_balance_query_args = record {
address : bitcoin_address;
network : bitcoin_network;
min_confirmations : opt nat32;
};`,
'',
);
newText = newText.replace(
`
type bitcoin_get_balance_query_result = satoshi;`,
'',
);
return newText;
}
main();