DEV Community

darkvallen
darkvallen

Posted on

Performing Atomic Swap using Soroban Python SDK

In my previous post, 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 written by yuzurush.

Preparation

The python script that we're going to use is soroban_auth_atomic_swap.py from Soroban Python SDK Examples Github Repository

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
Enter fullscreen mode Exit fullscreen mode
  • Alice
Public Key  GDAT5HWTGIU4TSSZ4752OUC4SABDLTLZFRPZUJ3D6LKBNEPA7V2CIG54
Secret Key  SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV
Enter fullscreen mode Exit fullscreen mode
  • Bob
Public Key  GBMLPRFCZDZJPKUPHUSHCKA737GOZL7ERZLGGMJ6YGHBFJZ6ZKMKCZTM
Secret Key  SAEZSI6DY7AXJFIYA4PM6SIBNEYYXIEM2MSOTHFGKHDW32MBQ7KVO6EN
Enter fullscreen mode Exit fullscreen mode

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. Here's both tokens balance on both accounts before swap:

  • Alice
Native Token (Lumen in `stroop`): 97999955005
CAT Token: 100000117450
Enter fullscreen mode Exit fullscreen mode
  • Bob
GBMLPRFCZDZJPKUPHUSHCKA737GOZL7ERZLGGMJ6YGHBFJZ6ZKMKCZTM
Native Token (Lumen in `stroop`): 102000023295
CAT Token: 199999882550
Enter fullscreen mode Exit fullscreen mode

Swapping The Tokens

To swap the tokens, open the soroban_auth_atomic_swap.py file and you can adjust some of following parameters:

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"
)
Enter fullscreen mode Exit fullscreen mode

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).

    Int128(1000),  # amount_a
    Int128(4500),  # min_b_for_a
    Int128(5000),  # amount_b
    Int128(950),  # min_a_for_b
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)