Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header><img src="https://docs.synthetix.io/img/logos/synthetix_blue_logo.png" width="100%" /></header>

<main>
  <p><button name="start-l1">Fetch L1</button> <button name="start-ovm">Fetch Optimism</button></p>
  <div class="two-cols">
    <ul>
      <li id="volume"><strong>Total Volume:</strong><var>?</var></li>
      <li id="count"><strong>Trades:</strong><var>?</var></li>    
      <li id="fees"><strong>Fees:</strong><var>?</var></li>
      <li id="accounts"><strong>Unique Accounts:</strong><var>?</var></li>
      <li id="mean"><strong>Average Trade Size:</strong><var>?</var></li>
    </ul>
    <div id="chart"></div>
  </div>
  
  <h1>Exchanges in the last 24 hours</h1>
  <p class="helper-text">Note: Fees show negative in situations where an exchange and corresponding oracle update have gone into the same block, with the exchange going in before the oracle update. Note that this is only a <a href="https://github.com/Synthetixio/synthetix-subgraph/issues/9">reporting bug here due to The Graph limitation</a>, not on-chain.</p>
  <table cellspacing=0>
    <thead><tr><th>Tx</th><th>Block</th><th>From</th><th>Synth From</th><th>Amount</th><th>(USD)</th><th>Synth To</th><th>Fees (USD)</th><th>Destination</th><th>Gwei</th><th>When</th></tr></thead>
    <tbody></tbody>
  </table>
</main>

              
            
!

CSS

              
                @snx-color: #004433;

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

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

main {
  padding: 20px;
}

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;
	}
}

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

ul {
	padding: 0;
	li {
		padding-left: 3px;
		list-style-type: none;
		strong {
			display: inline-block;
			width: 200px;
		}
	}
}

var {
	font-family: "Courier New", "Courier";
	font-size: 18px;
}

.helper-text { 
  color: #aaa;
}

table tbody {
  tr:nth-child(odd) {
    background-color: #eee;
  }
  td {
    padding: 5px;
  }
}

.two-cols {
  display: flex;
  > *:first-child {
    width: 400px;
  }
  > *:last-child {
    width: calc(100% - 420px);
  }
}

.helper-text {
  color: #ccc;
  font-style: italic;
  a {
    color: #aaa;
  }
}
              
            
!

JS

              
                const snxjs = synthetix({ newtork: 'mainnet' }); 
const { formatEther, formatUnits, toUtf8String, formatBytes32String } = snxjs.utils;

const loadingGIF = '<img src="https://media.giphy.com/media/TvLuZ00OIADoQ/giphy.gif" width=150 />';
const tableTarget = document.querySelector('table > tbody');

const esLink = ({ txn, address, label, useOvm }) => 
 `<a target="_blank" href="https://${useOvm ? 'optimistic.' : ''}etherscan.io/${txn ? 'tx' : 'address'}/${txn || address}">${label || txn || address}</a>`

const lookup = async ({ useOvm = false }) => {
  try {
    tableTarget.innerHTML = `<tr><td colspan=100>${loadingGIF}</td><tr>`;
    
    const ts = Math.floor(Date.now()/1e3);
    const oneDayAgo = ts - (3600 * 24);

    const synthExchanges = await snxData({ networkId: useOvm ? 10 : 1 }).synthExchanges({ minTimestamp: oneDayAgo });
    
    const exchanges = synthExchanges.map(({ hash, from, fromCurrencyKey, toCurrencyKey, fromAmountInUSD, feesInUSD, fromAmount, block, timestamp, toAddress, gasPrice, date }, i) => {
      
      return {
        hash,
        from,
        fromKey: fromCurrencyKey,
        toKey: toCurrencyKey,
        amount: fromAmount,
        usd: fromAmountInUSD,
        fees: feesInUSD,
        block,
        date: new Date(timestamp),
        account: toAddress,
        gasPrice,
        timestamp
      }
    });
    
    tableTarget.innerHTML = exchanges.map(({ hash, account, from, fromKey, toKey, amount, block, usd, date, gasPrice, fees }) => `
      <tr>
        <td>${esLink({ txn: hash, label: '<img width=24 src="https://etherscan.io/images/favicon2.ico" />', useOvm })}</td>
        <td>${block}</td>
        <td>${esLink({ address: from, label: `${from.slice(0, 6)}...${from.slice(-4)}`, useOvm })}</td>
        <td><img width=32 src="https://raw.githubusercontent.com/Synthetixio/synthetix-assets/master/synths/${fromKey}.svg" />${fromKey}</td>
        <td>${numbro(amount).format('0,0.0000')}</td>
        <td>${numbro(usd).format('$0,0.00')}</td>
        <td><img width=32 src="https://raw.githubusercontent.com/Synthetixio/synthetix-assets/master/synths/${toKey}.svg" />${toKey}</td>
        <td>${numbro(fees).format('$0,0.00')} (${numbro(fees/usd).format('0.00%')})</td>
        <td>${account !== from ? esLink({ address: account, label:  `${account.slice(0, 6)}...${account.slice(-4)}`, useOvm }) : ''}</td>
        <td>${formatUnits(gasPrice, 'gwei')}</td>
        <td class=helper-text style="font-size: 12px;">${date}</td>
      </tr>
    `).join('');
     
    const volume = exchanges.reduce((memo, { usd }) => Number(usd) + memo, 0);
    document.querySelector('#volume var').innerHTML = numbro(volume).format('$0,0.00');
    
    const fees = exchanges.reduce((memo, { fees}) => Number(fees) + memo, 0);
    document.querySelector('#fees var').innerHTML = numbro(fees).format('$0,0.00');
    
    const mean = volume / exchanges.length;
    document.querySelector('#mean var').innerHTML = numbro(mean).format('$0,0.00');
          
    const uniqAccounts = new Set(exchanges.map(({ from }) => from)).size;
    document.querySelector('#accounts var').innerHTML = uniqAccounts;
    
    document.querySelector('#count var').innerHTML = exchanges.length;
    
    //const maxGasLimit = await snxjs.Synthetix.gasPriceLimit();
    //document.querySelector('#gas var').innerHTML = formatUnits(maxGasLimit, 'gwei') + ' Gwei';
    
    
    // now fetch and populate chart
    /*
    const chartTarget = document.querySelector('#chart');
    chartTarget.innerHTML = loadingGIF;
    
    // TODO - below should be moved into snxData
    
    const type = 'days';
    const unit = 7;
    const typeToLabelFormatMap = {
			months: ts => moment(ts).format('MMM YY'),
			weeks: ts => 'Week ' + moment(ts).format('ww, YY'),
			days: ts => moment(ts).format('DD MMM YY'),
		};
		const typeWithoutPlural = type.slice(0, type.length - 1);
		// get entries from beyond a certain point
		const timestampInSecs = moment()
			.startOf(typeWithoutPlural)
			.subtract(unit, type)
			.unix();

		// results are reverse chronologically ordered
		const results = await snxData.exchanges.since({ timestampInSecs });
		const groups = [];
		const _cache = {};
		const lastMomentInWindow = moment(results[0].timestamp).endOf(typeWithoutPlural);

		for (const { timestamp, fromAmountInUSD, feesInUSD, fromAddress } of results) {
			const i = Math.abs(moment(timestamp).diff(lastMomentInWindow, type));
			// initialize the grouping
			groups[i] = groups[i] || {
				volume: 0,
				fees: 0,
				unique: 0,
				label: typeToLabelFormatMap[type](timestamp),
			};
			_cache[i] = _cache[i] || {};

			groups[i].volume = Math.round(fromAmountInUSD + groups[i].volume);
			groups[i].fees = Math.round(feesInUSD + groups[i].fees);
			groups[i].unique += !_cache[i][fromAddress] ? 1 : 0;

			_cache[i][fromAddress] = true; // track this address
		}
    
    new frappe.Chart(chartTarget, {  
      title: 'Last 7 Days Exchange Volume (in millions USD)',
      data: {
        labels: groups.map(({ label }) => label),
        datasets: [
          {
            name: 'USD',
            values: groups.map(({ volume }) => volume/1e6)
          }
        ]
      },
      type: 'bar', 
      colors: ['#7cd6fd', '#743ee2']
    });
    */
  } catch (err) {
    console.error(err);
  }
};

document.querySelector('button[name=start-l1]').addEventListener('click', lookup);

document.querySelector('button[name=start-ovm]').addEventListener('click', () => lookup({ useOvm: true }));
              
            
!
999px

Console