Skip to main content

Programmatic Trading

VladSwap is designed to be fully programmable, allowing you to build your own trading bots and other applications on top of the marketplace. This guide will walk you through the process of programmatically interfacing with the subp2p protocol to trade BTC for Alkanes trustlessly.

Connecting to the Network

The first step is to connect to the subp2p network. This can be done by using the subp2p-js library, which provides a simple and easy-to-use API for interacting with the network.

import { subp2p } from 'subp2p';

const subp2p = new subp2p({
relay: 'p2p.subfrost.io',
});

await subp2p.connect();

Creating an Order

Once you have connected to the network, you can create an order by creating a PSBT that represents your side of the trade. This can be done using a library such as bitcoinjs-lib.

import * as bitcoin from 'bitcoinjs-lib';

const psbt = new bitcoin.Psbt({ network: bitcoin.networks.testnet });

psbt.addInput({
hash: '...',
index: 0,
witnessUtxo: {
script: Buffer.from('...'),
value: 100000,
},
});

psbt.addOutput({
address: '...',
value: 90000,
});

// Add an output for the Alkanes
psbt.addOutput({
script: Buffer.from('...'), // This will be a custom script that represents the Alkanes
value: 100,
});

Broadcasting the Order

Once you have created the PSBT, you can broadcast it to the network using the ``subp2p.broadcast() method.

await subp2p.broadcast(psbt.toBase64());

Listening for Orders

You can also listen for new orders on the network by using the ``subp2p.on('broadcast', ...) event.

subp2p.on('broadcast', (data) => {
const psbt = bitcoin.Psbt.fromBase64(data);

// Validate the PSBT and, if it is valid, match it with your own order
});

Matching an Order

When you find an order that you would like to accept, you can create your own corresponding PSBT to complete the trade. The two PSBTs are then combined and broadcast to the Bitcoin network, where the atomic swap is executed in a single transaction.

For more information on how to create and match orders, please see the PSBT Marketplace documentation.