Skip to content

Instantly share code, notes, and snippets.

@dglowinski
Created December 18, 2017 16:22
Show Gist options
  • Save dglowinski/d201b707c67f0ac53c188e7943649b9f to your computer and use it in GitHub Desktop.
Save dglowinski/d201b707c67f0ac53c188e7943649b9f to your computer and use it in GitHub Desktop.
const axios = require("axios");
const Web3 = require("web3");
const HDWalletProvider = require("truffle-hdwallet-provider");
const conf = require("../app/config/config");
const abiArray = require("../app/oracle/abi/abi.json");
conf.load(config => {
const logger = require("../app/modules/logger");
const mnemonic = config["oracle.mnemonic"];
const walletHD = new HDWalletProvider(
mnemonic,
config["oracle.infuraAddress"] + config["oracle.token"]
);
const web3 = new Web3(walletHD);
const contractAddress = config["oracle.contractAddress"];
const instance = new web3.eth.Contract(abiArray, contractAddress);
// updateOracle(100)
getPrice()
function getPrice(cb) {
instance.methods
.priceEthUsd()
.call()
.then(price => {
console.log("price: ", price);
cb && cb(price);
});
}
function updateOracle(price) {
instance.methods
.updateEthUsd(Math.round(price * 100))
.send({
gas: 200000,
gasPrice: web3.utils.toWei(
config["oracle.gasPrice"].toString(),
"gwei"
),
from: walletHD.address
})
.on("transactionHash", hash => {
console.log("hash: ", hash);
logger.info(`Update transaction sent ${price} ${hash}`);
})
.on("receipt", receipt => {
console.log("receipt: ", receipt);
if (receipt.status === "0x0") {
logger.error(
`Update transaction failed receipt ${receipt.transactionHash}`
);
} else {
logger.info(
`Updated contract price: ${price} ${receipt.transactionHash}`
);
}
})
.on("error", error => {
logger.error(`Update contract error: ${error}`);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment