The simplest way to exchange tokens between users is to use the Stellar Distributed Exchange (DEX).
There are two types of operations that let you create offers on the DEX:
I included a snippet showing how you can create such transactions and submit them to the network.
const txA = new Stellar.TransactionBuilder(accountA, { fee })
.addOperation(Stellar.Operation.manageSellOffer({
selling: assetX,
buying: assetY,
amount: '1000000',
price: '1',
offerId: '0',
}))
.setTimeout(30)
.setNetworkPassphrase(Stellar.Networks.TESTNET)
.build();
txA.sign(A);
await server.submitTransaction(txA);
const txB = new Stellar.TransactionBuilder(accountB, { fee })
.addOperation(Stellar.Operation.manageBuyOffer({
buying: assetY,
selling: assetX,
buyAmount: '1000',
price: '1',
offerId: '0',
}))
.setTimeout(30)
setNetworkPassphrase(Stellar.Networks.TESTNET)
.build();
txB.sign(B);
await server.submitTransaction(txB);