An atomic swap is when you swap tokens between two accounts without using a third-party intermediary, such as a centralized exchange or custody provider, to facilitate the transfer. Native tokens issued using the Hedera Token Service (HTS) can be swapped with another or with HBAR in a single transaction using the TransferTransaction API call. For each atomic swap within a single transaction, you’ll need to designate an account to be debited (-) any number of tokens and the corresponding account which will receive those tokens.
Signing Requirements
The private keys for the accounts which are being debited tokens are required to sign the transaction.
Hedera accounts must be associated with the specified token before you can transfer a token to their account. Please see how to associate a token to an account here.
//Atomic swap between a Hedera Token Service token and hbarTransferTransaction atomicSwap =newTransferTransaction().addHbarTransfer(accountId1,newHbar(-1)).addHbarTransfer(accountId2,newHbar(1)).addTokenTransfer(tokenId, accountId2,-1).addTokenTransfer(tokenId, accountId1,1).freezeWith(client);//Sign the transaction with accountId1 and accountId2 private keys, submit the transaction to a Hedera networkTransactionResponse txResponse =atomicSwap.sign(accountKey1).sign(accountKey2).execute(client);//------------------------------------OR---------------------------------------//Atomic swap between two hedera Token Service created tokensTransferTransaction atomicSwap =newTransferTransaction().addTokenTransfer(tokenId1, accountId1,-1).addTokenTransfer(tokenId1, accountId2,1).addTokenTransfer(tokenId2, accountId2,-1).addTokenTransfer(tokenId2, accountId1,1).freezeWith(client);//Sign the transaction with accountId1 and accountId2 private keys, submit the transaction to a Hedera networkTransactionResponse txResponse =atomicSwap.sign(accountKey1).sign(accountKey2).execute(client);
//Atomic swap between a Hedera Token Service token and hbarconstatomicSwap=awaitnewTransferTransaction().addHbarTransfer(accountId1,newHbar(-1)).addHbarTransfer(accountId2,newHbar(1)).addTokenTransfer(tokenId, accountId2,-1).addTokenTransfer(tokenId, accountId1,1).freezeWith(client);//Sign the transaction with accountId1 and accountId2 private keys, submit the transaction to a Hedera networkconsttxResponse=await (await (awaitatomicSwap.sign(accountKey1)).sign(accountKey2)).execute(client);//------------------------------------OR---------------------------------------//Atomic swap between two hedera Token Service created tokensconstatomicSwap=awaitnewTransferTransaction().addTokenTransfer(tokenId1, accountId1,-1).addTokenTransfer(tokenId1, accountId2,1).addTokenTransfer(tokenId2, accountId2,-1).addTokenTransfer(tokenId2, accountId1,1).freezeWith(client);//Sign the transaction with accountId1 and accountId2 private keys, submit the transaction to a Hedera networkconsttxResponse=await (await (awaitatomicSwap.sign(accountKey1)).sign(accountKey2)).execute(client);
//Atomic swap between a Hedera Token Service token and hbaratomicSwap, err := hedera.NewTransferTransaction().AddHbarTransfer(accountId1, hedera.NewHbar(-1)).AddHbarTransfer(accountId2, hedera.NewHbar(1)).AddTokenTransfer(tokenId, accountId2, -1).AddTokenTransfer(tokenId, accountId1, 1).FreezeWith(client)txResponse, err := atomicSwap.Sign(accountKey1).Sign(accountKey2).Execute(client)//------------------------------------OR---------------------------------------//Atomic swap between two hedera Token Service created tokensatomicSwap, err := hedera.NewTransferTransaction().AddTokenTransfer(tokenId1, accountId1, -1).AddTokenTransfer(tokenId1, accountId2, 1).AddTokenTransfer(tokenId2, accountId2, -1).AddTokenTransfer(tokenId2, accountId1, 1).FreezeWith(client)txResponse, err := atomicSwap.Sign(accountKey1).Sign(accountKey2).Execute(client)