Performing Atomic Swap using Soroban Python SDK
Tags: soroban, stellar, python, sdk
Published: 2023-03-28 16:26:58

In my [previous post] (https://dev.to/darkvallen/checking-token-balance-using-soroban-python-sdk-2lek), I showed you how to check token balance using the Soroban Python SDK. In this post, we'll use Soroban Python SDK to invoke atomic swap example contract. To know more about soroban atomic swap example contract, you can check [this post] (https://dev.to/yuzurush/soroban-contracts-101-atomic-swaps-27oc) written by [yuzurush](https://dev.to/yuzurush/).

#Preparation
The python script that we're going to use is [`soroban_auth_atomic_swap.py`](https://github.com/StellarCN/py-stellar-base/blob/soroban/examples/soroban_auth_atomic_swap.py) from [Soroban Python SDK Examples Github Repository] (https://github.com/StellarCN/py-stellar-base/blob/soroban/examples)

We need 3 accounts: 

1. An account to act as the transaction submitter
2. An account that wants to swap native tokens for CAT tokens (Alice)
3. An account that wants to swap CAT tokens for native tokens (Bob)

- Transaction Submitter 
```
Public Key	GAERW3OYAVYMZMPMVKHSCDS4ORFPLT5Z3YXA4VM3BVYEA2W7CG3V6YYB
Secret Key	SBPTTA3D3QYQ6E2GSACAZDUFH2UILBNG3EBJCK3NNP7BE4O757KGZUGA
```
- Alice
```
Public Key	GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54
Secret Key	SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV
```
- Bob
```
Public Key	GBMLPRFCZDZJPKUPHUSHCKA737GOZL7ERZLGGMJ6YGHBFJZ6ZKMKCZTM
Secret Key	SAEZSI6DY7AXJFIYA4PM6SIBNEYYXIEM2MSOTHFGKHDW32MBQ7KVO6EN
```

##Checking Tokens Balance Before Swap
First, we will check both tokens (native & CAT) balance on both accounts (Alice & Bob). To check the balances you can read my post [here](https://dev.to/darkvallen/checking-token-balance-using-soroban-python-sdk-2lek/edit). Here's both tokens balance on both accounts before swap:

- Alice
```
Native Token (Lumen in `stroop`): 97999955005
CAT Token: 100000117450
```

- Bob
```
GBMLPRFCZDZJPKUPHUSHCKA737GOZL7ERZLGGMJ6YGHBFJZ6ZKMKCZTM
Native Token (Lumen in `stroop`): 102000023295
CAT Token: 199999882550
```

##Swapping The Tokens
To swap the tokens, open the `soroban_auth_atomic_swap.py` file and you can adjust some of following parameters:

```python
submitter_kp = Keypair.from_secret(
    "SBPTTA3D3QYQ6E2GSACAZDUFH2UILBNG3EBJCK3NNP7BE4O757KGZUGA"
)  # GAERW3OYAVYMZMPMVKHSCDS4ORFPLT5Z3YXA4VM3BVYEA2W7CG3V6YYB
alice_kp = Keypair.from_secret(
    "SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV"
)  # GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54
bob_kp = Keypair.from_secret(
    "SAEZSI6DY7AXJFIYA4PM6SIBNEYYXIEM2MSOTHFGKHDW32MBQ7KVO6EN"
)  # GBMLPRFCZDZJPKUPHUSHCKA737GOZL7ERZLGGMJ6YGHBFJZ6ZKMKCZTM
atomic_swap_contract_id = (
    "828e7031194ec4fb9461d8283b448d3eaf5e36357cf465d8db6021ded6eff05c"
)
native_token_contract_id = (
    "d93f5c7bb0ebc4a9c8f727c5cebc4e41194d38257e1d0d910356b43bfc528813"
)
cat_token_contract_id = (
    "8dc97b166bd98c755b0e881ee9bd6d0b45e797ec73671f30e026f14a0f1cce67"
)
```
> `submitter_kp` - Transaction Submitter Secret Key
> `alice_kp` - Alice Secret Key
> `bob_kp` - Bob Secret Key
> `atomic_swap_contract_id` - Contract ID of Atomic Swap Contract
> `native_token_contract_id` - Native Token's Contract ID
> `cat_token_contract_id` - CAT Token's Contract ID

Note: You can use any accounts as long as they hold both tokens, you can deploy your own atomic swap contract and you can use any token pair (even your own deployed token). 

```python
    Int128(1000),  # amount_a
    Int128(4500),  # min_b_for_a
    Int128(5000),  # amount_b
    Int128(950),  # min_a_for_b
```
You can adjust amount and minimal amount swapped for the tokens, by adjusting these lines of code.

Save `soroban_auth_atomic_swap.py` file with your changes and then run the script in a command prompt using this command :
```
python soroban_auth_atomic_swap.py
```
If the transaction resulted successful, then the next step is to check balance of both token on both accounts.

##Checking Tokens Balance After Swap
After checking balance both tokens on both accounts using the same way when we check before the swap, Here's both tokens balance on both accounts after swap:

- Alice
```
Native Token (Lumen in `stroop`): 97999953855
CAT Token: 100000121950
```
After the Swap Alice's Native Token decreased by 1150 (950 from the swap, 200 for checking both token balances) and her CAT Token increased by 4500.

- Bob
```
GBMLPRFCZDZJPKUPHUSHCKA737GOZL7ERZLGGMJ6YGHBFJZ6ZKMKCZTM
Native Token (Lumen in `stroop`): 102000024045
CAT Token: 199999878050
```
After the Swap Bob's Native Token increased by 750 (950 from the swap, decreased by 200 for checking both token balances) and his CAT Token decreased by 4500.

>Note: The fee for checking token balances is 100 Lumens for one time.

So, the swap performed using minimum tokens amount.

##Closing
In this blog post, we have gone over the steps to perform an atomic swap using the Soroban Python SDK. Cheers!