hunterlong / fiatcontract Goto Github PK
View Code? Open in Web Editor NEWEnables ethereum smart contracts to get how much $0.01 is in ETH. Implement in your own smart contract with ease.
Home Page: https://fiatcontract.com
Enables ethereum smart contracts to get how much $0.01 is in ETH. Implement in your own smart contract with ease.
Home Page: https://fiatcontract.com
Hi,
How about updates? Are you supporting this project?
Thanks.
Very Nice!!!
If you add support for ETH/CNY price and ETH/JPY price, then it will allow users to obtain ETH/SDR price.
In fact, it would be even better if you could add support directly for ETH/SDR price.
The current weights of SDR are given here.
As you can see, they may change every few years.
But all things considered, they are "pretty constant".
For example, here is a contract which maintains ETH/SDR price as a tuple of enumerator and denominator, and allows an accurate-as-possible conversion of ETH to SDR and SDR to ETH:
pragma solidity ^0.4.23;
import "./Ownable.sol";
contract MyContract is Ownable {
uint256 private ethValue;
uint256 private sdrValue;
constructor() public {
ethValue = 1;
sdrValue = 1;
}
function setRatio(uint256 _ethValue, uint256 _sdrValue) external onlyOwner {
ethValue = _ethValue;
sdrValue = _sdrValue;
}
function toSdrAmount(uint256 ethAmount) external view returns (uint256) {
return ethAmount * sdrValue / ethValue;
}
function toEthAmount(uint256 sdrAmount) external view returns (uint256) {
return sdrAmount * ethValue / sdrValue;
}
}
And here is an off-chain script (NodeJS) for updating the ETH/SDR price in that contract.
let fs = require("fs");
let Web3 = require("web3");
let request = require("request");
let BigNumber = require("bignumber.js");
let ETH_NODE_ADDRESS = process.argv[2];
let OWNER_PUBLIC_KEY = process.argv[3];
let OWNER_PRIVATE_KEY = process.argv[4];
let CONTRACT_ADDRESS = process.argv[5];
async function updatePrice(rates) {
web3 = new Web3(ETH_NODE_ADDRESS);
let abi = fs.readFileSync("MyContract.abi").toString();
let contract = new web3.eth.Contract(JSON.parse(abi), CONTRACT_ADDRESS);
let floatPrice = rates.USD * 0.4173 + rates.EUR * 0.3093 + rates.CNY * 0.1092 + rates.JPY * 0.0833 + rates.GBP * 0.0809;
let [sdrValue, ethValue] = new BigNumber(floatPrice).toFraction();
let transaction = contract.methods.setRatio(ethValue, sdrValue);
let options = {
to : transaction._parent._address,
data: transaction.encodeABI(),
gas : await transaction.estimateGas({from: OWNER_PUBLIC_KEY}),
};
let signedTransaction = await web3.eth.accounts.signTransaction(options, OWNER_PRIVATE_KEY);
let transactionReceipt = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);
let fixedPrice = await contract.methods.toSdrAmount(1).call();
console.log(`\nrates = ${JSON.stringify(rates, null, 4)}`);
console.log(`floatPrice: 1 ETH = ${floatPrice} SDR`);
console.log(`fixedPrice: 1 ETH = ${fixedPrice} SDR`);
if (web3.currentProvider.constructor.name == "WebsocketProvider")
web3.currentProvider.connection.close();
}
request.get("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,CNY,JPY,GBP", function (error, response, body) {
if (error)
console.log("error:", error);
else if (!response)
console.log("no response");
else if (response.statusCode != 200)
console.log("bad response");
else
updatePrice(JSON.parse(body));
});
NPM dependencies in case needed:
"bignumber.js": "^7.0.0"
"web3": "1.0.0-beta.34"
There are pros & cons in this off-chain solution compared with your on-chain solution, so it would be great to have both options at hand.
Thanks
Hi Hunter--
We sent along some more support; however, this time it doesn't seem to have rebooted the updates.
Any thoughts?
Thank you!
Hi! I have noticed that the main net contract is updated only every 12 hours instead of every hour as claimed on your website. Maybe a site miss-statement?
Just wanna say thanks, bro)
Really useful)
Hi, recently the standard gas price used on the network has been increased and the updates on the contract are not going through with the 2 Gwei gas price.
Maybe an increase of the Gas price together to a increased update time interval could balance the contract.
It looks like this may have lost support. Is that true? If so, any recommended alternatives?
With MetaMask installed on Chrome desktop browser,
Uncaught TypeError: Cannot read property 'valueOf' of null
at eth.js:403
at Object.callback (inpage.js:1)
at inpage.js:1
at inpage.js:1
at r.onreadystatechange (inpage.js:1)
at r.t.dispatchEvent (inpage.js:1)
at r._setReadyState (inpage.js:1)
at r._onHttpResponseEnd (inpage.js:1)
at n.IncomingMessage. (inpage.js:1)
at inpage.js:1
This appears to be a new anomaly presumably caused by something changed in MetaMask. Confirmed on two different machines. Can resolve by disabling MetaMask.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.