getRoyaltyRegistryEngineABI() : Array
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({
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(swapId) : Promise(Object)
Returns the major details of the swap. Example:
const {
addressMaker,
discountMaker,
valueMaker,
flatFeeMaker,
addressTaker,
discountTaker,
valueTaker,
flatFeeTaker,
swapStart,
swapEnd,
flagFlatFee,
flagRoyalties,
status,
royaltiesMaker,
royaltiesTaker,
} = await sdk.getSwapDetails(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 }) => {
})
getNetworksAvailable() : Object
Return a Object instance containing the networks currently supported by this SDK. Example:
const networks = sdk.getNetworksAvailable()
estimateGasCreateSwap() : ethers.BigNumber | null
Returns the gas estimation cost of the create swap operation. Example:
const estimateGas = await sdk.estimateGasCreateSwap(...params)
estimateGasCloseSwap() : ethers.BigNumber | null
Returns the gas estimation cost of the close swap operation. Example:
const estimateGas = await sdk.estimateGasCloseSwap(...params)
estimateGasCancelSwap() : ethers.BigNumber | null
Returns the gas estimation cost of the cancel swap operation. Example:
const estimateGas = await sdk.estimateGasCancelSwap(...params)
estimateGasEditTaker() : ethers.BigNumber | null
Returns the gas estimation cost of the cancel swap operation. Example:
const estimateGas = await sdk.estimateGasEditTaker(...params)
getRoyaltyRegistriesEngines() : ethers.BigNumber | null
Returns the royalty registries engines mapping supported by the SDK. Example:
const mapping = await sdk.getRoyaltyRegistriesEngines() : Object
getRoyaltyRegistryEngineABI() : ethers.BigNumber | null
Returns the ABI object related to the royalty registry engine. Example:
const abi = await sdk.getRoyaltyRegistryEngineABI() : Array<Object>
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, tokenId) : 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, tokenId) : void
Adding an ERC721 token and the relative token id to the AssetsArray object. tokenId
parameter must be a string
Let's see an example of implementation.
const sdk = new NFTTraderSDK(....)
const assetsArray = new sdk.AssetsArray()
assetsArray.addERC721Asset('0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', '2321')
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', '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', '133')
assetsArrayMaker.addERC1155Asset('0xedb61f74b0d09b2558f1eeb79b247c1f363ae452', ['1','3456'], ['1','1'])
assetsArrayTaker.addERC20Asset('0x6b175474e89094c44da98b954eedeac495271d0f', '10000000000000000000')
assetsArrayTaker.addERC721Asset('0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', '230')
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 : ''
})
Copyright
-
This SDK is the property of Salad Labs Inc. All of the contents of this SDK are protected from copying under International and Canadian copyright laws and treaties. Any unauthorized copying, alteration, distribution, transmission, performance, display or other use of this material is strictly forbidden and prohibited.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
© 02/18/2022, Salad Labs Inc.