Contracts Interface to Synthetix
The official JavaScript library for interacting with Synthetix protocol contracts.
This library can be used in 2 different environments:
- Common-js module for node environments
- UMD module for browser environments
Instantiating in node.js or the browser
const { synthetix } = require('@synthetixio/contracts-interface');
import { synthetix } from '@synthetixio/contracts-interface';
const { synthetix } = window;
const snxjs = synthetix({ network: 'mainnet' });
import { synthetix, Network } from '@synthetixio/contracts-interface';
const snxjs = synthetix({ network: Network.Mainnet });
Reading state
const snxjs = synthetix({ network: 'mainnet' });
const owner = await snxjs.contracts.Synthetix.owner();
const { toBytes32 } = snx;
const totalIssuedSynths = await snxjs.contracts.Synthetix.totalIssuedSynths(toBytes32('sUSD'));
const { formatEther } = snxjs.utils;
formatEther(await snxjs.contracts.SynthsUSD.totalSupply());
formatEther(await snxjs.contracts.ExchangeRates.rateForCurrency(snxjs.toBytes32('SNX')));
const snxAtBlock12m = await snxjs.contracts.ExchangeRates.rateForCurrency(snxjs.toBytes32('SNX'), {
blockTag: 12e6,
});
Signing transactions
const provider = ethers.providers.getDefaultProvider('kovan');
const signer = new ethers.Wallet(
'0xa0d951c494421559c63089093b020cf2f7aac003c916967dc36e989bc695222e',
provider
);
const snxjs = synthetix({ network: 'mainnet', signer });
const response = await snxjs.contracts.NativeEtherWrapper.mint({
value: parseEther('0.01'),
gasPrice: parseUnits('5', 'gwei'),
gasLimit: 500e3,
});
console.log('Submitted', response.hash);
await response.wait();
console.log('Mined', `https://etherscan.io/tx/${response.hash}`);
See the examples folder for more usage details.