-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoracle.js
103 lines (90 loc) · 2.69 KB
/
oracle.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const Web3 = require("web3");
const rpcUrl = // "https://ropsten.infura.io/InsertYourAPIKey"; //The url which links you to the ethereum network - Ropsten in this case.
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const privateKey = //"0xYourPrivateKey"; //Private Key to sign transactions with.
const account = web3.eth.accounts.privateKeyToAccount(privateKey); //account associated with private key
const fetch = require('node-fetch'); //To fetch APIs
const signedTxs = [];
let nonce;
//contract abi - the below is for the sample contract.
const abi = [
{
"constant": false,
"inputs": [
{
"name": "_number",
"type": "uint256"
}
],
"name": "setNumber",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getNumber",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "number",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
const contractAddress = //"address of deployed contract";
const sampleContract = new web3.eth.Contract(abi, contractAddress);
//Example Oracle sets number from the api below - gas price on mainnet.
async function main() {
let gasReq = await fetch('https://ethgasstation.info/json/ethgasAPI.json');
let gasInfo = await gasReq.json();
let gasAvg = await (gasInfo.average);
//sets input for setNumber function as gasAvg.
//Makes this into an object of the sendTx function (below) and triggers that function.
await sendTx(sampleContract.methods.setNumber(gasAvg));
//print average gas price in console
console.log("Avg gas price",gasAvg);
}
//function sending the transaction from our configured wallet (the private key we provided)
async function sendTx(txObject) {
const txTo = contractAddress;
const txData = txObject.encodeABI(); //txObject was set in main funtion
const txFrom = account.address;
const txKey = account.privateKey;
const gasPrice = (5*(10**9)); //5 gwei gas price
const gasLimit = await txObject.estimateGas(); //estimated gas cost of trnsaction
const tx = {
from : txFrom,
to : txTo,
nonce : nonce,
data : txData,
gas : gasLimit, gasPrice
};
//sign the transaction
const signedTx = await web3.eth.accounts.signTransaction(tx, txKey);
nonce++;
// push transaction - dont wait for confirmations just wait till broadcasted
signedTxs.push(signedTx.rawTransaction)
//send transaction
web3.eth.sendSignedTransaction(signedTx.rawTransaction, {from:account});
}
main();