Algorand Atomic Transfers - cogeorg/teaching GitHub Wiki

Using the sandbox, we will create an Atomic Transfer. Atomic transfers are batches of transactions that happen simultaneously. For example, Alice only wants to pay Bob if Bob also pays Charlie. The Algorand blockchain allows for Atomic swaps by grouping transactions into one file that is signed by the stakeholders and only when all signatures are collected, it is submitted to the blockchain.

Please fire up your sandbox using the testnet via

$ ./ sandbox up

Step 1: Creating wallet and accounts, funding the accounts

If you have already done this, feel free to skip this step.

  1. Create a wallet

    $ ./sandbox goal wallet new MyWallet
    
  2. Create three accounts

    $ ./sandbox goal account new
    $ ./sandbox goal account new
    $ ./sandbox goal account new
    

    You can list the accounts via

    $ ./sandbox goal account list
    [offline]	Unnamed-2	FHVO53QM54RDNTEMEVOZM5LXSTOAYQDN4UI5GCNPM236AXFW7AQAD35SQQ	0 microAlgos
    [offline]	Unnamed-1	KV72DRDRENU6BIS7ZCPA4YW6JAOIZ46KRRQSU6RJY7JUVA3Z4FD45Z2QMM	0 microAlgos
    [offline]	Unnamed-0	OYOAIIVCP4OTPYXVYAJ23TCTRKLE4OBQF7RMT6KREV6WBSLDJ247DWRSKY	0 microAlgos	*Default
    
  3. Fund the accounts

    Visit the Algorand dispenser and fund your accounts by pasting the addresses.

Step 2: Creating the individual transactions and combining them

  1. Create a transaction from Unnamed-2 to Unnamed-1

    $ ./sandbox goal clerk send -f FHVO53QM54RDNTEMEVOZM5LXSTOAYQDN4UI5GCNPM236AXFW7AQAD35SQQ -t KV72DRDRENU6BIS7ZCPA4YW6JAOIZ46KRRQSU6RJY7JUVA3Z4FD45Z2QMM -a 5000 -o /opt/algorand/node/data/1.tx -w MyWallet
    
    • -f: from
    • -t: to
    • -a: amount
    • -o: output file
    • -w: wallet
  2. Create a transaction from Unnamed-1 to Unnamed-0

    $ ./sandbox goal clerk send -f KV72DRDRENU6BIS7ZCPA4YW6JAOIZ46KRRQSU6RJY7JUVA3Z4FD45Z2QMM -t OYOAIIVCP4OTPYXVYAJ23TCTRKLE4OBQF7RMT6KREV6WBSLDJ247DWRSKY -a 6000 -o /opt/algorand/node/data/2.tx -w MyWallet
    
  3. Combining the unsigned transactions using docker

    $ docker exec -ti sandbox sh -c "cat /opt/algorand/node/data/1.tx /opt/algorand/node/data/2.tx > /opt/algorand/node/data/at1.tx"
    

    This command writes a new file called at1.tx that is the combination of 1.tx and 2.tx.

  4. Grouping the transaction

    $ ./sandbox goal clerk group -i /opt/algorand/node/data/at1.tx -o /opt/algorand/node/data/out.tx -w MyWallet
    

Step 3: Signing and submitting the combined transaction

  1. Sign

    $ ./sandbox goal clerk sign -i /opt/algorand/node/data/out.tx -o /opt/algorand/node/data/signout.tx -w MyWallet
    

    Note that this only works because all the accounts are on the same wallet. If that is not the case, the grouped combined transaction has to be split, signed individually by the different accounts on different wallets, and than combined again. For more information, see the Algorand docs.

  2. Inspect the signed transaction

    $ ./sandbox goal clerk inspect /opt/algorand/node/data/signout.tx
    /opt/algorand/node/data/signout.tx[0]
    {
      "sig": "RKBjg6wyZKoPTATqPgnJ3cazgBAqgRmYfjwFbG3zV9l4pzHDValliaLNMVz9kawXAMh5vQ1ukz427eqPFdMzAg==",
      "txn": {
        "amt": 5000,
        "fee": 1000,
        "fv": 4445054,
        "gen": "testnet-v1.0",
        "gh": "SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",
        "grp": "44lb4dxpy1IYsq61znwxDAUz9DX2zEMy4jHUy85MLds=",
        "lv": 4446054,
        "note": "Tsz8ZuJVtTU=",
        "rcv": "KV72DRDRENU6BIS7ZCPA4YW6JAOIZ46KRRQSU6RJY7JUVA3Z4FD45Z2QMM",
        "snd": "FHVO53QM54RDNTEMEVOZM5LXSTOAYQDN4UI5GCNPM236AXFW7AQAD35SQQ",
        "type": "pay"
      }
    }
    
    /opt/algorand/node/data/signout.tx[1]
    {
      "sig": "v+gZoq519IkjDVWXjtjJc4+bNq37obyzgx4q/erf9FwYVjuSE958TPbCzXEOoVr6dk6/KvF3p5/Io+RyF6DcBA==",
      "txn": {
        "amt": 6000,
        "fee": 1000,
        "fv": 4445109,
        "gen": "testnet-v1.0",
        "gh": "SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",
        "grp": "44lb4dxpy1IYsq61znwxDAUz9DX2zEMy4jHUy85MLds=",
        "lv": 4446109,
        "note": "i5Sn4W6Farg=",
        "rcv": "OYOAIIVCP4OTPYXVYAJ23TCTRKLE4OBQF7RMT6KREV6WBSLDJ247DWRSKY",
        "snd": "KV72DRDRENU6BIS7ZCPA4YW6JAOIZ46KRRQSU6RJY7JUVA3Z4FD45Z2QMM",
        "type": "pay"
      }
    }
    
    

    Note that you can use this command to also inspect the individual transactions.

  3. Submit to the network

    ./sandbox goal clerk rawsend -f /opt/algorand/node/data/signout.tx -w MyWallet
    
    Raw transaction ID Q65DUFOQLX3XD2MLYJWAGQTP7XUSQMWUTN4FHNBHPP74UMDCF77Q issued
    Raw transaction ID PK25DRFJ25AOUFV3ZXXRUPAE2WZDKH3TJMAO52YKB4FB2EOKRNOQ issued
    Transaction Q65DUFOQLX3XD2MLYJWAGQTP7XUSQMWUTN4FHNBHPP74UMDCF77Q still pending as of round 4445251
    Transaction Q65DUFOQLX3XD2MLYJWAGQTP7XUSQMWUTN4FHNBHPP74UMDCF77Q committed in round 4445253
    Transaction PK25DRFJ25AOUFV3ZXXRUPAE2WZDKH3TJMAO52YKB4FB2EOKRNOQ committed in round 4445253
    
  4. Check that the account balances have changed:

    $ ./sandbox goal account list
    Please enter the password for wallet 'MyWallet': 
    [offline]	Unnamed-2	FHVO53QM54RDNTEMEVOZM5LXSTOAYQDN4UI5GCNPM236AXFW7AQAD35SQQ	99994000 microAlgos
    [offline]	Unnamed-1	KV72DRDRENU6BIS7ZCPA4YW6JAOIZ46KRRQSU6RJY7JUVA3Z4FD45Z2QMM	99998000 microAlgos
    [offline]	Unnamed-0	OYOAIIVCP4OTPYXVYAJ23TCTRKLE4OBQF7RMT6KREV6WBSLDJ247DWRSKY	100006000 microAlgos	*Default
    

    Perfect, it worked! Note that there is a transaction fee of 1000 microAlgos.

You may shut down the sandbox via ./sandbox down and delete it via ./sandbox clean. down only stops the docker container while clean stops and removes it.