Javascript module for interacting with pNetwork.
pTokens.js is the library that allows interoperability with the pNetwork interacting with pNetwork bridges. This library supports only v2 bridges.
The package is published in the npm registry.
Initiate your Javascript/Typescript project and install it as a dependency:
$ npm i @p.network/ptokens
The full documentation can be found here.
This is a working example where pBTC on Ethereum tokens are pegged-out to pBTC on Algorand.
const {
ChainId,
pTokensSwapBuilder,
pTokensEvmAssetBuilder,
pTokensNode,
pTokensNodeProvider,
pTokensEvmProvider,
pTokensAlgorandAssetBuilder,
} = require('ptokens')
const PNETWORK_NODE = 'https://pnetwork-node-2a.eu.ngrok.io/v3'
const EVM_PROVIDER = 'evm-provider-url'
const PRIVATE_KEY = 'private-key'
const ALGORAND_DESTINATION_ADDRESS = 'destination-address'
async function pegOut() {
// create a pTokensNodeProvider and pTokensNode to interact with pNetwork
const provider = new pTokensNodeProvider(PNETWORK_NODE)
const node = new pTokensNode(provider)
// create builders
const evmBuilder = new pTokensEvmAssetBuilder(node)
const algorandBuilder = new pTokensAlgorandAssetBuilder(node)
const swapBuilder = new pTokensSwapBuilder(node)
// create an EVM provider
const evmProvider = new pTokensEvmProvider(EVM_PROVIDER)
evmProvider.setPrivateKey(PRIVATE_KEY)
// create a UTXO asset for BTC
evmBuilder.setBlockchain(ChainId.EthereumMainnet)
evmBuilder.setProvider(evmProvider)
evmBuilder.setSymbol('pbtc')
evmBuilder.setDecimals(18)
const evmAsset = await evmBuilder.build()
// create an Algorand asset for pBTC on Algorand Mainnet
algorandBuilder.setBlockchain(ChainId.AlgorandMainnet)
algorandBuilder.setProvider(evmProvider)
algorandBuilder.setSymbol('pbtc')
const algorandAsset = await evmBuilder.build()
// build the swap
swapBuilder.setSourceAsset(evmAsset)
swapBuilder.addDestinationAsset(algorandAsset, ALGORAND_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)
}
}
void pegOut()
Complete examples are available at examples.
If you wish to contribute, please open a new Pull Request.
Technically speaking, this is a monorepo containing multiple packages. These are managed using lerna. Typescript source code is transpiled and bundled using Rollup.
Rollup has the following option available
-w, --watch Watch files in bundle and rebuild on changes
Every package has a dedicated dev
script that runs rollup with the watch option.
These scripts can be run in parallel by executing the following command from the project root directory:
$ npm run dev
In this way, a developer can make adjustments to the codebase and test it on the fly, without the need to build the affected packages.
Tip: leave the command running on a separate shell.
To build a new version of the library, run:
$ npm run build
To run tests in Node.js, run:
$ npm test
To generate the documentation website, run:
$ npm run docs
The static website will be located in the docs/
directory.
Generated using TypeDoc