Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed usage for reading state #3

Closed
jjgonecrypto opened this issue Aug 6, 2020 · 0 comments
Closed

Proposed usage for reading state #3

jjgonecrypto opened this issue Aug 6, 2020 · 0 comments

Comments

@jjgonecrypto
Copy link
Contributor

This is from the codepen I have on synth distribution: https://codepen.io/justinjmoses/pen/vMKywz

It assumes that the browser bundle has been added to the codepen (or page), exposing the synthetix global to window.

  const network = 'mainnet';
  const { formatEther } = window.synthetix.ethers.utils; 
  const snxjs = window.synthetix({ network }); 
  // this instance exposes props for the given network: synths, sources, targets, users, as well as helper function toBytes32 - as per synthetix: https://github.com/Synthetixio/synthetix/blob/develop/index.js#L199

  const synths = snxjs.synths.map(({ name }) => name); 
  const fromBlock = blockTarget.value;
  const blockOptions = fromBlock ? { blockTag: Number(fromBlock) } : {};
  
  let totalInUSD = 0;
  
  // note how I can pass it a string and it will convert it to bytes32 for me
  const snxPrice = formatEther(await snxjs.ExchangeRates.rateForCurrency('SNX', blockOptions));  // note blockOptions must be passed to `ethers.Contract` as the final parameter (and fails if no archive node)
  // or better yet, can't we just expose these functions top level? 
  const snxPrice = formatEther(await snxjs.rateForCurrency('SNX', blockOptions)); 

  let results = await Promise.all(synths.map(async synth => {
    const totalAmount = await snxjs[synth].contract.totalSupply(blockOptions);
      
    const totalSupply = formatEther(totalAmount);  
    const rateForSynth = formatEther(await snxjs.rateForCurrency(synth, blockOptions));
    const totalSupplyInUSD = rateForSynth * totalSupply;
    totalInUSD += totalSupplyInUSD;
    const rateIsFrozen = await snxjs.rateIsFrozen(synth, blockOptions);
     
    return { synth, totalAmount, totalSupply, rateForSynth, totalSupplyInUSD, rateIsFrozen };
  }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants