123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <header><img src="https://developer.synthetix.io/api/img/logo.png" /></header>

<main>
  
  <p><input name="network" placeholder="Enter network (none for mainnet)" /> <input name="block" placeholder="Enter block number (none for latest)" />
    <button name="start">Start</button></p>
  <section>
    <div>
      <p id="network"><strong>Network:</strong> <var>...</var></p>
      <table cellpadding=0 cellspacing=0>
        <thead>
          <tr>
            <th></th>
            <th>Synth</th>
            <th>Rate</th>
            <th>Total Supply</th>
            <th>USD Value</th>
            <th>Percentage</th>
            <th>Non-Zero</th>
        </thead>
        <tbody>
        </tbody>
      </table>

      <dl>
          <dt>Total USD value of Synths</dt>
          <dd id="synthsTotal"></dd>
          <dt>SNXUSD price</dt>
          <dd id="snxusdPrice"></dd>
      </dl>
    </div>
    <div><h1>Compare Synths</h1><div id="graph"></div></div>
  </section>

  
</main>
            
          
!
            
              @snx-color: #004433;

body {
  font-family: 'Helvetica';
  padding: 0;
  margin: 0;
}

header {
  background-color: #0b0816;
  min-height: 50px;
  img {
    padding: 8px;
  }
}

main {
  section {
     display: flex;

     > div {
       width: 50%;
    }
    > div:nth-child(2) {
      width: calc(50% - 40px);
      margin-left: 40px;
     }
  }
  padding: 10px;
}

input { 
  width: 200px; 
  padding: 5px;
}

button {
	margin: 0px 0px 20px 0;
	padding: 10px 20px;
	font-size: 14px;
	border-radius: 4px;
	background-color: @snx-color;
	color: white;
	font-weight: bold;
	opacity: 0.8;
	cursor: pointer;
	&:hover {
		opacity: 1
	}
	&#cancel {
		background-color: red;
	}
}

table {
  padding: 0;
  .frozen {
    color: #aaa;
  }
  th {
    text-align: right
  }
  td, th {
    padding: 8px
  }
  tr:nth-child(even) {background: #EEE}
  td { text-align: right }
}
            
          
!
            
              
const tbodyTarget = document.querySelector('tbody');
const inputTarget = document.querySelector('input[name=network]');
const blockTarget = document.querySelector('input[name=block]');
const graphTarget = document.querySelector('#graph');
const networkTarget = document.querySelector('#network var');

const SUPPORTED_NETWORKS = {
  'mainnet': 1,
  'ropsten': 3,
  'rinkeby': 4,
  'kovan': 42,
};

const loadingGIF = '<img src="https://media.giphy.com/media/TvLuZ00OIADoQ/giphy.gif" width=150 />';

const start = async () => {
  tbodyTarget.innerHTML = loadingGIF;
  graphTarget.innerHTML = loadingGIF;
  
  const network = (inputTarget.value.toLowerCase() in SUPPORTED_NETWORKS) ?  inputTarget.value.toLowerCase() : 'mainnet';
  
  networkTarget.innerHTML = network;
  
  const networkId = SUPPORTED_NETWORKS[network];
  const snxjs = new SynthetixJs.SynthetixJs({ networkId }); 
  const toUtf8Bytes = SynthetixJs.SynthetixJs.utils.formatBytes32String;
  const formatEther = snxjs.utils.formatEther;
  
  const fromBlock = blockTarget.value;
  const blockOptions = fromBlock ? { blockTag: Number(fromBlock) } : {};
  
  const synths = snxjs.contractSettings.synths.map(({ name }) => name);
  let totalInUSD = 0;
  const snxPrice = await snxjs.ExchangeRates.contract.rateForCurrency(toUtf8Bytes('SNX'), blockOptions) / 1e18;
  let results = await Promise.all(synths.map(async synth => {
    const totalAmount = await snxjs[synth].contract.totalSupply(blockOptions);
      
    const totalSupply = formatEther(totalAmount);  
    const rateForSynth = await snxjs.ExchangeRates.contract.rateForCurrency(toUtf8Bytes(synth), blockOptions) / 1e18;
    const totalSupplyInUSD = rateForSynth * totalSupply;
    totalInUSD += totalSupplyInUSD;
    const rateIsFrozen = await snxjs.ExchangeRates.contract.rateIsFrozen(toUtf8Bytes(synth), blockOptions);
    return { synth, totalAmount, totalSupply, rateForSynth, totalSupplyInUSD, rateIsFrozen };
  }));
  
  results = results.sort((a, b) => a.totalSupplyInUSD > b.totalSupplyInUSD ? -1 : 1);
  tbodyTarget.innerHTML = '';
  results.forEach(({ synth, rateForSynth, totalSupply, totalSupplyInUSD, rateIsFrozen }, i) => {
    tbodyTarget.innerHTML += `<tr class="${rateIsFrozen && 'frozen'}"><td>${i+1}</td><td>${synth}</td><td>${numbro(rateForSynth).format('0.0000')}</td><td>${numbro(totalSupply).format('0,000.0000')} ${synth}</td><td>${numbro(totalSupplyInUSD).format('0,000.00')}</td><td>${numbro(100*totalSupplyInUSD/totalInUSD).format('0.00')}%</td>
<td>${totalSupply > 0 ? '✅' : '❌'}</td>
<td><a target=_blank href="http://api.ethplorer.io/getTopTokenHolders/${snxjs[synth].contract.address}?apiKey=freekey&limit=100">Holders</a></td>
</tr>`;
  });
  
  document.querySelector('#synthsTotal').innerHTML = numbro(totalInUSD).format('0,000.00');
  document.querySelector('#snxusdPrice').innerHTML = numbro(snxPrice).format('0.0000');
   
  const resultsWithValues = results.filter(({ synth }) => synth !== 'sUSD').filter(({ totalSupplyInUSD }) => Number(totalSupplyInUSD) > 100);
  
  new frappe.Chart(graphTarget, {  
    title: 'Synth Breakdown',
    data: {
      labels: resultsWithValues.map(({ synth }) => synth),
      datasets: [
        {
          name: 'USD',
          values: resultsWithValues.map(({ totalSupplyInUSD }) => totalSupplyInUSD)
        }
      ]
    },
    type: 'bar', 
    colors: ['#7cd6fd', '#743ee2']
  });

};

document.querySelector('button[name=start]').addEventListener('click', start);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console