-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.js
135 lines (128 loc) · 3.97 KB
/
task.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const fs = require('fs');
const axios = require('axios');
const { v4: uuidv4 } = require('uuid');
const { logger, getAPIKey } = require('./utils');
const { INVARIANTS_ITEMS } = require('./constant');
const BLAZ_BASE_URL = 'https://blaz.infra.fuzz.land';
async function createOffchain(
buildResultUrl,
projectType,
offchainConfig,
setupFile,
status = 0
) {
const invariants = Object.keys(INVARIANTS_ITEMS)
.map((category) => INVARIANTS_ITEMS[category].map((slot) => slot.name))
.flat();
const API_KEY = await getAPIKey();
await axios
.post(
`${BLAZ_BASE_URL}/task/offchain`,
{
task_name: 'Untitled Task Created by CLI',
project_type: projectType,
contracts_filename: '',
contract_address: '',
contracts: buildResultUrl,
offchain_config: JSON.stringify(JSON.parse(offchainConfig)),
chain: '',
custom_rpc_url: '',
to_detect: invariants,
invariants_contract: [],
sca_enabled: true,
n_cpus: 1,
liquidity_pools_definition: [],
token_price: [],
status,
upload_type: 0,
setup_file: setupFile,
},
{
headers: {
Authorization: API_KEY,
},
}
)
.then(({ data: { id, status, message } }) => {
if (status === 'success') {
logger.info(
`Created offchain task successfully, you can check the task detial at https://blaz.ai/project/${id}`
);
} else {
logger.error('Failed to create offchain task:', message);
}
})
.catch((err) => {
logger.info(err.message);
process.exit(1);
});
}
async function createOnchain(contractAddress, chain, blockNumber) {
const invariants = Object.keys(INVARIANTS_ITEMS)
.map((category) => INVARIANTS_ITEMS[category].map((slot) => slot.name))
.flat();
const data = {
task_name: 'Untitled Task Created by CLI',
chain: chain,
custom_rpc_url: '',
block_number: blockNumber,
contract_address: contractAddress,
to_detect: invariants,
invariants_contract: [],
sca_enabled: true,
n_cpus: 1,
liquidity_pools_definition: {},
token_price: {},
status: 0,
};
const API_KEY = await getAPIKey();
await axios
.post(`${BLAZ_BASE_URL}/task/onchain`, data, {
headers: {
Authorization: API_KEY,
},
})
.then(({ data: { id, status, message } }) => {
if (status === 'success') {
logger.info(
`Created onchain task successfully, you can check the task detial at https://blaz.ai/project/${id}`
);
} else {
logger.error('Failed to create onchain task:', message);
}
})
.catch((err) => {
logger.error('Error while creating onchain task:', err.message);
process.exit(1);
});
}
async function uploadBuildResult(filePath) {
const task_id = uuidv4();
let buffer;
const API_KEY = await getAPIKey();
const {
data: { uploadUrl },
} = await axios.get(
`${BLAZ_BASE_URL}/storage/upload_url/${task_id}-results.json`,
{
headers: {
Authorization: API_KEY,
},
}
);
try {
buffer = fs.readFileSync(filePath);
} catch (err) {
logger.error('uploaded file not found');
return;
}
await axios.put(uploadUrl, buffer, {
headers: { 'Content-Type': 'application/octet-stream' },
});
return uploadUrl;
}
module.exports = {
createOffchain,
createOnchain,
uploadBuildResult,
};