Trading
Methods used for trading ERC20 and NFT between peers. This includes creating trades and accepting trades in addition to configuring the visibility of the trades.
Creating Offers
When creating an offer, the user has the option to either post it on the PintSwap public orderbook for every pint user connected to the network OR privately share it with other individuals.
First, start by creating your first offer with the function below:
Broadcast Offer
await pintswap.broadcastOffer(offer)
Inserts an order into a pint user's peer data instance, pintswap.offers
map, such that it can be traded against, using the structure below:
pintswap.broadcastOffer({
gets: {
token: '0x0000000000000000000000000000000000000000',
amount: '0xde0b6b3a7640000'
},
gives: {
token: '0x8d008CAC1a5CB08aC962b1e34E977B79ABEee88D',
amount: '0xbe951906eba2a8000000'
}
});
For NFT trades it is possible to use a field similar to tokenId: '0x01'
instead of the amount
field in either the gets
or gives
field of the offer.
Public Orderbook
To publish offers to the public orderbook, the publishOffers()
function can be used like so:
Publish Offers
await pintswap.startPublishingOffers(ms)
Starts and stops publishing the orderbook stored in pintswap.offers
Uses libp2p GossipSub on the /pintswap/0.1.2/publish-orders
topic.
const subscription = pintswap.startPublishingOffers(10000);
subscription.stop(); // only needed if you want to cancel the publishing interval
Subscribe to Public Offers
await pintswap.subscribeOffers()
Starts listening for existing and new public orderbook offer publishes.
await pintswap.subscribeOffers();
pintswap.on('/pubsub/orderbook-update', () => {
pintswap.logger.info([ ...pintswap.offers.entries() ]);
});
Accepting Offers
Individual Offer
await pintswap.createTrade(pintAddress, offer)
Appropriate method when accepting and fulfilling an individual offer. This method is most appropriate for NFT and OTC trades.
Requires a PeerId
instance as first argument followed an offer of the structure:
const trade = pintswap.createTrade(pintAddress, {
gets: {
token: '0x0000000000000000000000000000000000000000',
amount: '0xde0b6b3a7640000'
},
gives: {
token: '0x8d008CAC1a5CB08aC962b1e34E977B79ABEee88D',
amount: '0xbe951906eba2a8000000'
}
});
const promise = trade.toPromise();
await promise;
Multiple Offers
await pintswap.createBatchTrade(pintAddress, fill)
Method capable of accepting one or multiple offers at once. Most appropriate for accepting multiple orderbook offers.
Requires a PeerId
instance as first argument followed by an array of order fills of the structure:
const trade = pintswap.createBatchTrade(PeerId.createFromB58String(await pintswap.resolveName('wock.drip')), [{
amount: '0x101010',
offer: {
gets,
gives
}
}, {
amount: '0x202020',
offers: {
gets,
gives
}
}]);
const promise = trade.toPromise();
await promise;
Last updated