<p>// 由于涉及到一些外部函数和类,以下代码只是大体框架,需要根据实际情况进行修改和补充</p>
<p>class Filfox {
constructor() {
this.baseUrl = 'https://filfox.info';
}</p>
<p>/**</p>
<ul>
<li>获取消息详情</li>
<li>@param {string} cid</li>
<li>@returns {Promise<object>}
*/
async getMessage(cid) {
const message = await this.request('/api/v1/message/' + cid);
return this.format(message);
}</li>
</ul>
<p>/**</p>
<ul>
<li>按消息类型获取最新的消息</li>
<li>@param {string} type</li>
<li>@param {number} num</li>
<li>@returns {Promise&lt;object[]&gt;}
*/
async getLastMessages(type, num = 1) {
const messages = (await this.request('/api/v1/message/list?pageSize=' + num + '&amp;page=0&amp;method=' + type))['messages'];
const parallel = new Parallel(5);
messages.forEach(message =&gt; {
parallel.add(async () =&gt; {
return await this.getMessage(message['cid']);
});
});
try {
return await parallel.wait();
} catch (e) {
console.log('Filfox.getLastMessages: ' + e.getMessage());
return e.getResults();
}
}</li>
</ul>
<p>/**</p>
<ul>
<li>按gas排序获取最新的一条消息</li>
<li>@param {string} type</li>
<li>@param {number} num</li>
<li>@returns {Promise<object>}
*/
async getLastMessageByGas(type, num = 1) {
const shortTypes = {'wd': 'SubmitWindowedPoSt', 'p1': 'PreCommitSector', 'p2': 'ProveCommitSector'};
const messages = await this.getLastMessages(shortTypes[type] ?? type, num);
messages.sort((a, b) =&gt; b['gasLimit'] * b['gasFeeCap'] - a['gasLimit'] * a['gasFeeCap']);
const message = messages[0];
if (Date.now() / 1000 - message['timestamp'] &gt; 600) {
throw new Error('未获取到最新消息');
}
return message;
}</li>
</ul>
<p>/**</p>
<ul>
<li>按gas排序获取最新的一条消息</li>
<li>@param {string} type</li>
<li>@param {number} num</li>
<li>@returns {Promise<object>}
*/
async getLastGas(type, num = 1) {
const message = await this.getLastMessageByGas(type, num);
return {
'cid': message['cid'],
'gasLimit': message['gasLimit'],
'gasPremium': message['gasPremium'],
'gasFeeCap': message['gasFeeCap'],
'timestamp': message['timestamp']
};
}</li>
</ul>
<p>/**</p>
<ul>
<li>获取当前高度</li>
<li>@returns {Promise<number>}
*/
async getHeight() {
return parseInt((await this.request('/api/v1/message/list?pageSize=1&amp;page=0'))['messages'][0]['height']);
}</li>
</ul>
<p>/**</p>
<ul>
<li>@param {string} path</li>
<li>@param {string} method</li>
<li>@returns {Promise<object>}
*/
async request(path, method = 'GET') {
const [result, statusCode] = await request(this.baseUrl + path, method);
if (statusCode !== 200) {
throw new Error('request failed: ' + result);
}
return JSON.parse(result);
}</li>
</ul>
<p>format(message) {
message['gasLimit'] = parseInt(message['gasLimit']);
message['gasPremium'] = message['gasPremium'].toString();
message['gasFeeCap'] = message['gasFeeCap'].toString();
if (!('timestamp' in message)) {
message['timestamp'] = message['createTimestamp'];
}
return message;
}
}</p>
把以下代码改成jsphpdeclarestrict_types=1;namespace AppTools;use AppToolsParallel;use AppToolsExceptionParallelExecutionException;class Filfox private $baseUrl = httpsfilfoxinfo; 获取消息详情 param

原文地址: https://www.cveoy.top/t/topic/bblW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录