Axelar GMP服务可以通过调用目标链的公共API来获取燃气价格。下面是一个使用Axelar GMP服务获取目标链燃气价格的示例代码:
import axios from "axios";
// 使用Axelar GMP服务的API URL
const apiUrl = "https://gmp.axelar.network";
// 目标链的公共API URL
const targetChainApiUrl = "https://target-chain-api.com";
// 获取目标链燃气价格的函数
async function getGasPrice() {
try {
// 调用Axelar GMP服务的API,获取目标链燃气价格
const response = await axios.get(`${apiUrl}/gas/price?chain=${targetChainApiUrl}`);
// 从响应中提取燃气价格
const gasPrice = response.data.price;
// 返回燃气价格
return gasPrice;
} catch (error) {
// 处理错误
console.error("Failed to get gas price:", error);
return null;
}
}
// 调用获取目标链燃气价格的函数
getGasPrice()
.then((gasPrice) => {
console.log("Gas Price:", gasPrice);
})
.catch((error) => {
console.error("Failed to get gas price:", error);
});
在上述代码中,我们首先定义了Axelar GMP服务的API URL和目标链的公共API URL。然后,在getGasPrice
函数中,我们使用axios
库发送GET请求到Axelar GMP服务的/gas/price
端点,并通过chain
参数指定目标链的API URL。最后,我们从响应中提取燃气价格并返回。
在主函数中,我们调用getGasPrice
函数来获取目标链的燃气价格,并在控制台打印出来。如果获取燃气价格失败,我们会在控制台输出错误信息。