JuChain 预言机服务为去中心化应用(dApps)提供可靠的链上价格数据。JU-USDT 价格预言机专门提供 JU 代币相对于 USDT 的实时价格,支持 DeFi 应用、交易平台和其他需要精准价格数据的智能合约。
[
{
"inputs": [
{"internalType": "address", "name": "initialOwner", "type": "address"},
{"internalType": "address", "name": "initialAuthorizedUpdater", "type": "address"}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{"inputs": [{"internalType": "address", "name": "owner", "type": "address"}], "name": "OwnableInvalidOwner", "type": "error"},
{"inputs": [{"internalType": "address", "name": "account", "type": "address"}], "name": "OwnableUnauthorizedAccount", "type": "error"},
{
"anonymous": false,
"inputs": [
{"indexed": false, "internalType": "address", "name": "previousUpdater", "type": "address"},
{"indexed": false, "internalType": "address", "name": "newUpdater", "type": "address"}
],
"name": "AuthorizedUpdaterChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{"indexed": true, "internalType": "address", "name": "previousOwner", "type": "address"},
{"indexed": true, "internalType": "address", "name": "newOwner", "type": "address"}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{"indexed": false, "internalType": "uint256", "name": "price", "type": "uint256"},
{"indexed": false, "internalType": "uint256", "name": "timestamp", "type": "uint256"},
{"indexed": false, "internalType": "address", "name": "updater", "type": "address"}
],
"name": "PriceUpdated",
"type": "event"
},
{"inputs": [], "name": "renounceOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function"},
{"inputs": [{"internalType": "address", "name": "newAuthorizedUpdater", "type": "address"}], "name": "setAuthorizedUpdater", "outputs": [], "stateMutability": "nonpayable", "type": "function"},
{"inputs": [{"internalType": "address", "name": "newOwner", "type": "address"}], "name": "transferOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function"},
{"inputs": [{"internalType": "uint256", "name": "_price", "type": "uint256"}], "name": "updatePrice", "outputs": [], "stateMutability": "nonpayable", "type": "function"},
{"inputs": [], "name": "authorizedUpdater", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "stateMutability": "view", "type": "function"},
{"inputs": [], "name": "getLatestPrice", "outputs": [{"internalType": "string", "name": "", "type": "string"}, {"internalType": "uint256", "name": "", "type": "uint256"}, {"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function"},
{"inputs": [], "name": "lastUpdatedAt", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function"},
{"inputs": [], "name": "latestPrice", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function"},
{"inputs": [], "name": "owner", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "stateMutability": "view", "type": "function"},
{"inputs": [], "name": "pricePrecision", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function"},
{"inputs": [], "name": "symbol", "outputs": [{"internalType": "string", "name": "", "type": "string"}], "stateMutability": "view", "type": "function"}
]
const { Web3 } = require('web3');
// 主网 RPC
const mainnetRpc = 'https://rpc.juchain.org';
// 测试网 RPC
const testnetRpc = 'https://testnet-rpc.juchain.org';
// 选择网络(主网/测试网)
const web3 = new Web3(mainnetRpc); // 或 testnetRpc
// 主网合约地址
const mainnetContractAddress = '0x49E5c7f25711abe668F404307b27f4bE4836B0e7';
// 测试网合约地址
const testnetContractAddress = '0x70D3Fc0bcf1ffD64111FC0C708DA407d9732Ab95';
// 选择合约地址
const contractAddress = mainnetContractAddress; // 或 testnetContractAddress
const contract = new web3.eth.Contract(abi, contractAddress);
// 获取完整价格信息
async function getCompletePrice() {
try {
const [symbol, price, timestamp] = await contract.methods.getLatestPrice().call();
const precision = await contract.methods.pricePrecision().call();
console.log('完整价格信息:');
console.log(`交易对: ${symbol}`);
console.log(`价格: ${price / precision} JU/USDT`);
console.log(`时间戳: ${timestamp} (${new Date(Number(timestamp) * 1000).toLocaleString()})`);
return { symbol, price, timestamp, precision };
} catch (error) {
console.error('获取价格信息失败:', error);
}
}
// 获取最新价格
async function getLatestPrice() {
try {
const price = await contract.methods.latestPrice().call();
const precision = await contract.methods.pricePrecision().call();
console.log(`最新价格: ${price / precision} JU/USDT`);
return price;
} catch (error) {
console.error('获取最新价格失败:', error);
}
}
// 获取授权更新者
async function getAuthorizedUpdater() {
try {
const updater = await contract.methods.authorizedUpdater().call();
console.log(`授权更新者: ${updater}`);
return updater;
} catch (error) {
console.error('获取授权更新者失败:', error);
}
}
// 更新价格(仅限授权更新者)
async function updatePrice(newPrice, privateKey) {
try {
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
const tx = contract.methods.updatePrice(newPrice);
const gas = await tx.estimateGas({ from: account.address });
const receipt = await tx.send({ from: account.address, gas });
console.log(`价格更新成功,交易哈希: ${receipt.transactionHash}`);
return receipt;
} catch (error) {
console.error('更新价格失败:', error);
}
}
// 执行所有查询
async function getAllInfo() {
await getCompletePrice();
await getLatestPrice();
await getAuthorizedUpdater();
}
getAllInfo();
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IJUOracle {
function getLatestPrice() external view returns (string memory, uint256, uint256);
function pricePrecision() external view returns (uint256);
}
contract JUPriceConsumer {
IJUOracle public oracle;
constructor(address _oracleAddress) {
oracle = IJUOracle(_oracleAddress);
}
function getJUPrice() public view returns (uint256) {
(, uint256 price, ) = oracle.getLatestPrice();
uint256 precision = oracle.pricePrecision();
return price; // 前端需除以 precision
}
function getFormattedPrice() public view returns (string memory symbol, uint256 price, uint256 precision, uint256 timestamp) {
(symbol, price, timestamp) = oracle.getLatestPrice();
precision = oracle.pricePrecision();
return (symbol, price, precision, timestamp);
}
}