> For the complete documentation index, see [llms.txt](https://pintswap.gitbook.io/pintswap/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pintswap.gitbook.io/pintswap/pintswap-sdk/trading.md).

# 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.&#x20;

First, start by creating your first offer with the function below:

### Broadcast Offer

```typescript
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:

```typescript

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

```typescript
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.

```typescript
const subscription = pintswap.startPublishingOffers(10000);
subscription.stop(); // only needed if you want to cancel the publishing interval
```

### Subscribe to Public Offers

```typescript
await pintswap.subscribeOffers()
```

Starts listening for existing and new public orderbook offer publishes.

```typescript
await pintswap.subscribeOffers();
pintswap.on('/pubsub/orderbook-update', () => {
  pintswap.logger.info([ ...pintswap.offers.entries() ]);
});
```

## Accepting Offers

### Individual Offer

```typescript
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:

```typescript
const trade = pintswap.createTrade(pintAddress, {
  gets: {
    token: '0x0000000000000000000000000000000000000000',
    amount: '0xde0b6b3a7640000'
  },
  gives: {
    token: '0x8d008CAC1a5CB08aC962b1e34E977B79ABEee88D',
    amount: '0xbe951906eba2a8000000'
  }
});

const promise = trade.toPromise();
await promise;
```

### Multiple Offers

```typescript
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:

```typescript
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;
```
