# ptokens-swap

> This package permits to build a `pTokensSwap` from a `pTokensSwapBuilder` instance. The created `pTokensSwap` object can be called with a specific method `execute()` to initiate the swap: the method return a [PromiEvent](https://www.npmjs.com/package/prom

Latest version **1.7.0** (published 2023-08-10) · 0 weekly downloads

## Install

```sh
npm install ptokens-swap
pnpm add ptokens-swap
yarn add ptokens-swap
bun add ptokens-swap
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.7.0 |
| Published | 2023-08-10 |
| First published | 2022-10-17 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 604.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | pnetwork |

## Links

- npm: https://www.npmjs.com/package/ptokens-swap
- npm.io page: https://npm.io/package/ptokens-swap

## Dependencies (8)

- [promievent](https://npm.io/package/promievent.md) ^0.1.5
- [bignumber.js](https://npm.io/package/bignumber.js.md) 9.1.0
- [ptokens-node](https://npm.io/package/ptokens-node.md) ^1.6.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.20.1
- [ptokens-helpers](https://npm.io/package/ptokens-helpers.md) ^1.6.0
- [ptokens-entities](https://npm.io/package/ptokens-entities.md) ^1.6.0
- [ptokens-constants](https://npm.io/package/ptokens-constants.md) ^1.6.0
- [light-async-polling](https://npm.io/package/light-async-polling.md) ^1.0.2

## Recent versions

- 1.7.0 (latest) — 2023-08-10
- 1.6.0 — 2023-07-28
- 1.5.0 — 2023-07-11
- 1.4.0 — 2023-06-16
- 1.3.0 — 2023-06-13
- 1.2.0 — 2023-05-26
- 1.1.0 — 2023-05-10
- 1.0.5 — 2023-03-24
- 1.0.4 — 2022-11-25
- 1.0.3 — 2022-10-22
- 1.0.2 — 2022-10-19
- 1.0.1 — 2022-10-18
- 1.0.0 — 2022-10-17

## README

This package permits to build a `pTokensSwap` from a `pTokensSwapBuilder` instance. The created `pTokensSwap` object can be called with a specific method `execute()` to initiate the swap: the method return a [PromiEvent](https://www.npmjs.com/package/promievent), a particular Promise that can also emit events that can be listened.

In particular, given two pTokens Assets already build, the swap takes place as follows:
```ts
const PNETWORK_NODE = 'https://pnetwork-node-2a.eu.ngrok.io/v3'
const DESTINATION_ADDRESS: 'destination-address'

// create a pTokensNodeProvider and pTokensNode to interact with pNetwork
const provider = new pTokensNodeProvider(PNETWORK_NODE)
const node = new pTokensNode(provider)

// create swap builder
const swapBuilder = new pTokensSwapBuilder(node)

// create assets
let sourceAsset: pTokensAsset
let destinationAsset: pTokensAsset

// ...

// build the swap
swapBuilder.setSourceAsset(sourceAsset)
swapBuilder.addDestinationAsset(destinationAsset, DESTINATION_ADDRESS)
swapBuilder.setAmount(0.000001 * 10 ** 18)
const swap = swapBuilder.build()

try {
    // execute the swap and listen to events
    await swap
        .execute()
        .on('inputTxBroadcasted', (_) => console.info('inputTxBroadcasted', _))
        .on('inputTxConfirmed', (_) => console.info('inputTxConfirmed', _))
        .on('inputTxDetected', (_) => console.info('inputTxDetected', _))
        .on('outputTxDetected', (_) => console.info('outputTxDetected', _))
        .on('outputTxBroadcasted', (_) => console.info('outputTxBroadcasted', _))
        .on('outputTxConfirmed', (_) => console.info('outputTxConfirmed', _))
} catch (err) {
    console.info('err', err)
}
```

See the full documentation [here](https://pnetwork-association.github.io/ptokens.js/modules/pTokens_Swap.html).

---
_Source: https://npm.io/package/ptokens-swap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
