Coder Social home page Coder Social logo

hunterlong / fiatcontract Goto Github PK

View Code? Open in Web Editor NEW
36.0 4.0 8.0 117 KB

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

JavaScript 100.00%
ethereum solidity ethereum-contract smart-contracts cryptocurrency coinmarketcap

fiatcontract's People

Contributors

hunterlong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fiatcontract's Issues

Adding support for ETH/CNY price and ETH/JPY price

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

Not Updating

Hi Hunter--

We sent along some more support; however, this time it doesn't seem to have rebooted the updates.

Any thoughts?

Thank you!

Update times

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?

thanks

Just wanna say thanks, bro)
Really useful)

Update times - gas price trouble

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.

No Longer Updating?

It looks like this may have lost support. Is that true? If so, any recommended alternatives?

MetaMask Interference

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.