📝 Reading Beacon via Static Call
You can also read data from Beacons from an off-chain application. Below snippets shows this in TypeScript and Python.
Check Beacon addresses and contract addresses to try it out. Also you can find RPC URLs at ChainList.
- TypeScript
- Python
const RPC_URL = "https://matic-mumbai.chainstacklabs.com";
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const DapiServerAddress = "0x71Da7A936f...";
const beaconId = "0xe6bc56045455c...";
const DapiServerAbi = '...';
const dapiServer = new ethers.Contract(DapiServerAddress, DapiServerAbi, provider);
dapiServer.readDataFeedWithId(beaconId).then((ret: any) => {
console.log(ret);
});
/*
Example output:
[
BigNumber { _hex: '0x0de0c919d15abc00', _isBigNumber: true },
1664290764,
value: BigNumber { _hex: '0x0de0c919d15abc00', _isBigNumber: true },
timestamp: 1664290764
]
*/
import json
from web3 import Web3
RPC_URL = "https://matic-mumbai.chainstacklabs.com"
web3 = Web3(Web3.HTTPProvider(RPC_URL))
dapiserver_address = "0x71Da7A936fCaEd1Ee364Df106B12deF6D1Bf1f14"
beacon_id = "0xe6bc56045455ca1aebc85ddd7a251ecf55b11c0797c53f2833d7caa6b1a91fa0"
with open("./DapiServer-abi.json", "r") as f:
abi = json.load(f)
contract = web3.eth.contract(address=dapiserver_address, abi=abi)
value, timestamp = contract.functions.readDataFeedWithId(beaconId).call()
print(value, timestamp) # Example output -> 1000020230000000000 1664290764