Synths can be directly exchanged for one another with zero slippage. To view a full list of all the synthetic assets available for trading on Synthetix, visit our tokens section.
Note: Synthetix uses a proxy system. The ABI of the underlying Synthetix ProxyERC20 contract you need is Synthetix. Learn more about how proxies work by visiting the overview page
const{SynthetixJs}=require('synthetix-js');constprivateKey='0x'+'1'.repeat(64);// don't actually put a private key in code obviously// parameters: default provider, default networkId, private key as a stringconstnetworkId=11155111;// sepolia, (use 1 for mainnet)constsigner=newSynthetixJs.signers.PrivateKey(null,networkId,privateKey);constsnxjs=newSynthetixJs({signer,networkId});const{toUtf8Bytes32,parseEther}=snxjs.utils;(async()=>{try{// send transactionconsttxn=awaitsnxjs.Synthetix.exchange(toUtf8Bytes32('sUSD'),parseEther('0.001'),toUtf8Bytes32('iETH'));console.log('hash is mining',txn.hash);// wait for miningawaittxn.wait();// fetch logs of transactionconst{logs}=awaitsigner.provider.getTransactionReceipt(txn.hash);// show themconsole.log(JSON.stringify(logs,null,'\t'));}catch(err){console.log('Error',err);}})();
constsynthetix=require('synthetix');// nodejsconstethers=require('ethers');// nodejs// or using ES modules:// import synthetix from 'synthetix';// import ethers from 'ethers';constnetwork='sepolia';constprovider=ethers.getDefaultProvider(network==='mainnet'?'homestead':network);const{address}=synthetix.getTarget({network,contract:'ProxyERC20'});const{abi}=synthetix.getSource({network,contract:'Synthetix'});constprivateKey='0x'+'1'.repeat(64);// don't actually put a private key in code obviouslyconstsigner=newethers.Wallet(privateKey).connect(provider);// see https://docs.ethers.io/ethers.js/html/api-contract.html#connecting-to-existing-contractsconstSynthetix=newethers.Contract(address,abi,signer);const{toBytes32}=synthetix;(async()=>{try{// send transactionconsttxn=awaitSynthetix.exchange(toBytes32('sUSD'),ethers.utils.parseEther('0.001'),toBytes32('iETH'));// wait for miningawaittxn.wait();// fetch logs of transactionconst{logs}=awaitprovider.getTransactionReceipt(txn.hash);// displayconsole.log(JSON.stringify(logs,null,'\t'));}catch(err){console.log('Error',err);}})();
pragma solidity0.5.16;import"synthetix/contracts/interfaces/IAddressResolver.sol";import"synthetix/contracts/interfaces/ISynthetix.sol";contractMyContract{// This should be instantiated with our ReadProxyAddressResolver// it's a ReadProxy that won't change, so safe to code it here without a setter// see https://docs.synthetix.io/addresses for addresses in mainnet and testnetsIAddressResolverpublicsynthetixResolver;constructor(IAddressResolver_snxResolver)public{synthetixResolver=_snxResolver;}functionsynthetixExchange(bytes32src,uintamount,bytes32dest)external{)ISynthetixsynthetix=synthetixResolver.getAddress("Synthetix");require(synthetix!=address(0),"Synthetix is missing from Synthetix resolver");// This check is what synthetix.exchange() will perform, added here for explicitnessrequire(!synthetix.isWaitingPeriod(src),"Cannot exchange during the waiting period");// Exchange for msg.sender = address(MyContract)synthetix.exchange(src,amount,dest);// Note: due to Fee Reclamation in SIP-37, the following actions will fail if attempted in the// same block (the waiting period for the "to" synth must first expire)// synthetixResolver.getSynth(dest).transfer(address(0), 1e12)// synthetix.exchange(dest, 1e12, "sBTC");// synthetix.settle(dest);}functionsynthetixExchangeOnBehalf(addressuser,bytes32src,uintamount,bytes32dest)external{ISynthetixsynthetix=synthetixResolver.getAddress("Synthetix");require(synthetix!=address(0),"Synthetix is missing from Synthetix resolver");// This check is what synthetix.exchange() will perform, added here for explicitnessrequire(!synthetix.isWaitingPeriod(src),"Cannot exchange during the waiting period");// Note: this will fail if `DelegateApprovals.approveExchangeOnBehalf(address(MyContract))` has// not yet been invoked by the usersynthetix.exchangeOnBehalf(user,src,amount,dest);// Note: due to Fee Reclamation in SIP-37, the following actions will fail if attempted in the// same block (the waiting period for dest must first expire)// synthetixResolver.getSynth(dest).transferFrom(user, address(0), 1e12)// synthetix.exchangeOnBehalf(user, dest, 1e12, "sBTC");// synthetixResolver.getAddress("Exchanger").settle(user, dest)}}