forked from droplinked/droplinked-contracts-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
76 lines (70 loc) · 2.18 KB
/
fetch.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
const ethers = require('ethers')
// Ethereum smart contract ABIs
const priceFeedABI = [
{
inputs: [],
name: "getLatestPrice",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
];
const bootstrapRegistryABI = [
{
inputs: [
{
internalType: "string",
name: "contractName",
type: "string",
},
],
name: "getContractAddress",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
];
// Function to get the contract address from a bootstrap registry
const getGasPriceContractAddress = async (provider) => {
const bootstrapRegistryContract = new ethers.Contract(
'0xDAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5',
bootstrapRegistryABI,
provider
);
return await bootstrapRegistryContract.getContractAddress("pricefeed");
};
// Function to fetch the latest price from the smart contract
const fetchLatestPrice = async () => {
const provider = new ethers.providers.JsonRpcProvider("https://governors.testnet.redbelly.network/");
const priceFeedContractAddr = await getGasPriceContractAddress(provider);
const priceFeedContract = new ethers.Contract(
priceFeedContractAddr,
priceFeedABI,
provider
);
try {
// Returns the latest price (6 Decimal Places) and the timestamp when it was updated
return await priceFeedContract.getLatestPrice();
} catch (error) {
console.error("Error calling contract function:", error);
}
};
// Calling fetchLatestPrice and logging the result
fetchLatestPrice().then(console.log);