NFT Trader Javascript SDK
Welcome to the official repository of the P2P OTC NFT Trader platform developed by the official NFT Trader team!
The primary goal with this package is to offer web3 developers a simple design to integrate our robust NFT Trader technology inside other external DApps (Decentralized Applications). This is open for all developers who would like to unlock the ability for your applications and protocols to have a system which allows for the secure swapping of NFTs (erc721), ERC20, ERC1155 tokens on Ethereum and other digital assets on EVM congruent chains.
The package is available for Node.js and browser platforms.
Installation
Using npm:
npm install @nfttrader/sdk-js
Using local js file:
<script src="/path/to/your/js/nfttrader-sdk.js"></script>
CommonJS usage
const NFTTraderSDK = require('@nfttrader/sdk-js')
const sdk = new NFTTraderSDK(...)
Browser usage
const sdk = new NFTTraderSDK(...)
Introduction
The NFTTraderSDK
class object is the action to which you can interact with the NFT Trader Smart Contract in a convenient manner. It contains a set of methods that allow you to simplify the interaction process with the blockchain.
The SDK is compatible on the following blockchains:
- Ethereum Mainnet
- Ethereum Rinkeby
- Ethereum Ropsten
- Ethereum Kovan
- Ethereum Goerli
- Polygon
- Polygon Mumbai
- xDai chain (Gnosis chain)
What Can I Do With This SDK?
With the NFT Trader Javascript SDK you can create a swap order, finalize a swap order, edit the Taker of the order or cancel a swap order. Furthermore, it provides you some other utility based methods that allow you to read details of the order book, listen to specific events from the smart contract and includes helper functions in order to help you format the parameters of the NFT Trader Smart Contract in the correct way. We will describe everything in more detail throughout the next sections, so do not worry if you don't fully understand what has already been stated. 🤓
The end purpose is very simple once you learn how to master the SDK. You will be able to include these sets of functionalities directly into your own platform and enable the swapping features for NFTs, ERC20 tokens and other digital assets. This is a very useful tool for many applications, particularly if you are planning to build a game with a swap market feature for trading game assets or an application / market in which you want to integrate a method for your users to swap assets with other users. There are many web3 protocols which will find this SDK useful for the purpose of trading digital assets.
Table Of Contents
How The Swap Works
The following scheme describes visually how the swap logic works.
Here is a more in depth explanation for the image above.
The swap "cycle" is composed by the following phases:
- Create swap phase (mandatory)
- Close swap phase (optional)
- Edit Taker phase (optional)
- Cancel swap phase (optional)
Create Swap Phase
The creation of the swap is marked as "mandatory" because it is the first operation you need to do in order to start the interaction with the system. When you are in this phase you can define the assets that will be included with the trades (ex. the Creator assets and the Taker assets), the Taker (Counterparty) of the swap, the duration and other parameters will be described in the next sections.
Once a user has created the swap, the NFT Trader Smart Contract will emit a swapEvent
with some useful data you can track for fulfilling your needs.
Furthermore, the SDK will emit other events placed at the javascript level in order to give you the ability to avoid listening directly the events on the blockchain. These types of events will be described in the next sections.
Close Swap Phase
This phase is marked as "optional" since is not mandatory for the Taker (counterparty) of the swap to close it. The Taker could be a specific address or everyone (it depends how the swap is configured in the Creation Swap phase).
If the swap is to be closed by the Taker, the NFT Trader Smart Contract will emit a swapEvent
with some useful data that you can track to fulfill your needs.
As for the Creation Swap Phase, the SDK will emit events placed at the javascript level in order to track the transaction status.
Edit Taker Phase
This phase is marked as "optional" since is not mandatory for the Creator to edit the Taker address. It could be useful for the Creator to edit the Taker if he wants to change the counterparty because he has found a new person interested with their deal. This phase can be performed only by the Creator of the swap and no one else.
Once a user has changed the Taker, the NFT Trader Smart Contract will emit a counterpartEvent
with the data related to the new counterparty that will be involved in the swap. As for the Creation Swap Phase, the SDK will emit events placed at the javascript level in order to track the transaction status.
Cancel Swap Phase
This phase is marked as "optional" since it is not mandatory for the Creator to cancel the swap. Only the Creator can cancel a swap and no one else.
Once a user has changed the Taker, the NFT Trader Smart Contract will emit a counterpartEvent
with the data related to the new counterparty involved in the swap.
As for the Creation Swap Phase, the SDK will emit events placed at the javascript level in order to track the transaction status.
Initialize The NFTTraderSDK Object
In order to interact with the NFT Trader Smart Contract you will need to create a new instance of the NFTTraderSDK
object.
There two basic ways to initialize this object because basically there are two possible scenarios.
- You are developing a Backend Platform (Node.js).
- You are developing a Single Page Application, a Website or a Plugin.
If you are developing a backend platform you can initialize the NFTTraderSDK
object in this way.
const sdk = new NFTTraderSDK({
jsonRpcProvider : 'RPC_URL_PROVIDER',
network : 'NETWORK',
signer : {
privateKey : '<PRIVATE_KEY_OF_A_WALLET_OR_ACCOUNT>'
},
avoidPrivateKeySigner : false
})
sdk.method1(...)
sdk.method2(...)
Why do I need to specify a private key? Because in a backend environment you do not have a crypto wallet like Metamask or similar that can provide you a way to interact directly with the blockchain, by sending a transaction to a smart contract. This is why you will need to specify a private key. In order to be able to perform writing operations on the blockchain.
Provide the privateKey
parameter is not mandatory. If you specify the avoidPrivateKeySigner
parameter to false
, you will be able to use the NFTTraderSDK
object but only for performing read operations on the smart contract.
IMPORTANT. WE NEVER STORE YOUR PRIVATE KEY. privateKey IS A READ-ONLY PARAMETER.
If you are developing a Frontend Application (SPA), a Website or a Plugin you can initialize the NFTTraderSDK
object in this manner.
window.addEventListener('load', async () => {
const sdk = new NFTTraderSDK({
web3Provider : window.ethereum,
network : 'NETWORK',
})
const ethers = sdk.getEthersJSInstance()
provider = new ethers.providers.Web3Provider(window.ethereum)
await provider.send('eth_requestAccounts', [])
sdk.method1(...)
sdk.method2(...)
})
Once you have initialized the NFTTraderSDK
object you will be ready to perform write or read operations on the NFT Trader Smart Contract.
Instance methods
The SDK will provide you the following list of methods:
- createSwap(config[, gasLimit[, gasPrice]]) : Promise(void)
- closeSwap(config[, gasLimit[, gasPrice]]) : Promise(void)
- cancelSwap(swapId[, gasLimit[, gasPrice]]) : Promise(void)
- editTaker(swapId, addressTaker[, gasLimit[, gasPrice]]) : Promise(void)
- on(eventName, callbackFn) : void
- off(eventName[, callbackFn]) : void
- setBlocksNumberConfirmationRequired(blocksNumberConfirmationRequired) : void
- getSwapDetails(maker, swapId) : Promise(Object)
- getSwapAssets(swapId) : Promise(Object)
- isERC20WhiteListed(erc20Address) : Promise(boolean)
- isNFTBlacklisted(assetAddress) : Promise(boolean)
- getPayment() : Promise(Object)
- getReferenceAddress() : Promise(Object)
- isBannedAddress(address) : Promise(boolean)
- getEthersJSInstance() : Ethers
Create A Swap
In order to create a swap you can use the following method:
const sdk = new NFTTraderSDK(....)
await sdk.createSwap({
ethMaker : 100000000000,
taker : '0x87B96FE67F93bc795B7bb6957A4812DA1ec5e4Cf',
ethTaker : 100000000000,
swapEnd : 0,
assetsMaker : [],
assetsTaker : [],
referralAddress : '0x0000000000000000000000000000000000000000'
},
gasLimit,
gasPrice
)
This method can emit 3 different types of events.
createSwapTransactionCreated
This is emitted when the transaction is created and it is waiting to be mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("createSwapTransactionCreated", ({ tx }) => {
})
createSwapTransactionMined
This is emitted when the transaction is mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("createSwapTransactionMined", ({ receipt }) => {
const events = param.receipt.events
const event = events[0]
const { _swapId } = event.args
})
Intercepting the createSwapTransactionMined
event can be very important if you want to extract the swapId
value. This parameter is the unique identifier of the swap and it is needed by the closeSwap
, cancelSwap
or editTaker
methods.
The alternative for tracking the swapId
parameter could be listen the swapEvent
directly on the blockchain.
createSwapTransactionError
This is emitted when an error occurs during the creation/mining of transaction process. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("createSwapTransactionError", ({ error, typeError }) => {
})
Close A Swap
In order to close a swap you can use the following method:
const sdk = new NFTTraderSDK(....)
await sdk.closeSwap({
maker : 'ADDRESS_OF_THE_MAKER',
swapId : 0,
referralAddress : '0x0000000000000000000000000000000000000000'
},
gasLimit,
gasPrice
)
This method can emit 3 different types of events.
closeSwapTransactionCreated
This is emitted when the transaction is created and it is waiting to be mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("closeSwapTransactionCreated", ({ tx }) => {
})
closeSwapTransactionMined
This is emitted when the transaction is mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("closeSwapTransactionMined", ({ receipt }) => {
})
closeSwapTransactionError
This is emitted when an error occurs during the creation/mining of transaction process. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("closeSwapTransactionError", ({ error, typeError }) => {
})
Cancel A Swap
In order to cancel a swap you can use the following method:
const sdk = new NFTTraderSDK(....)
await sdk.cancelSwap(
swapId,
gasLimit,
gasPrice
)
This method can emit 3 different types of events.
cancelSwapTransactionCreated
This is emitted when the transaction is created and it is waiting to be mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("cancelSwapTransactionCreated", ({ tx }) => {
})
cancelSwapTransactionMined
This is emitted when the transaction is mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("cancelSwapTransactionMined", ({ receipt }) => {
})
The alternative for tracking this event could be listen the swapEvent
directly on the blockchain.
cancelSwapTransactionError
This is emitted when an error occurs during the creation/mining of transaction process. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("cancelSwapTransactionError", ({ error, typeError }) => {
})
Edit Taker
In order to edit the taker of a swap you can use the following method:
const sdk = new NFTTraderSDK(....)
await sdk.editTaker(
swapId,
addressTaker,
gasLimit,
gasPrice
)
This method can emit 3 different types of events.
editTakerTransactionCreated
This is emitted when the transaction is created and it is waiting to be mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("editTakerTransactionCreated", ({ tx }) => {
})
editTakerTransactionMined
This is emitted when the transaction is mined. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("editTakerTransactionMined", ({ receipt }) => {
})
The alternative for tracking this event could be listen the counterpartEvent
directly on the blockchain.
editTakerTransactionError
This is emitted when an error occurs during the creation/mining of transaction process. This event can be intercepted by the on()
method (described in the next sections). Example:
sdk.on("editTakerTransactionError", ({ error, typeError }) => {
})
SDK events
As we mentioned earlier, the SDK provides several ways to track events by saving the developer to listen directly the swapEvent
or the counterpartEvent
from the blockchain.
We have already seen the events which the SDK fires on the previous sections but here is a complete list:
- createSwapTransactionCreated
- createSwapTransactionMined
- createSwapTransactionError
- cancelSwapTransactionCreated
- cancelSwapTransactionMined
- cancelSwapTransactionError
- closeSwapTransactionCreated
- closeSwapTransactionMined
- closeSwapTransactionError
- editTakerTransactionCreated
- editTakerTransactionMined
- editTakerTransactionError
These events can be tracked using the on()
method. Here is an implementation example of it.
const sdk = new NFTTraderSDK(....)
sdk.on('createSwapTransactionCreated', async ({tx}) => {
const txHash = tx.hash
console.log('Transaction hash is ', txHash)
await myApi.storeOnDB(txHash)
})
await sdk.createSwap(....)
Naturally, you can also remove the listener if you don't want to track the event anymore. It depends on the logic of your code.
Example:
const sdk = new NFTTraderSDK(....)
sdk.on('createSwapTransactionCreated', async ({tx}) => {
const txHash = tx.hash
console.log('Transaction hash is ', txHash)
await myApi.storeOnDB(txHash)
})
await sdk.createSwap(....)
sdk.off('createSwapTransactionCreated')
.off()
has two parameters, the eventName
and the callbackFn
. The first one is mandatory and it represents the event that we want to remove from the listener. The second parameter is the callback function and it is optional. Specifying the second parameter can be useful if you want to remove just one listener without removing the others. Example:
const sdk = new NFTTraderSDK(....)
sdk.on('createSwapTransactionCreated', async ({tx}) => {
const txHash = tx.hash
console.log('Transaction hash is ', txHash)
await myApi.storeOnDB(txHash)
})
sdk.on('createSwapTransactionCreated', async ({tx}) => {
console.log('bla bla')
})
await sdk.createSwap(....)
sdk.off('createSwapTransactionCreated', async ({tx}) => {
console.log('bla bla')
})
Read Methods
The SDK provides you several methods to get information from the NFT Trader Smart Contract. Here a list of read methods you can use.
getSwapDetails(maker, swapId) : Promise(Object)
Returns the major details of the swap. Example:
const {
id,
addressMaker,
discountMaker,
valueMaker,
flatFeeMaker,
addressTaker,
discountTaker,
valueTaker,
flatFeeTaker,
swapStart,
swapEnd,
flagFlatFee,
flagRoyalties,
status,
royaltiesMaker,
royaltiesTaker,
} = await sdk.getSwapDetails(maker, swapId)
getSwapAssets(swapId) : Promise(Object)
Return the swap assets array details. The structure of these arrays will be explained in the next sections. Example:
const swapId = 10
const {
assetsMaker,
assetsTaker,
} = await sdk.getSwapAssets(swapId)
isERC20WhiteListed(erc20Address) : Promise(boolean)
Return true
or false
if the ERC20 token is whitelisted by the NFT Trader Smart Contract. Example:
const isWhitelisted = await sdk.isERC20WhiteListed(
"0xdac17f958d2ee523a2206206994597c13d831ec7"
)
isNFTBlacklisted(assetAddress) : Promise(boolean)
Return true
or false
if the ERC721/1155 token is blacklisted by the NFT Trader Smart Contract. Example:
const isBlacklisted = await sdk.isNFTBlacklisted(
"0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"
)
getPayment() : Promise(Object)
Return an object representing the payment configuration of the NFT Trader Smart Contract. Example:
const {
flagFlatFee,
flagRoyalties,
flatFee,
bps,
scalePercent,
} = await sdk.getPayment()
getReferenceAddress() : Promise(Object)
Return an object representing the reference address configuration of the NFT Trader Smart Contract. Example:
const {
ROYALTYENGINEADDRESS,
TRADESQUAD,
PARTNERSQUAD,
VAULT,
} = await sdk.getReferenceAddress()
isBannedAddress(address) : Promise(boolean)
Return true
or false
if the msg.sender is banned by the NFT Trader Smart Contract. Example:
const isBanned = await sdk.isNFTBlacklisted(
"0x87B96FE67F93bc795B7bb6957A4812DA1ec5e4Cf"
)
Other methods
The SDK provides you other useful methods.
getEthersJSInstance() : Ethers
Return an Ether object instance you can use for interacting with the blockchain. Example:
const ethers = sdk.getEthersJSInstance()
setBlocksNumberConfirmationRequired(blocksNumberConfirmationRequired) : void
Set the blocks confirmation number used to consider a transaction mined. Example:
sdk.setBlocksNumberConfirmationRequired(10)
sdk.on("createSwapTransactionMined", ({ tx }) => {
})
Utilities
The SDK provides you a utility class object for building the assetsMaker
or the assetsTaker
arrays useful for the createSwap
method.
In order to build these two kinds of arrays there is a utility class called AssetsArray
.
To create an instance of AssetsArray
object you can do so in the following way:
const sdk = new NFTTraderSDK(....)
const assetsArray = new sdk.AssetsArray()
AssetsArray
class provide you the following methods:
- addERC20Asset(address, tokenAmount) : void
- addERC721Asset(address, tokenIds) : void
- addERC1155Asset(address, tokenIds, tokenAmounts) : void
- clearAssetsArray() : void
- getAssetsArray() : void
Let's see in details what these methods provide.
addERC20Asset(address, tokenAmount) : void
Adding an ERC20 token and the relative amount to the AssetsArray object. The token amount must be formatted correctly before passing it onto this method. The SDK will not perform any conversion based on the decimals of the token.
Let's see an example of implementation.
const sdk = new NFTTraderSDK(....)
const assetsArray = new sdk.AssetsArray()
assetsArray.addERC20Asset('0x6b175474e89094c44da98b954eedeac495271d0f', 10000000000000000000)
addERC721Asset(address, tokenIds) : void
Adding an ERC721 token and the relative token ids to the AssetsArray object. tokenIds
parameter must be an array with at least one element inside it.
Let's see an example of implementation.
const sdk = new NFTTraderSDK(....)
const assetsArray = new sdk.AssetsArray()
assetsArray.addERC721Asset('0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', [1,3456])
addERC1155Asset(address, tokenIds, tokenAmounts) : void
Adding an ERC1155 token, the relative token ids and amounts to the AssetsArray object. tokenIds
parameter must be an array with at least one element inside it. tokenAmounts
must be an array with at least one element inside it. The size of tokenIds
and tokenAmounts
array must be equal.
Let's see an example of implementation.
const sdk = new NFTTraderSDK(....)
const assetsArray = new sdk.AssetsArray()
assetsArray.addERC1155Asset('0xedb61f74b0d09b2558f1eeb79b247c1f363ae452', [1,3456], [1,1])
clearAssetsArray() : void
Clear the assets array object. It erases everything you have inputted before.
Let's see an example of implementation.
const sdk = new NFTTraderSDK(....)
const assetsArray = new sdk.AssetsArray()
assetsArray.addERC20Asset('0x6b175474e89094c44da98b954eedeac495271d0f', 10000000000000000000)
assetsArray.addERC721Asset('0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', [1,3456])
assetsArray.addERC1155Asset('0xedb61f74b0d09b2558f1eeb79b247c1f363ae452', [1,3456], [1,1])
assetsArray.clearAssetsArray()
getAssetsArray() : void
Returns the assets array.
Let's see an example of implementation.
const sdk = new NFTTraderSDK(....)
const assetsArrayMaker = new sdk.AssetsArray()
const assetsArrayTaker = new sdk.AssetsArray()
assetsArrayMaker.addERC20Asset('0x6b175474e89094c44da98b954eedeac495271d0f', 10000000000000000000)
assetsArrayMaker.addERC721Asset('0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', [1,3456])
assetsArrayMaker.addERC1155Asset('0xedb61f74b0d09b2558f1eeb79b247c1f363ae452', [1,3456], [1,1])
assetsArrayTaker.addERC20Asset('0x6b175474e89094c44da98b954eedeac495271d0f', 10000000000000000000)
assetsArrayTaker.addERC721Asset('0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', [230,12])
const assetsMaker = assetsArrayMaker.getAssetsArray()
const assetsTaker = assetsArrayTaker.getAssetsArray()
await sdk.createSwap({
.
.
assetsMaker : assetsMaker,
assetsTaker : assetsTaker
})
WebSocket provider
The SDK also provides a way to interact directly with the event emitted by the NFT Trader Smart Contract. The interactions are made through the WebSocketProvider
object.
To create an instance of WebSocketProvider
object you can do so in the following way:
const sdk = new NFTTraderSDK(....)
const webSocketProvider = new sdk.WebSocketProvider({
wssUrl : 'URL_OF_WEBSOCKET_PROVIDER',
network : 'NETWORK'
})
WebSocketProvider
class provides you the following methods:
- onSwapEvent(callbackFn[, config]) : void
- onCounterpartEvent(callbackFn[, config]) : void
- onPaymentReceived(callbackFn[, config]) : void
Let's see in the details what these methods provide.
onSwapEvent(callbackFn[, config]) : void
Add a listener through a websocket directly on the smart contract. This method will tracks every swapEvent
emitted by the NFT Trader Smart Contract. Optionally, you can filter which swapEvent
track by specifying the config
object. config
object it is used internally to filter the events with topics. The swapEvent
is fired during the creation of the swap, the closing of the swap or when a user cancels a swap.
const sdk = new NFTTraderSDK(....)
const webSocketProvider = new sdk.WebSocketProvider(....)
webSocketProvider.onSwapEvent(function () {
}, {
creator : '',
time : 1234567,
status : 1
})
onCounterpartEvent(callbackFn[, config]) : void
Add a listener through a websocket directly on the smart contract. This method will tracks every counterpartEvent
emitted by the NFT Trader Smart Contract. Optionally, you can filter which counterpartEvent
track by specifying the config
object. config
object it is used internally to filter the events with topics. The counterpartEvent
is fired when a user decides to edit the taker of the swap.
const sdk = new NFTTraderSDK(....)
const webSocketProvider = new sdk.WebSocketProvider(....)
webSocketProvider.onCounterpartEvent(function () {
}, {
swapId : 100,
counterpart : ''
})
onPaymentReceived(callbackFn[, config]) : void
Add a listener through a websocket directly on the smart contract. This method will track every paymentReceived
emitted by the NFT Trader Smart Contract. Optionally, you can filter which paymentReceived
track by specifying the config
object. config
object it is used internally to filter the events with topics. The paymentReceived
is fired when a user deposits Ether on the smart contract.
const sdk = new NFTTraderSDK(....)
const webSocketProvider = new sdk.WebSocketProvider(....)
webSocketProvider.onPaymentReceived(function () {
}, {
payer : ''
})
Credits
This SDK is the property of Salad Labs Inc.
Every unauthorized copy or distribution is stricly forbidden without the consent of Salad Labs Inc.
This SDK is protected by Copyright Property and Propietary Laws.