Get started integrating the Swap SDK into your app
Installation
You can install the SDK with yarn:
yarn add @traderxyz/nft-swap-sdk
or npm:
npm install @traderxyz/nft-swap-sdk
Configuration
To use the SDK, create a new NftSwapV4 instance.
import { NftSwapV4 } from'@traderxyz/nft-swap-sdk';// Supply a provider, signer, and chain id to get started// Signer is optional if you only need read-only methodsconstnftSwapSdk=newNftSwapV4(provider, signer, chainId);
Note: 0x v4 contracts with NFT support are currently live on Ethereum Mainnet, Polygon, Optimism, BSC, Fantom, and Celo, and Ropsten Testnet.
Quick Start
Let's walk through the most common NFT swap case: swapping an NFT (ERC721 or ERC1155) with an ERC20.
Swap an NFT with an ERC20
import { NftSwapV4 } from'@traderxyz/nft-swap-sdk';// Scenario: User A wants to sell their CryptoPunk for 420 WETH// Set up the assets we want to swap (CryptoPunk #69 and 420 WETH)constCRYPTOPUNK= { tokenAddress:'0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb', tokenId:'69', type:'ERC721',// 'ERC721' or 'ERC1155'};constFOUR_HUNDRED_TWENTY_WETH= { tokenAddress:'0x6b175474e89094c44da98b954eedeac495271d0f',// WETH contract address amount:'420000000000000000000',// 420 Wrapped-ETH (WETH is 18 digits) type:'ERC20',};// [Part 1: Maker (owner of the Punk) creates trade]constnftSwapSdk=newNftSwapV4(provider, signerForMaker,CHAIN_ID);constwalletAddressMaker='0x1234...';// Approve NFT to trade (if required)awaitnftSwapSdk.approveTokenOrNftByAsset(CRYPTOPUNK, walletAddressMaker);// Build orderconstorder=nftSwapSdk.buildOrder(CRYPTOPUNK,// Maker asset to swapFOUR_HUNDRED_TWENTY_WETH,// Taker asset to swap walletAddressMaker);// Sign order so order is now fillableconstsignedOrder=awaitnftSwapSdk.signOrder(order);// [Part 2: Taker that wants to buy the punk fills trade]constnftSwapSdk=newNftSwap(provider, signerForTaker,CHAIN_ID);constwalletAddressTaker='0x9876...';// Approve USDC to trade (if required)awaitnftSwapSdk.approveTokenOrNftByAsset(FOUR_HUNDRED_TWENTY_WETH, walletAddressTaker);// Fill order :)constfillTx=awaitnftSwapSdk.fillSignedOrder(signedOrder);constfillTxReceipt=awaitnftSwapSdk.awaitTransactionHash(fillTx.hash);console.log(`🎉 🥳 Order filled. TxHash: ${fillTxReceipt.transactionHash}`);
That's it! More examples and advanced usage can be found in the examples documentation.