If you have both asset (USD and EUR) on Stellar, there's no problem at all.
const {Server, Network, Keypair, TransactionBuilder, Operation, Asset} = require('stellar-sdk')
Network.useTestNetwork()
const server = new Server('https://horizon-testnet.stellar.org'),
aliceAddress = 'GDE5SEXCPDZ4CSQSYUJNBBC27OXD2ORQLPU2MGI2QRVD3VMFBDXAZVLK',
bobAddress = 'GC73Q2JR32JDI4Q5TRDQSHHV6ZZXEXWREQ3BLPQCEYT7QGKNVC2NBMYO'
//suppose we want to exchange two assets issued by the same anchor
const usd = new Asset('USD', 'GAMGGCTNMRFQ5J4HL7VQU3WCENM2D7VAUJFI3DEJMV3PICPYCCAI4SWC'),
eur = new Asset('EUR', 'GAMGGCTNMRFQ5J4HL7VQU3WCENM2D7VAUJFI3DEJMV3PICPYCCAI4SWC')
// load details for both accounts
Promise.all([
server.loadAccount(aliceAddress),
server.loadAccount(bobAddress)
])
.then(([aliceAccount, bobAccount]) => {
// here we can check if both accounts have enough funds for exchange
// if (aliceAccount.balance...)
// start building the transaction
// Alice will be the source account in this case and tx fees will be charged from her account
let transaction = new TransactionBuilder(aliceAccount)
.addOperation(Operation.payment({
source: aliceAddress,
destination: bobAddress,
asset: usd,
amount: '1000'
}))
.addOperation(Operation.payment({
source: bobAddress,
destination: aliceAddress,
asset: eur,
amount: '900'
}))
.build()
// now we need to sign the transaction using both accounts' private keys
const aliceKeys = Keypair.fromSecret('SBFIZKNU2AZWKC7C273IGQ4A2IBQJEDHZCNFQKB2BTT4FJKLATML3TYE'),
bobKeys = Keypair.fromSecret('SDKU7OHIEJ6YPU3M3CW446L3ZEPGYPO7MNSO352HG6R4N6JVBQ2WXXWP')
transaction.sign(aliceKeys)
transaction.sign(bobKeys)
return server.submitTransaction(transaction)
})
.then(res => console.log('Results:', res))
.catch(error => console.error(error))
The step with signing could be deferred. You generate the transaction, send it to Alice, she signs and send it back to you. Then you send the same tx to Bob, he signs it as well. Once both participants signed the tx, it can be submitted to network. The signatures can be gathered simultaneously and then merged into the tx envelope.